Click here to Skip to main content
15,888,351 members
Articles / Programming Languages / C#

"Soft Delete" or "Logical Delete" in ORM

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
10 Dec 2009CPOL2 min read 37K   8   4
"Soft Delete" or "Logical Delete" in ORM

People often ask which ORM supports "Soft Delete" or "Logical Delete". Let's try to figure out what it is and whether ORM should internally support it.

"Soft Delete" is a way of removing business entities, which implies that we mark an entity as deleted instead of physically removing it from the database. This approach is often used in Line of Business applications because of its several advantages:

  • It allows us to keep history for different  auditing sceneries. For example, somebody removed one document in the past from our workflow system, we surely want to be able to audit removing log and data removed document contained.
  • It allows to implement Recycle Bin approach in an easy way. We'd like to be able to recycle any document removed in the past.

To implement "Soft Delete" feature in a simple case, we should:

  • Create IsDeleted persistent property of bool type in all softly removable types.
  • Automatically filter all queries, i.e., automatically add Where(entity => !entity.IsDeleted) to each LINQ query.

In my example on DataObjects.Net, I use a single base class for all business objects, so I can add IsDeleted field to this class:

C#
public class BusinessObject : Entity
{
  [Field]
  public bool IsDeleted { get; set;}

  public new void Remove()
  {
    IsDeleted = true;
  }
}

Then, let's create DataContext class responsible for data access:

C#
public static class DataContext
{
  public static IQueryable<T> GetAll<T>() where T : BusinessObject
  {
    return Query<T>.All
      .Where(entity => !entity.IsDeleted);
  }
}

Now we can softly remove our entities and query not removed ones. I've created a small sample illustrating model of blog-publishing service, consisting of three classes: Blog, BlogPost and Comment. So I can query recent posts from my blog using such LINQ-query:

C#
from post in DataContext.GetAll<BlogPost>()
where 
  post.PublishDate > DateTime.Now-TimeSpan.FromDays(7) && 
  post.Blog == myBlog
select post;

This query will return all posts except softly deleted and I don't have to add appropriate check to every query in my application.

That is the simplest example of Soft (or Logical) delete implementation, we can surely take into account different aspects specific for a particular application. Generally, I am sure that "Soft Delete" shouldn't be internally implemented within an ORM framework, it's more flexible to implement it yourself.

This article was originally posted at http://alex-kofman.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer X-tensive
Russian Federation Russian Federation
See my blog on software development at Alex-Kofman.blogspot.com

Comments and Discussions

 
GeneralInteresting idea Pin
rijibarter20-Dec-09 7:14
rijibarter20-Dec-09 7:14 
General"Soft Delete" disadvantages Pin
Alex Kofman20-Dec-09 20:30
Alex Kofman20-Dec-09 20:30 
Note, that in typical business application very small amount of data can be deleted. Usually new documents are added every day and database grows any way, and this process doesn't depend on how you delete wrong documents. If you use soft deletes for some temporary entities, e.g. temporary relationships, it's really hard to use such softly deleted entities to obtain some useful information.

Another problem you can face is database consistency. For example you may want to create foreign key to some entity, that supports soft delete. So the foreign key must ensure that it references not removed entity, but such logic is very hard to implement on database level, because appropriate record will exist in a table after deleting.

In general it is not so easy to make a decision which entities will be softly deletable in your application.

Keep in mind, that there is another way to provide information about removed entities for some audit. It's logging, that can be also easily implemented. Logging can be also useful for keeping history about updated entities. So I recommend to think twice before making such decision.
GeneralUnique keys Pin
supercat910-Dec-09 7:51
supercat910-Dec-09 7:51 
GeneralRe: Unique keys [modified] Pin
Alex Kofman10-Dec-09 22:30
Alex Kofman10-Dec-09 22:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.