Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Injector registering IMappingEngine (AutoMapper)

I used Autofac before but now I want give SimpleInjector a try. My problem is, on calling the mappingEngine within my method I get the following error:

Missing type map configuration or unsupported mapping.

Mapping types: Something -> SomethingDto Destination path: IEnumerable`1[0]

Source value:

_mappingEngine.Map<IEnumerable<SomethingDto>>(IEnumerableOfSomething);

^-- doesn't work

Mapper.Map<IEnumerable<SomethingDto>>(IEnumerableOfSomething);

^-- works (That's not what I want)

Mapper.Map is not that what I want. Im registering Automapper based on this here:

Replace Ninject with Simple Injector

container.Register<ITypeMapFactory, TypeMapFactory>();
container.RegisterAll<IObjectMapper>(
    MapperRegistry.AllMappers());
container.RegisterSingle<ConfigurationStore>();
container.Register<IConfiguration>(() => 
    container.GetInstance<ConfigurationStore>());
container.Register<IConfigurationProvider>(() => 
    container.GetInstance<ConfigurationStore>());
container.Register<IMappingEngine, MappingEngine>();

Mapper.Initialize(x =>
            {
                var profiles = container.GetAllInstances<Profile>();

                foreach (var profile in profiles)
                {
                    x.AddProfile(profile);
                }
            });

        Mapper.AssertConfigurationIsValid();

My question ist, how do I register IMappingEngine in SimpleInjector and add my Profiles correctly?

Thanks in advance!

Greets mtrax

like image 983
emcoding Avatar asked Sep 21 '12 20:09

emcoding


1 Answers

Solved! :-)

I registered my profiles too late, after initating the MvcControllerFactory. I hope my pseudo example helps you to prevent such a mistake.

// SimpleInjector
var container = new Container();

// AutoMapper registration
container.Register<ITypeMapFactory, TypeMapFactory>();
container.RegisterCollection(MapperRegistry.Mappers);
container.RegisterSingleton<ConfigurationStore>();
container.Register<IConfiguration>(container.GetInstance<ConfigurationStore>);
container.Register<IConfigurationProvider>(container.GetInstance<ConfigurationStore>);
container.RegisterSingleton(() => Mapper.Engine);

// AutoMapper Profiles registration
container.RegisterCollection<Profile>(new MappingAProfile(), 
                                      new MappingBProfile(), 
                                      new MappingCProfile());

// Adding AutoMapper profiles
Mapper.Initialize(x =>
    {
        var profiles = container.GetAllInstances<Profile>();

        foreach (var profile in profiles)
        {
            x.AddProfile(profile);
        }
    });

Mapper.AssertConfigurationIsValid();

container.Verify();

container.RegisterAsMvcControllerFactory();

*RegisterAsMvcControllerFactory() to find at: Simple Injector MVC Integration Guide.

like image 119
emcoding Avatar answered Nov 12 '22 10:11

emcoding