1

As I move from MVC to doing some WPF with Prism, I buy a Prism book and once again i'm shocked to still see the Service Locator which is a known anti-pattern. I have read the books like Dependency Injection in .NET and Mark Seemann thoroughly goes through WHY Service Locator is an Anti-Pattern.

I am simply trying to help be an evangelist of best patterns and practices and would like to know who is NOT getting the memo that some of these patterns are actually bad.

Tom Stickel
  • 19,633
  • 6
  • 111
  • 113
  • 1
    I have found some people talking about not falling into the trap of prism service locator anti-pattern... http://arstechnica.com/civis/viewtopic.php?f=20&t=1111364 – Tom Stickel Sep 14 '12 at 05:15
  • 1
    Cause you didn't write a book and Mark Seemann did :) http://www.amazon.com/Dependency-Injection-NET-Mark-Seemann/dp/1935182501/ref=sr_1_1?s=books&ie=UTF8&qid=1347599851&sr=1-1&keywords=dependency+injection+in+.net – Tom Stickel Sep 14 '12 at 05:17
  • This should be on a Prism forum. It is not a question! – ColinE Sep 14 '12 at 05:21
  • 1
    Plenty of people that ARE VERY well known liked my similar question. :) http://stackoverflow.com/questions/9409068/why-is-mvc4-using-the-service-locator-anti-pattern Have a great day! – Tom Stickel Sep 14 '12 at 05:25
  • 2
    @TomStickel the difference between your earlier one and this one is that your MVC4 question (although not a constructive question) simply sounded curious. This one has *rant* written all over it! – ColinE Sep 14 '12 at 05:29
  • @ColinE Yes, I see your point I will certainly try to ask better questions. – Tom Stickel Sep 14 '12 at 05:38
  • @TomStickel What different answers did you expect [than on your other question](http://stackoverflow.com/q/9409068/590790)? If you are serious about 'being an evangelist', phrase elaborate generalized questions relating to the subject which aren't specific to one framework. E.g. analyze some of the counter arguments and see/question whether they hold. – Steven Jeuris Nov 14 '12 at 21:36

2 Answers2

3

There certainly is a balance between Science and Art. Patterns are discovered not intentionally invented. Some languages lend themselves to be subject to more software engineering principles. There certainly is a lot of room for discussion on this. I think that you are right to question the use of something, but I am not sure if you will find your answer as your other question you mentioned shows that the "VERY" well known folks seems to disagree with each other.

I need more clarification on the matter in needed and the context in which it is used. Overall I think the use of these "anti-pattern" should be analyzed. Singleton I read is bad, but we all know it has a purpose.

0

First to clear the facts: Prism does not encourage use of ServiceLocator. This renders the question moot. Maybe you would like to rename the question into 'Why does Prism book encourage...' which might or might not be true, as I haven't read it.

Prism is built on Unity IoC which is Microsoft's implementation of IoC container. If you register your types with it, it will build them properly. There are many articles on internet showing how to do this.

Now, the soapbox part:

I would argue that there are no such things as best practices, as any 'best practice' don't make sense in certain context. For example, when I see a singleton, it always rings an alarm, but there are situations where it is OK to have it. Even 'goto' is sometimes justified (oh my God! what did he just say?!?!)

Now, I don't know much about Prism, but IMO, service locator can make perfect sense, especially if it is used inside a framework, as it can leave the developer with more design and implementation choices, and that should be the developer's call.

To really understand whether it is a wrong decision (and it likely might be) it would be interesting to see what things developers using Prism can't do because of alleged use of ServiceLocator, which otherwise would be possible if Prism used DI.

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • Your question is not about Prism's us of SL, it's a rant about SL in general. I have no issues with your worshiping of guru Mark Seemann or promoting his book and I am not much into gurus anyway (although I have read his article when it came out). However, you haven't mention things which are impossible because of SL in this case. How does it stop YOU from using DI in your code? Do you intend to unit test the Prism itself? In similar manner, someone could tell you that using statically typed languages is also a bad practice, as you typically don't need DI with dynamic languages. Oh, really? – Zdeslav Vojkovic Sep 14 '12 at 17:56
  • Service locator does not restrict function, but it restricts discoverability - a feature shared by DI imo. In either scenario, you are required to know what service you want. – Gusdor Jul 30 '13 at 08:45