> So say one of those member domain objects was marked for deletion and was
deleted due to the commit. I wanted to detect that (I have an "OnCommit"
event that the parent domain object listens for from all its domain object
members), and null out the member variable. I can't expose properties for
each of these member variables (for a bunch of reasons), so I wanted to be
able to store a pointer to the original member and null out that memory
location when the event is fired.
Currently, I have the domain object listen for the events, and null out
their own members. This works fine, but I really wanted to get as much of
this generic code out of the domain object and into a base class. I want
to minimize the work somebody has to do to implement a domain object.
Well, this is more understandable. This is quite simple to do, in fact. Have
the DomainObject define a (virtual) method that nulls out it's members (if
there are any). Then any class that inherits from DomainObject can
*override* this method to null out it's own members (and optionally null out
it's base class members too). It doesn't matter if the event is handled by
the base class or the subclass... that's the beauty of Polymorphism.
Here's an example of how it would work:
/////////////////////
/// IneritanceTest.cs
///
using System;
namespace Tests {
public class TestClass {
public static void Main(string []args) {
BaseClass b = new BaseClass();
SubClass s = new SubClass();
Console.WriteLine("--- called from a baseclass object ---");
nullThisOut(b);
Console.WriteLine("--- called from a subclass object ---");
nullThisOut(s);
}
public static void nullThisOut(BaseClass obj) {
obj.nullMeOut();
}
}
public class BaseClass {
private String baseClassMember = "something";
public virtual void nullMeOut() {
Console.WriteLine("Nulling out BaseClass Member: {0}",
baseClassMember);
this.baseClassMember = null;
}
}
public class SubClass : BaseClass {
private String subClassMember = "something else";
public override void nullMeOut() {
Console.WriteLine("Nulling out SubClass Member: {0}",
subClassMember);
this.subClassMember = null;
// null out the parent's members too
base.nullMeOut();
}
}
}
Hope that helps,
-JG