8

我以前使用过 Autofac,但现在我想试试 SimpleInjector。我的问题是,在我的方法中调用 mappingEngine 时出现以下错误:

缺少类型映射配置或不支持的映射。

映射类型:Something -> SomethingDto 目标路径:IEnumerable`1[0]

源值:

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

^-- doesn't work

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

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

Mapper.Map 不是我想要的。我在这里基于此注册 Automapper:

将 Ninject 替换为 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();

我的问题是,如何在 SimpleInjector 中注册 IMappingEngine 并正确添加我的配置文件?

提前致谢!

迎接 mtrax

4

1 回答 1

8

解决了!:-)

在启动 MvcControllerFactory 后,我注册配置文件太晚了。我希望我的伪示例可以帮助您防止这样的错误。

// 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() 可在以下位置找到:Simple Injector MVC Integration Guide

于 2012-09-22T10:16:13.037 回答