Clemens on O/R Mapeers: Take II
Clemens has a new post about O/R Mappers, and this time he brings up several relevant points that I agree with :-). He brings up a comment (by Scott E) from the previous post:
This comment seems to assume that the hand coded DAL will not need to be tuned, which I find hard to believe. Yes, there are times where you'll need to tune the O/R where you will not need to tune the DAL, but the reverse is true as well. On general, O/R Mappers will generate better code than you'll write (I can't be smart on th 54th search SP that I write to do something a bit more complex than the code generator can handle).
No, it's not hard at all. You just need to define the association, the O/RM will take care of the rest. Both example that he gives can be done efficently by O/RM. Finding spesific values in an association:
select
p from Blog blog where blog.Id = @id count(blog.Posts.Comments) > 50Getting the sum of a property (not column, mind you):
select
sum(c.Credit) from Customer c where c.Id = @idBoth of this statements work on the logical representation of the class, not on the physical schema in the database. The second statement travese two 1:N relationships, without affecting performance. [That said, this is probably not the way to do this, using filtering on the collection is a better way to do it, and just as efficently.]
I just wrote a post on using Active Record with many to many associations. Check it out, it's not hard at all. Not even doing searching / filtering / aggregation on it.
Um, doesn't this belong to my concurrency strategy? Either I use optimistic concurrency and throws when the row was updated, or I handle it before had. In genenral, the in memory cache is short lived, it has a life time of a single request, so this isn't a problem. If it is, I can specify that the request will by-pass the first level cache.
In NHibernate, the scope is the session you're working on. A session is similar to a DbConnection in many ways, so it is usually a short lived object.
A transaction is a kind of a poisonous snake that will give you ACID stomach, I believe.
Yes, transactions are hard. No, I don't give them up when I'm using O/RM. I really don't care about identifying the subtree that the transaction is working on, that is the job of the O/RM and the DB, not the application developer. Here is how I'll manage a transaction:
using
(ITransaction trans = session.BeginTransaction(IsolationLevel.Snapshot)){
//do stuff
trans.Commit();
}
This will rollback automatically on exception, of course, but I can do it manually if I really want as well. What is the problem with that again?
No, it's not. All your programming is done against the logical layout of your objects not aginst the physical schema. You want to change the data model? Just change the mapping, and you are done. There are some cases where you can't do this without changing the code, but they would usually require code modifications to the code anyway.
Reporting is one thing that I'll try to do in SQL, because the tools to do it are great, and because I'll often be working on a de-normalized database, rather than my production one. But aggregation is easy. The example that Clemens gives is XPath vs. DOM manipulation. And he is correct, if I had to do this stuff manually, but I don't. I get the O/RM to do this stuff for me (in the database, where those actions belong).
One thing that I'm not sure that I understood is:
LINQ is relational->object mapping.
I'm not sure which O/RM he is talking about, but it's not one that I have used. And O/RM that deny the relational nature of the data just isn't going to work well. It seems to me that Clemens had a bad experiance with O/RMs (maybe the C++ one that he mentions?) and didn't look into the current offering in the field with enough depth. I just don't see many of the problems that he mentions as real problems unless you misuse the tools that you have.
That said, O/RM are indeed a leaky abstraction, and you need to understand what is happening under the cover, but GC is a leaky abstraction as well, and still it seems like a great productivity tool nonetheless.
Comments
Comment preview