2/09/2005

Submodules passed in the Ctor and Derived Properties

Recently a programming buddy and I were discussing various options for the population of various properties to a UI-oriented class from a generated Database - oriented class that "kind of" belongs with it. The idea was, he is generating the DB classes from stored procedures using a code - generator and it isn't feasible to change them around. However, he needed the flexibility to bring some or all of the properties in an instance of one of these DB - oriented classes into his UI-oriented class, allowing for the most flexibility without having to hard - code stuff or use reflection.

I suggested a possibility that I've used before: Pass an instance of the DB class into the Ctor of the UI class and store it in a private field there. Then, you can rig up the UI-class properties, fields or members in any way you want. This is far from a new idea - but our little exercise made me think how few times I've seen developers use this technique. Here's a simplistic illustration:

class MyUIClass
{

private MyDBClass _subModule;
public MyUIClass(MyDBClass subModule)
{
this._subModule=subModule;
}

private string _myUIProperty;
public string MyUIProperty
{
get
{
return _myUIProperty;
}

set
{
_myUIProperty =this._subModule.WhateverPropertyOrExpression;
}
}
// ....
}