I've been using Automapper and Autofac in a .Net app for some time. I configured them this way:
builder.RegisterAssemblyTypes(typeof (OneOfMyMappingProfiles).Assembly)
.Where(t => t.IsSubclassOf(typeof (Profile)))
.As<Profile>();
builder.Register(ctx => new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers))
.AsImplementedInterfaces()
.SingleInstance()
.OnActivating(x =>
{
foreach (var profile in x.Context.Resolve<IEnumerable<Profile>>())
{
x.Instance.AddProfile(profile);
}
});
builder.RegisterType<MappingEngine>()
.As<IMappingEngine>().SingleInstance();
With the latest build of Automapper (4.2) the API has changed and I am having trouble translating to the new API. ConfigurationStore no longer seems to exist. According to the docs, the way to register with an IOC is now like this:
var profiles =
from t in typeof (AutoMapperRegistry).Assembly.GetTypes()
where typeof (Profile).IsAssignableFrom(t)
select (Profile)Activator.CreateInstance(t);
var config = new MapperConfiguration(cfg =>
{
foreach (var profile in profiles)
{
cfg.AddProfile(profile);
}
});
For<MapperConfiguration>().Use(config);
For<IMapper>().Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));
BUT that is using StructureMap. The first half of this is no problem, but I am not sure how to translate the "For<>.Use()" portion. How do I do that in Autofac?