我有很多实体和存储库。我的实体有两种:可擦和不可擦。所以我有两个实体的基类。
不可磨灭的实体实现此基类:
public abstract class BaseEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime InsertedDate { get; set; }
public DateTime? UpdatedDate { get; set; }
//For recovering
public DateTime? DeletedDate { get; set; }
public bool Active { get; set; }
}
可擦除实体实现此基类:
public abstract class BaseErasableEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime InsertedDate { get; set; }
public DateTime? UpdatedDate { get; set; }
}
使用可擦除实体的存储库实现此基类:
public class BaseErasableRepository<TEntity, TRepository> : DbContext, IBaseErasableRepository<TEntity> where TEntity : BaseErasableEntity where TRepository : DbContext
{
public BaseErasableRepository(DbContextOptions<TRepository> options) : base(options)
{ }
protected DbSet<TEntity> Entities { get; set; }
public IEnumerable<TEntity> GetAll()
{
return Entities ?? throw new CannotFindEntityException();
}
public TEntity GetById(int id)
{
var entity = Entities.Find(id) ?? throw new CannotFindEntityException(id);
return entity;
}
public void Update(TEntity entity)
{
Entities.Update(entity);
SaveChanges();
}
public void Delete(TEntity entity)
{
Entities.Remove(entity);
SaveChanges();
}
public IEnumerable<TEntity> GetFiltered(Func<TEntity, bool> condition = null)
{
return Entities.Where(condition) ?? throw new CannotFindEntityException();
}
}
使用不可删除实体的存储库实现此基类:
public class BaseRepository<TEntity, TRepository> : DbContext, IBaseRepository<TEntity> where TEntity : BaseEntity where TRepository : DbContext
{
public BaseRepository(DbContextOptions<TRepository> options) : base(options)
{ }
protected DbSet<TEntity> Entities { get; set; }
public IEnumerable<TEntity> GetAll()
{
return Entities.Where(entity => entity.Active == true) ?? throw new CannotFindEntityException();
}
public TEntity GetById(int id)
{
var entity = Entities.Find(id) ?? throw new CannotFindEntityException(id);
if(!entity.Active)
throw new CannotFindEntityException();
return entity;
}
public void Update(TEntity entity)
{
Entities.Update(entity);
SaveChanges();
}
public void Delete(TEntity entity)
{
var toBeDeleteEntity = GetById(entity.Id);
toBeDeleteEntity.Active = false;
toBeDeleteEntity.DeletedDate = DateTime.Now;
Entities.Update(toBeDeleteEntity);
SaveChanges();
}
public IEnumerable<TEntity> GetFiltered(Func<TEntity, bool> condition = null)
{
return Entities.Where(entity => entity.Active).Where(condition) ?? throw new CannotFindEntityException();
}
}
我的问题:我有更多的存储库方法。它们对于两个存储库都是相同的。添加新功能时,我必须编写两次相同的代码。有没有更好的方法来使用单个基础存储库类?
看看通用存储库模式。它可以解决类似存储库中的代码复制问题。在此链接中,您可以找到此模式的描述,希望对您有所帮助https://dotnettutorials.net/lesson/generic-repository-pattern-csharp-mvc/
@@ Yojizan感谢您的回答。但是我已经使用了这种模式。如果检查上面的基本存储库类,则可以看到此信息。例如,ProductRepository是这样的:
public class ProductRepository : BaseRepository<Product,ProductRepository>, IProductRepository
{
public ProductRepository(DbContextOptions<ProductRepository> options) : base(options)
{
}
public void AddRange(IEnumerable<Product> products)
{
products.ToList().ForEach(product=>
{
product.Name = prodcut.Name.ToUpper();
product.Active = true;
product.InsertedDate = DateTime.Now;
});
Entities.AddRange(entities);
SaveChanges();
}
}
我只想进行优化或更改结构以防止在两个基本存储库中重复相同的代码