Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default Code for Get/Set Properties 2

Status
Not open for further replies.

Zyrenthian

Programmer
Mar 30, 2001
1,440
0
0
US
Hi All,
I have a base class I defined to maintain if a set property has been called. In each class that inherits from this base class, I add the line "IsDirty = true;".
Is it possible to have the base class define for all child classes, this line is added/executed by default? If so, is it also possible to specify this just for public properties?

Thanks,
Z
 
do you mean automatically set the isdirty flag to true when the property is changed? and if so, how to do this automatically?

you might be able to do this using an IoC DI container like Windsor and Interceptors. and add some logic within the interceptor to validate that
1. it is a property, not a function
2. it is public
3. you are setting, not getting.
which would call a function that sets isdirty to true.
this would mean you would need to expose a member which would change the state and is accessible to everyone.

you could also use reflection within the interceptor to change the isdirty flag.

without using an IoC container would require you to write a generic object wrapper to specifically handel this scenario, which would still need to include the steps listed above to set the flag.

are you trying to build an ORM for database access? if so i would save yourself some time and use a 3rd party ORM like LLBL ($), ActiveRecord or NHibernate. these frameworks manage the db communication for you, so you can focus on the core logic and "forget" about how to interact with the db.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
You could implement IPropertyChanged, but you're right back to inserting code into each property that you want to monitor for changes.

Inheritance won't help you, as the property that changed is in the inheriting class, not the base class. Using an abstract class won't help either -- as you'd have to define each property twice, and the one in the base would *still* have to have a call to [tt]OnPropertyChanged[/tt] or [tt]IsDirty=true;[/tt] in it.

If this is a huge problem for you (thousands of properties that need this functionality), you might look into code generation. Otherwise, warm up your copy/paste fingers...

Chip H.


____________________________________________________________________
www.chipholland.com
 
Thanks for the responses guys :) In either response, it just ends up being too much work. I have come to the same conclusion from a different angle.

I thought of the reusability of this code and it is a nightmare. It requires that every set call triggers this flag.

Ultimately, it all boils down to the same conclusion. Too much work and not enough benefit.

Thanks again guys :)

Z

Btw... Chip, have you ventured out yet? I wish you the best of luck and fun :)
 
Not yet -- I'm getting some work done on the house before I leave so I can rent it out. It seems that door factories don't always have the parts on hand to, you know, make doors.

Two week delay so far...
[hourglass]

Chip H.


____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top