使用 Unity IoC 容器

发布于 2024-11-03 21:23:18 字数 1712 浏览 2 评论 0原文

有人可以帮我解决这个问题吗?我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不弃不离 2024-11-10 21:23:18

错误是正确的。您正在映射:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文