c# 如何将 dapperRow 调整为在网格视图中显示的强类型对象

c# How to dapperRow to strongly typed object displayed in grid view

我确信我没有像你们大多数人那样的最佳实践解决方案,但我是这个行业的新手,所以请给我一个机会:P

我在我的 sqlite 数据库中创建了一个 table,并在 Employee table 中添加了一个 Employee。 我将我的 GridView 的 DataContext 设置为从我的 ModelView 返回的列表。 但是我的网格是空的。我想我在转换我的 dapperRow 时存在逻辑错误。 请看一下。我没有得到错误或其他东西。我的 gridView 是空的。

 public List<Employee> GetAllEmployees()
    {
        List<Employee> employees = new List<Employee>();
        List<dynamic> results = _dataAccess.SelectAllEmployees();
        List<IDictionary<string, object>> dapperRowDic = results.Select(x => (IDictionary<string, object>)x).ToList();
        foreach(var dapperRow in dapperRowDic)
        {
            object id;
            object firstName;
            object lastName;
            object experience;
            object salary;

            dapperRow.TryGetValue("ID", out id);
            dapperRow.TryGetValue("FirstName", out firstName);
            dapperRow.TryGetValue("LastName", out lastName);
            dapperRow.TryGetValue("Experience", out experience);
            dapperRow.TryGetValue("Salary", out salary);

            employees.Add(new Employee((int)(long)id,(string)firstName,(string)lastName, (int)(long)experience, (int)(long)salary));
        }

        return employees;
    }

public class SQLiteDataAccess : IDataAccess
{
    public SQLiteDataAccess()
    {
        if (!File.Exists(SqLiteBaseRepository.DbFile))
            CreateDatabase();
    }

    public void InsertEmployee(int id,string firstName, string lastName, int experience, int salary)
    {
        using (var con = SqLiteBaseRepository.SimpleDbConnection())
        {
            var parameters = new { FIRSTNAME = firstName, LASTNAME = lastName, EXPERIENCE = experience, SALARY = salary, ID = id };
            con.Execute("insert into Employee (ID,FirstName,LastName,Experience,Salary) " +
                "Values (@ID ,@FIRSTNAME, @LASTNAME, @EXPERIENCE,@SALARY)", parameters);
        }
    }

    public List<dynamic> SelectAllEmployees()
    {
        using (var con = SqLiteBaseRepository.SimpleDbConnection())
        {
            return con.Query<dynamic>("SELECT * FROM Employee").ToList();
        }
    }

public partial class MainWindow : Window
{
    private EmployeeViewModel _employeeViewModel;

    public MainWindow()
    {
        InitializeComponent();
        string mode = "SQLite";
        _employeeViewModel = new EmployeeViewModel(DataAccessFactory.GetDataAccessObject(mode));
        List<Employee> employees = _employeeViewModel.GetAllEmployees();
        this.DataContext = employees;
        this.Show();
    }

    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
        int id = int.Parse(txtId.Text);
        string firstName = txtFirstName.Text;
        string lastName = txtLastName.Text;
        int experience = int.Parse(txtExperience.Text);
        int salary = int.Parse(txtSalary.Text);
        _employeeViewModel.CreateEmployee(id, firstName,lastName,experience,salary);
        this.DataContext = _employeeViewModel.GetAllEmployees();
    }
}

<Window x:Class="Employee_List_SQLite_Dapper_Wpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Employee_List_SQLite_Dapper_Wpf"
    mc:Ignorable="d"
    Title="" Height="450" Width="800">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"></ColumnDefinition>
        <ColumnDefinition Width="200"></ColumnDefinition>
        <ColumnDefinition Width="200"></ColumnDefinition>
        <ColumnDefinition Width="200"></ColumnDefinition>
        <ColumnDefinition Width="35"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Label FontSize="20" Grid.Row="0" Grid.Column="0">Employee</Label>
    <Label Grid.Row="1" Grid.Column="0">ID:</Label>
    <Label Grid.Row="2" Grid.Column="0">First Name:</Label>
    <Label Grid.Row="3" Grid.Column="0">Last Name:</Label>
    <Label Grid.Row="4" Grid.Column="0">Experience:</Label>
    <Label Grid.Row="5" Grid.Column="0">Salary:</Label>
    <TextBox x:Name="txtId" Grid.Row="1" Grid.Column="1" Margin="2"></TextBox>
    <TextBox x:Name="txtFirstName" Grid.Row="2" Grid.Column="1" Margin="2"></TextBox>
    <TextBox x:Name="txtLastName" Grid.Row="3" Grid.Column="1" Margin="2"></TextBox>
    <TextBox x:Name="txtExperience" Grid.Row="4" Grid.Column="1" Margin="2"></TextBox>
    <TextBox x:Name="txtSalary" Grid.Row="5" Grid.Column="1" Margin="2"></TextBox>
    <Button Name="btnSave" Grid.Row="5" Grid.Column="2" Width="80" Click="btnSave_Click">Save</Button>
    <ListView Grid.Row="6" Grid.ColumnSpan="5" ItemsSource="{Binding Employee}" Margin="5,20,20,20">
        <ListView.View>
            <GridView x:Name="gridEmployee">
                <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" Width="30"/>
                <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="200" />
                <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="200"/>
                <GridViewColumn Header="Experience" DisplayMemberBinding="{Binding Experience}" Width="100"/>
                <GridViewColumn Header="Salary" DisplayMemberBinding="{Binding Salary}" Width="150"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

检索所有员工的 Dapper 代码应如下所示:

var employees = connection.GetAll<Employee>();

就是这样。您可以找到有关 GetAll<T>() 方法的更多信息 here.

您的插入员工代码看起来不错,但您真正需要做的是:

var employee = new Employee
{
    FirstName = firstName,
    LastName = lastName,   // etc.
}

connection.Insert(employee);

您需要正确注释您的 Employee class 才能使其正常工作。我上面链接的 Dapper.Contrib 页面解释了如何执行此操作。

对于 MVVM,我推荐一个很好的教程,例如 this one