This is a migrated thread and some comments may be shown as answers.

Soft deletes / overriding delete logic

3 Answers 81 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
John Tabernik
Top achievements
Rank 1
John Tabernik asked on 24 Feb 2014, 06:46 PM
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: 
delete-action="dbdefaultrule"

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

Thanks very much for your help!  OpenAccess/DataAccess is an outstanding tool!

3 Answers, 1 is accepted

Sort by
0
John Tabernik
Top achievements
Rank 1
answered on 24 Feb 2014, 08:04 PM
Ignore my question about "delete-action=dbdefaultrule."  This appears to be metadata about the constraint.  Thanks.
0
Accepted
Kristian Nikolov
Telerik team
answered on 27 Feb 2014, 10:41 AM
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;
        base.SaveChanges();
    }
  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.

Regards,
Kristian Nikolov
Telerik
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
0
John Tabernik
Top achievements
Rank 1
answered on 27 Feb 2014, 02:08 PM
This is extremely helpful!!  Thanks for the very detailed reply.
Tags
General Discussions
Asked by
John Tabernik
Top achievements
Rank 1
Answers by
John Tabernik
Top achievements
Rank 1
Kristian Nikolov
Telerik team
Share this question
or