【问题标题】:How map generic types with Unity?如何用 Unity 映射泛型类型?
【发布时间】:2012-06-26 13:01:59
【问题描述】:

我正在尝试使用 Unity 配置向类属性添加依赖项,并且我尝试注入的类型是通用的。

我有界面

public interface ISendMessage
{
    void Send(string contact, string message);
}

public class EmailService : ISendMessage
{
    public void Send(string contact, string message)
    {
        // do
    }
}

public class MessageService<T> where T : ISendMessage
{          
}

我尝试在其他类中通过构造函数注入来使用它

public MyService(MessageService<ISendMessage> messageService)
{
}

我如何注入MessageService&lt;EmailService&gt; 而不是MessageService&lt;ISendMessage&gt;

我尝试通过 app.config 来实现

<alias alias="MessageService'1" type="MyNamespace.MessageService'1, MyAssembly" />
    <alias alias="EmailMessageService'1" type="MyNamespace.MessageService'1[[MyNamespace.EmailService, MyAssembly]], MyAssembly" />

我收到错误

无法解析类型名称或别名 MessageService'1。请 检查您的配置文件并验证此类型名称。

以及如何传入MessageService&lt;T&gt; 实现参数MessageService&lt;EmailService&gt;

谢谢

更新

我将我的课程修改为以下内容:

public class MessageService<T> where T : ISendMessage
    {
        private T service;

        [Dependency]
        public T Service
        {
            get { return service; }
            set { service = value; }
        }
}

并使用配置

<alias alias="ISendMessage" type="MyNamespace.ISendMessage, MyAssembly" />
    <alias alias="EmailService" type="MyNamespace.EmailService, MyAssembly" />

<register type="ISendMessage" mapTo="EmailService">
        </register>

它有效:-)

【问题讨论】:

标签: c# .net dependency-injection unity-container ioc-container


【解决方案1】:

您不能简单地将MessageService&lt;ISendMessage&gt; 转换为MessageService&lt;EmailService&gt;。为此,您需要 MessageService&lt;T&gt; 变体。仅接口(和委托)支持方差。这不是 Unity 的事情,这是 .NET 框架的“限制”(自 4.0 起支持 C#)。所以需要实现如下接口:

// note the 'out' keyword!!
public interface IMessageService<out T> 
    where T : ISendMessage
{
    T GetSendMessage();
}

MessageService&lt;T&gt; 类必须实现这个接口。但即使使用此代码,Unity 也不会自动注入此代码。您必须在这两种类型之间进行映射。例如,这是一个可能的注册:

container.Register<MessageService<ISendMessage>>(
    new InjectionFactory(c =>
        c.Resolve<MessageService<EmailService>>())); 

请注意,我使用基于代码的配置。尽可能避免使用基于 XML 的配置,因为 XML 配置脆弱、容易出错、功能较弱且难以维护。仅注册在部署期间或之后实际需要的类型(就个人而言,即使那样我也不会使用您的 DI 容器的 XML API)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多