Soft deletes / overriding delete logic

4 posts, 1 answers
  1. John Tabernik
    John Tabernik avatar
    3 posts
    Member since:
    Apr 2010

    Posted 24 Feb 2014 Link to this post

    I don't want to perform physical deletes in my model--I want all the deletes to be soft deletes.  I am not looking for the model to do this automatically--I just want to be able to override the delete method.  What is the best way to do this?

    I found a post related to this here.  There are a few differences for the delete as opposed to the update--I want to call the SaveChanges() from the Delete() instead of the base.Delete().  Will that be a problem at all?  I am assuming I just need to set the soft delete field for each "entity" that was passed to the delete method.  Any hints on logic to do this, i.e. the objects that I should be executing the commands on?  There does not seem to be a lot of example code for this since I am basically overriding the core functionality of the product.

    Another route might be to use stored procs for the CUD operations.  It does not look like there is a way to only do the delete operations, and I would rather not code all three just to override the deletes.

    Finally, in the rlinq file (when viewed as text) I noticed the following on each entity: 

    Is there some easy way to use this to override the Delete()?

    Thanks very much for your help!  OpenAccess/DataAccess is an outstanding tool!
  2. John Tabernik
    John Tabernik avatar
    3 posts
    Member since:
    Apr 2010

    Posted 24 Feb 2014 in reply to John Tabernik Link to this post

    Ignore my question about "delete-action=dbdefaultrule."  This appears to be metadata about the constraint.  Thanks.
  3. DevCraft banner
  4. Answer
    Kristian Nikolov
    Kristian Nikolov avatar
    206 posts

    Posted 27 Feb 2014 Link to this post

    Hi John,

    We are happy you are pleased with Telerik Data Access.

    Both approaches - using Delete method or Stored Procedures are viable for the execution of soft deletes. In my answer I will address these approaches.

    Regarding soft deletes using the Delete method, I would like to point out that you do not need to override or hide the default Delete method of the OpenAccessContext class. The reason is that your Delete method will have a different signature from the default one. To implement the approach you could use the following steps:
    1. Create an interface ISoftDeletable with property IsDeleted of type bool. See this link if you are using Domain Model.
    2. Make sure your Persistent classes are implementing the interface.
    3. Your model should have a partial class which inherits OpenAccessContext. You can extend this partial class in another code file.
    4. In the new code file for the partial context class, you can define the new Delete method:
      public void Delete<T>(T entity) where T : ISoftDeletable
          entity.IsDeleted = true;
    5. You can then call this new delete method the same way you would the default one.

    As for the approach using stored procedures, you could create a stored procedure which would handle the soft delete on you backend. The next step depends on your model type:

    With both approaches you could consider to extend the partial context class with a property which retrieves only the entities which are not marked as deleted.

    I hope this helps. If you have any more questions, feel free to post in our forums again.

    Kristian Nikolov
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  5. John Tabernik
    John Tabernik avatar
    3 posts
    Member since:
    Apr 2010

    Posted 27 Feb 2014 in reply to Kristian Nikolov Link to this post

    This is extremely helpful!!  Thanks for the very detailed reply.
Back to Top