Creating new instances while still using Dependency Injection
A quick description of the environment:
I have a class that represents a chatroom and has a dependency on a logger. It's not the same as a system-wide logger with cross-cutting concerns, but a logger that's tied to that specific chatroom. It logs all activity in that chatroom to it's unique log file. When the chatroom is created I want to open the log file, and when it's destroyed I want to clos开发者_如何转开发e the log file.
The Problem
Here's the relevant code I'm using.
public interface IChatroomLogger
{
void Log(ServerPacket packet);
void Open();
void Close();
}
public class ChatroomLogger : IChatroomLogger
{
// chatroom name will be used as a file name
public ChatroomLogger(string chatroomName) { ... }
public void Log(ServerPacket packet) { ... }
}
public class Chatroom
{
public Chatroom(string name, IChatroomLogger logger)
{
this.name = name;
this.logger = logger;
this.logger.Open();
}
public IChatromLogger Logger { get { return this.logger; } }
}
public interface IChatManager
{
Chatroom Get(chatroomName);
}
It's used in the application like this:
var room = ChatManager.Get(chatroomName);
romm.DoStuff();
room.Logger.LogPacket(receivedPacket);
The ChatManager
is a class which holds references to all chatrooms and is responsible for creating and removing them. I haven't written it yet but that's the interface I've been coding against.
The Question
How do I get ChatManager
to create new instances of Chatroom
and still use dependency injection??
I'm using Unity to do all my other DI stuff. So far it's worked great. But I'm not sure how to work around this conundrum.
When my concrete implementation of ChatManager
creates new chatrooms, it has to pass in an IChatroomLogger
. It doesn't know how to construct that...but Unity does. But then I have to pass in IUnityContainer
into the ChatManager
.
public class ChatManager : IChatManager
{
public ChatManager(IUnityContainer container)
{
this.container = container;
}
public Chat Get(string chatroomName)
{
// get logger from Unity somehow. Not sure how I'd
// pass chatroomName to the concrete instance
var logger = ...
return new Chatroom(chatroomName, logger);
}
}
That just seems wrong for some reason. It seems cleaner to not have the domain know anything about what DI container I'm using.
How could I get new instances of class Chatroom
while my application in the middle of running without resorting to some sort of service locator design? Am I overthinking it? Is it not a big deal to pass around Unity? Any thoughts are welcome!
You're right. Using the IUnityContainer like this is no longer using Dependency Injection. Instead it's using the "Service Locator" pattern. What I usually do in cases like this is create an IFactory<T>
interface, like this:
public IFactory<T>
{
T Get();
}
Then implement the interface with a class that does know about and use the IUnityContainer
. Set up your bindings so that IFactory<>
requests will create an instance of this factory class. That way, you can inject the IFactory<Logger>
interface into your ChatManager
, and call .Get()
any time you want a logger instance.
In general, I also think you should avoid passing the container around.
- Add a IChatroomProvider to your design with a Create(string roomname) method
- In ChatroomProvider, use the container to create named instances (the container will manage the lifecyle of each named instance for you so you don't need to manage a dictionary).
- Register the ChatroomProvider with the container
- Let ChatManager take a dependency on IChatroomProvider
- When ChatManager needs a chatroom it can just ask its IChatroomProvider
- Use the container to create the ChatManager instance
- Logger instances will be created when chatrooms are created
精彩评论