ADO.NET v2.0: ObjectSpaces Delivers an O/R Mapper
page 1 of 1
Published: 30 Oct 2003
Unedited - Community Contributed
Abstract
ADO.NET v2.0 will finally provide a built-in object-relational mapper called ObjectSpaces. What is an O/R mapper? Its a framework that lets you totally avoid SQL and stored procs! Note that I'm not talking about generating the SQL or stored procs, like many tools do now; no, I mean totally avoid it and have the framework transparently do all the CRUD for you. That may sound like an impossible dream, if you've never looked into these types of tools, but its very doable and its been rather commonplace in the Java world for quite some time. ASP.NET, and now Avalon, are declarative GUI models -- ObjectSpaces is just declarative CRUD.
by Paul Wilson
Feedback
Average Rating: 
Views (Total / Last 10 Days): 18287/ 26

ADO.NET v2.0: ObjectSpaces Delivers an O/R Mapper

ADO.NET v2.0: ObjectSpaces Delivers an O/R Mapper

Paul Wilson
www.WilsonDotNet.com
www.ASPAlliance.com/PaulWilson

Previous Article            Download Sample            Next Article

Overview

ADO.NET v2.0 will finally provide a built-in object-relational mapper called ObjectSpaces. What is an O/R mapper? Its a framework that lets you totally avoid SQL and stored procs! Note that I'm not talking about generating the SQL or stored procs, like many tools do now; no, I mean totally avoid it and have the framework transparently do all the CRUD for you. That may sound like an impossible dream, if you've never looked into these types of tools, but its very doable and its been rather commonplace in the Java world for quite some time. ASP.NET, and now Avalon, are declarative GUI models -- ObjectSpaces is just declarative CRUD.

I'm not going to delve into the theory behind O/R mappers, as its well-documented elsewhere, but some of the benefits are provider-agnostic applications (works with Access, SQL, Oracle) as well as obviously the avoidance of writing and maintaining the same-old boring CRUD code. If you can't wait for ObjectSpaces in .NET v2.0, then try some of the current .NET tools, the leading ones being EntityBroker, by Thomas Tomiczek, and LLBLGen Pro, by Frans Bouma, along with free tools like Norpheme, by Scott Bellware, and Sisyphus, which is open source. I've personally used EntityBroker and Norpheme, and the others mentioned are well-known too. [Update: I've also now released the WilsonORMapper, which is based on the ObjectSpaces syntax.]

Retrieving Objects

The ObjectSpace class (in the System.Data.ObjectSpaces namespace) is the key object used, with methods to get objects (single object, object sets, object streams), re-synchronize to the latest, track and persist changes and deletions to objects, and handle transactions. The ObjectSpace is constructed using a MappingSchema (xml file) and an ADO.NET connection:

  string mapping = Server.MapPath("~/Mappings/NorthwindMap.xml");
  string connect = "Server=(local);DataBase=Northwind;Integrated Security=SSPI";
  ObjectSpace mapper = new ObjectSpace (mapping, new SqlConnection(connect));

Use the GetObjectSet method to retrieve a bindable (and remoteable) collection of objects:

  ObjectSet objects = mapper.GetObjectSet(typeof(Category), "");

Use the GetObject method, along with a where clause, to retrieve a single object instance:

  Category category = (Category) mapper.GetObject(typeof(Category), "ID=" + id);

Persisting Objects

Once you retrieve an object, you need to tell ObjectSpaces to track all of your change:

  mapper.StartTracking(category, InitialState.Unchanged);

Then, simply change the object properties as needed and tell ObjectSpaces to persist it:

  mapper.PersistChanges(category);

If you need to delete the object then just tell ObjectSpaces before you persist changes:

  mapper.MarkForDeletion(category);

If you need to create an object then use new, instead of GetObject, and tell ObjectSpaces:

  category = new Category();
  mapper.StartTracking(category, InitialState.Inserted);

Mapping Schemas

Unlike other O/R mappers, ObjectSpaces does not require any attributes in your classes to specify the database schema, nor does it require you to inherit from a specific class. Instead, everything is usually specified in an xml file, or even a series of xml files, although you can also define the entire schema on the fly in code if that is necessary. Please download my sample application if you want to see a simple mapping schema example based on the Categories and Products tables from the standard Northwind SQL Database.

A mapping schema basically consists of 3 related schemas, which can be in separate files. The first is the MSD, or mapping schema definition, which maps the actual database fields to your object's properties -- I actually map to private fields wrapped in properties. The next is the RSD, or relational schema definition, that defines database field types. The last is the OSD, or object schema definition, where more object details are defined. This is a very cumbersome process in the alpha, so expect a GUI mapping tool in the beta.

Conclusion

ObjectSpaces finally delivers a real flexible and robust built-in O/R mapper in .NET v2.0. Its very easy to use, especially when the GUI mapper to define schemas becomes available. It supports most data types, complex relationships, transactions, identity and GUID keys, as well as most advanced O/R mapper concepts, like lazy loading and object inheritance. It only works with MS SQL Server in the alpha, but hopefully that will change in the beta. I really look forward to ObjectSpaces -- its one of the very best features of .NET v2.0.

Version Note

This article was based on an early Alpha release of ASP.NET v2.0 and VS.NET Whidbey. It is possible that some implementation or v1.* compatibility details will change.

Author Bio

Paul Wilson is a software architect in Atlanta, currently with PRG-Schultz. He specializes in Microsoft technologies, including .NET, C#, ASP, SQL, COM+, and VB. His WilsonWebForm Control allows Multiple Forms and Non-PostBack Forms in ASP.NET. He is a Microsoft MVP in ASP.NET and is also recognized as an ASPInsider. He is a moderator on Microsoft's ASP.NET Forums, as well as one of the top posters. He is holds the MCSD, MCAD, MCDBA, and MCSE certifications. Please visit his website, www.WilsonDotNet.com, or email him at Paul@WilsonDotNet.com.


User Comments

Title: ss   
Name: ss
Date: 2008-11-10 12:51:33 AM
Comment:
good one
Title: jk   
Name: jk
Date: 2008-11-08 8:54:32 AM
Comment:
its good. i have learnt basics about OR Mapper.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 2:42:48 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search