简体   繁体   English

Ninject循环依赖-已经使用属性注入

[英]Ninject cyclic dependency - already using property injection

I'm having a problem with a cyclic dependency in a project using dependency injection. 我在使用依赖项注入的项目中遇到循环依赖项问题。 In looking around, it seems that the only way to avoid it, other than restructuring (I did some of that too), is to use property injection. 在环顾四周时,除了进行重组(我也做过其中的一些工作)之外,似乎唯一避免这种情况的方法是使用属性注入。 I tried this, and it doesn't seem to help, but I'm not sure why. 我试过了,似乎没有帮助,但是我不确定为什么。 Here is the path that's causing issues. 这是导致问题的路径。

Activation path:
  6) Injection of dependency IUserRepository into property UserRepository of type ModelFactory{UserRole}
  5) Injection of dependency IUserRoleFactory into parameter userRoleFactory of constructor of type UserRoleService
  4) Injection of dependency IUserRoleService into property UserRoleService of type InternalUserBehavior
  3) Injection of dependency IInternalUserBehavior into parameter internalUserBehavior of constructor of type UserRepository
  2) Injection of dependency IUserRepository into parameter userRepository of constructor of type HomeController
  1) Request for HomeController

Now, it seems to know it's using property injection, and all of the behaviors and factories are in the same scope (call scope right now, but I've tried thread scope too), as well as the UserRepository. 现在,似乎知道它正在使用属性注入,并且所有行为和工厂都在同一个作用域(现在调用作用域,但我也尝试过线程作用域)以及UserRepository。

My understanding of the process is that it should be getting to 4, and be able to actually create the objects. 我对过程的理解是应该达到4,并且能够实际创建对象。 At this point, it should have a reference to a HomeController, IUserRepository, and IInternalUserBehavior. 在这一点上,它应该具有对HomeController,IUserRepository和IInternalUserBehavior的引用。 Then it should work on 5, and insert the completed IUserRoleService into the InternalUserBehavior. 然后,它应该在5上工作,并将完整的IUserRoleService插入InternalUserBehavior。 Finally, it should insert the the previously instantiated user repository (since it's in the same scope) into the property in the ModelFactory 最后,它应该将先前实例化的用户存储库(因为它在同一范围内)插入到ModelFactory的属性中

So I guess the short version of my question is: Why isn't property injection solving my cyclic dependency issue? 所以我想我的问题的简短版本是:为什么属性注入不能解决我的循环依赖问题?

It would help to see your code... but I think you are not understanding the process here. 这将有助于查看您的代码...但是我认为您不了解此处的过程。

At step 4) , Ninject has just created InternalUserBehavior and is injecting the properties. 在步骤4) ,Ninject刚刚创建了InternalUserBehavior并正在注入属性。 In step 5) , Ninject finds that it needs to create UserRoleService and proceeds to step 6) to create and then populate a ModelFactory{UserRole} . 在步骤5) ,Ninject发现它需要创建UserRoleService并继续执行步骤6)以创建然后填充ModelFactory{UserRole}

I assume your classes look something like: 我假设您的课程如下所示:

public class UserRoleService
{
   public UserRoleService(ModelFactory<UserRole> factory){}
}

public class ModelFactory<T>
{
    [Inject]
    public IUserRepository UserRepository { get; set; }
}

You definitely have a cyclic dependency, regardless of property injection. 不管属性注入如何,您肯定具有循环依赖性。 You need to resolve the cyclic dependency. 您需要解决循环依赖性。

Another route would be to use Lazy<IUserRepository> with constructor injection to avoid the cyclic dependency. 另一种方法是将Lazy<IUserRepository>与构造函数注入配合使用,以避免循环依赖性。 I have an answer that can help with that binding. 我有一个可以帮助您进行绑定的答案。

Effectively, your ModelFactory<> becomes: 实际上,您的ModelFactory<>变为:

public class ModelFactory<T>
{
    [Inject]
    public Lazy<IUserRepository> UserRepository { get; set; }
}

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

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