Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM - Does the Database Schema Drive the Entity Composition or Vice-Versa?

We've had quite a bit of discussion among our development group concerning whether the composition of entities should drive the database design, or should the database design drive the composition of the entities.

For those who have dealt with this, what has been your philosophy? Of course, not every entity maps 1:1 to a database table. But, for those that do, how have you handled this? IOW, which comes first, the database table and then a corresponding entity or an entity and then a database table to persist it?

Thanks.

like image 890
Randy Minder Avatar asked Feb 14 '10 02:02

Randy Minder


2 Answers

"entity and then a database table to persist it"

The Entity is what your program manipulates. That's the essence of what's being processed.

The database representation of that entity (like flat-file representations or GUI representations) are just handy representations of the entity.

You may have to think a bit about DB representation when it comes to certain things that relational databases are particularly bad at. Many-to-many relationships, for example, require introducing an extra table because the database has limitations that your object model doesn't have. You may have some entity design considerations to cope with this, but those a few and well-understood.

The database is less important.

The Entity definitions are central and essential.

like image 52
S.Lott Avatar answered Nov 15 '22 19:11

S.Lott


Your database will likely outlive whatever application you build today. All the performance and the scalability are going to be driven by your database schema. A sound database model is the foundation on which any application is built, and I'd say is where you should invest most effort in design and testing, for it will give the biggest benefits.

That being said, of course your application will prefer to manipulate domain entities, and manipulating unnatural entities driven by relational theory as opposed to business entities will just complicate things. My view is that is the role of ORM to match the two, as best as possible. But whenever inevitable conflicts appear, the right of way should be given by the driving factor of your performance and scalability: the database schema.

like image 43
Remus Rusanu Avatar answered Nov 15 '22 19:11

Remus Rusanu