11

我如何注入以下依赖项?

public class Authenticate : AuthorizeAttribute
{
        [Dependency]
        public IAuthenticate AuthenticateLibrary { get; set; }

        [Dependency]
        public ILibrary BaseLibrary { get; set; }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
        }
}

我正在使用 Unity 2 注入所有控制器。是否有 Unity 2 的教程并将依赖项注入过滤器?

4

3 回答 3

13

Brad Wilson 有一个很好的关于服务位置的系列,其中包括如何创建自己的支持依赖注入的过滤器提供程序:http: //bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html(向下滚动到“向过滤器添加依赖注入”部分)。

  • 复制他为 UnityFilterAttributeFilterProvider.cs 提供的代码。

UnitFilterAttributeFilterProvider.cs

using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.Practices.Unity;

public class UnityFilterAttributeFilterProvider : FilterAttributeFilterProvider {
    private IUnityContainer _container;

    public UnityFilterAttributeFilterProvider(IUnityContainer container) {
        _container = container;
    }

    protected override IEnumerable<FilterAttribute> GetControllerAttributes(
                ControllerContext controllerContext,
                ActionDescriptor actionDescriptor) {

        var attributes = base.GetControllerAttributes(controllerContext,
                                                      actionDescriptor);
        foreach (var attribute in attributes) {
            _container.BuildUp(attribute.GetType(), attribute);
        }

        return attributes;
    }

    protected override IEnumerable<FilterAttribute> GetActionAttributes(
                ControllerContext controllerContext,
                ActionDescriptor actionDescriptor) {

        var attributes = base.GetActionAttributes(controllerContext,
                                                  actionDescriptor);
        foreach (var attribute in attributes) {
            _container.BuildUp(attribute.GetType(), attribute);
        }

        return attributes;
    }
}
  • 修改 global.asax.cs 的 Application_Start 以使 UnityFilterAttributeFilterProvider 成为 MVC 应用程序的过滤器提供程序。

.

protected void Application_Start() {
    // ...

    var oldProvider = FilterProviders.Providers.Single(
        f => f is FilterAttributeFilterProvider
    );
    FilterProviders.Providers.Remove(oldProvider);

    var container = new UnityContainer();
    var provider = new UnityFilterAttributeFilterProvider(container);
    FilterProviders.Providers.Add(provider);

    // ...
}
  • 使用 [Dependency] 属性装饰您希望 Unity 为其注入值的属性。然后你应该很高兴。
于 2011-05-26T04:19:50.850 回答
9

由于 Unity 没有实例化过滤器,因此无法注入它们。您将不得不求助于服务定位器模式,如下所示:

public class Authenticate : AuthorizeAttribute
{
    public IAuthenticate AuthenticateLibrary { get; private set; }

    public ILibrary BaseLibrary { get; private set; }

    public Authenticate()
    {
        AuthenticateLibrary = DependencyResolver.Current.GetService<IAuthenticate>();
        BaseLibrary = DependencyResolver.Current.GetService<ILibrary >();
    }
    ...
}
于 2011-05-26T00:55:31.457 回答
8

对于所有(像我一样)来到这里寻找 MVC4 + Unity 解决方案的人,虽然接受的答案非常有效,但我想补充一点,现在您也可以简单地覆盖类的GetFilters方法FilterAttributeFilterProvider

public class CustomFilterProvider : FilterAttributeFilterProvider
{
    private readonly IUnityContainer container;

    public CustomFilterProvider(IUnityContainer container)
    {
        this.container = container;
    }

    public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        var filters = base.GetFilters(controllerContext, actionDescriptor);

        var enumerable = filters as IList<Filter> ?? filters.ToList();

        foreach (var filter in enumerable)
        {
            container.BuildUp(filter.Instance.GetType(), filter.Instance);
        }

        return enumerable;
    }
}

干杯。

于 2013-07-08T16:10:06.533 回答