Best practices question ... monitoring an objects contents ...

J

Jamie Risk

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?
 
P

Peter Duniho

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:

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 just
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:
public class patient
{
private string name;
private string surname;

private bool dirty;

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

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 for
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 that
has copies of the properties in question, which you then compare
explicitly at a later point in time. Or, some mechanism I haven't listed
yet could be used instead of any of these.

Any or all of these are appropriate, depending on your needs and what the
current architecture allows.

Pete
 
J

Jamie Risk

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!
 
S

sloan

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top