Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Ninject with Simple Injector

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.

like image 792
Don Merdino Avatar asked Jul 25 '12 21:07

Don Merdino


People also ask

Why use Simple Injector?

The purpose of a Simple Injector is to provide . NET application developers with an easy, flexible, and fast Inversion of Control library that uses best practices to steer developers to success.

Why Ninject is used?

It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner. By using Ninject to support your software's architecture, your code will become easier to write, reuse, test and modify.

How to implement dependency injection in c# using Ninject?

Step 1: We are creating an instance of Class StandardKernel. Step 2: Then we will load the Kernel. Step 3: Get the instance of the specific service that we want to inject. Step 4: Then inject the dependency.

What is Injector in dependency injection?

Dependency Injection. As you can see, the injector class creates an object of the service class, and injects that object to a client object. In this way, the DI pattern separates the responsibility of creating an object of the service class out of the client class.


1 Answers

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>();
like image 186
Steven Avatar answered Oct 13 '22 00:10

Steven