How to register AutoMapper 4.2.0 with Simple Injector
This would be the equivalent:
container.RegisterInstance<MapperConfiguration>(config);
container.Register<IMapper>(() => config.CreateMapper(container.GetInstance));
Simple Injector's IPackage interface seems like the nearest equivalent of StructureMap's Registry type. Here's the package I use, building from @Steven's answer:
using System;
using System.Linq;
using System.Reflection;
//
using AutoMapper;
//
using SimpleInjector;
using SimpleInjector.Packaging;
public class AutoMapperPackage : IPackage
{
public void RegisterServices(Container container)
{
var profiles = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(x => typeof(AutoMapper.Profile).IsAssignableFrom(x));
var config = new MapperConfiguration(cfg =>
{
foreach (var profile in profiles)
{
cfg.AddProfile(Activator.CreateInstance(profile) as AutoMapper.Profile);
}
});
container.RegisterInstance<MapperConfiguration>(config);
container.Register<IMapper>(() => config.CreateMapper(container.GetInstance));
}
}
You would need to add the SimpleInjector.Packaging package, and then add a call to container.RegisterPackages();
in your bootstrap/configuration code.
Essentially, the only thing that really changes from StructureMap would be the last two lines.