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

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

问题描述

我想注册一个通用型的统一2.0的配置文件,但似乎无法得到它的权利。我已经提到了这里的MS文档:<一href="http://msdn.microsoft.com/en-us/library/ff660933%28v=PandP.20%29.aspx#_Generic_Types">http://msdn.microsoft.com/en-us/library/ff660933%28v=PandP.20%29.aspx#_Generic_Types

在code是这样的:

 公共接口IRepository&LT; T&GT;其中T:类
{
    ...
}

公共类GenericRepository&LT; T&GT; :IRepository&LT; T&GT;其中T:类
{
    ...
}

公共类BlogRepository:GenericRepository&LT; BlogRepository&GT;
{
    ...
}
 

在XML配置我目前所面对的loks是这样的:

 &LT;团结&GT;
    &LT;! - 别名 - &GT;
    &LT;别名别名为BlogIRepository
           TYPE =X.Services.Interfaces.IRepository [X.Domain.Entities.Blog,X.Domain],X.Services/&GT;

    &LT;别名别名为BlogRepository
           TYPE =X.Repositories.BlogRepository,X.Repositories/&GT;

    &LT;! - 类注册 - &GT;
    &LT;容器名称=发展&GT;
        &LT;! - 通用连接字符串的值 - &GT;
        &LT;实例名称=康恩TYPE =System.String值=blahblahblah/&GT;
        &LT;注册类型=BlogIRepositorymapTo =BlogRepository&GT;
            &LT;构造&GT;
                &LT; PARAM NAME =的connectionStringTYPE =System.StringdependencyName =康恩/&GT;
            &LT; /构造&GT;
        &LT; /注册&GT;
    &LT; /容器&GT;
&LT; /团结和GT;
 

根据该文件你用方括号的泛型类型(S),如果类型不是系统类型您所提供的完全限定的类型里面更多的方括号注册泛型类型。这就是我所做的一切,我想。然而 - 没有worky

修改:从MSDN网站的例子:

 &LT;注册类型=的IDictionary [字符串[MyApp.Interfaces.ILogger,MyApp的]/&GT;
 

产生的错误是:

  

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

解决方案

MSDN是没有错的。我们特别增加了一些快捷方式解析规则,这样你就不必在大多数情况下输入所有'的和方括号。

我在一起的例子,看起来大多是像您一样的耳光:

 公共接口IRepository&LT; T&GT;其中T:类
{

}

公共类GenericRepository&LT; T&GT; :IRepository&LT; T&GT;其中T:类
{

}

公共类BlogRepository:GenericRepository&LT;博客&GT;
{

}

公共类博客
{

}
 

我的XML配置是这样的:

 &LT;统一的xmlns =htt​​p://schemas.microsoft.com/practices/2010/unity&GT;
    &LT;命名空间名称=UnityConfigExample/&GT;
    &LT;装配NAME =UnityConfigExample/&GT;

    &LT;集装箱&GT;
      &LT;注册类型=IRepository []mapTo =GenericRepository []/&GT;
      &LT;注册类型=IRepository [博客]mapTo =BlogRepository/&GT;
    &LT; /容器&GT;
  &LT; /团结和GT;
 

和它只是工作。

是你任何机会尝试使用别名IRepository而不是命名空间/集搜索?我得到了以下的工作,以及使用别名:

 &LT;统一的xmlns =htt​​p://schemas.microsoft.com/practices/2010/unity&GT;
    &LT;别名别名为IRepositoryTYPE =UnityConfigExample.IRepository`1,UnityConfigExample/&GT;
    &LT;别名别名为GenericRepositoryTYPE =UnityConfigExample.GenericRepository`1,UnityConfigExample/&GT;
    &LT;别名别名为BlogRepositoryTYPE =UnityConfigExample.BlogRepository,UnityConfigExample/&GT;
    &LT;别名别名为博客TYPE =UnityConfigExample.BlogRepository,UnityConfigExample/&GT;

    &LT;集装箱&GT;
      &LT;注册类型=IRepository []mapTo =GenericRepository []/&GT;
      &LT;注册类型=IRepository [博客]mapTo =BlogRepository/&GT;
    &LT; /容器&GT;
  &LT; /团结和GT;
 

当您指定一个别名的类型,您必须使用CLR类型的语法。在其他地方,你可以使用通用的快捷语法。

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.

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

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