[英]How to map from a DTO with a foreign key into a hierarchy of nested objects
I'm wishing to use AutoMapper in C# .NET Core to map a set of API responses which are implicitly structured by foreign keys to a set of entity objects so I can store in a DB. 我希望在C#.NET Core中使用AutoMapper将一组由外键隐式构造的API响应映射到一组实体对象,以便我可以存储在数据库中。 I'm receiving the following payload from 3 individual endpoints:. 我正在从3个单独的端点接收以下有效负载:
public class CompanyResponse
{
public int CompanyId { get; set; }
public string Name { get; set; }
public string HeadOffice { get; set; }
}
public class FactoryResponse
{
public int FactoryId { get; set; }
public string Address { get; set; }
public int CompanyId { get; set; }
}
public class ProductResponse
{
public int ProductId { get; set; }
public string Name { get; set; }
public int FactoryId { get; set; }
}
This is the nested structure I would like to hold them in: 这是我想保留它们的嵌套结构:
public class Company
{
public string Name { get; set; }
public string HeadOffice { get; set; }
public IEnumerable<Factory> Factories { get; set; }
}
public class Factory
{
public string Address { get; set; }
public IEnumerable<Product> Products { get; set; }
}
public class Product
{
public string Name { get; set; }
}
There is a one to many relationship from Company -> Factory, and Factory -> Product respectively. 公司->工厂和工厂->产品之间存在一对多的关系。
Mapping over the properties like "Name", and "HeadOffice" etc is easy, and I have created AutoMapper maps from each DTO to their corresponding object. 在“名称”和“ HeadOffice”等属性上进行映射很容易,并且我已经创建了从每个DTO到其对应对象的AutoMapper映射。 However I do not know how to deduce the list of child objects from the Id's. 但是,我不知道如何从Id推导出子对象的列表。
Here are the maps I have so far: 这是我到目前为止的地图:
CreateMap<CompanyResponse, Company>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
.ForMember(dest => dest.HeadOffice, opt => opt.MapFrom(src => src.HeadOffice));
CreateMap<FactoryResponse, Factory>()
.ForMember(dest => dest.Address, opt => opt.MapFrom(src => src.Address));
CreateMap<ProductResponse, Product>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));
Any help would be appreciated! 任何帮助,将不胜感激!
People have mentioned that AutoMapper has no knowledge of other collections - hence in order to solve my problem I had to create the hierarchy within the response models. 人们提到AutoMapper不了解其他集合-因此,为了解决我的问题,我不得不在响应模型中创建层次结构。 I did this by adding an IEnumerable property within each parent to hold their children. 为此,我在每个父级中添加了IEnumerable属性来容纳其子级。
Then I looped over each of the models and added their corresponding children as follows: 然后,我遍历了每个模型,并添加了它们对应的子元素,如下所示:
var companyResults = await companyEndpointService.GetAllAsync();
var factoryResults = await factoryEndpointService.GetAllAsync();
var productResults = await productEndpointService.GetAllAsync();
foreach (var company in companyResults)
{
company.Factories = factoryResults
.Where(factory => factory.CompanyId == company.CompanyId);
foreach (var factory in company.Factories)
{
factory.Products = productResults
.Where(product => product.FactoryId == factory.FactoryId);
}
}
After this heirarchy was set up, I was then able to perform a simple map as follows: 设置此层次结构后,我便可以执行以下简单映射:
_mapper.Map<IEnumerable<CompanyResponse>, IEnumerable<Company>>(companyResults);
All the nested mapping happened automagically, and everything was good to go. 所有嵌套的映射都是自动发生的,一切都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.