1

Our products have a IsDeleted flag, so the products stay in the database when deleting them. (They are just flagged as deleted)

It is annoying to always check the deleted flag when querying the server (where c.IsDeleted == False && ...). Is there a way to centralize this? In the DbContainer / Context or somewhere else?

Jannik
  • 2,310
  • 6
  • 32
  • 61
  • You can create an interface and apply that to your entities, so you can filter them in a generic way. This won't work for child entities though, there you'll either have to build an expression through reflection to build the proper query, or remove the deleted child entities from memory. – CodeCaster Nov 14 '14 at 07:49
  • 1
    I have posted a soft delete solution for Entity Framework over here http://stackoverflow.com/a/18985828/150342 – Colin Nov 14 '14 at 09:19
  • 1
    Another option is the DevForce EntityServerQueryInterceptor using an [EntityQueryFilterCollection](http://drc.ideablade.com/ApiDocumentation2012/IdeaBlade.EntityModel~IdeaBlade.EntityModel.EntityQueryFilterCollection.html). – Kim Johnson Nov 14 '14 at 17:20
  • Thanks Kim, thats what I was searching for :) – Jannik Nov 17 '14 at 06:41

1 Answers1

1

Another solution is to use an extension method. This won't exactly centralize, but will encapsulate.

public static IQueryable<T> Undeleted(this IQueryable<T> queryable)
    where T : ISoftDeletable
{
    return queryable.Where(x => !x.IsDeleted);
}

Interface:

public interface ISoftDeletable
{
    bool IsDeleted { get; }
}

Usage:

var undeleteds = myEntitySet.Undeleted().ToArray();
danludwig
  • 46,965
  • 25
  • 159
  • 237