Your simple solution is fine if you donβt want to mock the linker in unit tests or create maps with modified configurations for nested lifetime areas (the latter looks a little strange to me, but who knows).
If you need it, you can take some code snippets from the Mapper class and register the following components:
builder.Register(ctx => new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers())) .AsImplementedInterfaces() .SingleInstance(); builder.RegisterType<MappingEngine>() .As<IMappingEngine>();
I'm not sure if you really need to do IMappingEngine singleton. It should be light enough to create addiction.
Now you can use it as follows:
You can also configure automatic registration of profiles as follows:
builder.Register(ctx => new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers())) .AsImplementedInterfaces() .SingleInstance() .OnActivating(x => { foreach (var profile in x.Context.Resolve<IEnumerable<Profile>>()){ x.Instance.AddProfile(profile); } });
Then simply register the Profile implementation anywhere in the Autofac configuration or in the module to connect it to the configuration.
source share