即使在参考以下帖子后,我仍然难以统一注册我的通用依赖项。
我的代码更简单,因为它没有实现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