Click here to Skip to main content
Click here to Skip to main content

Object Relational Persistence in .NET

By , 20 Dec 2005
 

Introduction

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.

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:

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.

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):

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:

Improvements

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...

Conclusion

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

License

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

About the Author

Karthikeyan Arunachalam
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRapidly Develop .NET Applications with O/R MappingmemberIqbal M Khan19 Mar '07 - 0:01 
Develop complex object oriented applications in 50% less time with TierDeveloper. Save time without compromising on quality of your code. TierDeveloper is an object to relational mapping (O/R mapping) code generation tool that lets you map and generate well designed .NET business and data objects. TierDeveloper also generates custom ASP.NET and Windows Forms apps.
 
Read more
http://www.alachisoft.com/tdev/overview.html
 
Download
http://www.alachisoft.com/download.html
GeneralSmall issuememberxistence30 Aug '06 - 5:10 
Hi,
 
I like solution very much and did some experiments myself. In one of those experiments I found that the data layer provided with the article will have some problems in a multithreaded environment (running on IIS for example).
 
The following issue occurred:
- First thread will retrieve cached command (using the DbCommandStore class) and start executing it.
- When the second thread at the same time retrieves the same cached command, it cannot execute it because the command already has is reader bound to it which is running on the first thread.
 
At the moment I solved this problem by putting a lock on the code which retrieves the command, this way I synchronized the retrieval of command objects however this takes away the whole advantage of having a multithreaded environment.
 
I just wrote this message as a warning, it might be something to look into. But never the less this is a very nice implementation of Object / Relation mapping.
GeneralSome thoughts...memberSteven Campbell29 Dec '05 - 7:52 
I agree, XML is a nicer way to represent the schema, mostly because it is easier to reuse in other contexts (tests, documentation, etc).
 
I really like the custom build-provider idea. It is always of value when things become strong-typed. An article on how to do that would be nice.
 
You mentioned the problem of the xml schema being fragile. It is simple enough to rectify that with unit tests and a prgram that validates the XML against a particular database. That is the sort of thing that makes the more heavy-weight solutions (NHibernate etc.) nice - they have more of the little extras available already.
 

 

my blog
QuestionNice Article - Questions ?membererickm_us27 Dec '05 - 2:41 
Just wondering how an update would be handled.
What if I want to update just a few columns. Would I have to build multiple classes for each update? (PersonUpdateWithNameOnly, PersonUpdateWithAddressOnly, etc?).
How would I update a view?
How would a select based on columns that are not part of the predefined where clause?
I've had less than pleasant experiences with hard coded SQL no matter where it is defined (xml, app.config, const, etc)
Thank you for your feedback
AnswerRe: Nice Article - Questions ?memberArunachalam Karthikeyan28 Dec '05 - 7: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 articlemembersherwinds27 Dec '05 - 1:34 
this is a nice article.
 
what version of .NET this applies for and how do you compare this to an ORM like NHibernate?
GeneralRe: Nice articlememberArunachalam Karthikeyan28 Dec '05 - 7:33 
This solution is applicable for .NET 2.0. NHibernate can make use of this feature to improve ASP.NET developers productivity.
QuestionWhy would you want to do something like that ?memberTimMerksem27 Dec '05 - 1:18 
If you want to map database schema's to classes, just use the Strongly Typed DataSet which has much better performance and is easier to use.
Generalnice read, very bad presentationmemberogrig20 Dec '05 - 12:16 
Good article, short and to the point.
But using images for the code samples makes it a real pain to use. You cannot read it properly, you cannot print it properly and you cannot copy and paste at all.
 
I can understand that seeing code samples side by side is sometimes nice, but you also use images when there is no need for it (why use a jpg for figure 2?).
The only thing that could not have been represented in text is figure 5, but why do I need to read your article to see how IntelliSense looks like?!? That figure is just useless.
 
I'm sorry to go overboard for something like this, but I really feel you could present your material a lot better if you get rid of the images.
 
OGR
GeneralRe: nice read, very bad presentationmemberIgor Vigdorchik20 Dec '05 - 12:25 
I agree that the article could have been presented better but I gave it 5 anyway since IMO it does deserve it.
GeneralRe: nice read, very bad presentationmemberogrig20 Dec '05 - 13:54 
I never suggested anything else. I didn't vote for the article yet myself as I want to do my own tests first, but it will probably be a high mark from me as well.
 
And now that I've read it and liked it I could probably live with it Wink | ;) , but I certainly don't want this idea to become a new trend.

 
OGR

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 20 Dec 2005
Article Copyright 2005 by Karthikeyan Arunachalam
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid