Configuring AutoMapper 4.2 with built in IoC in ASP.NET Core 1.0 MVC6
This answer suits the MVC 6 approach a little more around the Controller layer:
I migrated from AutoMapper 4.1.1 to 4.2.0, had a few issues figuring out the intricacies but got there in the end.
First I separated the AutoMapper Profile build into a new class (see below) to save clogging up the Startup class.
using AutoMapper;using YourModels;using YourViewModels;namespace YourNamespace{ public class AutoMapperProfileConfiguration : Profile { protected override void Configure() { CreateMap<Application, ApplicationViewModel>(); CreateMap<ApplicationViewModel, Application>(); ... } }}
I made the following amendments to the Startup class.
I added private member variable of type MapperConfiguration.
private MapperConfiguration _mapperConfiguration { get; set; }
In the Startup constructor I added the following code to instantiate my new AutoMapper Profile.
_mapperConfiguration = new MapperConfiguration(cfg =>{ cfg.AddProfile(new AutoMapperProfileConfiguration());});
In ConfigureServices()
I dropped my new AutoMapper Profile into a Singleton.
services.AddSingleton<IMapper>(sp => _mapperConfiguration.CreateMapper());
It was then just a simple operation to inject it the relevant controllers.
using AutoMapper;using ...namespace YourNamespace{ public class ApplicationsController : BaseController { [FromServices] private IMapper _mapper { get; set; } [FromServices] private IApplicationRepository _applicationRepository { get; set; } public ApplicationsController( IMapper mapper, IApplicationRepository applicationRepository) { _mapper = mapper; _applicationRepository = applicationRepository; } // GET: Applications public async Task<IActionResult> Index() { IEnumerable<Application> applications = await _applicationRepository.GetForIdAsync(...); if (applications == null) return HttpNotFound(); List<ApplicationViewModel> viewModel = _mapper.Map<List<ApplicationViewModel>>(applications); return View(viewModel); } ...}
Thanks to Rexebin over at https://pintoservice.wordpress.com/2016/01/31/dependency-injection-for-automapper-4-2-in-asp-net-vnext-mvc-project/ for his post which help enourmously.
You can also use the extension package from the automapper creator.
You can pass in a configuration, specify assemblies to scan or pass nothing and let it scan assemblies from the DependencyContext.
https://github.com/AutoMapper/AutoMapper.Extensions.Microsoft.DependencyInjection
https://www.nuget.org/packages/AutoMapper.Extensions.Microsoft.DependencyInjection/
public void ConfigureServices(IServiceCollection services){ //configure DI services.AddTransient<IFoo, Foo>(); //Add automapper - scans for Profiles services.AddAutoMapper(); //or specify services.AddAutoMapper(cfg => { cfg.AddProfile<ViewModelProfile>(); ... }); ...
In your ConfigurationServices you can new up an instance of MapperConfiguration and then create your maps and add them.
public void ConfigureServices(IServiceCollection services){ MapperConfiguration configuration = new MapperConfiguration(cfg => { cfg.AddProfile<MappingProfile.Profile1>(); cfg.AddProfile<MappingProfile.Profile2>(); }); services.AddInstance(typeof (IMapper), configuration.CreateMapper());}
Then you just inject the IMapper in your constructor and map
public class Handler{ private readonly ProfileContext _db; private readonly IMapper _mapper; public Handler(ProfileContext db, IMapper mapper) { _db = db; _mapper = mapper; } public void Handle(Profile1 request) { ProfileModel profile = _mapper.Map<Profile1, ProfileModel>(request); _db.Profiles.Add(profile); try { db.SaveChanges(); } catch (Exception ex) { throw; } return profile; }}