-
Notifications
You must be signed in to change notification settings - Fork 36
Description
I have these bindings in NinjectModule:
this.Bind<IBar>().ToMethod(ctx => new Bar()).WhenTargetHas<BarAttribute>();
this.Bind<IBar>().ToMethod(ctx => new Bar2());
When I inject IBar
, Ninject
correctly injects new Bar()
when there's BarAttribute
and
injects new Bar2()
without BarAttribute
.
For instance,
public class BarController
{
public BarController([Bar]IBar bar) {} // injects new Bar()
}
public class Bar2Controller
{
public Bar2Controller(IBar bar) {} // injects new Bar2()
}
But using BindHttpFilter
, this "chain" behavior is different..
this.BindHttpFilter<FooAttribute>(FilterScope.Action)
.WhenActionMethodHas<AllowAnonymousAttribute>()
.WithConstructorArgument("foo", (object)null);
this.BindHttpFilter<FooAttribute>(FilterScope.Action);
public class FooController
{
[AllowAnonymous]
public Method() {}
}
public class FooAttribute : AuthorizeAttribute
{
public FooAttribute(IDependency dependency) {}
}
When I request /foo/method
, Ninject calls FooAttribute twice and injects null (using first binding) as expected, then instantiates FooAttribute again with IDependency binding.
What I expected was Ninject.Webapi chain the conditions and solve at the first match, as it does with normal Bind<>.
To solve my need (inject some paremeter when action is decorated with [AllowAnonymous]
, and another parameter when action isn't decorated with [AllowAnonymous]
), I need to create mutual exclusive conditions which is a bad to maintain these two behaviors with Ninject Bind<> and Ninject.Web.WebApi BindHttpFilter<>:
this.BindHttpFilter<FooAttribute>(FilterScope.Action)
.WhenActionMethodHas<AllowAnonymousAttribute>()
.WithConstructorArgument("foo", (object)null);
this.BindHttpFilter<FooAttribute>(FilterScope.Action)
.When((httpConfig, actionDescriptor) => !actionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any())
What do you think ?