Structure Map 和 Windsor 有配置示例:http://www.cprieto.com/index.php/2009/08/20/using-automapper-with-castle-windsor/ http://www.cprieto.com/index.php/2009/08/20/using-automapper-with-castle-windsor/
但我还没有找到 Ninject 的任何内容。
你知道如何将这些映射转换为 Ninject 吗?
这真的很简单,只需加载这个模块:
public class AutoMapperModule : NinjectModule
{
public override void Load()
{
Bind<ITypeMapFactory>().To<TypeMapFactory>();
foreach (var mapper in MapperRegistry.AllMappers())
Bind<IObjectMapper>().ToConstant(mapper);
Bind<Configuration>().ToSelf().InSingletonScope()
.WithConstructorArgument("mappers",
ctx => ctx.Kernel.GetAll<IObjectMapper>());
Bind<IConfiguration>().ToMethod(ctx => ctx.Kernel.Get<Configuration>());
Bind<IConfigurationProvider>().ToMethod(ctx =>
ctx.Kernel.Get<Configuration>());
Bind<IMappingEngine>().To<MappingEngine>();
}
}
对此有几点需要注意:
而不是仅仅提供MapperRegistry.AllMappers
作为构造函数参数Configuration
,它实际上束缚了每个人IObjectMapper
然后使用内核本身来获取构造函数参数WithConstructorArgument
捆绑。这样做的原因是为了让您可以加载自己的IObjectMapper
如果您决定要编写自己的自定义映射器,请绑定到内核。
自我约束的原因Configuration
然后方法绑定IConfiguration
and IConfigurationProvider
与 Windsor 不同的是,Ninject 不提供任何一流的支持来将多个接口绑定到单个目标范围,因此出现了这个 hack。
这就是全部内容了。编写容器类并依赖于IConfiguration
(如果您想创建新地图)和/或IMappingEngine
(实际进行映射),Ninject 将毫无问题地注入它们。
如果您想要超松耦合并在自己的类中定义每个映射,那么您可能需要看看约定扩展 https://github.com/ninject/ninject.extensions.conventions对于 Ninject,它可以进行类似于 Windsor 的装配扫描FromAssembly
。这也可以加载任何自定义IObjectMapper
您可以在单独的库中定义的类。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)