我正在尝试在一个项目中实现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;}
}
我的数据库看起来像....
表:员工(EmployeeCode,[姓名])
表:休假(VacationId,VacationDate,EmployeeCode,ApprovedByEmployeeCode)
视图:VacationsView(因此,我不必继续写[和更改] 一遍又一遍的选择
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来自动填充对象,并告诉它如何使用存储过程中用于“员工”属性的不同列名称。
public interface IMyRepository
{
IList<Vacation> GetAllVacations();
}
....
var repo = conn.As<IMyRepository>();
return repo.GetAllVacations();
这有效(如无错误),并且我假期的所有属性均已正确映射,但是我的两个“雇员”属性为null(按预期,因为列名未与的属性名对齐)员工对象)。我不知道如何告诉Insight“使用CreatedBy ..”字段来构建“ Employee”属性,而“ Approving ...”字段来构建“ ApprovedByEmployee”属性。
我已经能够使用具有回调和columnOverride的OneToOne来完成它,然后使用标准的Query()。 I.E ..
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的自动界面功能。
我想做的是可能的吗?
答案 0 :(得分:1)
组装重复的子对象并不是当前自动完成的事情,并且接口实现没有给您正确的钩子以覆盖行为。
一些选项:
A。更改结果集的形状,以将员工作为具有属性的列表返回。
B。如果未密封这些类,请从Employee派生,以便Insight可以区分这些类:
public class AssigningEmployee : Employee {
public string AssigningName { get { return Name; } set { Name = Value; } }
...
}
这些解决方案都是 meh 。 Insight.Database的全部目的是正常工作,而无需进行大量额外工作。所以...
我打开了一个github问题来跟踪此问题: