Replace Ninject with Simple Injector

2019-02-05 07:09发布

问题:

I've used Ninject for my application. Ninject is really simple and easy to learn, but its quite slow and I try to use another IoC to compare if its faster as with Ninject.

There are a lot of IoC containers for MVC3 and Simple Injector looks really good to me, but I've a lot of problems with replacting Ninject with Simple Injector.

Especially with the AutoMapper. I try to convert this lines into Simple Injector code.

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>()

Do you can help me? I've read the documentation and googled, but no solution so far.

回答1:

This Ninject registration roughly translates to the following Simple Injector registration:

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