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.]
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);
Once you retrieve an object, you need to tell ObjectSpaces to track all of your change:
Then, simply change the object properties as needed and tell ObjectSpaces to persist it:
If you need to delete the object then just tell ObjectSpaces before you persist changes:
If you need to create an object then use new, instead of GetObject, and tell ObjectSpaces:
category = new Category();
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.
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.
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.
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