0

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?

Petrus Theron
  • 27,855
  • 36
  • 153
  • 287

1 Answers1

0

You can leverage the container - even if it's not built yet - by using lamdba-based registrations.

Your registration of MapperConfiguration could look something like:

class MyRegistry : Registry
{
    public MyRegistry()
    {
        For<MapperConfiguration>()
            .Use("Use StructureMap context to resolve AutoMapper services", ctx =>
            {
                return new MapperConfiguration(config =>
                {
                    config.ConstructServicesUsing(type => ctx.GetInstance(type));
                });
            });
    }
}

This way you avoid the chicken-and-egg problem.

WARNING

I haven't tested this code, and I'm not familiar with StructureMap.

Mickaël Derriey
  • 12,796
  • 1
  • 53
  • 57