Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通用接口和面向对象结构的最佳实践_C#_Oop_Generics_Interface - Fatal编程技术网

C# 通用接口和面向对象结构的最佳实践

C# 通用接口和面向对象结构的最佳实践,c#,oop,generics,interface,C#,Oop,Generics,Interface,我有一个接口,它有20个应该实现的属性。所有这些属性都应该返回一个接口类型或从这个接口继承的任何东西,这20个属性中的每个属性都返回一个不同的类型 有没有比下面更好的方法 public interface IRepository<Product, ProductCategory, Category, ProductImage> where Product : IProduct where ProductCategory : IProductCategory

我有一个接口,它有20个应该实现的属性。所有这些属性都应该返回一个接口类型或从这个接口继承的任何东西,这20个属性中的每个属性都返回一个不同的类型

有没有比下面更好的方法

    public interface IRepository<Product, ProductCategory, Category, ProductImage>
    where Product : IProduct
    where ProductCategory : IProductCategory
    where Category : ICategory
    where ProductImage : IProductImage
{
    IQueryable<Product> Products { get; set; }
    IQueryable<ProductCategory> Products { get; set; }
    IQueryable<Category> Products { get; set; }
    IQueryable<ProductImage> Products { get; set; }
}
公共接口IRepository
何处产品:IPProduct
其中ProductCategory:IPProductCategory
其中类别:ICategory
其中ProductImage:IProductImage
{
IQueryable乘积{get;set;}
IQueryable乘积{get;set;}
IQueryable乘积{get;set;}
IQueryable乘积{get;set;}
}
我已经简要介绍了上面的代码,在接口中只有四种泛型类型


谢谢。

通常,使用接口可以避免将代码绑定到特定的具体类型

为什么你可以写:

public interface IRepository
{    
    IQueryable<IProduct> Products { get; set; }
    IQueryable<IProductCategory> Products { get; set; }
    IQueryable<ICategory> Products { get; set; }
    IQueryable<IProductImage> Products { get; set; }
}
公共接口IRepository
{    
IQueryable乘积{get;set;}
IQueryable乘积{get;set;}
IQueryable乘积{get;set;}
IQueryable乘积{get;set;}
}

通常,人们使用接口来避免将代码绑定到特定的具体类型

为什么你可以写:

public interface IRepository
{    
    IQueryable<IProduct> Products { get; set; }
    IQueryable<IProductCategory> Products { get; set; }
    IQueryable<ICategory> Products { get; set; }
    IQueryable<IProductImage> Products { get; set; }
}
公共接口IRepository
{    
IQueryable乘积{get;set;}
IQueryable乘积{get;set;}
IQueryable乘积{get;set;}
IQueryable乘积{get;set;}
}

因为如果我实现了这个接口,所有属性都应该是一个精确的接口类型,而不是实现它的任何属性,这是我的主要问题。谢谢你的尝试。因为如果我实现了这个接口,所有的属性都应该是一个精确的接口类型,而不是实现它的任何属性,这是我的主要问题。谢谢你的努力。