Linq to Entities: Microsoft’s first go at O/R Mapping (they need our feedback now!) – level 300

First, none of this is NDA.  Others discussing it are Ayende and Sam.   Here's a quick rundown of how you would get Linq to entities working:

  • Create the metadata file using the wizard.
  • Select some database tables to map to objects.
    • Wait for the designer to generate the mapping meta data.
    • Wait for the designer to generate some partial classes that might serve as entity objects.
  • Add code to these new classes to do interesting things within your domain.
  • Using Linq queries to work with these objects and watch the SQL be generated and sent to the database.

This sounds easy  when you remember that the designer is generating all the ugly goo that _has_ to be there.  First, the classes must inherit from the mapping base class.  They are working on a way to make it implement an interface instead.  This is a non-starter first of all because it would require the domain assembly to carry around a reference to a data access dependency.  Next, for dirty-tracking, each property mutator must raise an event to let the framework know that a property changed.  Every property.  Let me repeat:  Every property setter must raise an event for dirty tracking to work.  This is another non-starter.  The team plans to have all this generated for you in partial classes. 

I have email addresses for several people on the team, and I'll be letting them know what is so compelling about NHibernate and why the above issues have to be resolved before RTM and not after.

In some cases, you don't care if the classes are data-transfer objects instead of domain objects.  In that case, use Linq to SQL, not Linq for Entities.  With Linq to SQL, you can use DTOs all over the place and work with the data pretty easily.  Heck, you could even use Linq to SQL to create your own data layer for hydrating your domain objects manually from the DTOs you decorate with the Linq to SQL attributes.

Linq to Entities is a different animal and is meant to persist domain objects.  Domain objects ARE the most important part of the application.  The domain defines the business problem scope.  Persisting to a data store is secondary to the domain model's structure and behavior. 

I want Linq to Entities to succeed.  I want frameworks and tools to continually get better; therefore, I will be communicating with the product team to voice my strong concerns over the current direction.