C# 通用存储库实现
C# Generic Repository Implementation
我想要存储库的通用实现可以了解基本实体 Class 并访问其属性的存储库。基本实体具有实体的所有基本属性,如 Id、DateCreated、DateAdded、IsActive 等。
但我的问题是,即使 Base 实体的所有 sub class 都继承自它,它们的 Repository Class 要求它们的实现并且看不到它们是从 super class 派生的具有所有需要的属性,我不能重用我在那里写的代码。
public interface IGenericRepo<T>
{
int Add(T Entity);
int Delete(T Entity);
}
public interface IProductRepository: IGenericRepo<Product>
{
Product GetBestselling();
}
public class GenericRepo : IGenericRepo<BaseEntity>
{
public int Add(BaseEntity Entity)
{
Console.WriteLine(Entity.Id);
return Entity.Id;
}
public int Delete(BaseEntity Entity)
{
Console.WriteLine(Entity.Id);
return Entity.Id;
}
}
public class ProductRepo : GenericRepo, IProductRepository
{
public Product GetBestselling()
{
throw new NotImplementedException();
}
}
public class BaseEntity
{
public int Id { get; set; }
public int DateAdded { get; set; }
}
public class Product : BaseEntity
{
public string Name { get; set; }
public string Fname { get; set; }
}
上面的代码要求实现 IProductRepository,我该如何实现?
public interface IGenericRepo<T>
{
int Add(T Entity);
int Delete(T Entity);
}
public class BaseEntity
{
public int Id { get; set; }
public int DateAdded { get; set; }
}
public class Product : BaseEntity
{
public string Name { get; set; }
public string Fname { get; set; }
}
public class GenericRepo : IGenericRepo<BaseEntity>
{
public int Add(BaseEntity Entity)
{
Console.WriteLine(Entity.Id);
return Entity.Id;
}
public int Delete(BaseEntity Entity)
{
Console.WriteLine(Entity.Id);
return Entity.Id;
}
}
public interface IProductRepository
{
Product GetBestselling();
}
public class ProductRepo : GenericRepo, IProductRepository
{
public Product GetBestselling()
{
throw new NotImplementedException();
}
}
这应该没有任何问题
您可以考虑使用 generic constraints 来声明您的通用存储库:
public class GenericRepo<T> : IGenericRepo<T> where T : BaseEntity
{
public int Add(T Entity) => Entity.Id;
public int Delete(T Entity) => Entity.Id;
}
public class ProductRepo : GenericRepo<Product>, IProductRepository
{
public Product GetBestselling() => throw new NotImplementedException();
}
这让基础 class 可以像使用 BaseEntity 一样使用通用参数,但您的 ProductRepo 可以更具体。请注意 IGenericRepo<BaseEntity>
!= IGenericRepo<Product>
,以允许隐式转换,请参阅 covariance and contravariance。
我想要存储库的通用实现可以了解基本实体 Class 并访问其属性的存储库。基本实体具有实体的所有基本属性,如 Id、DateCreated、DateAdded、IsActive 等。
但我的问题是,即使 Base 实体的所有 sub class 都继承自它,它们的 Repository Class 要求它们的实现并且看不到它们是从 super class 派生的具有所有需要的属性,我不能重用我在那里写的代码。
public interface IGenericRepo<T>
{
int Add(T Entity);
int Delete(T Entity);
}
public interface IProductRepository: IGenericRepo<Product>
{
Product GetBestselling();
}
public class GenericRepo : IGenericRepo<BaseEntity>
{
public int Add(BaseEntity Entity)
{
Console.WriteLine(Entity.Id);
return Entity.Id;
}
public int Delete(BaseEntity Entity)
{
Console.WriteLine(Entity.Id);
return Entity.Id;
}
}
public class ProductRepo : GenericRepo, IProductRepository
{
public Product GetBestselling()
{
throw new NotImplementedException();
}
}
public class BaseEntity
{
public int Id { get; set; }
public int DateAdded { get; set; }
}
public class Product : BaseEntity
{
public string Name { get; set; }
public string Fname { get; set; }
}
上面的代码要求实现 IProductRepository,我该如何实现?
public interface IGenericRepo<T>
{
int Add(T Entity);
int Delete(T Entity);
}
public class BaseEntity
{
public int Id { get; set; }
public int DateAdded { get; set; }
}
public class Product : BaseEntity
{
public string Name { get; set; }
public string Fname { get; set; }
}
public class GenericRepo : IGenericRepo<BaseEntity>
{
public int Add(BaseEntity Entity)
{
Console.WriteLine(Entity.Id);
return Entity.Id;
}
public int Delete(BaseEntity Entity)
{
Console.WriteLine(Entity.Id);
return Entity.Id;
}
}
public interface IProductRepository
{
Product GetBestselling();
}
public class ProductRepo : GenericRepo, IProductRepository
{
public Product GetBestselling()
{
throw new NotImplementedException();
}
}
这应该没有任何问题
您可以考虑使用 generic constraints 来声明您的通用存储库:
public class GenericRepo<T> : IGenericRepo<T> where T : BaseEntity
{
public int Add(T Entity) => Entity.Id;
public int Delete(T Entity) => Entity.Id;
}
public class ProductRepo : GenericRepo<Product>, IProductRepository
{
public Product GetBestselling() => throw new NotImplementedException();
}
这让基础 class 可以像使用 BaseEntity 一样使用通用参数,但您的 ProductRepo 可以更具体。请注意 IGenericRepo<BaseEntity>
!= IGenericRepo<Product>
,以允许隐式转换,请参阅 covariance and contravariance。