正准备写一个基于NBear IoC的框架。现在已经做了一个Service的接口,如下
namespace ServiceInterface
{
    public interface IEntityOperator<EntityType> : IServiceInterface
    {
        EntityType[] GetAllEntities();
    }
}
接口的实现,如下
namespace ServiceImpls
{
    public class EntityService<EntityType> : IEntityOperator<EntityType>
        where EntityType : Entity, new()
    {
        public EntityType[] GetAllEntities()
        {
            return Gateway.Default.FindArray<EntityType>(WhereClip.All, OrderByClip.Default);
        }
        
    }
}
以后准备丰富了接口的方法,就可以很方便的访问数据库。但在配置web.config的时候出现了问题一般非泛型的接口,在web.config中这样配置,如下
<component id="category service" service="ServiceInterface.ICategoryService, ServiceInterface" type="ServiceImpls.CategoryService, ServiceImpls"/>但因为使用了泛型,我采用如下配置
<component id="product service" service="ServiceInterface.IEntityOperator&lt;EntityType&gt;, ServiceInterface" type="ServiceImpls.EntityService&lt;EntityType&gt;, ServiceImpls"/>其中&lt;EntityType&gt;被程序读出来后是“<EntityType>”的样子但无论按正常的方式配置还是后面的配置,程序都提示我的接口实现无法located错误码为The type name ServiceImpls.EntityService, ServiceImpls could not be located

The type name ServiceImpls.EntityService<EntityType>, ServiceImpls could not be located不知道怎么配置是正确的。

解决方案 »

  1.   

    很奇怪lz会写出这样的代码。您不知道怎么实例化一个泛型类吗?实例化的时候必须同时指明<>中的实际类型是什么,否则会失败。
      

  2.   

    我的实例化代码是这样的ServiceFactory factory = ServiceFactory.Create();IEntityOperator<Products> ProductOperator = factory.GetService<IEntityOperator<Products>>();Products是Entity的子类问题不是出在实例化,而是在ServiceFactory.Create()上,此处ServiceFactory 读取config文件,将接口的实现和接口绑定但由于使用了泛型,ServiceImpls.EntityService <EntityType>并没有被识别出来,也就没有被绑定到接口这是我初步分析的结果,但不知道是否有道理
      

  3.   


    原来castle有具体的规范http://www.castleproject.org/container/documentation/v1rc3/usersguide/genericssupport.html<component 
    id='int.repos.generic' 
    service='Namespace.IRepository`1, AssemblyName' 
    type='Namespace.Repository`1, AssemblyName' />“`1”即可表达我的那个泛型