Capture Delete to change status rather then remove record

Posts   
 
    
like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 21-Apr-2009 13:50:43   

I'd like to write some code (adding to the CustomEntityBase class?) so that when I call the Delete on any of my entities the StatusID field of the entity is set rather than the Entity being deleted. Is this possible?

I've tried things like in the CustomEntityBase class:


Private _RealDelete As Boolean

Public Property RealDelete() As Boolean
      Get
        Return _RealDelete
      End Get
      Set(ByVal value As Boolean)
        _RealDelete = value
      End Set
    End Property

Protected Overrides Sub OnDelete()
      If _RealDelete Then
        MyBase.OnDelete()
      Else
        Me.Fields("StatusId").CurrentValue = StatusEntity.Deleted
        Me.Save()
      End If
    End Sub
 Public Overloads Function Delete() As Boolean
      If _RealDelete Then
        MyBase.OnDelete()
      Else
        Me.Fields("StatusId").CurrentValue = StatusEntity.Deleted
        MyBase.Save()
      End If
    End Function

Any suggestions gratefully received!

Winforms Vb.net in Visual Studio 2008 Sp1, llblgen 2.6, access mdb

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 21-Apr-2009 20:09:18   

Here is what Frans has to say about that kind of thing. That being said, you should be able to change it using Auditors and dependency injection.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 21-Apr-2009 21:32:30   

It depends if you are using self-servicing or adapter. If SS you will need to modify the template so that it sets the flag for you rather than deleting the entity.

If adapter, you can inherit from DataAccessAdapter and override the delete methods to set the flag for you rather than deleting the entities.

It's generally a crappy solution for performace though as every query then needs to filter out "deleted" entities - and because the selectvity on the "deleted" column won't be very high (all of your "undelted" records are null - i'm guessing this will be the majority of them) indexing it doesn't gain you a great deal - you will end up with a lot of table scans.

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 22-Apr-2009 09:49:03   

Wow.. just read Frans's post re soft deletes. My situation is different again (I think) I have a "mission-critical" application running at a number of remote sites) which all need to be periodically synchronised with each other and a master database. (Local copies of the database are held so the the application can continue to be used if the communications link back the the master database is broken) Any of the remote sites may choose to delete a record (albeit on very rare occaisions). The only way I can think of doing this is to flag a record as deleted so that when the remote and master databases are compared I can flag records as deleted in the master which will then ripple out the the other remote sites as they sync. If I just remove a record from the local then the sync with the master will just bring the record back.

What I wanted to do was to be able to call Entity.Delete to "soft-delete" the record, but if I set Entity.TrueDelete=True then call Entity.Delete the record would be removed from the database.

I guess what I can do is create an Entity.FlagAsDeleted in CommonBaseEntity as a equally "elegant" solution.

  Public Function FlagAsDeleted() As Boolean
      Me.Fields("StatusId").CurrentValue = StatusEntity.Deleted
      Return Me.Save()
    End Function

Thanks for your comments.

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 22-Apr-2009 19:50:35   

I honestly think Auditors is the way to go....

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 22-Apr-2009 20:08:21   

Hi Like -

What database are you using? Are you planning on manually synching these databases? I've been there before. Not worth it.

Would not SQL Replication take care of all this for you?

Ryan

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 23-Apr-2009 21:13:02   

I'm with Ryan - manually keeping DB's in sync is a colossal PITA. Just think of all the work that has gone into making SQL replication work - why re-invent the wheel...!

Matt

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 27-Apr-2009 11:50:56   

I have a working solution now.

I am syncing a number of remotely located Microsoft Access 97 databases with a central Oracle one. I don't want the "overhead" of having to support local installations of Oracle/SQL Server/MSDE at the remote sites: with Access 97 databases I can copy an "empty"installation of the application to a pc at the remote location, then press the replicate button to get a new pc up and running within minutes. I am able to work on the principal of 'who updated it last gets to win' on a record by record basis and there will only be deletes(flagged or otherwise) on a very rare occasions so things are pretty simple.

Thanks for everyone for your comments.