使用 Unity IoC 容器
有人可以帮我解决这个问题吗?我正在尝试使用 Unity IoC 进行注入。代码如下:
IUnityContainer container = new UnityContainer()
.RegisterType<IBaseModel, EmployeeModel>()
.RegisterType<IProxyObjectAdapter, EmployeeProxyObjectAdapter>()
.RegisterType<IAdventureWorksRepository<IProxyObjectAdapter>,AdventureWorksWCFRepository<IProxyObjectAdapter>>();
Unity 在最后一行抱怨。这是错误消息:
'AdventureWorksManagement.ServiceAgents.AdventureWorksWCFRepository' 不能用作类型参数“TTo” 在泛型类型或方法中 'Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType(Microsoft.Practices.Unity.IUnityContainer, 参数 Microsoft.Practices.Unity.InjectionMember[])'。 没有隐式引用 转换自 'AdventureWorksManagement.ServiceAgents.AdventureWorksWCFRepository' 到 'AdventureWorksManagement.ServiceAgents.IAdventureWorksRepository'。 C:\我的开发\我的测试 地面\我的测试 Ground\AdventureWorksManagement\ModelView\EmployeeViewModel.cs 94 33 AdventureWorksManagement
以下是类定义:
/// <summary>
/// Repository for WCF agents
/// </summary>
public class AdventureWorksWCFRepository<T> : IAdventureWorksRepository<IBaseModel> where T: IProxyObjectAdapter
{
/// <summary>
/// WCF service end
/// </summary>
private readonly AdvWorksManagementService.EmployeeServiceContractClient servicePoint;
/// <summary>
/// Adapter used to translate WCF proxy objects to domain objects
/// </summary>
private T proxyobjectAdapter;
.....
请指教。 休眠根
Can someone help me with this? I'm trying to use Unity IoC for injection. Here's the code:
IUnityContainer container = new UnityContainer()
.RegisterType<IBaseModel, EmployeeModel>()
.RegisterType<IProxyObjectAdapter, EmployeeProxyObjectAdapter>()
.RegisterType<IAdventureWorksRepository<IProxyObjectAdapter>,AdventureWorksWCFRepository<IProxyObjectAdapter>>();
Unity is complaining at that last line. Here's the error message:
'AdventureWorksManagement.ServiceAgents.AdventureWorksWCFRepository'
cannot be used as type parameter 'TTo'
in the generic type or method
'Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType(Microsoft.Practices.Unity.IUnityContainer,
params
Microsoft.Practices.Unity.InjectionMember[])'.
There is no implicit reference
conversion from
'AdventureWorksManagement.ServiceAgents.AdventureWorksWCFRepository'
to
'AdventureWorksManagement.ServiceAgents.IAdventureWorksRepository'. C:\My Development\My Testing
Ground\MyTesting
Ground\AdventureWorksManagement\ModelView\EmployeeViewModel.cs 94 33 AdventureWorksManagement
Here's the class definition:
/// <summary>
/// Repository for WCF agents
/// </summary>
public class AdventureWorksWCFRepository<T> : IAdventureWorksRepository<IBaseModel> where T: IProxyObjectAdapter
{
/// <summary>
/// WCF service end
/// </summary>
private readonly AdvWorksManagementService.EmployeeServiceContractClient servicePoint;
/// <summary>
/// Adapter used to translate WCF proxy objects to domain objects
/// </summary>
private T proxyobjectAdapter;
.....
Please advice.
dormantroot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误是正确的。您正在映射:
IAdventureWorksRepository; -> AdventureWorksWCFRepository
但是,在代码定义中,AdventureWorkdsWCFRepository实现 IAdventureWorksRepository<IBaseModel>。这不是您要从中映射的界面。我怀疑 IBaseModel 在代码中应该是“T”。
The error is correct. You're mapping:
IAdventureWorksRepository<IProxyObjectAdapter> -> AdventureWorksWCFRepository<IProxyObjectAdapter>
However, in the code definition, AdventureWorkdsWCFRepository<T> implements IAdventureWorksRepository<IBaseModel>. Which is not the interface you're trying to map from. I suspect that IBaseModel was supposed to be "T" in the code.