使用Unity IoC容器

时间:2011-04-27 23:35:37

标签: unity-container

有人可以帮我吗?我正在尝试使用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,   PARAMS   Microsoft.Practices.Unity.InjectionMember [])”。   没有隐含的参考   转换自   'AdventureWorksManagement.ServiceAgents.AdventureWorksWCFRepository'   至   'AdventureWorksManagement.ServiceAgents.IAdventureWorksRepository'。 C:\我的开发\我的测试   地面\ MyTesting   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;

     .....

请指教。 dormantroot

1 个答案:

答案 0 :(得分:0)

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

IAdventureWorksRepository&LT; IProxyObjectAdapter&GT; - &GT; AdventureWorksWCFRepository&LT; IProxyObjectAdapter&GT;

但是,在代码定义中,AdventureWorkdsWCFRepository&lt; T&gt;实现IAdventureWorksRepository&lt; IBaseModel &gt;。这不是您尝试映射的界面。我怀疑IBaseModel在代码中应该是“T”。