简体   繁体   English

如何跨Visual Studio项目初始化自动映射器

[英]how to initialize automapper accross Visual Studio Projects

I'm using automapper in multiple dll projects of my Visual Studio ASP.NET MVC application. 我在Visual Studio ASP.NET MVC应用程序的多个dll项目中使用了自动映射器。 I created a AutoMapperConfiguration class in each project that I call in the Application_Start() method of my main WebUI project. 我在主WebUI项目的Application_Start()方法中调用的每个项目中创建了一个AutoMapperConfiguration类。

The thing is I am trying to initialize automapper in the same way for each individual project of my Visual Studio solution and I get an error saying automapper can only be initialized once... 问题是我试图以相同的方式为Visual Studio解决方案的每个单独项目初始化自动映射器,但我收到一条错误消息,说自动映射器只能被初始化一次。

The way I'm initializing automapper works if I only do it for one ddl project, but not for multiple dll projects, and I also get an warning saying in version 8.1.1 of automapper that the Mapper.Initialize method is depreciated. 如果仅对一个ddl项目执行此操作,而对多个dll项目不执行此操作,则初始化自动映射程序的方式将起作用,并且在自动映射程序的版本8.1.1中,我还会收到一条警告,指出Mapper.Initialize方法已弃用。 In version 9.0.0 the initialize method does not exist anymore... 在9.0.0版中,不再存在initialize方法...

How could I achieve this? 我怎样才能做到这一点?

Thanks for your help, 谢谢你的帮助,

E. E.

I have this code in each dll project: 我在每个dll项目中都有以下代码:

public class AutoMapperConfiguration
    {
        public static void ConfigureAutoMapper()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.AddProfile<BusinessToDomainProfile>();
                cfg.AddProfile<DomainToBusinessProfile>();
            });
        }
    }

and I call it it my main WebUI project like this in the Global.asax Appplication_Start Method: 我在Global.asax Appplication_Start方法中将其称为我的主要WebUI项目:

   Domain.Mappers.AutoMapperConfiguration.ConfigureAutoMapper(); 
   Business.Mappers.AutoMapperConfiguration.ConfigureAutoMapper(); 

Put your Profile classes in your different projects and initialize AutoMapper once in your ASP.NET MVC project. 将您的Profile类放在不同的项目中,并在ASP.NET MVC项目中初始化一次AutoMapper。

// Load all profiles in an assembly by specifying a type from the assembly
Mapper.Initialize(cfg => {
    cfg.AddProfiles(typeof(Student), typeof(Course));
});

Do not call Mapper.Initialize in any class library project (dll). 不要在任何类库项目(dll)中调用Mapper.Initialize。 The executable (Web app, console app, etc) is responsible for the initialization. 可执行文件(Web应用程序,控制台应用程序等)负责初始化。

You can only Initialize the Mapper once. 您只能初始化一次映射器。 Usually you initialize in your 'Set as StartUp Project' so in your case, collect all those cfg.AddProfile()'s and put them in your global.asax.cs Application_Start Initialize() call. 通常,您在“设置为启动项目”中进行初始化,因此,在这种情况下,请收集所有这些cfg.AddProfile()并将其放入您的global.asax.cs Application_Start Initialize()调用中。

I'm working on and older app with a Global.asax.cs Application_Start() method that configures AutoMapper profiles from a range of projects like this: 我正在使用Global.asax.cs Application_Start()方法来处理较旧的应用程序,该方法可从一系列项目中配置AutoMapper配置文件,如下所示:

Mapper.AddProfile<AutoMapperConfigurationApi>();
Mapper.AddProfile<AutoMapperConfiguration>();
Mapper.AddProfile<AutoMapperWebConfiguration>();

Where each profile eg AutoMapperConfigurationApi is a standalone config class in a different project: 其中每个配置文件(例如AutoMapperConfigurationApi)是不同项目中的独立配置类:

namespace API_MVC
{
    public class AutoMapperConfigurationApi : Profile
    {
        protected override void Configure()
        {
            CreateMap<EnquiryCreateRequest, EnquiryDto>();

        }
    }
}

I also have a .Net Core 3 app that's only months old and I'm using: 我也有一个只有.NET Core 3的应用程序,而且我使用的是:

In my Startup.cs MVC Project: 在我的Startup.cs MVC项目中:

public void ConfigureServices(IServiceCollection services)
{
     ...

     services.AddAutoMapper(new Assembly[] {
              typeof(SomeProject.AutoMapperProfile).Assembly,
              typeof(SomeOtherProject.AutoMapperProfile).Assembly
            });
     ...
}

With AutoMapperProfile.cs files set up like so: 通过设置AutoMapperProfile.cs文件,如下所示:

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        CreateMap<MyModel, MyModelDto>();
    }
}

I'm using dependency injection for the Mapper in controllers eg: 我在控制器中为Mapper使用依赖注入,例如:

public MyController
{
    public MyController(IMapper mapper)
    {

    }
}

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

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