Search code examples
c#dependency-injectionninjectsimple-injector

Replace Ninject code with Simple Injector


I have found this post Replace Ninject with Simple Injector

but havent found the solution for following migrations. With Ninject:

public class ServiceClass : IServiceClass
...
IKernel kernel
...
this.kernel.Bind<IServiceClass>().ToMethod(context => this);

I try to use this construction with Simple Injector:

public class ServiceClass : IServiceClass
   ...
   Container container
   ...
   this.container = container;
   this.container.Register<IServiceClass>(() => container.GetInstance<ServiceClass >());

is it equal to Ninject one?

The second part in Ninject is:

public void BindSomeCallback(DelegateNumberOne delegateNumberOne)
{        
   this.kernel.Rebind<DelegateNumberOne>().ToConstant(delegateNumberOne);
}

to:

public void BindSomeCallback(DelegateNumberOne delegateNumberOne)
{ 
   this.container.Register<DelegateNumberOne, delegateNumberOne>();
}

Solution

  • is it equal to Ninject one?

    Nope. This is:

    this.container.Register<IServiceClass>(() => this);
    

    But since you're actually registering a singleton, you can better write it as follows:

    this.container.RegisterSingle<IServiceClass>(this);
    

    The second part in Ninject is:

    Again here, you want to register a delegate as singleton:

    this.container.RegisterSingle<DelegateNumberOne>(delegateNumberOne);