Inisght.Database 自动存储库和自定义列映射

Inisght.Database Auto Repository and Custom Column Mapping

我正在尝试在一个项目中实现 Insight.Database,并且在试图利用自动接口实现并将对象属性映射到数据库中的奇数列名称时遇到困难 运行。

我有以下结构...

class Employee 
{
    string EmployeeCode {get; set;}
    string Name {get; set;}
}

class Vacation 
{
    int VacationId {get; set;}
    DateTime VacationDate {get; set;}
    string EmployeeCode {get; set;}
    string ApprovedByEmployeeCode {get; set;}
    Employee Employee {get; set;}
    Employee ApprovedByEmployee {get; set;}
}

我的数据库看起来像....

Table:员工(员工代码,[姓名])

Table:假期(VacationId、VacationDate、EmployeeCode、ApprovedByEmployeeCode)

视图:VacationsView(这样我就不必一直写[和更改] 同样的 SELECT 一遍又一遍)

SELECT v.VacationId, v.VacationDate, v.EmployeeCode, v.ApprovedByEmployeeCode, e1.EmployeeCode AS CreatedByEmployeeCode, e1.[Name] AS CreatedByName, e2.EmployeeCode AS ApprovingEmployeeCode, e2.[Name] AS ApprovingName
FROM Vacations v 
INNER JOIN Employees e1 ON v.EmployeeCode = e1.EmployeeCode
INNER JOIN Employees e2 ON v.ApprovedByEmployeeCode = e2.EmployeeCode

存储过程:GetAllVacations

SELECT * FROM VacationsView

最后,使用 Insight.Database,我试图拥有一个接口,该接口将自动填充我的对象并告诉它如何使用我的存储过程中的不同列名来实现 "employee" 属性。

public interface IMyRepository
{
    IList<Vacation> GetAllVacations();
}

....

var repo = conn.As<IMyRepository>();
return repo.GetAllVacations();

这有效(因为没有错误)并且我假期的所有属性都已正确映射,但我的两个 "employee" 属性为空(正如预期的那样,因为列名称不符合雇员对象的 属性 名称)。我想不通的是如何告诉 Insight "Use CreatedBy.." 字段构建 "Employee" 属性 和 "Approving..." 字段构建 "ApprovedByEmployee" 属性.

我已经能够使用带有回调和 columnOverride 的 OneToOne 完成它,然后使用标准的 Query()。即..

var vacationStructure =
            new OneToOne<Vacation, Employee, Employee>(
    callback: (vacation, createdBy, approvedBy) =>
    {
            vacation.Employee = createdBy;
            vacation.ApprovedByEmployee = approvedBy;
    }, columnOverride: new ColumnOverride[]
    {
            new ColumnOverride<EmployeeModel>("CreatedByEmployeeCode", "EmployeeCode"),
            new ColumnOverride<EmployeeModel>("CreatedByName", "Name"),
            new ColumnOverride<EmployeeModel>("ApprovingEmployeeCode", "EmployeeCode"),
            new ColumnOverride<EmployeeModel>("ApprovingName", "Name")
    });

....


var results = await conn.QueryAsync("GetAllVacations", new {employeeCode}, Query.Returns(_vacationStructure));

不过,我真的很想利用 Insight 的自动界面功能。

我想做的事情可行吗?

组装重复的子对象目前不是自动完成的,接口实现没有给你正确的挂钩来覆盖行为。

部分选项:

一个。将结果集的形状更改为 return 员工作为具有属性的列表。

乙。如果 类 未密封,则从 Employee 派生,以便 Insight 可以区分 类:

public class AssigningEmployee : Employee { public string AssigningName { get { return Name; } set { Name = Value; } } ... }

这些解都是meh。 Insight.Database 的全部要点是 工作 而无需大量额外工作。所以...

我打开了一个 github 问题来跟踪这个:

https://github.com/jonwagner/Insight.Database/issues/384