2013-02-21 150 views
1

即使在參考the following後,我仍然很難統一註冊我的通用依賴。Unity 2.0通過配置註冊通用

我的代碼是在簡單,它沒有實現GenericRepository<T>和如下:

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

public class WorkflowSerializer : ISerializer<XmlFormWorkflow> 
{ 
... 
} 

public class WorkflowManager 
{ 
    private readonly ISerializer<XMLFormWorkflow> _serializer; 

    public WorkflowManager(ISerializer<XmlFormWorkflow> serializer) 
    { 
     _serializer = serializer; 
    } 
} 

我使用看起來如下conig文件:

<unity> 
    <alias alias="ISerializer" type="Common.ISerializer`1, Common" /> 
    <alias alias="WorkflowSerializer" type="Common.WorkflowSerializer, Common" /> 
    <alias alias="XMLFormWorkflow" type="Common.XMLFormWorkflow, Common" /> 
    <container> 
    <register type="ISerializer[XMLFormWorkflow]" mapTo="WorkflowSerializer" /> 
    </container> 
</unity> 

的錯誤,我我得到如下:

Resolution of the dependency failed, type = "Business.WorkflowManager", name = "(none)". 
Exception occurred while: while resolving. 
Exception is: InvalidOperationException - The current type,  Common.ISerializer`1[Common.XMLFormWorkflow], is an interface and cannot be constructed.  Are you missing a type mapping? 
----------------------------------------------- 
At the time of the exception, the container was: 
    Resolving parameter "serializer" of constructor Business.WorkflowManager(Common.ISerializer`1[[Common.XMLFormWorkflow, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] serializer) 
    Resolving Common.ISerializer`1[Common.XMLFormWorkflow],(none) 

我試過包括在鋁ias

<alias alias="ISerializer" type="Common.ISerializer`1[[Common.XMLFormWorkflow, Common]], Common" /> 

但這似乎並不奏效。我可以在WorkflowManager的內部強烈輸入WorkflowSerializer,但這是我想避免的。

我只是希望我沒有忽略一些簡單的東西。 TIA

回答

0

它似乎我忽視檢查我的WorkflowManager類下的名稱空間。 ReSharper喜歡將文件的名稱空間與該文件所在的文件夾相關聯。在我的情況的問題是:

namespace Common.Implementations 
{ 
    public class WorkflowSerializer : ISerializer<XMLFormWorkflow> 
    { 
     ... 
    } 
} 

一旦我刪除從命名空間中的.Implementations,團結能夠成功建立依賴關係。

希望我的大腦放屁有助於你們長遠。