Udi Dahan   Udi Dahan  -  The  Software  Simplist
 
Enterprise  Development  Expert  &  SOA  Specialist
 
 
Home Blog Consulting Training Articles Speaking About Contact
  

Object Relational Mapping Sucks!

Wednesday, June 25th, 2008.

For reporting, that is.image

And doesn’t handle concurrency!

Unless you don’t expose setters.

I guess it depends, doesn’t it?

Well, that was Ted’s assertion in his recent Pragmatic Architecture column on data access.

But, “it depends” doesn’t get the system built, does it?

So, here are some rules for using o/r mapping that will get you 99% of the way there.

Yes, you heard me.

Rules.

They do not depend.

If you’re doing something significantly bigger than enterprise-scale development, and you are already doing this, and it isn’t enough, give me a call. Here we go.

  1. No reporting.

    I mean it. Don’t report off of live data.
    This isn’t just a o/r mapping thing.
    Users can tolerate some, if not quite a lot of latency.

    And it’s not like objects are even used. It’s just rolled up data. Not a single behaviour for miles.

  2. Don’t expose setters

    You want multiple users sharing and collaborating on data, right? Then don’t force them to either overwrite each others data, or throw away their own. There is one simple way to avoid that: Get an object, call a method. Once the object has the most up to date data, pass all the client data in via a method call. The object will decide if its valid, from a business perspective as well, and then update the appropriate fields.

    Now your DBAs can vertically partition tables accordingly, and improve throughput. After that, you can increase the isolation level, to improve safety, without hurting throughput.

    This will also keep your logic encapsulated, bringing you closer to a true Domain Model.

    If your O/R mapping tool requires you to have setters on your domain classes, hide those from your service layer behind an interface.

  3. Grids are like reports.

    No o/r mapping required there either. While you probably won’t be showing grids of yesterday’s data to users in an interactive environment, it’s still just data - no behaviour.

    However, users should NOT update data in those grids. This gets back to rule 2. Have users select a specific task they want to perform, pop open a window, and have them do it there. Change customer address. Discount order. You get the picture. That way you’ll know what method to call on those objects you designed in rule 2.

Before wrapping up, one small thing.

You can use an O/R mapping tool to do reporting, just, for the love of Bill, don’t use the same classes you designed for your OLTP domain model. But, just because you can, doesn’t necessarily mean you should. Datasets datatables are probably just as viable a solution.

  
If you liked this article, you might also like articles in these categories:


Check out my Best Content If you've got a minute, you might enjoy taking a look at some of my best articles.
I've gone through the hundreds of articles I've written over the past 4 years and put together a list of the best ones as ranked by my 2000+ readers.
You won't be disappointed.

Subscribe to my feedIf you'd like to get new articles sent to you when they're published, it's easy and free.
Subscribe right here.



Something on your mind? Got a question? I'd be thrilled to hear it.
Leave a comment below or email me, whatever works for you.

12 Comments

  1. Bill Pierce Says:

    Can you expound on #2? Does no setter mean I don’t expose a domain object directly to a user? I’m used to setting properties on an object then doing something like session.Save(…). Does no setter mean a user is exposed data via a DTO?, modifies the values it needs to, then passes that to a method that re-retrieves a domain object and ensures it hasn’t changed since the user modified the DTO and updates it accordingly?


  2. David Says:

    Hi Udi,

    Apologies in advance for the ignorant question :)…

    How do you generate reports based on data that depends on application/business logic (i.e. up in object world, above the ORM layer)? Is it just a matter of pulling the relevant data from an app/service interface into a reporting DB?

    Regards,
    David


  3. Alex Simkin Says:

    Hi Udi,

    How would you satisfy the requirement of the “real-time visibility” when requirement says: “I want report that shows on-hand inventory at the moment when I pressed [Quantity on Hand] button” ?


  4. udidahan Says:

    Bill,

    That’s correct - UI code doesn’t use classes from the domain model for capturing data entered by the user. You may have a presentation model whose classes are designed for that.

    Messages are used to communicate the change. While messages are data transfer objects, they do not represent the entities so much as what needs to happen to those entities. Not Customer, Order, etc - rather ChangeCustomerAddress, CancelOrder, etc.

    In that sense, you’re not doing a “vanilla update” of the entities in the domain model, but calling domain-specific methods on them:

    using (ISession session = GetSession())
    using (ITransaction tx = session.BeginTransaction())
    {
    Address a = Convert.From<AddressDTO>.To<Address>(message.Address);
    Customer c = session.Get<Customer>(message.CustomerId);

    c.ChangeAddress(a);
    tx.Commit();
    }


  5. udidahan Says:

    David,

    Reporting is done by having your OLTP system publish events about things it has changed (using the domain model), and your reporting (OLAP) system subscribing to those events, and writing the data to whatever structure is most suitable for it.

    The question was not at all ignorant.

    Reporting is handled in such a way that it has no impact at all on the domain model.


  6. udidahan Says:

    Alex,

    “Real-time visibility” needs to be handled with some kind of publish/subscribe infrastructure otherwise users have to keep refreshing their screen. Object relational mapping makes no difference either way to that requirement.

    Once multiple users / multiple event sources can update the same data, more powerful solutions are needed.


  7. Domain Model Pattern Says:

    […] From some pointers about how to use (and not to use) these tools, see why object-relational mapping sucks. […]


  8. Sean Kearon Says:

    Hi Udi

    This approach is very interesting and I think the same as Ayende was recommending to me on the NHibernate users group. Do you have a small example project where point 2 can be seen in action?

    Thanks

    Sean


  9. Colin Jack Says:

    Interesting stuff, I agree with 1/3 but not 100% sure on 2.

    On the change address without setters example, let’s say Address is a value object and we do this:

    customer.CurrentAddress = address;

    Is this not an equally viable solution for many cases?

    Also I’m not recommending any automatic binding to the domain or saying this is the right way to do things in all cases but equally I’m just not sure that always creating a DTO/message and a method always makes sense.


  10. udidahan Says:

    Colin,

    It all depends on your domain, but I’d say that as rules get added around that change of address that the setter becomes less suitable.

    For instance, if we start correlating address to income brackets and make additional changes to the domain objects as a result of that change, or if we use that information for proximity calculations for preferred shipping partners.

    Also, we need to understand that the ORM will be using that setter to fill the object with data, in which case we won’t want to do all sorts of validation or business logic, leading us to map to private fields, a no-no in terms of maintainability.

    So, while it may “work” in many cases, using a method is the preferred option. Keep in mind that putting a DTO/message on top of the method, while compatible, is not necessary.

    Does that make sense?


  11. Colin Jack Says:

    “For instance, if we start correlating address to income brackets and make additional changes to the domain objects as a result of that change, or if we use that information for proximity calculations for preferred shipping partners.”

    Sorry get you now and yeah couldn’t agree more. Definitely wouldn’t trigger that sort of behavior from a property, I was just thinking of at most putting that address value object into the appropriate temporal collection.

    If I did have that sort of behavior then absolutely I agree methods are the way to go, and if its cross aggregate maybe even a service.

    “Does that make sense?”

    As always it makes perfect sense and ta for replying.


  12. Object Relational Mapping Sucks - Hibernate Critical « Akantos Says:

    […] deny it for using in small projects, but for the serious ones the Udi Dahan’s article Object Relational Mapping Sucks must be considered. Also Brian Pontarali article series’ Hibernate Pitfalls  should be […]


Your comment...



If this is your first time commenting, it may take a while to show up.
I'm working to make that better.

Subscribe here to receive updates on comments.
  
   


Don't miss my best content
 
Locations of visitors to this page

Recommendations

Sam Gentile Sam Gentile, Independent WCF & SOA Expert
“Udi, one of the great minds in this area.
A man I respect immensely.”





Simon Segal Simon Segal, CTO at IT Results Pty Ltd
“Udi is one of the outstanding software development minds in the world today, his vast insights into Service Oriented Architectures and Smart Clients in particular are indeed a rare commodity. Udi is also an exceptional teacher and can help lead teams to fall into the pit of success. I would recommend Udi to anyone considering some Architecural guidance and support in their next project.”

Ohad Israeli Ohad Israeli, Chief Architect at Hewlett-Packard, Indigo Division
“When you need a man to do the job Udi is your man! No matter if you are facing near deadline deadlock or at the early stages of your development, if you have a problem Udi is the one who will probably be able to solve it, with his large experience at the industry and his widely horizons of thinking , he is always full of just in place great architectural ideas.
I am honored to have Udi as a colleague and a friend (plus having his cell phone on my speed dial).”

Eli Brin, Program Manager at RISCO Group
“We hired Udi as a SOA specialist for a large scale project. The development is outsourced to India. SOA is a buzzword used almost for anything today. We wanted to understand what SOA really is, and what is the meaning and practice to develop a SOA based system.
We identified Udi as the one that can put some sense and order in our minds. We started with a private customized SOA training for the entire team in Israel. After that I had several focused sessions regarding our architecture and design.
I will summarize it simply (as he is the software simplist): We are very happy to have Udi in our project. It has a great benefit. We feel good and assured with the knowledge and practice he brings. He doesn’t talk over our heads. We assimilated nServicebus as the ESB of the project. I highly recommend you to bring Udi into your project.”

Yoel Arnon Yoel Arnon, MSMQ Expert
“Udi has a unique, in depth understanding of service oriented architecture and how it should be used in the real world, combined with excellent presentation skills. I think Udi should be a premier choice for a consultant or architect of distributed systems.”

Vadim Mesonzhnik, Development Project Lead at Polycom
“When we were faced with a task of creating a high performance server for a video-tele conferencing domain we decided to opt for a stateless cluster with SQL server approach. In order to confirm our decision we invited Udi.

After carefully listening for 2 hours he said: "With your kind of high availability and performance requirements you don’t want to go with stateless architecture."

One simple sentence saved us from implementing a wrong product and finding that out after years of development. No matter whether our former decisions were confirmed or altered, it gave us great confidence to move forward relying on the experience, industry best-practices and time-proven techniques that Udi shared with us.
It was a distinct pleasure and a unique opportunity to learn from someone who is among the best at what he does.”

Jack Van Hoof Jack Van Hoof, Enterprise Integration Architect at Dutch Railways
“Udi is a respected visionary on SOA and EDA, whose opinion I most of the time (if not always) highly agree with. The nice thing about Udi is that he is able to explain architectural concepts in terms of practical code-level examples.”

Sean Farmar Sean Farmar, Chief Technical Architect at Candidate Manager Ltd
“Udi has provided us with guidance in system architecture and supports our implementation of NServiceBus in our core business application.

He accompanied us in all stages of our development cycle and helped us put vision into real life distributed scalable software. He brought fresh thinking, great in depth of understanding software, and ongoing support that proved as valuable and cost effective.

Udi has the unique ability to analyze the business problem and come up with a simple and elegant solution for the code and the business alike.
With Udi's attention to details, and knowledge we avoided pit falls that would cost us dearly.”

Motty Cohen, SW Manager at KorenTec Technologies
“I know Udi very well from our mutual work at KorenTec. During the analysis and design of a complex, distributed C4I system - where the basic concepts of NServiceBus start to emerge - I gained a lot of "Udi's hours" so I can surely say that he is a professional, skilled architect with a fresh ideas and unique perspective for solving complex architecture challenges. His ideas, concepts and parts of the artifacts are the basis of several state-of-the-art C4I systems that I was involved in their architecture design.”

Aaron Jensen Aaron Jensen, VP of Engineering at Eleutian Technology
“Awesome. Just awesome.

We’d been meaning to delve into messaging at Eleutian after multiple discussions with and blog posts from Greg Young and Udi Dahan in the past. We weren’t entirely sure where to start, how to start, what tools to use, how to use them, etc. Being able to sit in a room with Udi for an entire week while he described exactly how, why and what he does to tackle a massive enterprise system was invaluable to say the least.

We now have a much better direction and, more importantly, have the confidence we need to start introducing these powerful concepts into production at Eleutian.”

Gad Rosenthal Gad Rosenthal, Department Manager at Retalix
“A thinking person. Brought fresh and valuable ideas that helped us in architecting our product. When recommending a solution he supports it with evidence and detail so you can successfully act based on it. Udi's support "comes on all levels" - As the solution architect through to the detailed class design. Trustworthy!”

Robert Lewkovich, Product / Development Manager at Eggs Overnight
“Udi's advice and consulting were a huge time saver for the project I'm responsible for. The $ spent were well worth it and provided me with a more complete understanding of nServiceBus and most importantly in helping make the correct architectural decisions earlier thereby reducing later, and more expensive, rework.”

Ray Houston Ray Houston, Director of Development at TOPAZ Technologies
“Udi's SOA class made me smart - it was awesome.

The class was very well put together. The materials were clear and concise and Udi did a fantastic job presenting it. It was a good mixture of lecture, coding, and question and answer. I fully expected that I would be taking notes like crazy, but it was so well laid out that the only thing I wrote down the entire course was what I wanted for lunch. Udi provided us with all the lecture materials and everyone has access to all of the samples which are in the nServiceBus trunk.

Now I know why Udi is the "Software Simplist." I was amazed to find that all the code and solutions were indeed very simple. The patterns that Udi presented keep things simple by isolating complexity so that it doesn't creep into your day to day code. The domain code looks the same if it's running in a single process or if it's running in 100 processes.”

Liron Levy, Team Leader at Rafael
“I've met Udi when I worked as a team leader in Rafael. One of the most senior managers there knew Udi because he was doing superb architecture job in another Rafael project and he recommended bringing him on board to help the project I was leading.
Udi brought with him fresh solutions and invaluable deep architecture insights. He is an authority on SOA (service oriented architecture) and this was a tremendous help in our project.
On the personal level - Udi is a great communicator and can persuade even the most difficult audiences (I was part of such an audience myself..) by bringing sound explanations that draw on his extensive knowledge in the software business. Working with Udi was a great learning experience for me, and I'll be happy to work with him again in the future.”

Eytan Michaeli Eytan Michaeli, CTO Korentec
“Udi was responsible for a major project in the company, and as a chief architect designed a complex multi server C4I system with many innovations and excellent performance.”

Evgeny-Hen Osipow, Head of R&D at PCLine
“Udi has helped PCLine on projects by implementing architectural blueprints demonstrating the value of simple design and code.”

Nimrod Peleg Nimrod Peleg, Lab Engineer at Technion IIT
“One of the best programmers and software engineer I've ever met, creative, knows how to design and implemet, very collaborative and finally - the applications he designed implemeted work for many years without any problems!”

Consult with Udi

Guest Authored Books
Chapter: Introduction to SOA    Article: The Enterprise Service Bus and Your SOA



Creative Commons License  © Copyright 2008, Udi Dahan.