Dinesh has a post where he talks about the relative advantages and disadvantages of ORM mapping schemes. There seems to be quite a bit of noise about DLinq only supporting attribute mapping in the PDC.
Here is my take on the subject:
Whenever possible, make mapping via attributes possible. The ability to quickly tag a class with attributes and have a workable mapping is very important in most design scenarios, where you want maximum flexibility in changing the mapping. Even though that a compiler can't catch logical mistakes in the mapping, there is a certain class of mistake thati it would catch, so it's good to have it.
Here are Dinesh' take on the advantages of external mapping:
- Same classes can be mapped to multiple data models using different mapping files
- Avoids clutter in source code, especially in case of complex mapping
- Hides database-specific information from users of object model
- Some schema changes can be handled without recompilation (IMO, the class of changes that can be handled is often exaggerated)
I agree that 1 and 3 are good points for extenral mapping, but I feel that they can be hanlded via a secondary mapping which is external to the object while keeping most of the mapping in code. I wrote several applications using NHibernate, which uses XML mapping extensively. It was often a pain to trace the difference between the mapping and the model. The errors can be subtle and hard to track down.
I completely disagree with the advantanges of both 2 and 4. First, clutter in code? Those classes are the gateway to the database, the last thing I want to see is people starting to think of them like normal objects: customers.HugeOrdersCollection.Count - (Load all the orders to memory and return their number, prohibitely expensive operation). The idea in ORM is to make it easier to work with the database, not to divorce all knowledge of its existance. I know that this isn't what Dinesh suggested, but that is the thought behind removing "clutter" from the code.
About changing the schema without recompiling. Care to give me a real scenario where you would be changing a live database where you don't have to recompile? I can't think of any non-really-trivial changes that you can make that wouldn't force you to change code.
More posts in "Re" series:
- (09 Jan 2018) Early bird pricing for RavenDB workshops about to close
- (24 Dec 2013) End of year 32% discount coupon is still valid
- (24 Apr 2013) RavenDB Webinar Tomorrow
- (07 Oct 2011) RavenDB and NHibernate courses–New York coming up
- (24 Aug 2011) Advanced NHibernate Course–Warsaw, October 2011
- (26 Jul 2011) RavenDB & NHibernate Training - August 15 - 16, Chattanooga, TN
- (12 Jan 2011) NHibernate Course in Dallas, March 2011
- (11 Feb 2010) Linq to SQL Profiler goes 1.0 on the 14th