Unity 2.0 通过 XML 注册泛型类型 [英] Unity 2.0 registering generic types via XML

查看:31
本文介绍了Unity 2.0 通过 XML 注册泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Unity 2.0 的配置文件中注册通用类型,但似乎无法正确设置.我在这里指的是 MS 文档:http://msdn.microsoft.com/en-us/library/ff660933%28v=PandP.20%29.aspx#_Generic_Types

I am trying to register a generic type in a config file for Unity 2.0 but can't seem to get it right. I have been referring to the MS documentation here : http://msdn.microsoft.com/en-us/library/ff660933%28v=PandP.20%29.aspx#_Generic_Types

代码如下:

public interface IRepository<T> where T : class
{
    ...
}

public class GenericRepository<T> : IRepository<T> where T : class
{
    ...
}

public class BlogRepository : GenericRepository<BlogRepository>
{
    ...
}

我现在的 XML 配置看起来像这样:

The XML config i have at the moment loks like this:

<unity>
    <!-- Aliases -->
    <alias alias="BlogIRepository" 
           type="X.Services.Interfaces.IRepository[[X.Domain.Entities.Blog, X.Domain]], X.Services"/>

    <alias alias="BlogRepository" 
           type="X.Repositories.BlogRepository, X.Repositories"/>

    <!-- Type registration -->
    <container name="development">
        <!-- Common connection string value -->
        <instance name="Conn" type="System.String" value="blahblahblah"/>
        <register type="BlogIRepository" mapTo="BlogRepository">
            <constructor>
                <param name="connectionString" type="System.String" dependencyName="Conn"/>
            </constructor>
        </register>
    </container>
</unity>

根据注册泛型类型的文档,您在泛型类型周围使用方括号,如果类型不是系统类型,则在更多方括号内提供完全限定类型.这就是我所做的,我想.然而 - 没有工作.

According to the documentation to register generic types you use square brackets around the generic type(s), and if the type is not a system type you provide the fully qualified type inside more square bracket. Which is what i have done, i think. Yet - no worky.

编辑:来自 MSDN 站点的示例:

EDIT: Example from the MSDN site:

<register type="IDictionary[string, [MyApp.Interfaces.ILogger, MyApp]]"/>

产生的错误是:

无法解析类型名称或别名 IRepository.请检查您的配置文件并验证此类型名称.

The type name or alias IRepository could not be resolved. Please check your configuration file and verify this type name.

推荐答案

MSDN 没有错.我们特别添加了一些快捷解析规则,这样大多数情况下您就不必输入所有的`'s和方括号.

MSDN is NOT wrong. We specifically added some shortcut parsing rules so that you don't have to enter all the `'s and square brackets in most cases.

我拼凑了一个和你的很像的例子:

I slapped together an example that looks mostly like yours:

public interface IRepository<T> where T: class
{

}

public class GenericRepository<T> : IRepository<T> where T : class
{

}

public class BlogRepository : GenericRepository<Blog>
{

}

public class Blog
{

}

我的 XML 配置如下所示:

My XML config looks like this:

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <namespace name="UnityConfigExample"/>
    <assembly name="UnityConfigExample"/>

    <container>
      <register type="IRepository[]" mapTo="GenericRepository[]" />
      <register type="IRepository[Blog]" mapTo="BlogRepository" />
    </container>
  </unity>

它只是有效.

您是否有机会尝试使用 IRepository 的别名而不是命名空间/程序集搜索?我使用别名也可以使用以下方法:

Were you by any chance trying to use an alias for IRepository instead of the namespace / assembly search? I got the following to work as well using aliases:

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="IRepository" type="UnityConfigExample.IRepository`1, UnityConfigExample" />
    <alias alias="GenericRepository" type="UnityConfigExample.GenericRepository`1, UnityConfigExample"/>
    <alias alias="BlogRepository" type="UnityConfigExample.BlogRepository, UnityConfigExample"/>
    <alias alias="Blog" type="UnityConfigExample.BlogRepository, UnityConfigExample"/>

    <container>
      <register type="IRepository[]" mapTo="GenericRepository[]" />
      <register type="IRepository[Blog]" mapTo="BlogRepository" />
    </container>
  </unity>

为别名指定类型时,必须使用 CLR 类型语法.您可以在其他任何地方使用通用快捷语法.

When you specify the type for an alias, you must use the CLR type syntax. Everywhere else you can use the generic shortcut syntax.

这篇关于Unity 2.0 通过 XML 注册泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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