与 ninject 的循环依赖 [英] Cyclic dependency with ninject

查看:27
本文介绍了与 ninject 的循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何用 ninject 绑定这样的东西的正确方法.

I'm trying to figure out correct way how to bind something like this with ninject.

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

它给了我一个例外:

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 实例(如单例).

I don't know how to write BaseModule. I need only one instance of MainClass and one instance of OtherClass (like singletons).

我尝试过这样的事情:

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

但有同样的错误.

以及如何编写只使用 MainClass 和 IMainService 接口的一个实例的绑定?

And how to write binding for using only one instance for MainClass and IMainService interfaces?

感谢您的回答.

推荐答案

正如错误消息所说,你在 MainClassOtherClass 之间有一个循环依赖,因为你可以't 在没有另一个实例的情况下创建一个.理想情况下,您应该重构您的类层次结构以消除此要求.

As the error message says, you have a cyclic dependency between MainClass and OtherClass since you can't create one without an instance of the other. Ideally, you should restructure your class hierarchy to remove this requirement.

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

If you can't, the solution is to use property injection for one (or both) of the classes, e.g.

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;
    }
}

这篇关于与 ninject 的循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆