Technology with opinion

Wednesday, March 17, 2010

Utility function to facilitate Domain AddChild functions

As I make certain domain models more rich, one of the first things I will do is make the property of Child lists on the target domain model to a setter that is internal or public:

There's nothing wrong with this code, it's short & concise. My only problem with it is that there is a repeatable pattern here, we are always going to check to see if the child is null, we will always need to check for the nullability of the target list, etc, etc. If we could centralize this pattern, then if it changes we could change it there too. The silver lining being that if we can reduce it to one line of code and that is always a good thing :-).

Utility Function for Adding Child to Parent: Usage:

It works pretty well and I've ran unit tests against it for both generic lists and non-generic lists. I first started off thinking I could do this with delegates, but I concluded that this code was not a proper use of delegates. Feel free to sound off if I've gone completely mad or if there is a better way to do this.