Groups | Blog | Home
all groups > dotnet framework > june 2007 >

dotnet framework : Best practices question ... monitoring an objects contents ...


Peter Duniho
6/28/2007 10:32:02 AM
On Thu, 28 Jun 2007 09:56:39 -0700, Jamie Risk <risk.#.@intectus.com> =

[quoted text, click to view]
f =

[quoted text, click to view]
l:

I'm not sure what "best practices" has to do with anything here. There =
=

are a variety of mechanisms by which the information you seek could be =

obtained, but what's the "best" way surely depends on how you want it to=
=

work, and what sort of access you have to the classes.

Assuming that semantically what you want to know is whether a mutable =

class is "dirty" (that is, has been instantiated with some specific =

values, at least one of which has subsequently been changed), I would ju=
st =

include a boolean flag in the class to indicate that state. In any of =

your properties, have the setter also set the flag to true. Then you =

could have a read-only property that retrieves the flag.

For example:

[quoted text, click to view]

However, obviously you could accomplish this in a variety of other ways.=
=

For example, make the object cloneable and equatable, and then create a =
=

clone immediately after instantiation which you can then later compare f=
or =

equality. Or you could create an event (per-instance) or events =

(per-property) that are raised whenever a property is modified; =

subscribing to those events would tell you as it happens whether changes=
=

have occurred. Or you could simply create a different data structure th=
at =

has copies of the properties in question, which you then compare =

explicitly at a later point in time. Or, some mechanism I haven't liste=
d =

yet could be used instead of any of these.

Any or all of these are appropriate, depending on your needs and what th=
e =

current architecture allows.

Jamie Risk
6/28/2007 12:56:39 PM
I'm using a class in my project and I'd like to determine if the
any of the fields in the object have been modified since the
original instantiation. For intent and purposes, the following
class is typical:

public class patient
{
private string name;
private string surname;

public Name {
get { return this.name; }
set { this.name = value; }
}
public Surname {
get { return this.surname; }
set { this.surname = value; }
}
}

The example is only meant to show that the class in question
isn't inheriting from another class.

Whether the modification check happens synchronously (through a
method or property access) or asynchronously (such as an event)
isn't all that important. All that I'm trying to avoid is
re-doing the original class or inheriting from the original
class and overriding all the accessor methods.


Suggestions?
Jamie Risk
6/28/2007 1:43:05 PM
Save for the cloneable/equatable choices, I'd considered what
you wrote.

I was hoping there might be a class other than "object" I could
inherit from or an obscure CLR artifact that would track then
publish an event when contents of an object changed.

If such a creature existed I'd suggest that it would fall into
the "best practices" category.

Nonetheless, thanks!

[quoted text, click to view]
sloan
6/29/2007 8:54:59 AM

The CSLA framework does alot of this "IsDirty" stuff.

But its not a simple as just inheriting from something.

The framework is free (google "csla rocky") and you'll find it.

But you'll probably have to buy the book to understand what's going on.

(there is a vb.net and c# version of the book).




[quoted text, click to view]

AddThis Social Bookmark Button