Mvc 3/Unity 2 将依赖项注入过滤器?

发布于 2024-11-10 04:26:36 字数 403 浏览 0 评论 0原文

我如何注入以下依赖项?

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 和将依赖项注入过滤器的教程吗?

How can i inject the following dependencies ??

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

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

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
        }
}

I am using Unity 2 to inject all the controllers. Is there a tutorial for Unity 2 and injecting dependencies into filters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪落纷纷 2024-11-17 04:26:36

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 为其注入值的属性。然后你就可以出发了。

Brad Wilson has a good series on Service Location which includes how to create your own filter provider that can support dependency injection: http://bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html (Scroll down to the section "Adding Dependency Injection to Filters").

  • Copy the code he provides for the 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;
    }
}
  • Modify the Application_Start of the global.asax.cs to make the UnityFilterAttributeFilterProvider the filter provider for your MVC app.

.

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);

    // ...
}
  • Decorate the properties that you want Unity to inject a value for with the [Dependency] attribute. And then you should be good to go.
黑寡妇 2024-11-17 04:26:36

由于 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 >();
    }
    ...
}

Since Unity is not instantiating the Filters, it cannot inject them. You would have to resort to service locator pattern such as in the following:

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 >();
    }
    ...
}
江湖正好 2024-11-17 04:26:36

对于所有来到这里寻找 MVC4 + Unity 解决方案的人(像我一样),虽然接受的答案工作得很好,但我想补充一点,现在您也可以简单地重写 GetFilters 方法。 code>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;
    }
}

干杯。

For all people that (like me) arrived here looking for a solution in MVC4 + Unity, although the accepted answers works perfectly, I wanted to add that now you can also simply override the GetFilters method of the FilterAttributeFilterProvider class:

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;
    }
}

Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文