Conditional bindings for filters in Ninject.Web.Mvc

102 views
Skip to first unread message

GP

unread,
Mar 11, 2011, 9:37:03 AM3/11/11
to ninject
I have a question regarding conditional bindings for filters in
Ninject.Web.Mvc. Is it possible to use conditional bindings to bind
the same filter to several different conditions? So in the example
below I want it to apply MyActionFilter to the MyMethod() method once
for each of the tagged attributes:

class KeyStore
{
public static Aap { // ... }
public static Noot { // ... }
public static Mies { // ... }
// ...
}

class AapAttribute : Attribute { }
// ...

class MyActionFilter : IActionFilter
{
public MyActionFilter (string key)
{
// ...
}

// ...
}

class Bindings : NinjectModule
{
public override void Load()
{
// ...

this.BindFilter<MyActionFilter>(FilterScope.Action, 0)
.WhenActionHas<AapAttribute>()
.WithConstructorArgument("key", context => KeyStore.Aap);

this.BindFilter<MyActionFilter>(FilterScope.Action, 0)
.WhenActionHas<NootAttribute>()
.WithConstructorArgument("key", context => KeyStore.Noot);

this.BindFilter<MyActionFilter>(FilterScope.Action, 0)
.WhenActionHas<MiesAttribute>()
.WithConstructorArgument("key", context => KeyStore.Mies);

// ...
}
}

[Aap, Noot, Mies]
public ActionResult MyMethod()
{
// ...
}

I tried this but I get an ActivationException stating that there are
more than one matching bindings available.

Thx!

Remo Gloor

unread,
Mar 11, 2011, 12:01:25 PM3/11/11
to ninject
No that's not possible at the moment. I think the best way is to
create 3 derived classes(AapActionFilter, ....) that pass the
configuration to the base class.

Remo Gloor

unread,
Mar 11, 2011, 12:06:10 PM3/11/11
to ninject
I had a short look at the source and it seems to be possible to add
this in a future version.

GP

unread,
Mar 11, 2011, 12:27:55 PM3/11/11
to ninject
Thank you! Indeed, derived classes seems to be the best solution as
long as your configuration variations are limited. Looking forward to
how Ninject and its Mvc extension develops further. Thanks again!
Reply all
Reply to author
Forward
0 new messages