Ninject action filter lifetime questions in MVC 3

I would like to use a global reach action filter in my MVC 3 application using Ninject; however, I am trying to understand the lifetime of this filter, its dependency and how to make changes in its dependency by decorating my controllers and / or methods of actions.

I would like my filter type to depend on objects whose lifetime is associated with a scope request, so something like this:

public sealed class MyGlobalActionFilter : IActionFilter
{
    public MyGlobalActionFilter(IService1 svc1, IService2 svc2, RequestType reqType)
    {
        // code here
    }

    // IActionFilter implementation here...
}

... and in the config module ...

Bind<IService1>().To<ConcreteService1>().InRequestScope()
Bind<IService2>().To<ConcreteService2>().InRequestScope()
BindFilter<MyGlobalActionFilter>(FilterScope.Global, null)
    .WhenControllerHas<RequestTypeAttribute>()
    .WithConstructorArgumentFromControllerAttribute<RequestTypeAttribute>(
        "reqType", 
        x => x.RequestType
    );
BindFilter<MyGlobalActionFilter>(FilterScope.Global, null)
    .WhenActionMethodHas<RequestTypeAttribute>()
    .WithConstructorArgumentFromActionAttribute<RequestTypeAttribute>(
        "reqType", 
        x => x.RequestType
    );
BindFilter<MyGlobalActionFilter>(FilterScope.Global)
    .When(x => true)
    .WithConstructorArgument("reqType", RequestType.Undefined)

And an attribute on controllers and / or action methods to represent the "request type" for a particular application:

[RequestType(RequestType.Type1)]
public sealed class SomeController : Controller { /* code here*/ }

, ? MyGlobalActionFilter HTTP? , , ?

, RequestType BindFilter , , , , , RequestType , a RequestTypeAttribute .

, !

+5
2

Microsoft, IFilterProvider . , , . , , , InRequestScope , Ninject .

, :

  • ActionFilterAttribute, IActionFilter , .
  • FilterScope.Global . /, .

, . , RequestType.Undefined , . , , , , .

+4

"System.Web.Mvc.GlobalFilters.Filters" - , " ", / , , IoC . , / ... ?

public abstract class BaseFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //user some service locator to retrieve IService1, IService2

        //some logic based on RequestType
    }

    protected RequestType { get; set; }
}

public class SomeFilter : BaseFilter
{
    public SomeFilter(RequestType requestType)
    {
        RequestType = requestType;
    }
}
0

All Articles