Frage

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.

War es hilfreich?

Lösung

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.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top