【问题标题】:Replace Ninject with Simple Injector将 Ninject 替换为 Simple Injector
【发布时间】:2012-07-25 21:17:04
【问题描述】:

我在我的应用程序中使用了 Ninject。 Ninject 非常简单易学,但速度很慢,我尝试使用另一个 IoC 来比较它是否比 Ninject 更快。

有很多用于 MVC3 的 IoC 容器,Simple Injector 对我来说看起来非常好,但我在用 Simple Injector 替换 Ninject 时遇到了很多问题。

尤其是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>()

你能帮我吗?我已经阅读了文档并在 Google 上搜索过,但目前还没有解决方案。

【问题讨论】:

  • 除非您说明您需要帮助的内容,否则我们无法帮助您。什么失败了?

标签: c# dependency-injection ninject automapper simple-injector


【解决方案1】:

这个 Ninject 注册大致翻译成下面的 Simple Injector 注册:

container.Register<ITypeMapFactory, TypeMapFactory>();
container.RegisterCollection<IObjectMapper>(MapperRegistry.AllMappers());
container.RegisterSingleton<IConfiguration, ConfigurationStore>();
container.RegisterSingleton<IConfigurationProvider, ConfigurationStore>();
container.Register<IMappingEngine, MappingEngine>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多