Replacing singletones with ServiceLocators using dependency injection

I wrote a great application using DI. The application consists of a bootstrap during initialization, where most of the dependencies are introduced. All is well.

However, there are some services * that I cannot just deploy everywhere. One good example is the journal service. This is a log, so each individual class in a solution may want to use it for debugging or tracing purposes. Not every class is created during initialization, some of them are provided by a third party (the application is to some extent the basis). Right now, my decision is to use singles; I even created some wrapper classes for singleton, so I can inject it where possible.

I was wondering if the best approach would be to use ServiceLocator in these places. . This completes the removal of the hard link that the singleton calls. Of course, the classes will be bound to the locator, but I could provide them with any implementation.

* In DDD terminology.

PS: I use .NET here, but I will not mark it; I believe this question applies to any language that accepts DI.

+4
source share
2 answers

In a Java EE 6/7 environment, the best option for cross-cutting aspects is to use interceptors. Cross cutting functionality can be easily transferred to reusable interceptors. I do not know if there is something similar in .NET.

, , Service Locator CDI. . CDI , .

bean , @Singleton.

Java EE 6/7.

0

Paul Vargas... , , - (AOP), , , .

, DI . Spring.NET, . , , .

http://www.springframework.net/doc/reference/html/aop-quickstart.html

0

All Articles