Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strike>I'm not positive, but I believe you can just use an empty constructor (for the <em>attribute</em> part) and then have a constructor that actually injects the value (for the <em>filter</em> part).*</strike></p> <p><strong>Edit</strong>: After a little reading up, it appears that the accepted way to do this is via property injection:</p> <pre><code>public class MyActionFilter : ActionFilterAttribute { [Injected] public IMyService MyService {get;set;} public override void OnActionExecuting(ActionExecutingContext filterContext) { MyService.DoSomething(); base.OnActionExecuting(filterContext); } } </code></pre> <p>Regarding the <em>why not use a Service Locator</em> question: It mostly just reduces the flexibility of your dependency injection. For example, what if you were injecting a logging service, and you wanted to automatically give the logging service the name of the class it's being injected into? If you use constructor injection, that would work great. If you're using a Dependency Resolver/Service Locator, you'd be out of luck.</p> <h3>Update</h3> <p>Since this got accepted as the answer, I'd like to go on the record to say that I prefer <a href="https://stackoverflow.com/questions/7192543/injecting-depdencies-into-asp-net-mvc-3-action-filters-whats-wrong-with-this-ap/7194467#7194467">Mark Seeman's approach</a> because it separates the Action Filter responsibility away from the Attribute. Furthermore, Ninject's MVC3 extension has some very powerful ways to configure action filters via bindings. See the following references for more details:</p> <ul> <li><a href="https://github.com/ninject/ninject.web.mvc/wiki/Dependency-injection-for-filters" rel="noreferrer">https://github.com/ninject/ninject.web.mvc/wiki/Dependency-injection-for-filters</a></li> <li><a href="https://github.com/ninject/ninject.web.mvc/wiki/Conditional-bindings-for-filters" rel="noreferrer">https://github.com/ninject/ninject.web.mvc/wiki/Conditional-bindings-for-filters</a></li> <li><a href="https://github.com/ninject/ninject.web.mvc/wiki/Filter-configurations" rel="noreferrer">https://github.com/ninject/ninject.web.mvc/wiki/Filter-configurations</a></li> </ul> <h3>Update 2</h3> <p>As @usr pointed out in the comments below, <code>ActionFilterAttribute</code>s are instantiated when the class is loaded, and they last the entire lifetime of the application. If the <code>IMyService</code> interface is not supposed to be a Singleton, then it ends up being a <a href="http://blog.ploeh.dk/2014/06/02/captive-dependency/" rel="noreferrer">Captive Dependency</a>. If its implementation isn't thread-safe, you could be in for a lot of pain.</p> <p>Whenever you have a dependency with a shorter lifespan than your class's expected lifespan, it's wise to inject a factory to produce that dependency on-demand, rather than injecting it directly.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload