简体   繁体   English

用Simple Injector替换Ninject代码

[英]Replace Ninject code with Simple Injector

I have found this post Replace Ninject with Simple Injector 我发现这篇文章用简单的注入器替换Ninject

but havent found the solution for following migrations. 但还没有找到后续迁移的解决方案。 With Ninject: 使用Ninject:

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

I try to use this construction with Simple Injector: 我尝试将此结构与Simple Injector结合使用:

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

is it equal to Ninject one? 等于Ninject一吗?

The second part in Ninject is: Ninject的第二部分是:

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

to: 至:

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

is it equal to Ninject one? 等于Ninject一吗?

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: Ninject的第二部分是:

Again here, you want to register a delegate as singleton: 再次在这里,您想将委托注册为单例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM