I solved this by doing this in my shared repository, I also made a read-only repository for objects that do not have isDeleted (they are not at all controlled by the application, just read), readonly repo gets all the records
a simple repo inherits only readonly and overrides methods that should not return objects marked as isDeleted = false;
objects that should be simply marked IsDeleted = true inherit from DelEntity
public class DelEntity : Entity, IDel { public bool IsDeleted { get; set; } }
my shared repository:
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); } }
my repository for readonly operations
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(); } }
this solution is a little for my business, but I hope you can get some ideas from it.
source share