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

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

问题描述

我试图在Unity 2.0的配置文件中注册一个通用类型,但似乎不能正确。我一直在这里参考MS文档: http://msdn.microsoft.com/en-us/library/ff660933%28v=PandP.20%29.aspx#_Generic_Types



代码如下所示:

  public interface IRepository< T>其中T:class 
{
...
}

public class GenericRepository< T> :IRepository< T>其中T:class
{
...
}

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

现在有这样的情况:

 < unity> 
<! - 别名 - >
< 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/>

<! - 类型注册 - >
< container name =development>
<! - 公共连接字符串值 - >
< instance name =Conntype =System.Stringvalue =blahblahblah/>
< register type =BlogIRepositorymapTo =BlogRepository>
< constructor>
< param name =connectionStringtype =System.StringdependencyName =Conn/>
< / constructor>
< / register>
< / container>
< / unity>

根据注册泛型类型的文档,您可以在泛型类型周围使用方括号,如果类型不是系统类型,则在更方括号内提供完全限定类型。这是我做了什么,我想。



EDIT :示例来自MSDN网站:

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

生成的错误是:


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



解决方案

MSDN没有错。我们特别添加了一些快捷方式解析规则,以便在大多数情况下不必输入所有的方括号。



大多像您的:

  public interface IRepository< T>其中T:class 
{

}

public class GenericRepository< T> :IRepository< T>其中T:class
{

}

public class BlogRepository:GenericRepository< Blog>
{

}

public class Blog
{

}



我的XML配置如下所示:

  ; 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而不是命名空间/程序集搜索?我有以下工作以及使用别名:

 < unity xmlns =http://schemas.microsoft.com / practices / 2010 / unity> 
< alias alias =IRepositorytype =UnityConfigExample.IRepository`1,UnityConfigExample/>
< alias alias =GenericRepositorytype =UnityConfigExample.GenericRepository`1,UnityConfigExample/>
< alias alias =BlogRepositorytype =UnityConfigExample.BlogRepository,UnityConfigExample/>
< alias alias =Blogtype =UnityConfigExample.BlogRepository,UnityConfigExample/>

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

指定别名的类型时,必须类型语法。在其他地方你可以使用通用的快捷键语法。


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

The code looks like this:

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

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

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

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.

EDIT: Example from the MSDN site:

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

The error generated is:

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

解决方案

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
{

}

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>

and it just works.

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>

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天全站免登陆