使用Ninject过滤器构造器注入 [英] Filter constructor injection using Ninject

查看:194
本文介绍了使用Ninject过滤器构造器注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法,使用Ninject注入构造函数依赖关系的过滤器。我发现很多文章描述属性注入这是现在劝告反对,但文章涉及剩余设有工厂,定位器,全球包装或存根属性复杂的设置。

I am trying to find a way to use Ninject to inject constructor dependencies into filters. I am finding many articles describing property injection which is now advised against, but the remainder of articles involve complex setups with factories, locators, global wrappers or stub attributes.

使用MVC让您覆盖几乎任何部分的运作,我会认为这将是简单地以类似的方式向你如何创建自己的依赖解析器创建自己的过滤器供应商的情况。

With MVC allowing you to override almost any part of it's operation I would have thought it would be simply a case of creating your own filter provider in a similar fashion to how you create your own dependency resolver.

什么是允许注射现在正确的方法,或者如果你使用某些类型的过滤器VS别人它变得更容易?

What is the now correct way to allow injection, or does it become easier if you use certain types of filters vs others?

 public class UserValidationAttribute : ActionFilterAttribute
 {
    private IRepository repository;

    public UserValidationAttribute(IRepository repository)
    {
        this.repository = repository;
    }
}


推荐答案

假设该属性是成为元数据的一部分,这意味着它应在的编译时间被实例化,它是不可能有任何IOC容器注入的属性的信息库。在容器运行时操作。

Assuming that the attribute is to be a part of the metadata, which means that it should be instantiated at the compile time, it is not possible to have a repository injected into an attribute by any ioc container. Containers operate in run time.

这篇关于使用Ninject过滤器构造器注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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