Click here to Skip to main content
15,506,552 members
Articles / Web Development / ASP.NET
Posted 20 Dec 2005


41 bookmarked

Object Relational Persistence in .NET

Rate me:
Please Sign up or sign in to vote.
3.94/5 (17 votes)
20 Dec 20053 min read
Value added object persistence scheme in .NET.


Most of you would have come across or used a generic framework for object persistence in .NET. I have used two different schemes - one of them decorates the fields/properties with attributes and the framework uses reflection to retrieve metadata and persist the object (See Figure 1). In the other scheme, the metadata is externalized using XML files (See Figure 2) and again reflection is used to complete the persistence.

Image 1

Image 2

Pros and cons

I personally prefer the externalized XML scheme for the following reasons:

  1. Even if the source code is not available, we can define mapping for classes and store them (very useful when third party libraries are used).
  2. Class definitions are not cluttered with mapping data.
  3. Multiple mapping can be defined if the same object has to be persisted in different schema. Basically it allows for flexibility and extensibility.

In my personal view, both the schemes are fragile in nature. There is no type safety or compile time check to ensure that the mappings are complete and valid.

Improved solution

I recently developed a prototype framework for object-relational persistence which provides basic data CRUD (Create, Retrieve, Update, Delete) services to business objects using the externalized XML mapping scheme. I ensured that the scheme can embrace System.Transaction for transaction support.

In Figure 3, you can see the class definition, mapping data and the code snippet which creates an instance of the employee class and saves it in a database. This implementation is like other existing implementations and I have added value to it using a new feature introduced in ASP.NET 2.0 compilation model:

Image 3

I wrote a custom build provider and mapped it to files with extension ".orm" in the App_code folder (see Figure 4). This custom build provider parses the existing mappings and generates a wrapper class for each class mapping which ensured that only the defined operations are applicable on the business object.

Image 4

For example, if the ORM file had mapping definition for INSERT and UPDATE operations only, then the wrapper class will have methods only for Insert and Update operations. If the class name is not spelled correctly or if there are duplicate mapping definitions for the same class, an error is thrown during compilation. If the developer makes changes to the mapping definitions and saves the files, Visual Studio 2005 immediately recognizes the change, initiates the build and intellisense is made available for the newly added methods immediately (See Figure 5):

Image 5

Figure 6 shows a sample code generated by the build provider behind the scenes. Using this approach, the fragile mapping definitions in the XML files are validated and made to safe code during compilation time. This will greatly reduce the developers time during development and unit testing cycles:

Image 6


This build provider can be further extended to verify whether all the stored procedures in the mapping files exist in the database or not, verify whether all the required stored procedure parameters are mapped or not and more...


I strongly believe that this compilation support will add more value to the existing object relational persistence schemes and open up many more possibilities.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralRapidly Develop .NET Applications with O/R Mapping Pin
Iqbal M Khan19-Mar-07 1:01
Iqbal M Khan19-Mar-07 1:01 
GeneralSmall issue Pin
mvanbeusekom30-Aug-06 6:10
mvanbeusekom30-Aug-06 6:10 
GeneralSome thoughts... Pin
Steven Campbell29-Dec-05 8:52
Steven Campbell29-Dec-05 8:52 
QuestionNice Article - Questions ? Pin
erickm_us27-Dec-05 3:41
erickm_us27-Dec-05 3:41 
AnswerRe: Nice Article - Questions ? Pin
Arunachalam Karthikeyan28-Dec-05 8:40
Arunachalam Karthikeyan28-Dec-05 8:40 
THere is a static method called Execute in ObjectStore. You can customize the build provider to call this method for other stored procedures. I have just proved this scheme for CRUD operations but there is no limitation. You can extend it to meet ur needs.
GeneralNice article Pin
sherwinds27-Dec-05 2:34
sherwinds27-Dec-05 2:34 
GeneralRe: Nice article Pin
Arunachalam Karthikeyan28-Dec-05 8:33
Arunachalam Karthikeyan28-Dec-05 8:33 
QuestionWhy would you want to do something like that ? Pin
TimMerksem27-Dec-05 2:18
TimMerksem27-Dec-05 2:18 
Generalnice read, very bad presentation Pin
ogrig20-Dec-05 13:16
ogrig20-Dec-05 13:16 
GeneralRe: nice read, very bad presentation Pin
Igor Vigdorchik20-Dec-05 13:25
Igor Vigdorchik20-Dec-05 13:25 
GeneralRe: nice read, very bad presentation Pin
ogrig20-Dec-05 14:54
ogrig20-Dec-05 14:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.