简体   繁体   English

如何从具有外键的DTO映射到嵌套对象的层次结构

[英]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.

相关问题 如何使用AutoMapper将Dto映射到具有嵌套对象的现有对象实例? - How do you map a Dto to an existing object instance with nested objects using AutoMapper? 如何为嵌套对象创建 dto 验证 - how to create dto validation for nested objects 关于如何从域(ORM)对象映射到数据传输对象(DTO)的建议 - Suggestions on how to map from Domain (ORM) objects to Data Transfer Objects (DTO) 如何为表和视图使用相同的DTO外键关系 - How to use same DTO foreign key relationship for table and view 如何将DTO从EF映射到模型 - How to map DTO from EF to Model 对象模式的嵌套层次结构 - Nested hierarchy of objects pattern 如何将具有嵌套属性的模型映射到平面Dto? - How do I map a model with nested properties to a flat Dto? 如何在TPT继承层次结构中为子类型定义外键 - How to define a foreign key to a sub type in a TPT inheritance hierarchy 如何使用外键属性的自定义名称映射相关对象,如PostId而不是Post_Id - How to map related objects with a custom name for the foreign key property, like PostId instead of Post_Id 将外键值包含到单个记录的 DTO 中 - Including foreign key values into a DTO for a single record
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM