Using an ORM? Think Objects!

I recently had some time on airplanes to read through Bitter EJB, POJOs in Action, and  Better, Faster, Lighter Java. All three books were good, but the last one was my favorite, and was recommended to me by Ian Cooper. No, I’m not planning on trading in assemblies for jar files just yet. I read the books to get some insight and perspectives into specific trends in the Java ecosystem. A Sound Of Thunder

It’s impossible to summarize the books in one paragraph, but I’ll try anyway:

Some Java developers shun the EJB framework so they can focus on objects. Simple objects. Testable objects. Malleable objects. Plain old Java objects that solve business problems without being encumbered by infrastructure and technology concerns.

That’s the gist of the three books in 35 words. The books also talk about patterns, anti-patterns, domain driven design, lightweight frameworks, processes, and generally how to  write software. You’d be surprised how much content is applicable to .NET. In fact, when reading through the books I began to think of .NET and Java as two parallel universes whose deviations could be explained by the accidental killing of one butterfly during a time traveling safari.

The focus of this post is one particular deviation that really stood out.

From Objects To ORMs

The Java developers who focus on objects eventually have to deal with other concerns like persistence. Their  object focus naturally leads some of them to try object-relational mapping frameworks. ORMs like Hibernate not only provide these developers with productivity gains, but do so in a relatively transparent and non-intrusive manner. The two work well together right from the start as the developers understand the ORMs, and the ORMs seem to understand the developers.

From DataSets to ORMs

.NET includes includes DataSets, DataTables, and DataViews. There is an IDE with a Data menu, and a GUI toolbox with Data tab full of Data controls and DataSources. It’s easy to stereotype mainstream .NET development as data centric. When you introduce an ORM to a .NET developer who has never seen one, the typical questions are along the lines of:

How do I manage my identity values after an INSERT?

... and ...

Does this thing work with stored procedures?

Perfectly reasonable questions given the data-centric atmosphere of .NET, but you can almost feel the tension in these questions. And that is the deviation that stood out to me. On the airplane, I read about Java developers who focused on objects and went in search of ORMs. In .NET land, I’m seeing the ORMs going in search of the developer who is focused on data. The ORMs in particular are LINQ to SQL (currently shipping in Visual Studio) and the Entity Framework (shipping in SP1). Anyone expecting something like “ADO.NET 3.5” is in for a surprise. Persistent entities and DataSets are two different creatures, and require two different mind sets.

Will .NET Developers Focus On Objects?

It’s possible, but the tools make it difficult. The Entity Framework, for instance, presents developers with cognitive dissonance at several points. The documentation will tell you the goal is for EF to create a rich, conceptual object model, but the press releases proclaim that the Entity Framework simplifies data-centric development.  There will not be any plain old CLR objects (POCOs) in EF, and the object-focused implicit lazy-loading that comes standard in most ORMs isn’t available (you can read any property on this entity, um, except that one – you’ll have to load it first).

LINQ to SQL is different. LINQ to SQL is objects all the way down. You can use plain old CLR objects with LINQ to SQL if you dig beyond the surface. However, the surface is a shiny designer that looks just like the typed DataSet designer. LINQ to SQL also needs some additional mapping flexibility to truly separate the object  model from the underlying database schema – hopefully we’ll see this in the next version.

What To Do?

If you are a .NET developer who is starting to use an ORM –any ORM, you owe it to yourself and your project to reset your defaults and think differently about the new paradigm. Forget what you know about DataSets and learn about the unit of work pattern. Forget what you know about data readers and learn how an ORM identity map works. Think objects first, data second. If you can’t think of data second, an ORM might not be the technology for you. 


Posted Jul 13 2008, 09:40 PM by scott-allen

Comments

Max Schmeling wrote re: Using an ORM? Think Objects!
on 07-16-2008 10:42 AM

I'll admit that I may be misunderstanding you, or maybe the purpose of Linq-To-SQL, but I have a comment on this part of your post:

"LINQ to SQL also needs some additional mapping flexibility to truly separate the object  model from the underlying database schema – hopefully we’ll see this in the next version."

Linq-To-SQL isn't supposed to separate the object model from the underlying database schema. It is supposed to be a quick and simple way of getting the data to and from the database. The Entity Framework is the tool, or set of tools, that is supposed to allow you to completely separate the object model from the database schema to work with your objects in a more intuitive and logical way.

scott-allen wrote re: Using an ORM? Think Objects!
on 07-16-2008 11:12 AM

Max:

I think you are correct - LINQ to SQL is the quick and dirty solution, especially when compared to the features in EF.

The statement you highlighted was a personal opinion (or hope) that LINQ to SQL will mature a bit further and give us some additional mapping options. EF comes with it's own set of issues and I personally prefer the design of LINQ to SQL - it's just not usable in many scenarios because of the limitations.

Scott Rudy wrote re: Using an ORM? Think Objects!
on 07-27-2008 12:17 AM

I personally would like to see Microsoft abandon some of this in favor of code generation. ORM is nice and flexible, but it just adds another layer of complexity, in my opinion. I think it would be better to design a POCO and allow templates to be used in forward generation of a data access layer, table and stored procedures. No fancy, bloated data objects or abstraction layers needed, just plain old maintainable code.

I'll admit I haven't given the Entity Framework a real chance yet, but I am quite confomfortable where I am. I don't think anyone was happier than me when the 2.0 framework added what is perhaps my favorite framework feature to date, the BindingList<T>.

laughing John wrote re: Using an ORM? Think Objects!
on 09-15-2008 3:03 AM

I think the only thing that these ORM frameworks show is that we have a crying need for a proper OO database. I know they're already out there but I think most companies (probably wrongly) are wary of anything that doesn't come from the big boys like MS/Oracle/IBM/

The worst thing about a lot of ORMs (Hibernate/NHibernate perhaps being one exception) is that they are intrusive and they tend to lead to an object design that mirrors the database rather than the other way round. The business objects often need to inherit an interface and/or have attributes and often have an interface forced upon them that may not be appropriate to the business requirements.

Of course other technologies like serialization and binding have a similar effect of placing requirements on the objects.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?