1

即使在参考以下帖子后,我仍然难以统一注册我的通用依赖项。

我的代码更简单,因为它没有实现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)

我试过在别名中包含类型

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

但这似乎也不起作用。我可以WorkflowSerializer在里面用力输入,WorkflowManager但这是我想避免的。

我只是希望我没有忽略一些简单的事情。TIA

4

1 回答 1

0

看来我忽略了检查我的WorkflowManager班级所在的命名空间。ReSharper 喜欢将文件的命名空间与文件所在的相应文件夹相关联。就我而言,问题是:

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

一旦我.Implementations从命名空间中删除了,unity 就能够成功地构建依赖关系。

从长远来看,希望我的大脑放屁能帮助你们中的一个人。

于 2013-02-21T16:37:21.013 回答