ninject的依赖注入问题 [英] Dependency injection issue with ninject

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

问题描述





我正在编写一个mvc 4应用程序并使用Ninject进行依赖注入。下面给出的依赖注入类



Hi,

I am writing a mvc 4 application and using Ninject for dependency injection. The dependency injection class as given below

public class NinjetDependencyResolver : IDependencyResolver
{
    private IKernel kernel;

    public NinjetDependencyResolver()
    {
        kernel = new StandardKernel();
        this.AddBindings();
    }

    private void AddBindings()
    {
        //Add binding here
        kernel.Bind<IHRRepository>().To<HRRepository>();
    }

    public object GetService(Type serviceType)
    {
        return kernel.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return kernel.GetAll(serviceType);
    }
}





此类与控制器一起工作正常。但是我已经创建了一个类,如下所示





this class is working ok with the Controllers. But I have created a class as given below

public class RoleVerfier
     {
       private IHRRepository repository;

       public RoleVerfier(_repository)
       {

           this.repository = _repository;
       }

       //some code here for funcionality
   }





当我尝试创建RoleVerifier类的对象时,如下所示





and when I try to create a object of the RoleVerifier class as given below

try
{
       RoleVerfier verifier = new RoleVerfier();
                            
       //some code here for funcionality
                           
}
catch (Exception ex)
{

}





由于RoleVerifier类构造函数要求提供IHRRepository接口,因此会产生编译时错误。但我认为IHRRepository界面应该由Ninject自动提供,因为它正在为Controller类做。





我不知道是什么问题是什么?



我尝试过:



请帮忙我尽快



It is giving a Compile time error because the RoleVerifier Class constructor is asking to supply IHRRepository interface. But i think IHRRepository interface should be automatically provided by the Ninject as it is doing for the Controller classes.


I do not know what is the issue?

What I have tried:

Please help me as soon as possible

推荐答案

您的代码是创建类的新实例,因此您需要遵守构造函数规则。您的代码中没有任何内容正在使用Ninject,因此您不会进行依赖项解析。你需要做的是使用DependencyResolver获取你的类的实例



c# - Asp.net mvc 4依赖解析器 - Stack Overflow [ ^ ]



如果你的ninject解析器注册为依赖解析器然后根据上面的链接使用DependencyResolver将对象创建传递给ninject,并且ninject将为您注入构造函数。您的代码适用于控制器的原因是因为当mvc框架创建控制器的实例时,它使用DependencyResolver来执行此操作。您不能只使用new创建类似于普通的类,并期望它们通过ninject路由。
Your code is create a new instance of the class so you need to obey the constructor rules. There is nothing in your code that is using Ninject so you're not doing dependency resolving. What you have to do is use the DependencyResolver to get the instance of your class

c# - Asp.net mvc 4 dependency resolver - Stack Overflow[^]

If your ninject resolver is registered as a dependency resolver then using DependencyResolver as per the link above will pass the object creation to ninject and the constructor will be injected for you by ninject. The reason your code works for controllers is because when the mvc framework creates an instance of your controller it uses the DependencyResolver to do so. You can't just create classes like normal using "new" and expect them to be routed through ninject.


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

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