I have a generic repository implementation. I’m using asp.net mvc c#, code first entity framework.
I created an interface named ISoftDelete:
public interface ISoftDelete
{
bool IsDeleted { get; set; }
}
I implemented Delete and GetById in my base repository as follows:
public virtual void Delete(T entity)
{
if (entity is ISoftDelete)
{
((ISoftDelete)entity).IsDeleted = true;
}
else
{
dbset.Remove(entity);
}
}
public virtual T GetById(long id)
{
T obj = dbset.Find(id);
if (obj is ISoftDelete)
{
if (((ISoftDelete)obj).IsDeleted)
return null;
else
return obj;
}
else
{
return obj;
}
}
Now, I have 2 questions.
1) Is this approach is a good approach? Any performance related issues?
2) My original GetAll function in the base repository is like this:
public virtual IEnumerable<T> GetAll()
{
return dbset.ToList();
}
How shall I modify it in order to list records where IsDeleted == false, when T is derived from ISoftDelete?
Thank you!
1) It does not seem ok for checking
if (entity is ISoftDelete)every time you need to know it. If you are sure you are not going to check it anywhere else it may be ok. In terms of performance it would be better if you eleminate records that haveIsDeleted == trueand never fetch them from db. You may need to derive a new base repository which overrides these methods and implements new logics for ISoftDelete object.2) may be something like