views:

162

answers:

2

I would like to know which is the common practice for a domain implementation. Designing the business objects first, that need persistence or the database schema first, generating it from an entity relationship diagram (and afterwards the ORM poco*'s)?

I am going to start a solution, but I would like to know which is the most preferable "pattern".

(*powered by NHibernate)

+6  A: 

Depends on whether you're an object or relational modeler. Preference is dictated by what you know best.

I'm an object person, so I'd say model the problem in objects and then get the relational schema from that.

I think there are lots of issues around data that aren't addressed by objects (e.g., indexing, primary and foreign keys, normalization) that say you still have some work to do when you're finished.

But any relational person will argue that they're primary and should be in the driver's seat.

I doubt that there will be a definitive answer to this one. I don't believe there should be. There's an object-relational impedance mismatch that's real. Objects are instance-centric; relational models are set-based. Both need careful consideration.

duffymo
+1 for object-relational impedance mismatch.
James Jones
I have just found a nice documented answer at NHibernate in Action, this book I recently bought. It says that, if you are in a from-scratch-project, a top down approach is good. Design the entities, decorate them and then the database. I think I am oo'ed my shelf, so I will try this approach, but I think I will be bound to the ORMapper (in case I change my mind later in the following days), won't I?
Aggelos Mpimpoudis
@Aggelos - if you layer your application properly, there is no risk of tying yourself to a certain ORM. It's just not common for people to layer their application properly because it is somewhat difficult to do correctly.
Michael Maddox
+1 for indicating mismatch and that indexing and normalization are not inherent in object structure - primarily because objects don't have to be built to be stored and queried (like records in tables); However, keys in relational modeling are isomorphic to references in object modeling (i.e. it's possible to construct a mapping from references to keys and an inverse mapping of keys to references. OR/Ms like Hibernate show this to be true.)
Travis Heseman
A: 

Common practice are both, and it comes down to the preference of each implementer. As duffymo suggested you should go with the one you know best.

However you should also take into consideration what your regular patterns for working with data are. Having something that's nicely modeled in either one, but very costly in terms of performance is not a good choice. The balance is somewhere in the middle.

I personally tend to pay more attention to the database side of things mainly because databases are the ones that are harder to scale. Keeping this is mind when designing a database helps. You don't have to necessarily make the initial design following strict scaling rules, but having it in mind might help you not to make design decisions that will be the equivalent of shooting yourself in the foot, when later on the need for scaling arises.

Mircea Grelus