如何使用依赖注入与属性? [英] How to use dependency injection with an attribute?

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

问题描述

在一个MVC项目中,我创建了我有以下 RequirePermissionAttribute ,它可以放在任何需要特定权限的操作上(这个例子已经被简化了): p>

In an MVC project I'm creating I have the following RequirePermissionAttribute that gets put on any action that needs specific permissions (it's been simplified for this example):

public class RequirePermissionAttribute : ActionFilterAttribute, IAuthorizationFilter
{
    public Operation Permissions { get; set; }

    public RequirePermissionAttribute() { }

    public RequirePermissionAttribute(Operation permissions)
    {
        this.Permissions = permissions;
    }

    public bool AuthorizeCore(HttpContextBase httpContext)
    {
        IAuthorizationService authServ = new ASPNETAuthorizationService();
        return authServ.Authorize(httpContext);
    }

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        Enforce.ArgNotNull(filterContext);

        if (this.AuthorizeCore(filterContext.HttpContext))
        {
            // code snipped.
        }
        else
        {
            // code snipped.
        }
    }
}

所以这个问题显然是这样的是我的授权属性依赖于我创建的 ASPNETAuthorizationService 。我不能去构造方法,因为属性是编译时检查。

So the problem obviously with this is that my authorize attribute has a dependency on the ASPNETAuthorizationService that I created. I can't go the constructor way since attributes are compile-time checked.

有一件事要提到,我使用我自己的小IoC,我做了不支持物业注入(尚未)。当然,如果我去了物业注入路线,我必须增加支持(我必须做一些研究)。

One thing to mention, I'm using my own little IoC that I made and it doesn't have support for property injection (yet). Of course, if I did go the property injection route, I'd have to add support for it (which I'd have to do some research on).

什么是注入某个属性类的最佳方式是

What's the best way to inject something into an attribute class?

推荐答案

我原以为这是不可能的,但是我已经纠正了。这是Ninject的一个例子:

I originally thought this was not possible, but I stand corrected. Here's an example with Ninject:

http://codeclimber.net.nz/archive/2009/02/10/how-to-use-ninject- to-inject-dependencies-into-asp.net-mvc.aspx

现在这是一个很老的问题,框架已经发生了很大的变化。 Ninject现在允许您将绑定添加到特定的基于特定属性的存在过滤器,代码如下:

This is a pretty old question by now, and frameworks have changed quite a bit. Ninject now allows you to add bindings to specific filters based on the presence of specific attributes, with code like this:

// LogFilter is applied to controllers that have the LogAttribute
this.BindFilter<LogFilter>(FilterScope.Controller, 0)
     .WhenControllerHas<LogAttribute>()
     .WithConstructorArgument("logLevel", Level.Info);

// LogFilter is applied to actions that have the LogAttribute
this.BindFilter<LogFilter>(FilterScope.Action, 0)
     .WhenActionHas<LogAttribute>()
     .WithConstructorArgument("logLevel", Level.Info);

// LogFilter is applied to all actions of the HomeController
this.BindFilter<LogFilter>(FilterScope.Action, 0)
     .WhenControllerTypeIs<HomeController>()
     .WithConstructorArgument("logLevel", Level.Info);

// LogFilter is applied to all Index actions
this.BindFilter(FilterScope.Action, 0)
     .When((controllerContext,  actionDescriptor) =>
                actionDescriptor.ActionName == "Index")
     .WithConstructorArgument("logLevel", Level.Info);

这是符合原则的,

This is in keeping with the principle, argued by Mark Seeman and by the author of Simple Injector, which is that you should keep the logic of your action filter separate from the custom attribute class.

MVC 5和6也使它更容易将值注入到属性中,而不是以前的值。不过,将您的操作过滤器与您的属性分开是真正的最佳方法。

MVC 5 and 6 also make it far easier to inject values into attributes than it used to be. Still, separating your action filter from your attribute is really the best approach to take.

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

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