4

I wrote a big application using DI. The application is composed by a bootstrapper upon initialization, where most dependencies are injected. All is fine.

However, there are some services* that I can't simply inject everywhere. One good example is the Log service. It's a log, so, every single class in the solution may want to use it for debugging or tracing purposes. Not every class is created upon initialization, some are provided by third party (the application is somewhat a framework). Right now, my solution is to use singletons; I even created some wrapper classes for the singleton, so I can inject it where possible.

I was wondering if a better approach would be to use a ServiceLocator in those places. This would complete remove the hard coupling that a singleton causes. Classes would be coupled to the locator, yes, but I could provide any implementation to them.

*In the DDD terminology.

P.S.: I'm using .NET here, but I won't tag it so; I believe the question applies to any language that accepts DI.

Bruno Brant
  • 8,226
  • 7
  • 45
  • 90
  • 5
    Neither option is good. Logging is a cross-cutting concern, so is better dealt with as a Decorator: http://stackoverflow.com/a/7906547/126014 – Mark Seemann Oct 18 '13 at 16:13

2 Answers2

0

In Java EE 6/7 enviroment, the best option for cross-cutting aspects is to use interceptors. The cross cutting functionality can be easily factored out into reusable interceptors. I don't know if there is something similar in .NET.

In another way, the design pattern Service Locator has been replaced in some degree by CDI. The pattern isolates to the application code from the details of the service implementation. CDI can provide the same level of isolation from service details, but in more easy way.

Just in case you need to maintain the state of a bean during the lifetime of the application, you should use a @Singleton.

The above applies to Java EE 6/7 environment.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Paul, can you send me some information over CDI? I'm not familiar with the acronym. – Bruno Brant Oct 29 '13 at 21:26
  • See more in [_23.2 Overview of CDI_](http://docs.oracle.com/javaee/7/tutorial/doc/cdi-basic002.htm) in Java EE platform and [_List of .NET Dependency Injection Containers (IOC)_](http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx). – Paul Vargas Oct 30 '13 at 02:33
  • what is "CDI"? Its preferable that when introducing an acronym that you describe its abbreviation. – enorl76 Mar 31 '15 at 18:30
0

Adding on to Paul Vargas... A concept similar to interceptors is that of aspect-oriented programming (AOP), which is probably what you want to look into.

I'm not sure which DI framework you're using. If you're using Spring .NET, the functionality is definitely available. This is useful, for example, in adding debug- or trace-level logging as you enter and exit each method call.

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

Sarah Cartwright
  • 136
  • 1
  • 10
  • I'm actually using Unity (EntLib); Although Mark comment and most answers are right, the issue lies with code that I had not inject. I will have to burn more brain mass here. But thanks for the answer, really. – Bruno Brant Oct 29 '13 at 21:26