Re: Autofac Configuration of AutoMapper

385 views
Skip to first unread message

Nicholas Blumhardt

unread,
Sep 18, 2009, 12:02:31 AM9/18/09
to aut...@googlegroups.com, automapp...@googlegroups.com
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?




Jimmy Bogard

unread,
Sep 18, 2009, 8:17:05 AM9/18/09
to automapp...@googlegroups.com
Looks right - I'm not sure how AutoFac does lifetime management, but the Configuration hierarchy should be singleton, as well as IMappingEngine.
Reply all
Reply to author
Forward
0 new messages