Search code examples
c#asp.net-mvcninjectaction-filter

Avoid the magic string when injecting a constructor argument from an attribute using Ninject's BindFilter method?


I have the following code in my Ninject module:

this.BindFilter<PermitFilter>(FilterScope.Controller, 0)
    .WhenControllerHas<PermitAttribute>()
    .WithConstructorArgumentFromControllerAttribute<PermitAttribute>(
        "permissions",
         attribute => attribute.Permissions);

Does Ninject provide any alternatives to using a string for the name of the constructor argument? For example, I would rather put a Permissions attribute on the constructor and refer to it in the binding statement (as with Ninject's regular binding mechanism). Is anything like this possible (or in the works for the next version)?


Solution

  • With the next release you can specify it about like this:

    this.BindFilter(
        x => new PermitFilter(
            x.Inject<ISomeDependency>(),
            x.FromControllerAttribute<PermitAttribute>().GetValue(attribute => attribute.Permissions)), 
            FilterScope.Controller, 
            0)
        .WhenActionMethodHas<PermitAttribute>();