Fog Creek Software
Discussion Board




DataTable & Business Objects

I had a couple of questions concerning best practices for mapping datatables to business objects:
* Should business objects  contain a direct reference to a datatable?
*Should a business object be passed  to a another data access class which contains a reference to a datatable and  populates the business object?
*Should the business object handle the persisting of itself to a database or should this be handled be a seperate class?

Any comments would greatly be appreciated.

Jr. Programmer
Tuesday, October 28, 2003

Hi

Business objects (BO) should have no knowledge about the underlying persistence mechanism. I use a persistence framework that takes care of BO persistence. For each BO there is a data management object (DMO) that takes care of BO persistence. There are general DMO that are used to save and retrieve simple BO (with SQL statements generated on the fly). More complex BO have special DMO dedicated for their persistence. This enables me to change different persistence mechanisms (different RDMS, XML files, etc) without any changes in BO or UI layer. I just have to select another DM layer.

There are different strategies for mapping BO to data tables in database. For simple BO I map one BO to one data table.

There are different strategies for mapping inherited objects to database tables:
-    you can create one large table with fields for all properties in base BO and all inherited BO.
-    You can create one table for base BO and separate tables for descendant BO. Those tables have fields only for properties added to a descendant.
-    You can have separate table for each BO that contains fields for properties of all the ancestors and newly added to this BO.

Branko
Wednesday, October 29, 2003

I think his question was specifically about DataTable in .NET, rather than generically tables in a database.

In answer to that question, DataTable is suitable for what you want. It need not represent the database layout at all. It's basically an in-memory database in its own right.

Brad Wilson (dotnetguy.techieswithcats.com)
Wednesday, October 29, 2003

Well, since the Jr. Programmer didn't mention .Net, I don't see how you can derive that his question was "specifically about DataTable in .NET"?

I would like to thank Branko though, for sharing his techniques for modelling Business Objects (in any language).

Wayne
Thursday, October 30, 2003

>>More complex BO have special DMO dedicated for their
>>persistence.
>>
>>This enables me to change different persistence
>>mechanisms (different RDMS, XML files, etc) without any
>>changes in BO or UI layer

Honestly, you guys with all your incredibly flexible DAL's BO's and crap. "persistence"? - for f's sake. Do you ever get anything done? Hey look i can change this to persist to an xml file, and this to sql server and this to oracle.... hey, by rewriting all the code in this layer... erm, I don't have to change the code in this layer blah blah -- do your customers really care about your "architecture"? All you do is write code all day to feed your architectural monster don't you?

Keep it simple estupidos! ;-)

pragmatic devil's advocate
Friday, October 31, 2003

*  Recent Topics

*  Fog Creek Home