Design Questions - Static Classes, Dependency Injection etc

Feb 25, 2011 at 12:44 PM
Edited Feb 25, 2011 at 1:28 PM

Hi,

I'm just getting started with Unity and IoC frameworks so excuse this rather basic question.

I'm stuggling to work out the best way to design my framework classes. I'm creating an API that will wrap a messaging library. This messaging library will then be used by various modules to communicate with other instances of the application. Although I can see many examples of using dependency injection in MVP style situations I'm wondering is it still applicable for API's like I'm creating?

In the past I would have implemented this with a combination of Singletons, static classes and so forth. An example problem I'm facing is I have a static class that reads  a configuration file into memory. I would like to log various messages when this file is parsed. In the past I would have created a private logger doing something like below.

private static readonly ILogger Logger = LogProvider.GetLog("SomeLogComponentName");

However many of the examples I see in Unity seem to inject these dependencies like below

#constructor

public foo(ILogProvider logProvider){

ILogger logger = logProvider.GetLog("SomeLogComponentName");

}

Which of these approaches is better and why? I'm guessing the later because here we are dealing purely with interfaces and the nature of dependency injection would make it easier to test our code. However sometimes it is not possible to take that approach, like in the case of a static class which requires a parameterless contructor. In this case we could need some InitialiseMethod I assume to achieve the same.

I'd be interested to hear thoughts on this. I feel like I'm missing a part of the puzzle here.

Futher to this I wonder if it is good practice to pass the Container between classes and thus I can resolve and add any objects?

Thanks in advance

 

Feb 27, 2011 at 3:54 AM

First, you should not pass around the container itself around your classes.  Doing this defeats the purpose of using dependency injection because you now take hard dependency to the container itself. 

On your question which is better, are you asking which is better, using static class or an interface to get your dependencies?  As for me, even assuming that the static class also uses a dependency injection container under the hood, I always prefer to use an interface over a static class. 

In addition, in your example, if you only need to obtain an ILogger object in your foo class, you can just have it injected with an ILogger object itself rahter than the ILogProvider.

I believe this thread will also help you.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 2, 2011 at 11:30 AM

Hi,

Thanks for your reply.

Let me rephrase my question a little better.

1. How does Unity help me with static classes? Clearly I can use dependency injection in the constructor because it must be parameterless. However if I need to access some other dependency like ILogger how do I best access this from within a static class?

2. Is creating a singleton still a good idea when using Unity or are there better ways to create this type of contruct?

Thanks

 

Mar 3, 2011 at 4:57 AM

Static classes are directly opposed to DI. The only way you're going to get injection into a static in this fashion is if you also have a static container, and the Instance property (or whatever you use) of the class can call into that static container to get the actual instance that's returned. It's going to be awkward, but it can work - Enterprise Library 5 does exactly this to keep the old, static facade based interfaces working.

Code using these static objects are hard to test, brittle, and more difficult to evolve over time.

If I'm using a container, I'll actually use the container. You can mark an object in the container as a singleton very easily. Register your logger as an object in the container along with everything else. Then all you need to do is inject that logger into your objects, just like the examples you've seen.

The reason you don't see examples of using DI containers with statics is because they don't work well together.