Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code generators or ORMs?

What do you suggest for Data Access layer? Using ORMs like Entity Framework and Hibernate OR Code Generators like Subsonic, .netTiers, T4, etc.?

like image 454
Mahdi Taghizadeh Avatar asked Dec 17 '22 10:12

Mahdi Taghizadeh


1 Answers

For me, this is a no-brainer, you generate the code.

I'm going to go slightly off topic here because there's a bigger underlying fallacy at play. The fallacy is that these ORM frameworks solve the object/relational impedence mismatch. This claim is a barefaced lie.

I find the best way to resolve the object/relational impedance mismatch is to either use OOP exclusively and use an object database or use the idioms of the relational database exclusively and ignore OOP.

The abstraction "everything is a table" is to me, much more powerful than the abstraction "everything is a class." It takes less code, less intellectual effort and leads to faster code when you code to the database rather than to an object model.

To me this seems obvious. If your application is data driven then surely your code should be data driven too? Yet to say this is hugely controversial.

The central problem here is that OOP becomes a really leaky abstraction when used in conjunction with a database. Code that look perfectly sensible when written to the idioms of OOP looks completely insane when you see the traffic that code generates at the database. When that messiness becomes a performance problem, OOP is the first casualty.

There is really no way to resolve this. Databases work with sets of data. OOP focus on instances of classes. Trying to marry the two is always going to end in divorce.

So to answer your question, I believe you should generate your classes and try and make them map the underlying database structure as closely as possible.

like image 161
Simon Johnson Avatar answered Dec 26 '22 14:12

Simon Johnson