Design Patterns: A patern for detecting business object changes needed

Dear Developer Community,

Im looking for a design pattern or best practice to solve the following problem:

I have my business objects (such as Account, Client, Complaint etc) all derived from a BusinessObject base class. These business objects might also consume other business objects, such as Account containing a List<AccountContact>, or Complaint having List<Comments>.

What Im looking for is a mechanism of probing a business object for changes. Something like:

Account acc = new Account();

acc.CompanyName = "New Company";

if (acc.HasObjectChanged) MessageBox.Show("Object Changed!")

else MessageBox.Show("Didnt change yet!");

Any suggestions

Kind regards,

Marcel



Answer this question

Design Patterns: A patern for detecting business object changes needed

  • Bruce Baker

    My base business object class manages an EntityStatus flag to define whether the data is an insert, update, delete, or unchanged. When the object property is set, the set method calls the base class EntityStateChanged to define the appropriate state.

    Hope this helps...


  • sql2000_2005

    The only practical method I can think of is to have the HasObjectChanged property defined in BusinesObject, with a public getter, and a protected setter. Then on every property setter in each derived class, set HasObjectChanged to true. Reset it to false when the object is saved.

  • xlordt

    Normally I'd use the Observer pattern to get informed of changes to an object rather than trying to figure out what changes there are at a given point of time.

  • skuehner

    Peter,

    I considered all the mentioned options but I think I want to go with the Observer patern you mentioned. So after some research, I found that it could be implemented in C# with the Event-Listner patern.

    Can you give me some guidance towards implementing it in my design then. You see, I dont think I totally understand the model.

    How would the event be registered if I wanted to know whether an AccountContact object changed in the following code:

    public class Account : BusinessObject{

    List<AccountContact> contacts;

    }

    Thank you for your time.

    Marcel


  • Design Patterns: A patern for detecting business object changes needed