在N层应用程序中配置Automapper [英] Configuring Automapper in N-Layer application

查看:98
本文介绍了在N层应用程序中配置Automapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个N层应用程序,如下所示

I have an N-Layer application as shown below

MyApp.Model -包含edmx和数据模型

MyApp.Model - contains edmx and data models

MyApp.DataAccess -具有EF的存储库

MyApp.Domain -域/业务模型

MyApp.Services -服务(类库)

MyApp.Api -ASP.NET Web API

MyApp.Api - ASP.NET Web API

我将Unity用作IoC容器,并使用 Automapper 进行OO映射.

I am using Unity as my IoC container and Automapper for OO mapping.

我的DataAccess层引用包含我所有数据对象的模型层.

My DataAccess layer references Model layer which contains all my Data objects.

我不想在我的Api层中引用我的模型项目.因此,从服务层返回DomainObjects(业务模型)并映射到API层中的DTO(DTO在API层中).

I do not want to refer my model project in my Api layer. So returning DomainObjects (business models) from service layer and mapping to DTOs in API layer(DTOs are in API layer).

我在如下所示的API层中将domainModel配置为DTO映射

I configured domainModel to DTO mapping in API layer as below

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?

要在我的API层中执行操作,我没有引用我的模型项目.我相信我应该在服务层执行此操作,但不确定如何执行此操作.请帮助如何配置此映射.

To do in my API layer I do not have reference to my model project. 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

  1. 在何处放置AutoMapper映射注册dll 说要使用静态构造函数,我认为这不是在所有模型(我有100个模型)中添加的好选择,另一个答案是要使用PreApplicationStartMethod,为此我必须添加对System.web.dll的引用对我的服务不正确.

  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.

https://groups.google.com/论坛/#!topic/automapper-users/TNgj9VHGjwg 也没有正确回答我的问题.

https://groups.google.com/forum/#!topic/automapper-users/TNgj9VHGjwg also did not answer my question properly.

推荐答案

您需要创建映射配置文件,然后告诉AutoMapper在引用所有较低层的最上层/最外层(调用)中使用这些配置文件.在您的示例中:

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. 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>();
});

,或者如果您使用的是DI容器(例如 SimpleInjector ):

or if you are using a DI container (e.g. SimpleInjector):

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

这篇关于在N层应用程序中配置Automapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆