我通过在我的通用存储库中执行此操作解决了这个问题,我还为没有 isDeleted 的实体创建了一个只读存储库(它们根本不由应用程序管理,只是读取),只读存储库获取所有记录
简单的 repo 继承了 readonly 并覆盖了不应返回标记为 isDeleted = false 的实体的方法;
应该标记为 IsDeleted = true 的实体继承自 DelEntity
public class DelEntity : Entity, IDel
{
public bool IsDeleted { get; set; }
}
我的通用存储库:
public class Repo<T> : ReadRepo<T>, IRepo<T> where T : DelEntity, new()
{
public Repo(IDbContextFactory a) : base(a)
{
}
...
public override IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
return c.Set<T>().Where(predicate).Where(o => o.IsDeleted == false);
}
public override IEnumerable<T> GetAll()
{
return c.Set<T>().Where(o => o.IsDeleted == false);
}
}
我的只读操作存储库
public class ReadRepo<T> : IReadRepo<T> where T : Entity, new()
{
protected readonly DbContext c;
public ReadRepo(IDbContextFactory f)
{
c = f.GetContext();
}
public T Get(long id)
{
return c.Set<T>().Find(id);
}
public virtual IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
return c.Set<T>().Where(predicate);
}
public virtual IEnumerable<T> GetAll()
{
return c.Set<T>();
}
public int Count()
{
return c.Set<T>().Count();
}
}
这个解决方案有点针对我的情况,但希望你能从中得到一些想法