I am trying to find a way to use Ninject to inject constructor dependencies into filters. I am finding many articles describing property injection which is now advised against, but the remainder of articles involve complex setups with factories, locators, global wrappers or stub attributes.
With MVC allowing you to override almost any part of it's operation I would have thought it would be simply a case of creating your own filter provider in a similar fashion to how you create your own dependency resolver.
What is the now correct way to allow injection, or does it become easier if you use certain types of filters vs others?
Assuming that the attribute is to be a part of the metadata, which means that it should be instantiated at the compile time, it is not possible to have a repository injected into an attribute by any ioc container. Containers operate in run time.
There is a way to use constructor injection.
Then you create a filter class as an IActionFilter.
The BindFilter syntax is part of Ninject.Web.Mvc.FilterBindingSyntax.
If you want to inject dependencies to a filer, you want to use property injection instead of constructor injection.
Not the answer you're looking for? Browse other questions tagged c# asp.net-mvc ninject or ask your own question.
Injecting dependencies into ASP.NET MVC 3 action filters. What's wrong with this approach?