Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Let's talk practicality rather than sitting in some ivory tower and muttering about best practices

With AR, if this is the worst case where you write some raw SQL, what's the alternative? The alternative seems far more painful pragmatically speaking and this, while being a little ugly, seems to work just fine without impacting productivity or performance.



  > With AR, if this is the worst case where you write some
  > raw SQL, what's the alternative? The alternative seems
  > far more painful pragmatically speaking and this, while
  > being a little ugly, seems to work just fine without
  > impacting productivity or performance.
Do you know if other ORMs allow this kind of relatively painless use of "raw SQL?"

I've only used ActiveRecord and some of the .NET "micro ORMs" like Dapper and PetaPOCO.

Dapper and PetaPOCO definitely support raw SQL. That's kind of their main focus - they take a row of SQL results and map those database columns to your class's properties, and by design they don't do a whole lot else.

But I don't know about other big/popular ORMs like Hibernate...


Even the bulkiest ORMs allow you to use raw SQL. That's why you can use 80 - 90% of the features on the regular basis and hand-tweak regions which cause performance problems or places where you just have to write SQL (e.g. recursive queries).

In EF, there's either: Database.SqlQuery<T> - http://msdn.microsoft.com/en-us/library/gg696545%28v=vs.113%...

which can return any object or: DbSet<T>.SqlQuery - http://msdn.microsoft.com/en-us/library/gg696332%28v=vs.113%...

which returns tracked entities, so you can write raw SQL (e.g. call a stored procedure) and just use the 'mapper' part of the framework.

NHibernate has CreateSQLQuery - http://www.nhforge.org/doc/nh/en/#querysql-creating

I like micro-ORMs, but when you want to skip writing tedious INSERT or UPDATE queries, you have to add extensions to them (at least to Dapper); that, and SQL strings do not really lend well to refactoring and type safety...


Oh man, that's great to know. Thank you for that reply!


No problem! Happy to help :)

ORMs get a lot of flak and while some of it is truly earned, the rest comes from the misuse/abuse of the tool. I always thought that using ORM functionality (where convenient) together with SQL (where necessary or convenient for different facet of the application) was the best from both worlds.

And then you can of course mix different ORMs in one project, so you can use EF in areas where performance does not really matter that much or if you're doing a lot of CRUD and Dapper (or something like Insight.Database if you like stored procedure-to-interface mapping) in hot paths or analytic-heavy piece.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: