Marius Gheorghe

public class Developer : TableMetadata

<January 2009>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567


Navigation

Links

Subscriptions

Post Categories



Mapping objects

There are 2 ways to map objects to  database schemas:

 1. normal classes. These are classes who don't have
a super class (besides System.Object). The mapping is held externally
in a single xml config file (or you can have a single file for each database table schema).
   They can look like this

public class Person
{

   public int Id
   {
     //get-set 
   } 
    
   public string Name
   {
     //get-set 
   }
}  


  The advantage of this model is that you have the "clean" model. No methods, no nothing....just the
clean model. The disadvantages are :

- external dependencies (the xml mapping file(s)).
- informations about the mapped object are found in external classes. This usually creates
a cumbersome API. For instance if you want to now if the Name is null you'll have something like

//check for null.
 IsNull(p, "Name");

- fool around with xml files to update the changes in the database schema.
- the overhead of the mapping information. Usually the mapping info is read when needed
and cached for subsequent needs. The mappinf indo for a single table is not much but when
you cache the mapping information for 100 tables the situation changes.


 2. The mapping is done "in code" and the class inherits from a super class.
This is the approach i have taken with DataBlock. You have a super class (called TableMetadata)
and all the mapped classes inherit from it

public class Person : TableMetadata
{
    //generated implementation 
}

The advantages of this model are :

- you don't write anything. You don't fool around with xml files and you don't write
the class definition. Everthing is generated for you based on the database schema. It's
a few seconds process.
- no external dependencies. Everything compiles into an assembly.
- no overhead of mapping information. Because the mapping info is "in code" the code just runs.
No need to read (and cache) at runtime the mapping info from the xml file.
- information is part of the mapped object. You don't pass an instance of the object
to an "external" API. Taking the above example....if you want to know if a certain field
has a null value :

p.IsNull("Name");

or by index

p.IsNull[1];

  This is both an advantage and the only disadvantage that the model has.
It's a disadvantage because the "mapped object" is not clean anymore. Besides the mapping
object information you also have methods to "query" the mapped objects for informations.
  But the adavantage of beeing much faster (direct calls vs xml reading + reflection) clearly
overweights the disadvantage.
   

posted on Wednesday, March 16, 2005 12:19 AM by Gheorghe Marius





Powered by Dot Net Junkies, by Telligent Systems