Attributes and Events

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to receive a notification (via an event), whenever a property of a
class is
modified.
Of course, this would be possible if i write code such as the following to
EACH "set" part of each property:
if(Modified != null) Modified(this, EventArgs.Empty); //Modified is an event

Yet, this is annoying. How could I avoid this code duplication? Do
attributes help here somehow?
 
No, you need to do it this way. There are a number of reasons for why. I can
list several if you really need to know.

Pete
 
Whow...this is really hard to believe...
It is duplicated code in many many classes and properties.
I wanted to write some code to hide Database logic from classes used in the
business logic and one of the first steps is to notify the data stroe about
changes in property values. My solution is not very elegant unfortunately. I
actually want to fire a changed event without coding the call into each and
every property. Ideally, I want to tag the properties which should be mapped
to the data store and the event firing mechanism is "injected" in all those
properties.
 
Whow...this is really hard to believe...
It is duplicated code in many many classes and properties.
I wanted to write some code to hide Database logic from classes used in the
business logic and one of the first steps is to notify the data stroe about
changes in property values. My solution is not very elegant unfortunately. I
actually want to fire a changed event without coding the call into each and
every property. Ideally, I want to tag the properties which should be mapped
to the data store and the event firing mechanism is "injected" in all those
properties.

Having had a lot of experience writing OPFs (Object Persistence Frameworks),
I would advise you to look at developing your own structs that provide
events that can be caught by your OPF. There really is no other way but,
although it is a bit of a hassle to start with, it is only a one-off hit to
set up the basic types.

Something like :

/////////////////////////
public struct IntegerType
{
public delegate void IntegerTypeChangeHandler(int oldValue, int
newValue);

private int value;

public event IntegerTypeChangeHandler ValueChanged;

public IntegerType(int value)
{
this.value = value;
ValueChanged = null;
}

public IntegerType(IntegerType other) : this(other.value) {}

public static implicit operator IntegerType(int value)
{
return new IntegerType(value);
}

public static implicit operator int(IntegerType value)
{
return value.value;
}

public int Value
{
get { return value; }
set
{
if(ValueChanged != null)
ValueChanged(this.value, value);
this.value = value;
}
}
}
////////////////////////////////////////

This can then be added as a public field if necessary as it has implicit
casts to/from its native type thereby avoiding the necessity of properties
because the struct iss essentially a wrapper around the true native type,
and the event will be called whenever the value changes.

Joanna
 
Back
Top