Where do ORM take place?

I often hear people beat ORMs for inflexibility and “leaky abstraction,” but you really don’t hear why they are problematic. When used correctly, which specific ORM errors? I ask this because I'm working on PHP orm, and I would like it to solve the problems that many other ORMs face, such as lazy loading and lack of subqueries.

Please be specific with your answers. Show code or describe the database schema in which ORM fights. It doesn't matter the language or ORM.

+6
orm
source share
5 answers

One of the biggest problems I noticed with all the ORMs that I used is updating only a few fields without first retrieving the object.

For example, let's say I have a Project object displayed in my database with the following fields: Id, name, description, owning_user. Let's say through ajax I just want to update the description field. In most ORMs, the only way to update the database table with only the identifier and description values ​​is to either get the project object from the database, set the description, and then send the object back to the database (this requires two database operations just for one simple update ) or update it using stored procedures (this is the method I'm currently using).

+5
source share

The objects and records in the database are really not so similar. They scored slots in which you can store things, but more on that. Databases have a completely different concept of identity than programming languages. They cannot handle composite objects well, so instead you will have to use additional tables and foreign keys. Most of them do not have a concept of inheritance type. And the natural way to navigate through a network of objects (follow some signs in one object, get another object and play again) is much less effective when comparing with the database world, because you have to make multiple round trips of data that you do not need.

In other words: abstraction cannot be very good in the first place; these are not ORM tools that are bad, but a metaphor that they implement. Instead of perfect isomorphism, this is just a superficial similarity, so the task itself is not a very good abstraction. (However, this is even more useful than dealing with databases, but the contempt for ORM tools mainly refers to database administrators who look at ordinary programmers.)

+4
source share

ORMs can also write code that is inefficient. Since database performance is critical to most systems, they can cause problems that could have been avoided if the person wrote the code (but which might not have been better if the person in question did not understand the setup database performance). This is especially true when the request becomes complex.

I think my biggest problem with them is that, by distracting the details, younger programmers get less understanding of how to write the queries they need in order to be able to handle extreme cases and places where ORM writes really bad code. It is very difficult to learn advanced things when you never had to understand the basics. ORM is in the hands of someone who understands, joins and groups, and cutting-edge queries are good. In the hands of a person who does not understand logical algebra and joins, as well as many other basic SQL concepts, this is very bad, which leads to a very poor design of the database and queries.

Relational databases are not objects and should not be construed as such. The attempt to make an eagle in a silk wallet, as a rule, failed. It is much better to find out what a good eagle is and why and let the eagle fly than to have a bad wallet and a dead eagle.

+3
source share

As I see it, it is. To use ORM, you usually have to add a few php functions and then connect to the database and essentially execute a MySQL query or something like that.

Why all the abstractions between code and database? Why can't we use what we already know? As a rule, a web developer knows his backend language, his db language (some SQL) and some external interfaces, such as html, css, js, etc.

In essence, we are trying to add an abstraction layer that includes many functions (and we all know that php functions can be slower than assigning a variable). Yes, it is microcomputation, but nevertheless it develops.

Now we have not only several functions, but we also need to find out how ORM works, so it is wasted there for some time. I thought the whole idea of ​​code separation was to keep your code separate at all levels. If you are in the world of LAMP, just create your query (you must know MySQL) and use the existing php functionality for prepared statements. DONE!

LAMP WAY:

  • create a query (string);
  • use mysqli prepared statements and retrieve data into an array.

ORM WAY:

  • run a function that receives an object
  • which executes a MySQL query
  • run another function that adds conditional
  • run another function that adds another conditional
  • run another function that combines
  • run another function that adds conventions to join
  • run another function that prepares
  • runs another mysql query
  • run another function that retrieves data
  • runs another mysql query

Does anyone have problems with the ORM stack? Why are we becoming such lazy developers? Or so creative that we harm our code? If it does not break, do not correct it. In turn, correct your development team to understand the basics of a web developer.

+1
source share

ORMs are trying to solve a very complex problem. There are abundant cases and major design compromises without clear or obvious solutions. When you optimize your ORM design for situation A, you are essentially inconvenient for solving situation B.

There are ORMs that handle lazy loading and subqueries “good enough,” but it's almost impossible to get from “good enough” to “great”.

When developing an ORM, you should have a nice handle on all the possible inconvenient database designs that are expected to handle ORM. You must explicitly compromise around situations that you are prepared to handle awkwardly.

I do not see ORMs as inflexible or more leaky than your average complex abstraction. However, some ORMs are better than others in this regard.

Good luck reinventing the wheel.

0
source share

All Articles