Re: Autofac Configuration of AutoMapper

Showing 1-2 of 2 messages
Re: Autofac Configuration of AutoMapper Nicholas Blumhardt 9/17/09 9:02 PM
Re-posting to AutoMapper Users, since I had to join first :)

2009/9/17 Nicholas Blumhardt <nicholas....@gmail.com>
Hi Keith,

I think this is along the lines of what you want:

builder.Register(c => Mapper.Engine).As<IMappingEngine>();

builder.Register<
TypeMapFactory>().As<ITypeMapFactory>();

builder.Register(c => AutoMapper.Mappers.MapperRegistry.AllMappers());

builder.Register<AutoMapper.Configuration>()
    .As<AutoMapper.Configuration>()
    .As<IConfigurationProvider>()
    .As<IConfiguration>();

The invocation of AllMappers() is registered as type IEnumerable<IObjectMapper>, which allows the constructor of Configuration to be autowired.

I've never used AutoMapper myself, so I'm cross-posting to AutoMapper Users in the hope that someone there can validate this :)

Cheers,

Nick


2009/9/17 Keith <krou...@gmail.com>


Hi,

I am trying to configure AutoMapper with Autofac and am running into
an error. Basically, it is telling me that I have no resolvable
constructors for Configuration.

Here is what I am trying to do:

builder.Register(c => Mapper.Engine).As<IMappingEngine>();
           builder.Register<TypeMapFactory>().As<ITypeMapFactory>();
           builder.Register<AutoMapper.Configuration>(c => new
AutoMapper.Configuration(c.Resolve<ITypeMapFactory>(),
AutoMapper.Mappers.MapperRegistry.AllMappers.Invoke()))
               .As<AutoMapper.Configuration>().SingletonScoped();
           builder.Register<AutoMapper.Configuration>
().As<IConfigurationProvider>();
           builder.Register<AutoMapper.Configuration>
().As<IConfiguration>();

In the code above, AutoMapper.Mappers.MapperRegistry.AllMappers is
Func<IEnumerable<IObjectMapper>>

There is an example of this using StructureMap here:
http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/05/11/automapper-and-ioc.aspx

The line that I am struggling to recreate is this:
ForRequestedType<Configuration>()
           .CacheBy(InstanceScope.Singleton)
           .TheDefault.Is.OfConcreteType<Configuration>()
           .CtorDependency<IEnumerable<IObjectMapper>>().Is(expr =>
expr.ConstructedBy(MapperRegistry.AllMappers));

Is there anything equivalent to CtorDependency or is there another way
to construct a statement that will do something similar?




Re: Autofac Configuration of AutoMapper Jimmy Bogard 9/18/09 5:17 AM
Looks right - I'm not sure how AutoFac does lifetime management, but the Configuration hierarchy should be singleton, as well as IMappingEngine.