Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, there are downsides, as there are <a href="https://stackoverflow.com/questions/5653783/is-idependencyresolver-an-anti-pattern/5654204#5654204">lots of issues with IDependencyResolver</a> itself, and to those you can add the use of a <a href="https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons">Singleton</a> Service Locator, as well as <a href="https://stackoverflow.com/questions/7099406/what-is-the-real-difference-between-bastard-injection-and-poor-mans-injection">Bastard Injection</a>.</p> <p>A better option is to implement the filter as a normal class into which you can inject whichever services you'd like:</p> <pre><code>public class MyActionFilter : IActionFilter { private readonly IMyService myService; public MyActionFilter(IMyService myService) { this.myService = myService; } public void OnActionExecuting(ActionExecutingContext filterContext) { if(this.ApplyBehavior(filterContext)) this.myService.DoSomething(); } public void OnActionExecuted(ActionExecutedContext filterContext) { if(this.ApplyBehavior(filterContext)) this.myService.DoSomething(); } private bool ApplyBehavior(ActionExecutingContext filterContext) { // Look for a marker attribute in the filterContext or use some other rule // to determine whether or not to apply the behavior. } private bool ApplyBehavior(ActionExecutedContext filterContext) { // Same as above } } </code></pre> <p>Notice how the filter examines the filterContext to determine whether or not the behavior should be applied.</p> <p>This means that you can still use attributes to control whether or not the filter should be applied:</p> <pre><code>public class MyActionFilterAttribute : Attribute { } </code></pre> <p>However, now that attribute is completely inert.</p> <p>The filter can be composed with the required dependency and added to the global filters in global.asax:</p> <pre><code>GlobalFilters.Filters.Add(new MyActionFilter(new MyService())); </code></pre> <hr> <p>For a more detailed example of this technique, although applied to ASP.NET Web API instead of MVC, see this article: <a href="http://blog.ploeh.dk/2014/06/13/passive-attributes" rel="noreferrer">http://blog.ploeh.dk/2014/06/13/passive-attributes</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks for your answer, but no offense, all your links simply point to philosophical arguments about naming of patterns. After reading through all of your answers to other questions you've linked, and to the blog posts, I still don't see any concrete issues that would arise from using my approach. Add to that that now any ActionFilter I use in my application I need to remember to add to the global filters, and I'd argue that this approach could lead to errors of its own. If you can give a concrete example as to how my approach can lead to errors, then I'll admit I'm being thickheaded :)
      singulars
    2. COYou asked about what the downsides would be: the downsides are decreased maintainability of your code base, but that hardly feels *concrete*. This is something that creeps up on you. I can't say that if you do what you propose to do, you will have a race condition, or the CPU may overheat, or kittens will die. That's not going to happen, but if you don't follow proper design patterns and avoid anti-patterns, your code will rot and four years from now you'll want to rewrite the application from scratch (but your stakeholders won't let you).
      singulars
    3. COFair enough. I will agree with you, that if I'm building an all purpose ActionFilter that may be used across projects, then yes, this can be an anti-pattern, as the default constructor may be called, and if DependencyResolver isn't used, you'd get some weird errors. However 1)your approach still requires registration with the GlobalFilters which can lead to the same issue I've stated above, and 2)What if this action filter isn't uses across applications? What if this action filter is internal? Still as bad?
      singulars
 

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