views:

60

answers:

1

Hi,

i have to develop a mechanism to check two object properties for changes.
All properties which are needed to check are marked with an attribute.

Atm i
- read all properties from acutal object via linq
- read the corresponding property from old object
- fill an own object with the two properties (old and new value)

In Code the call to the workerclass looks like this

    public void CreateHistoryMap(BaseEntity actual, BaseEntity old)
    {
        CreateHistoryMap(actualEntity, oldEntity)
                       .ForEach(mapEntry => CreateHistoryEntry(mapEntry),
                                mapEntry => IfChangesDetected(mapEntry));
    }

CreateHistoryMap builds up the HistoryMapEntry which contains the two properties.
CreateHistoryEntry build up the object which is saved to database, the IfChangesDetected check the object for changes.

I have to handle own special application types to generate history values to database (like concatinating list values and so on).

My problem is now, that i have to read the values of the properties twice
- for change detection
- and for the concreate CreateHistoryEntry

How can i eliminate this problem or how can i implement the change tracking scenario with the nice c# 3.5 features?

Thanks a lot.

A: 

You could take the approach taken by the CSLA.NET framework, which features undoable objects (and property change tracking for data binding). Its a very clean implementation and is placed in the base class of your business objects, to get the benefit you simply derive.

Link: http://www.lhotka.net/cslanet/

The classes in question are BusinessBase and UndoableBase - you can also cut out just the change tracking code and leave the rest - although CSLA.NET is well designed, there is a lot of logic embedded into the business objects.

Adam
Thanks for the hint to the framwork, looks really interesting. But i can't use it. I have to develop the change tracking / history work on my own :/I
K.Hoffmann
You could still put this logic into the class itself. A class can clone a copy of itself via serialisation and you can then define an interface, base class, or helper method to take the two instances of the class and compare its properties. The question is also do you care about internal object state or just public state?
Adam
The problem is, that in our application almost all entity fields and processes are configurable by the user (with admin rights in application). I am not allowed to use this framework. (i try it @home i think, cause it looks really interesting)
K.Hoffmann