2 ms·
Personally, i think that both proponents and opponents of ORMs have a number of good points; personally, i also rather enjoyed the idea about using ORMs with ge
by KronisLV 5y ago
Personally, i think that both proponents and opponents of ORMs have a number of good points; personally, i also rather enjoyed the idea about using ORMs with generated code (e.g. schema first, a bit of model driven development) for the simpler things and then dropping down to SQL for non-trivial queries. Essentially knowing both SQL and what your ORM provides and using both for the use cases where they excel.
That said, why does no one almost ever mention ORMs that allow you to generate SQL code dynamically and then map it to objects? The best and perhaps only example that i can think of is myBatis XML mapping, which to me felt like a really powerful, versatile and actually easy to understand approach: https://www.tutorialspoint.com/mybatis/mybatis_mapper_xml.htm https://www.tutorialspoint.com/mybatis/mybatis_mapper_xml.ht...
That way you get the power of SQL, the ability to customize your queries at runtime as needed (e.g. filtering criteria or ordering, or pagination, or even selecting column values from different tables if need be) while also getting good performance (since it's just SQL queries which you can optimize as needed) with a pretty decent mapping layer.
The biggest drawbacks that i can think of personally would be not being able to work with your DB directly if you don't have query logging on and can't get the full query out of the app when you need it, for debugging. Of course, doing that is pretty easy, however i hate how most ORMs out there don't let you log SQL queries with parameter examples (not just some special character for where the parameter value is supposed to be) in case you ever want to copy those into your SQL shell/tool of choice to run it. Of course, that's more of an inconvenience, rather than a deal breaker.
Have a look at some of the example dynamic SQL here: https://www.tutorialspoint.com/mybatis/mybatis_dynamic_sql.htm https://www.tutorialspoint.com/mybatis/mybatis_dynamic_sql.h...
Why don't more stacks strive for something like that?
(sidenote: they also have docs on GitHub, but GitHub is down at the moment, what a day)