简体   繁体   中英

how to initialize automapper accross Visual Studio Projects

I'm using automapper in multiple dll projects of my Visual Studio ASP.NET MVC application. I created a AutoMapperConfiguration class in each project that I call in the Application_Start() method of my main WebUI project.

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...

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. In version 9.0.0 the initialize method does not exist anymore...

How could I achieve this?

Thanks for your help,

E.

I have this code in each dll project:

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:

   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.

// 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). The executable (Web app, console app, etc) is responsible for the initialization.

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.

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:

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

Where each profile eg AutoMapperConfigurationApi is a standalone config class in a different project:

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:

In my Startup.cs MVC Project:

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:

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

I'm using dependency injection for the Mapper in controllers eg:

public MyController
{
    public MyController(IMapper mapper)
    {

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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