ASP.NET MVC 5 Filter和Ninject

[英]ASP.NET MVC 5 Filter and Ninject


I have this DDD application that has MVC on the Presentation, with Ninject.

我有這個DDD應用程序,在演示文稿上有MVC,有Ninject。

I have the CrossCutting Layer binding the injections from all the layers, and it works like a charm.

我有CrossCutting Layer綁定所有圖層的注入,它就像一個魅力。

What i can't make work, is when it comes to Filter Attributes. I have this filter that checks one cookie and depending on it's value, executes queries on the Database. With this Architecture, i just can't make my Filter access my Repository directly, it would be disrespectful to the methodology i'm applying.

我不能做的工作就是過濾屬性。我有這個過濾器檢查一個cookie,並根據它的值,在數據庫上執行查詢。使用這種架構,我只是不能讓我的過濾器直接訪問我的存儲庫,這對我正在應用的方法是不尊重的。

I have my IFooAppService interface and have it's methods that eventually will hit the repository for Data requesting. I can successfully inject it in any Controller constructor and it will execute my _fooAppService.Query(), but i have this XooFilter above some actions in my application, and i just can't pass my _fooAppService to it. Here's my XooFilter:

我有我的IFooAppService接口,並擁有最終將訪問數據庫請求的存儲庫的方法。我可以在任何Controller構造函數中成功注入它,它將執行我的_fooAppService.Query(),但我在我的應用程序中有一些XooFilter上面的一些操作,我只是無法將_fooAppService傳遞給它。這是我的XooFilter:

public class XooFilter : ActionFilterAttribute
{
    private readonly IFooAppService _fooAppService;

    public XooFilter(IFooAppService fooAppService)
    {
        _fooAppService = fooAppService;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // LOGIC...

        var fooValue = _fooAppService.Query();

        // MORE LOGIC..
    }
}

But, when i insert my filter above any Action [XooFilter], it won't work. It says the filter has no constructor with 0 elements.

但是,當我在任何Action [XooFilter]上面插入我的過濾器時,它將無法正常工作。它說過濾器沒有帶0個元素的構造函數。

I just can't figure it out how to do it nicely and keep the characteristics from DDD and IoC. I found the article below, but couldn't find any answer:

我只是無法弄清楚如何做得很好並保持DDD和IoC的特性。我找到了下面的文章,但找不到任何答案:

Injecting dependencies into ASP.NET MVC 3 action filters. What's wrong with this approach?

將依賴項注入ASP.NET MVC 3動作過濾器。這種方法有什么問題?

2 个解决方案

#1


You could resolve your service this way

您可以通過這種方式解決您的服務

System.Web.Mvc.DependencyResolver.Current.GetService(typeof (IFooAppService ));

#2


There's a Ninject extension package called Ninject.Web.Mvc (which you may already have). This allows you to set filter binding like so;

有一個名為Ninject.Web.Mvc的Ninject擴展包(您可能已經擁有)。這允許您像這樣設置過濾器綁定;

public class LoggingModule : NinjectModule
{
    public override void Load()
    {
        this.Bind<ILog>().ToMethod(GetLogger);
        this.BindFilter<LogFilter>(FilterScope.Controller, 0)
            .WithConstructorArgument("logLevel", Level.Info);
    }

    private static ILog GetLogger(IContext ctx)
    {
        var filterContext = ctx.Request.ParentRequest.Parameters
            .OfType<FilterContextParameter>().SingleOrDefault();

        return
            LogManager.GetLogger(filterContext == null
                ? ctx.Request.Target.Member.DeclaringType
                : filterContext.ActionDescriptor.ControllerDescriptor.ControllerType);
    }
}

Note the BindFilter method call. This works as of v.3.3.0.

注意BindFilter方法調用。這適用於v.3.3.0。

Once configured, using it is like the usual constructor injection;

配置完成后,使用它就像通常的構造函數注入一樣;

public class LogFilter : IActionFilter
{
    public LogFilter(ILog logger)
    {
        // store your instance of "logger" here
    }
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/05/316bda04b4e39da2cfc27c283e3a9c9.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com