用简单的注射器替换ninject
-
11-12-2019 - |
题
我已经为我的应用程序使用了ninject。ninject真的很简单易学习,但它非常慢,我试图使用另一个IOC来比较它与ninject的速度更快。
MVC3有很多IOC容器,简单的注射器对我来说真的很好,但我有很多问题与简单注射器。
尤其是具有AutoMapper
。我尝试将此线转换为简单的注射器代码。
Bind<ITypeMapFactory>().To<TypeMapFactory>();
foreach (var mapper in MapperRegistry.AllMappers())
{
Bind<IObjectMapper>().ToConstant(mapper);
}
Bind<ConfigurationStore>().ToSelf().InSingletonScope()
.WithConstructorArgument("mappers",
ctx => ctx.Kernel.GetAll<IObjectMapper>());
Bind<IConfiguration>()
.ToMethod(ctx => ctx.Kernel.Get<ConfigurationStore>());
Bind<IConfigurationProvider>().ToMethod(ctx =>
ctx.Kernel.Get<ConfigurationStore>());
Bind<IMappingEngine>().To<MappingEngine>()
.
你可以帮帮我吗?我读过文档并谷歌剧,但到目前为止没有解决方案。
解决方案
此ninject注册大致转换为以下简单的注射器注册:
container.Register<ITypeMapFactory, TypeMapFactory>();
container.RegisterCollection<IObjectMapper>(MapperRegistry.AllMappers());
container.RegisterSingleton<IConfiguration, ConfigurationStore>();
container.RegisterSingleton<IConfigurationProvider, ConfigurationStore>();
container.Register<IMappingEngine, MappingEngine>();
. 不隶属于 StackOverflow