Ninject循环依赖-已使用属性注入 [英] Ninject cyclic dependency - already using property injection

查看:156
本文介绍了Ninject循环依赖-已使用属性注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用依赖项注入的项目中,循环依赖项存在问题.在环顾四周时,除了进行重组(我也做过其中的一些工作)之外,似乎唯一避免这种情况的方法是使用属性注入.我试过了,似乎没有帮助,但是我不确定为什么.这是引起问题的路径.

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

现在,似乎知道它正在使用属性注入,并且所有行为和工厂都在同一个作用域(现在调用作用域,但我也尝试过线程作用域)以及UserRepository.

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.

我对过程的理解是应该达到4,并且能够实际创建对象.此时,它应该具有对HomeController,IUserRepository和IInternalUserBehavior的引用.然后,它应该在5上工作,并将完整的IUserRoleService插入InternalUserBehavior.最后,它应该将先前实例化的用户存储库(因为它在相同的作用域内)插入到ModelFactory的属性中

My understanding of the process is that it should be getting to 4, and be able to actually create the objects. At this point, it should have a reference to a HomeController, IUserRepository, and IInternalUserBehavior. Then it should work on 5, and insert the completed IUserRoleService into the InternalUserBehavior. Finally, it should insert the the previously instantiated user repository (since it's in the same scope) into the property in the 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.

在步骤4)中,Ninject刚创建了InternalUserBehavior并正在注入属性.在步骤5)中,Ninject发现需要创建UserRoleService,然后继续执行步骤6)进行创建,然后填充ModelFactory{UserRole}.

At step 4), Ninject has just created InternalUserBehavior and is injecting the properties. In step 5), Ninject finds that it needs to create UserRoleService and proceeds to step 6) to create and then populate a ModelFactory{UserRole}.

我认为您的课程如下:

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.

另一种方法是将Lazy<IUserRepository>与构造函数注入配合使用,以避免循环依赖性. 我有一个可以帮助您进行绑定的答案.

Another route would be to use Lazy<IUserRepository> with constructor injection to avoid the cyclic dependency. I have an answer that can help with that binding.

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

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

这篇关于Ninject循环依赖-已使用属性注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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