Examples of mid/large-scale web-apps built without an ORM

activerecordhibernateorm

I've been reading a lot of hate spewed on ORMs, and am genuinely interested in finding out if there is a better way of doing things.

Unfortunately, while there are tons of books, blog posts, and articles about modeling and building apps using various ORMs, none exist for the non-ORM approach.

Are there any mid/large sized open source webapps built without an ORM? Nothing beats reading actual source code.

Alternatively, here's what I'm trying to find out:

  1. just because you aren't using an ORM, various associations between tables don't disappear. How do you model them in your app?
  2. For simple stuff like loading a row given the PK, how much boilerplate does one have to write? Are there any libraries that ease this pain?
  3. For simple stuff like loading 1:many associations, how much boilerplate is required?
  4. What does the code look like, when you're exposing a JSON API in your web app? The JSON API will inherently have associations in it (I.e. User JSON will have Posts inside it, which will have Comments inside it). How is this materially different from what an ORM does?
  5. What does the low-level API, which implements various business logic, look like? If not objects, what arguments does this API take? What does it return?

My exposure to ORMs is only via Rails/ActiveRecord, and I can't visualise myself writing a DB backed application without duplicating all the boilerplate that Rails takes care of, for me. It could be that hibernate or nhibernate are different beasts and active record is just fine. Which would actually make sense and lay this matter to rest. At least for me.

Best Answer

Stack Overflow uses a micro-orm. Unlike Hibernate, it's just a thin veneer around SQL queries.

Answers to your questions:

  1. By writing a SQL query.

  2. It can vary from one line of code to about 40. Having ready-made DTO's does help.

  3. The DTO looks the same. You might run 2 queries instead of one, and lazy-load the second one.

  4. In Javascript, JSON translates directly to Javascript objects. For other languages, you can use something like Newtonsoft.Json to translate the JSON to objects in your language of choice.

  5. Here is some example code:

    var result = database.ExecuteSqlQuery<Customer>(
        "SELECT * FROM Customer WHERE CustomerID = {0}", customerID).FirstOrDefault();
    

Where ExecuteSqlQuery returns a List of Customer objects. Customer is a class containing public members corresponding to some or all of your columns in your query.

Dapper is capable of running queries just like this one. It will return a collection of objects of a specific type, or a collection of dynamic objects.

Related Topic