How to use Ninject to inject services into MVC 3 FilterAttributes?

Ninject.Web. Mvc has this functionality built in called "BindFilter" which lets you map an attribute (that takes some or no constructor args) to a filter (which has its constructor args injected). Additionally, you can use it to copy values from the attribute and inject them as constructor args to the filter if you want.It also lets you change scope on your filters to be per action or per controller etc so that they actually get re-instantiated (normal action filters don't get re-instantiated per request) Here's an example of how I've used it to do a UoW action filter.

Ninject.Web. Mvc has this functionality built in called "BindFilter" which lets you map an attribute (that takes some or no constructor args) to a filter (which has its constructor args injected). Additionally, you can use it to copy values from the attribute and inject them as constructor args to the filter if you want.It also lets you change scope on your filters to be per action or per controller etc so that they actually get re-instantiated (normal action filters don't get re-instantiated per request).

Here's an example of how I've used it to do a UoW action filter.

Thank you for your reply. What I have written should work (in theory) as I have followed similar tutorials/wiki material online. Ideally I don't want to introduce another dependency to the project and overcomplicate things :( Currently, when I do the binding I have an option Bind().To().InRequestScope(); which should hopefully re-instantiate logger on each request.

– CodeRush Oct 5 at 15:27 1 So you're not using Ninject.Web. Mvc? You have also implemented a controller factory and all the other various parts where DI comes in?

That seems like a lot of unnecessary work when there's a well written, widely used, and unit tested library out there... – Brook Oct 5 at 15:31 It also prevents you from having Inject in your filter code, DE-coupling you from ninject, not the other way around. – Brook Oct 5 at 15:36 I didn't implement controller factory, I had to implement IDependencyResolver interface which has two method for obtaining a concrete implementation. This however doesn't work for attributes.

I will have to use Ninject.Web. MVC, but I'm still hoping to find a fix/solution to my problem as the majority of code is already in place. Sorry, I didn't fully understand your comment regarding de-coupling from Ninject.

Thank you :) – CodeRush Oct 5 at 15:46 I did look into Ninject.Web. MVC - I think it's great and I should have looked at it earlier. I found that some of their documentation is very brief, so it will keep me busy over the weekend.

Thank you for your advise. – CodeRush Oct 57 at 10:48.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions