Remix.run Logo
MarcLore 3 hours ago

The pendulum between 'use an ORM for everything' and 'raw SQL only' has been swinging for 20 years. In practice, the sweet spot I've found is using an ORM for simple CRUD (saves hours of boilerplate) but dropping to raw SQL the moment you need joins across 3+ tables, CTEs, or window functions. The real danger isn't ORMs themselves - it's developers who never learn SQL because the ORM shields them, then they can't debug the N+1 query that's killing production.

jonathanlydall 2 hours ago | parent | next [-]

Absolutely, “ORM == bad” viewpoint strikes me as highly ignorant of all the benefits they provide, particularly in statically typed languages.

People like me don’t choose an ORM to save me from having to learn SQL (which you’ll still need to know), it’s because 99% of the time it’s a no brainer as it vastly increases productivity and reduces bugs.

In a language like C#, EF Core can easily cover 95% (likely more) of your SQL needs with performance as good as raw SQL, for the small percentage of use cases its performance is lacking, you fall back to raw SQL.

But if saving you from writing 95%+ of SQL queries was not compelling enough, it’s just one benefit of EF Core. Another major time saving benefit is not having to manually map a SQL result to objects.

But an often super underrated and incredibly valuable benefit, especially on substantial sized code bases, is the type safety aspect. The queries written using LINQ are checked for free at compile time against mistakes in column or table names.

Want to refactor your schema because your business domain has shifted or you just understand it better than before? No problem. Use standard refactoring tools on your C# code base, have EF Core generate the migration and you’re done in 10s of minutes, including fixing all your “SQL queries” (which were in LINQ).

EF Core is almost always a no brainer for any team who wants high quality and velocity.

senfiaj 2 hours ago | parent | prev | next [-]

Totally. ORMs are not evil. They help to reduce the boilerplate and improve the code maintainability.

We have a complex project with a custom back-end framework that also includes a custom ORM. The ORM covers about 95% of the use cases just fine. It reduces a lot of boilerplate and also makes it possible to find the logic much easier (for example, setting and reading some column from a specific table). Although I have to say, it works that well also thanks to good integration with the framework (fronted, included) and advanced features, like calculated fields with automatic recalculation triggering, integration with user permissions, ability to define custom user permission validation logic before / after saving rows, etc. If we really need an unusual operation where the ORM doesn't suffice (most of the time complicated search queries or some aggregation), we can write raw SQL and, if possible, encapsulate it at repository / service level. But 95% of use cases are covered by the ORM (including the management of parent / child relationships).

I think some "old school" folks like to bash ORMs and view them as very limited and inefficient compared to raw SQL. But you can use them both: primarily ORM and in specific cases switching to raw SQL when ORM doesn't suffice.

aardvark179 3 hours ago | parent | prev | next [-]

It’s been swinging for at least 30 years, Toplink came out in 1994, and I have worked on systems that were written half a decade earlier that contained things we’d all call ORMs.

j45 2 hours ago | parent | prev | next [-]

Agreed, ORMs are OK to start with if you like and then optimize the queries where needed.

There are cases where older ORMS might be more optimized for some cases compared to new ones, and vice versa.

Avoiding learning SQL is the biggest gap. Selecting a NoSQL database because it seems easier, and then spending so much time trying to make a NoSQL database into a relational database is usually not too pretty.

le-mark 2 hours ago | parent | prev | next [-]

Gavin King, creator of hibernate has said many times that you don’t have to use it for everything. Indeed your code can use the entity manager to execute sql. The problem is sql databases are hard, data modeling is hard, it requires a lot of care discipline and even experience (gasp!) to get right.

rrr_oh_man 2 hours ago | parent | prev | next [-]

Edit: Thanks for the merciless downvotes. :)

But it’s a bot writing the above. Look at the user's comment history. "Not x, but y", poorly formatted listicles, mid-paragraph questions to the reader, and em dashes galore.

zelphirkalt 38 minutes ago | parent | next [-]

I don't see any emdashes in the above, nor do I see any mid-paragraph questions.

luckylion 2 hours ago | parent | prev [-]

The day where they'll be hard to tell apart from humans is close. My alarms didn't ring on this on already, I'll be taken out by the first wave of impostors :(

I agree after a closer look though. the pattern is so strong, you can identify it visually. comments 2, 3, and 4 are all three paragraphs, the next three are all one longer paragraph, and all are of very similar length.

3 hours ago | parent | prev [-]
[deleted]