Do you need an ORM?



Do you need an ORM for your project given you use a relational database? And not just some lightweight like Dapper but a big one: NHibernate, Entity Framework, Hibernate? I’d like to address this question with this post.

ORMs: the bad side

Quick note: in this article, by ORM I mean a big ORM, such as NHibernate and Entity Framework, unless otherwise stated.

Big ORMs have a bad reputation among some programmers. The criticism basically boils down to 3 points: complexity, performance drawbacks, and overall leakiness.

  • ORMs bring a lot of additional complexity to the table. It’s true that it takes a lot of time to learn and understand how an ORM works. At the same time, the benefits you’d be able to take out of it seem questionable due to the next two points.
  • They impact performance. An ORM, like any abstraction, brings overhead. A big ORM results in big overhead and thus inevitably damages performance.
  • They are not able to solve the problem completely. Despite all the effort ORMs put into solving the object-relational mapping problem, the abstractions they offer leak, and you still have to dive into SQL and try to solve some of the problems manually.

The points above sometimes entail a decision to stop using ORMs entirely (or just not bother with them in the first place) and either switch to a lightweight one or even write an own implementation.

But are they really that bad?

First of all, I have to point out that writing your own ORM is almost always a bad idea. There are lots of solutions on the market already. Your implementation is most likely going to become a clone of one of them, except that it will have fewer features and more bugs.

Why? Because object-relational mapping is hard. There are fundamental differences in the OO-based and relational-based approaches. No ORM is able to marry them entirely well, neither will yours. Martin Fowler has a great article on this topic, check it our for more details.

That presumably leaves us with the two options: either accept the shortcomings stated above or avoid using ORMs, falling down to SQL and abandoning the object-oriented paradigm.

There’s a third option, however. The one that combines the benefits of the two approaches pretty nicely.

ORMs: area of application

I believe there’s a key misconception when it comes to choosing between using an ORM and working with SQL directly. ORMs are often taken as a replacement for the necessity to learn SQL, as well as various pitfalls you may run into with relational databases. That’s a false perception.

When using an ORM, you still need to know how a database works and you still need to learn how to code in SQL. An ORM is a supplement for your SQL skills, not a replacement for them. The key point here is to define the area of application for ORMs so that we can employ their strengths and avoid their weaknesses. So, what is it?

Every application can be mentally separated into two parts: write model and read model. You don’t have to do canonical CQRS in order to perform such a separation. Just recall the operations in your code base that mutate data in the database somehow (updating the user’s name for example) and the operations that just send the data from the DB straight to the client (getting a list of users). The former would be your write model and the latter – the read model.

So, the trick is that ORMs are for the write models only. They are good at mutating data, persisting your domain model, executing OLTP operations. Don’t try to also use ORMs in the read model (unless it’s really simple), leave it to SQL and/or some lightweight ORM, such as Dapper.

Do you need an ORM: are of application for ORMs

ORMs: area of application

This separation leads to a nice collaboration between an ORM and plain SQL. Most of the problems attributed to ORMs vanish after we start following this guideline.

The bad side isn’t that bad

Let’s once again walk through the commonly stated ORMs’ weak points keeping in mind this rule of thumb.

Performance. While ORMs do spend additional time generating SQL and the resulting queries are sometimes extremely inefficient (I’m looking at you, Entity Framework), it damages performance only in sophisticated read scenarios.

On the contrary, the use of an ORM in the write model often increases performance. That’s because when you mutate data, other factors come into play. For example, most ORMs implement the Unit of Work design pattern: they postpone updates made to objects during a business transaction to the end of it and execute them all at once, decreasing the number of roundtrips needed to the database. Many ORMs have such improvements which result in the overall performance surplus. Again, in write models only.

Lazy load pitfalls. Another feature programmers often complain about is lazy load. The criticism boils down to the fact that it’s easy to use it wrong. Basically, if you detach an object from a session/context, and then try to access its uninitialised lazy-evaluated property, it blows up with an exception. This often happens when we use domain objects to display information on the screen or render the output HTML. In other words, when we use ORM-initialized domain classes in the read model.

If we follow the guideline we discussed above, this problem also goes away. A better approach in this situation is to create a separate set of DTOs to which you map the results of your SELECT queries directly, without involving an ORM. I give an example of extracting a read model using DTOs here.

The learning curve

The last point of criticism I want to address here is the learning curve. It’s true it is steep. And it’s true ORMs don’t cover 100% of our needs when working with relational stores because, well, they are leaky abstractions. So is it worth it?

Yes, definitely. Learning an ORM pays off for three reasons.

First of all, ORMs implement a lot of patterns and best practices for working with relational databases. Unit of Work, Identity Map, inheritance mapping strategies, just to name a few. Martin Fowler wrote a whole book on that topic before any of the big ORMs was released. It’s a good idea to learn an ORM just for that reason – to see how those design patterns were implemented in practice.

Secondly, ORMs save us lots of time by removing the necessity to write tons of boilerplate code. Just imagine creating four separate SQL queries (select, update, insert, delete) for each entity in your domain model. And that’s only for simple CRUD scenarios, the situation gets worse if you try to persist 1-to-many or many-to-many relationships.

And finally, ORMs give us functionality which is really hard to implement manually; the cost of doing so often becomes prohibitive. For example, how much time it would take you to implement selective updates for the fields in your database? In other words, include in UPDATE statements only those properties of domain entities that were changed during a business transaction? I bet you wouldn’t even try to do this (I definitely wouldn’t). At the same time, it’s a single line of code in NHibernate mapping files.

It’s true that from the moment you introduce an ORM in your project, you start dealing not only with the database itself (ORMs don’t free us from the necessity to do this) but also with the ORM on top of it. This includes such unpleasant things as debugging your mappings by looking at the SQL the ORM generates, dealing with bugs in it (or just some surprising and unexpected behavior) and so on.

But the benefits you get out of it are totally worth the price, unless the project you are working on is really small. It’s amazing how much more productive you become when you master an ORM.

Note that I use the term “ORM”/”big ORM” without specifying what particular ORM I mean by that. While it’s true that some ORMs are better than others, the knowledge you get by learning one is pretty much universal. If, for example, you know Entity Framework, it would be easy for you to switch to NHibernate and vise versa.

“Write your own ORM” exercise

Although I strongly recommend you to refrain from creating your own ORM, it might become a good exercise. Just don’t use the result of it in production.

I have to admit I came through this process myself. And that’s why I recommend you not repeat it unless your sole purpose of doing this is learning.

Many years ago, I worked on my own ORM. It was even used in a couple of small production systems. To my excuse, it was in the times before the Entity Framework 1.0 release. NHibernate was around already, but I was ignorant enough not to know about its existence.

It was interesting to dive into the problem of object-relational mapping, it helped me realize how difficult this task was. Later on, when I encountered NHibernate, it was even more interesting to compare my design decisions to those of NHibernate. Although it wasn’t terribly pleasant to see how shallow my understanding of the topic was at the time and how much more sense NHibernate made comparing to the wheel I reinvented.

So, do you need an ORM?

If you have any more or less complex project and you work with a relational database, then yes, definitely.

  • Big ORMs seem “bloated” not because they are bad tools, but rather because the underlying problem of object-relational mapping is hard.
  • An ORM is not a replacement for your SQL/DB design skills, it’s a supplement for them.
  • Use a big ORM in write models only; limit its usage in read models.
  • Although the overhead of learning an ORM and maintaining it in your project is big, the benefits you get overweight the costs.

Related articles

Share




  • Anders Baumann

    Hi Vladimir.

    I agree with your conclusions.

    As an alternative to Dapper or raw SQL you can use Automapper and LINQ projections (https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions). The LINQ projection built by AutoMapper is translated directly to a SQL query by the query provider. So the mapping occurs at the SQL/ADO.NET level, and don’t touch the domain entities. All data is eagerly fetched and loaded into your DTOs. Pretty cool!

    Automapper’s projections are good for relatively simple DTOs. If your DTO has a lot of calculated properties/columns then you should probably create a dedicated database view instead.

    Best regards,
    Anders

    • http://enterprisecraftsmanship.com/ Vladimir Khorikov

      Hi Anders, didn’t know about that feature of AutoMapper, thanks for sharing!

  • Ben Kalegin

    Hi Vladimir,

    Thanks you for the post, I like your blog for clarity and fair reasoning.

    I had similar ‘ignorant luxury’ to write my little ORMs years ago (in Java, Delphi). Later there were EJBs, Bold for Delphi, Hibernates and now I use EF6. Coupled with Linq, EF offers tremendous capabilities to keep business logic managed, transparent and expressive.

    I love to use EF/Linq for reading DTOs and I would appreciate if you elaborate a bit statement “don’t use it for transferring data to the client”. What language are you propose to use for the read queries instead of linq?

    • http://enterprisecraftsmanship.com/ Vladimir Khorikov

      Hi Ben, thanks for your comment.

      This statement is indeed somewhat vague. What I mean by it is not that you shouldn’t use an ORM to transfer data from DB to client but rather that you shouldn’t use it in conjunction with domain entities. In other words, the use of the same entities for both read and write models is usually a bad practice, unless the write model is really simple.

      Use of DTOs is perfectly fine here, regardless of how they are instantiated, be it manually or with an ORM. In fact, I refer to this practice as one of the CQRS variations, namely “Type 1”: http://enterprisecraftsmanship.com/2015/04/20/types-of-cqrs/

      I’ll try to paraphrase the statement now.

  • John

    “Quick note: in this article, by ORM I mean a big ORM, such as NHibernate and Entity Framework, unless otherwise stated.”

    Right away, this reads like a preface to a straw-man argument.

    “Do you need a car? In this article, I mean a big car, like a Hummer or Suburban.”

    Well, OK, but already that’s a much weaker claim than the title.

    Nevertheless, I was willing to read the article and find out why I might not need an ORM. Unfortunately, you never explained it. You claimed that ORMs are for “write models” and not “read models”, but never said why you thought this was the case.

    How does it help me to write out exactly the same boilerplate SQL for reading from the database? I think you got this backwards. ORMs are great for reading, because reading the database is always the same. Writing is almost always different, and transactions don’t map that well to objects in most popular programming languages.

  • Stewart Glover

    As here you mentioned few ORMs for .Net such as NHibernet, Entity Framework, etc. I would like to add one more .Net ORM framework and that is NJDX, it works best providing poweful yet easy to use features. http://www.softwaretree.com
    Thanks!