创建新实例,同时仍然使用依赖注入 [英] Creating new instances while still using Dependency Injection

查看:255
本文介绍了创建新实例,同时仍然使用依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代表一个聊天室,对一个日志依赖的类。这是不一样的用横切关注全系统的记录,但是这依赖于特定聊天室的记录器。它记录在聊天室中的所有活动,以它独特的日志文件。在创建聊天室我想打开日志文件,当它的毁灭,那么我要关闭的日志文件。

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 close the log file.

下面是我使用的相关代码。

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);



ChatManager 是一个类持有引用所有的聊天室,并负责创建和删除它们。我没有写这些事,但是这就是我一直在编码对接口。

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.

我如何获得 ChatManager 以创建聊天室的新实例,并且仍然使用依赖注入??

How do I get ChatManager to create new instances of Chatroom and still use dependency injection??

我使用Unity做所有我的其他东西的DI。到目前为止,它的工作太棒了。但我不知道如何解决这个难题。

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.

在我的具体实施 ChatManager 创建新的聊天室,它在 IChatroomLogger 通过。它不知道如何构建...但不团结。但后来我在 IUnityContainer 来传递到 ChatManager

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);
    }
}

这似乎只是错误的某些原因。这似乎吸尘器没有域知道什么DI容器我使用的是什么。

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!

推荐答案

您说得对。使用这样的IUnityContainer是不再使用依赖注入。相反,它使用了服务定位器的格局。我通常做在这样的情况下是创建一个 IFactory的< T> 接口,是这样的:

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();
}



然后实现与一类接口的确的了解并使用 IUnityContainer 。设置您的绑定,这样 IFactory的<> 请求将创建这个工厂类的一个实例。这样,你可以注入 IFactory的<记录仪> 界面到您的 ChatManager ,并调用获得()你想有一个记录器实例。

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.

这篇关于创建新实例,同时仍然使用依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆