简体   繁体   English

在N层应用程序中配置自动映射

[英]Configuring Automapper in N-Layer application

I have an N-Layer application as shown below 我有一个N层应用程序,如下所示

MyApp.Model - contains edmx and data models MyApp.Model - 包含edmx和数据模型

MyApp.DataAccess - Repositories with EF MyApp.DataAccess - 使用EF的存储库

MyApp.Domain - Domain/business models MyApp.Domain - 域/业务模型

MyApp.Services - services(class library) MyApp.Services - 服务(类库)

MyApp.Api - ASP.NET Web API MyApp.Api - ASP.NET Web API

I am using Unity as my IoC container and Automapper for OO mapping. 我使用Unity作为我的IoC容器,使用Automapper进行OO映射。

My DataAccess layer references Model layer which contains all my Data objects. 我的DataAccess图层引用了包含所有Data对象的Model层。

I do not want to refer my model project in my Api layer. 我不想在我的Api层中引用我的模型项目。 So returning DomainObjects (business models) from service layer and mapping to DTOs in API layer(DTOs are in API layer). 因此,从服务层返回DomainObjects(业务模型)并映射到API层中的DTO(DTO在API层中)。

I configured domainModel to DTO mapping in API layer as below 我在API层中将domainModel配置为DTO映射,如下所示

public static class MapperConfig
{
    public static void Configure() {
        Mapper.Initialize(
            config => {
                config.CreateMap<StateModel, StateDto>();
                config.CreateMap<StateDto, StateModel>();

                //can't do this as I do not have reference for State which is in MyApp.Model
                //config.CreateMap<State, StateModel>();
                //config.CreateMap<StateModel, State>();
            });
    }
}

Now my question is how/where to configure my auto mapper mappings to convert my Entity models to Domain models? 现在我的问题是如何/在哪里配置我的自动映射器映射以将我的实体模型转换为域模型?

To do in my API layer I do not have reference to my model project. 要在我的API层中执行,我没有引用我的模型项目。 I believe I should do this in service layer but not sure how to do that. 我相信我应该在服务层执行此操作但不确定如何执行此操作。 Please help how to configure this mapping. 请帮助您如何配置此映射。

Note: Before asking here I googled with all eyes 注意:在问这里之前我用Google搜索了所有的眼睛

  1. Where to place AutoMapper map registration in referenced dll says to use static constructor which I do not think a good option to add in all my models (I have 100 models) and another answer says to use PreApplicationStartMethod for which I have to add reference to System.web.dll to my services which is not correct. 在引用的dll中放置AutoMapper地图注册的位置说使用静态构造函数,我认为不是在我的所有模型中添加一个好选项(我有100个模型)而另一个答案说使用PreApplicationStartMethod我必须添加对System的引用.web.dll到我的服务哪个不正确。

  2. https://groups.google.com/forum/#!topic/automapper-users/TNgj9VHGjwg also did not answer my question properly. https://groups.google.com/forum/#!topic/automapper-users/TNgj9VHGjwg也没有正确回答我的问题。

You need to create mapping profiles in each of your layer projects, then tell AutoMapper to use those profiles in the topmost/outermost (invoking) layer that references all the lower layers. 您需要在每个图层项目中创建映射配置文件 ,然后告诉AutoMapper在引用所有较低层的最顶层/最外层(调用)层中使用这些配置文件。 In your example: 在你的例子中:

MyApp.Model MyApp.Model

public class ModelMappingProfile : AutoMapper.Profile
{
    public ModelMappingProfile()
    {
        CreateMap<StateModel, StateDto>();
        CreateMap<StateDto, StateModel>();
    }
}

MyApp.Api MyApp.Api

public class ApiMappingProfile : AutoMapper.Profile
{
    public ApiMappingProfile()
    {
        CreateMap<State, StateModel>();
        CreateMap<StateModel, State>();
    }
}

MyApp.Services MyApp.Services

Mapper.Initialize(cfg => 
{
    cfg.AddProfile<MyApp.Model.ModelMappingProfile>();
    cfg.AddProfile<MyApp.Model.ApiMappingProfile>();
});

or if you are using a DI container (eg SimpleInjector ): 或者如果您使用DI容器(例如SimpleInjector ):

container.RegisterSingleton<IMapper>(() => new Mapper(new MapperConfiguration(cfg => 
{
    cfg.AddProfile<MyApp.Model.ModelMappingProfile>();
    cfg.AddProfile<MyApp.Model.ApiMappingProfile>();
})));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM