How to tell AutoMapper 5 to use StructureMap for constructing services without creating a bootstrapping problem, i.e. new MapperConfiguration(cfg => cfg.ConstructServicesUsing(some_IContainer)) when the config is being made via StructureMap?
Custom resolvers need a service locator to be used by AutoMapper, but IContainer does not exist yet while AutoMapper is being initialised inside a StructureMap registry. The static ObjectFactory.Container has been deprecated in StructureMap, so I have a lazy ObjectFactory:
public static class ObjectFactory
{
private static readonly Lazy<Container> _containerBuilder =
new Lazy<Container>(defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication);
public static IContainer Container
{
get { return _containerBuilder.Value; }
}
private static Container defaultContainer()
{
return new Container(x =>
{
x.AddRegistry<MyRegistry>(); // AutoMapper is configured here
});
}
}
I can't reference ObjectFactory.Container from an AutoMapper Profile because I get a stack overflow or "Value referenced inside lazy factory".
Is there a way to tack on .ConstructUsing(some_IContainer) after configuring AutoMapper?
You can leverage the container - even if it's not built yet - by using lamdba-based registrations.
Your registration of
MapperConfigurationcould look something like:This way you avoid the chicken-and-egg problem.
WARNING
I haven't tested this code, and I'm not familiar with StructureMap.