在映射时如何处理循环引用? [英] How can you deal with circular references when mapping?
问题描述
以此数据库为例
员工
- id - int(PK)
- name - varchar
- id - int(PK)
- employee_id - int(FK) / li>
- amount - float
实体框架将生成类似于以下的模型:
public partial class Employee
{
public Employee()
{
this.Salaries = new的HashSet<薪酬和GT;();
}
public int id {get;组; }
public string name {get;组;
}
public partial class Salary
{
public int id {get;组; }
public int employee_id {get;组; }
public float amount {get;组; }
public Employee employee {get;组;
}
Emplyee引用他的薪资清单,而每个薪水指向哪个员工他拥有这导致循环引用问题。
我遵循存储库模式,并使用AutoMapper将Employee转移到EmployeeDTO和工资到SalaryDTO。我希望这些DTO能够保持这个孩子关系的信息。但是,我不想递归地做这个。我可以这样做。
public partial class EmployeeDTO
{
public EmployeeDTO()
{
this.Salaries = new HashSet< SalaryChildDTO>();
}
public int id {get;组; }
public string name {get;组; }
}
public partial class SalaryChildDTO
{
public int id {get;组; }
public float amount {get;组; }
}
但这将成为维护噩梦。
我如何告诉AutoMapper只映射一个堕胎,或者实现类似的目标?
我最终去了DTO - ChildDTO路线,因为我发现一个易于管理的方式。
public partial class EmployeeDTO
{
public int id {get;组; }
public string name {get;组; }
public virtual IEnumerable< SalaryChildDTO>工资{get;组; } //注意Virtual
}
public partial class EmployeeChildDTO:EmployeeDTO
{
[IgnoreMap] // MAGIC!
public override IEnumerable< SalaryChildDTO>工资{get;组; } //覆盖! :o
}
public partial class SalaryDTO
{
public int id {get;组; }
public int employe_id {get;组; }
public float amount {get;组; }
public virtual EmployeeChildDTO Employee {get;组; } //再次注意虚拟
}
public partial class SalaryChildDTO:SalaryDTO
{
[IgnoreMap] // MAGIC!
public override EmployeeChildDTO Employee {get;组; } //覆盖! :o
}
这样,唯一影响孩子的数据库更改DTO是FK我可以用另一种方式做到这一点(EmployeeDTO扩展了EmployeeChildDTO),并避免覆盖和虚拟和忽略地图,但是我想保持核心的DTO作为基类。
Take this database for example
Employee
- id - int (PK)
- name - varchar
Salary
- id - int (PK)
- employee_id - int (FK)
- amount - float
Entity Framework will generate models similar to these:
public partial class Employee
{
public Employee()
{
this.Salaries = new HashSet<Salary>();
}
public int id { get; set; }
public string name { get; set; }
}
public partial class Salary
{
public int id { get; set; }
public int employee_id { get; set; }
public float amount { get; set; }
public Employee employee { get; set; }
}
The Emplyee references a list of his Salaries while each Salary points to which employee he owns. This results in a circular reference problem.
I follow a repository pattern and use AutoMapper to transfer Employee to EmployeeDTO and Salary to SalaryDTO. I want those DTOs to keep infomation of it's children relationships. However, I dont want to do this recursively. I COULD do something like.
public partial class EmployeeDTO
{
public EmployeeDTO()
{
this.Salaries = new HashSet<SalaryChildDTO>();
}
public int id { get; set; }
public string name { get; set; }
}
public partial class SalaryChildDTO
{
public int id { get; set; }
public float amount { get; set; }
}
But this would become a maintenance nightmare.
How can I tell AutoMapper to only map a single decendant, or acheive a similar goal?
I ended up going the DTO - ChildDTO route because I found an easy to manage way of doing it.
public partial class EmployeeDTO
{
public int id { get; set; }
public string name { get; set; }
public virtual IEnumerable<SalaryChildDTO> Salaries { get; set; } //Notice the Virtual
}
public partial class EmployeeChildDTO : EmployeeDTO
{
[IgnoreMap] //MAGIC!
public override IEnumerable<SalaryChildDTO> Salaries { get; set; } //Override! :o
}
public partial class SalaryDTO
{
public int id { get; set; }
public int employe_id { get; set; }
public float amount { get; set; }
public virtual EmployeeChildDTO Employee { get; set; } //Notice the Virtual once again
}
public partial class SalaryChildDTO : SalaryDTO
{
[IgnoreMap] //MAGIC!
public override EmployeeChildDTO Employee { get; set; } //Override! :o
}
That way, the only database changes that affects the child DTOs are FKs!
I could have done it the other way arround (EmployeeDTO extends EmployeeChildDTO) and avoid Overrides and Virtuals and IgnoreMaps but I wanted to keep the core DTO as the base class.
这篇关于在映射时如何处理循环引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!