Unity配置和嵌套泛型类型

时间:2011-01-01 11:04:38

标签: c# generics dependency-injection unity-container

我是Unity新手,在WCF服务中遇到配置和嵌套泛型类型的问题。一切都运行正常,直到我遇到需要定义工厂的情况,该工厂返回基于通用接口的类型实例。

代码看起来像这样:

public interface IFactory<T, TResult>
{
    TResult CreateInstance(T input);
}

public interface IFilter<T>
{
    // throws an exception if an item is to be filtered
    void Filter(T itemToFilter);
}

// classes implementing this interface can have multiple filters    
public interface IFilterRunner<T>
{        
    void RunFilters(T itemToFilter);
}

public class FilterRunnerFactory : IFactory<BaseType, IFilterRunner<BaseType>>
{
    public IFilterRunner<BaseType> CreateInstance(BaseType input)
    {
        if (input is SubType)
        {
            return new SubTypeFilterRunner();
        }

        throw new InvalidOperationException("Could not create an IFilterRunner for the input.");
    }
}

public class Service
{
    private readonly IFactory<BaseType, IFilterRunner<BaseType>> _filterRunnerFactory;

    public Service(IFactory<BaseType, IFilterRunner<BaseType>> filterRunnerFactory)
    {
        _filterRunnerFactory = filterRunnerFactory;
    }    
}

Unity配置如下所示:

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <alias alias="BaseType" type="SomeNamespace.BaseType, SomeAssembly, Version=1.0.0.0, Culture=neutral" />
  <alias alias="IFactory" type="SomeNamespace.IFactory`2, SomeAssembly, Version=1.0.0.0, Culture=neutral" />
  <alias alias="IFilterRunner" type="SomeNamespace.IFilterRunner`1, SomeAssembly, Version=1.0.0.0, Culture=neutral" />
  <alias alias="IService" type="SomeNamespace.IService, SomeAssembly, Version=1.0.0.0, Culture=neutral" /> 
  <containers>
    <container>
      <register type="IFactory[BaseType,IFilterRunner]" mapTo="SomeNamespace.FilterRunnerFactory, SomeAssembly, Version=1.0.0.0, Culture=neutral"/>

      <register type="IService" mapTo="SomeNamespace.Service, SomeAssembly, Version=1.0.0.0, Culture=neutral">
        <constructor>
          <param name="filterRunnerFactory" />
        </constructor>
      </register>
    </container>
  </containers>
</unity>

配置似乎有效,但是当WCF服务本身被实例化时,我收到以下错误:

  

InvalidOperationException - The   当前类型,   SomeNamespace.IFactory 2[SomeNamespace.BaseType,SomeNamespace.IFilterRunner 1 [SomeNamespace.BaseType]]   是一个接口,不能   建造。你错过了一个类型吗?   映射?

我尝试过各种不同的选项,但都会导致配置无效。此外,我还有其他工厂定义被定义为返回抽象基类型或基于非泛型接口的类型实例,并且它们工作正常。不同之处在于该工厂返回IFilterRunner - 一个通用接口。

有没有人有任何建议?提前感谢困惑的开发人员。

1 个答案:

答案 0 :(得分:2)

我通过为IFilterRunner的别名指定通用参数来解决您的问题。

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <alias alias="BaseType" type="SomeNamespace.BaseType, SomeAssembly, Version=1.0.0.0, Culture=neutral" />
  <alias alias="IFactory" type="SomeNamespace.IFactory`2, SomeAssembly, Version=1.0.0.0, Culture=neutral" />
  <alias alias="IFilterRunner" type="SomeNamespace.IFilterRunner`1[[SomeNamespace.BaseType, SomeAssembly, Version=1.0.0.0, Culture=neutral]], SomeAssembly, Version=1.0.0.0, Culture=neutral" />
  <alias alias="IService" type="SomeNamespace.IService, SomeAssembly, Version=1.0.0.0, Culture=neutral" />
  <containers>
    <container>
      <register type="IFactory[BaseType,IFilterRunner]" mapTo="SomeNamespace.FilterRunnerFactory, SomeAssembly, Version=1.0.0.0, Culture=neutral"/>

      <register type="IService" mapTo="SomeNamespace.Service, SomeAssembly, Version=1.0.0.0, Culture=neutral">
        <constructor>
          <param name="filterRunnerFactory" />
        </constructor>
      </register>
    </container>
  </containers>
</unity>