IsDirty event in object graph; good pattern needed

  • Thread starter Thread starter wbekker
  • Start date Start date
W

wbekker

Hi,

I'm searching for a good pattern for the following problem:

In a large object tree, all object implement a property called IsDirty.
That flag is set when a property is modified. If a child object is
dirty, the parent object must return IsDirty = true as well. This works.

I now want to have an event on the root object of the tree, that signals
when one object in the tree is set to dirty. What is a good approach for
this?

All i can come up with is a brute fore approach where i get a list of
all the objects in the tree, and subscribe to an event of each object
called something like "DirtyStateChanged".

Ward
 
Hi,

It depends on how quickly you are required to determine the dirty state. You
can act proactively as you have described, so you spend CPU cycles on
handling the events, but you have the answer ready in no time. Or, when the
IsDirty property is queried on the root object, just do a recursive walk on
the object graph and if at least one of the child objects is dirty, the
entire graph is dirty.
 
wbekker said:
Hi,

I'm searching for a good pattern for the following problem:

In a large object tree, all object implement a property called IsDirty.
That flag is set when a property is modified. If a child object is dirty,
the parent object must return IsDirty = true as well. This works.

I now want to have an event on the root object of the tree, that signals
when one object in the tree is set to dirty. What is a good approach for
this?

All i can come up with is a brute fore approach where i get a list of all
the objects in the tree, and subscribe to an event of each object called
something like "DirtyStateChanged".

Ward

I had a similar problem when creating a menu for a website.
I solved it by having the nodes be parent-aware and have IsDirty bubble up.

Hence, a leaf could make an entire branch dirty.

But while we're at it. I would rename IsDirty to an protected member called
invalidated. And sport the method Invalidate(). Guess I'm too fond of
Delphi.

- Michael S
 
wbekker said:
All i can come up with is a brute fore approach where i get a list of
all the objects in the tree, and subscribe to an event of each object
called something like "DirtyStateChanged".

If you have an ownership/parentage relationship the child objects could also call directly into the
parent instead.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 
As a number of people here have stated, you're probably better off walking
up the parent hierarchy. You are accomplishing essentially the same thing
as the event subscription, but this way, you don't have to use events.

Remember: each event subscription is a new object allocation -- the
delegate. And that delegate essentially goes into a collection (the events
are multicast -- they don't look like collections, but you are holding on to
each delegate)
 
Thank you all for the great suggestions, i will give the "walking up the
parent hierarchy" a try!

Ward
 
Back
Top