c# - ninject 的循环依赖

标签 c# dependency-injection inversion-of-control ninject

我正在尝试找出如何使用 ninject 绑定(bind)这样的东西的正确方法。

interface IMainService
{
    void DoStuff();
}

interface IOtherService
{
    void DoSomeMagic();
}

abstract class BaseClass
{
    //many stuff here
}

class MainClass : BaseClass, IMainService
{
    public MainClass(IOtherService s)
    {
    }

    public void DoStuff()
    {
        throw new NotImplementedException();
    }

    //do many other things
}

class OtherClass : IOtherService
{
    public OtherClass(IMainService s)
    {
    }

    public void DoSomeMagic()
    {
        throw new NotImplementedException();
    }
}

class BaseModule : NinjectModule
{
    public override void Load()
    {
        Bind<MainClass>().To<MainClass>();
        Bind<IMainService>().To<MainClass>();
        Bind<IOtherService>().To<OtherClass>();
    }
}

static class Program
{
    static void Main()
    {
        var kernel = new StandardKernel(new BaseModule());
        var main = kernel.Get<MainClass>();
    }
}

它给了我异常(exception):

Error activating IOtherService using binding from IOtherService to OtherClass
A cyclical dependency was detected between the constructors of two services.

Activation path:
  4) Injection of dependency IOtherService into parameter s of constructor of type MainClass
  3) Injection of dependency IMainService into parameter s of constructor of type OtherClass
  2) Injection of dependency IOtherService into parameter s of constructor of type MainClass
  1) Request for MainClass

Suggestions:
  1) Ensure that you have not declared a dependency for IOtherService on any implementations of the service.
  2) Consider combining the services into a single one to remove the cycle.
  3) Use property injection instead of constructor injection, and implement IInitializable if you need initialization logic to be run after property values have been injected.

我不知道怎么写BaseModule。我只需要一个 MainClass 实例和一个 OtherClass 实例(如单例)。

我试过这样的事情:

Bind<MainClass>().To<MainClass>().InSingletonScope();
Bind<IMainService>().To<MainClass>().InRequestScope();
Bind<IOtherService>().To<OtherClass>().InSingletonScope();

但同样的错误。

以及如何编写仅使用 MainClass 和 IMainService 接口(interface)的一个实例的绑定(bind)?

感谢您的回答。

最佳答案

如错误消息所述,您在 MainClassOtherClass 之间存在循环依赖,因为您无法在没有另一个实例的情况下创建一个。理想情况下,您应该重组类层次结构以消除此要求。

如果不能,解决方案是对一个(或两个)类使用属性注入(inject),例如

public interface IMainService
{
    void DoStuff();
    IOtherService OtherService { set; }
}

public class MainClass
{
    public IOtherService OtherService { get; set; }
    public void DoStuff() { ... }
}

public class OtherService
{
    public OtherService(IMainService main)
    {
        main.OtherService = this;
    }
}

关于c# - ninject 的循环依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3511547/

相关文章:

javascript - AppGyver 和 AngularJS : dependency injection failing with 2 services from same module

asp.net-mvc-3 - 策略模式在 Asp.net MVC 3.0 中创建与 Controller 类的依赖关系

c# - 给要加密的数据加盐?

c# - Xamarin.iOS : Black screen after launch screen when no storyboard

c# - 尽管 TransactionScopeOption.Suppress 嵌套事务回滚

c# - C# 中的 List<T<U>> 其中 T 和 U 是接口(interface)

c# - 我知道如何使用依赖注入(inject),但我认为它没有实际优势

java - Spring容器可以使用自定义方法注入(inject)协作者吗?

c# - 如何在Module中使用container.Resolve?

c#-4.0 - StructureMap - 每个命名实例有不同的生命周期