views:

208

answers:

2

As I understand it all DDD entities should have an ID. So my question is in a master detail relationship, say a Product and a ProductDetail, should the ProductDetail have any knowledge of the Product? Is it necesarry with a ProductID property in the ProductDetail class? In a database this is of course normal as it is the only way to link the two objects but is this best practice in DDD? I am using Linq2Sql as a ORM mapper so this comes as a given but I think this is not the right way. Anybody with some words of wisdom on this?

+1  A: 

Foreign keys are artifacts of relational data model. Object model operates with the concept of container. So, Order contains a collection of Order Lines. Order Line contains Product, etc.

Prankster
What's that got to do with a Domain Driven Design?
jlembke
Foreign key ProductId in ProductDetails class has nothing to do with DDD - It is artifact of relational data model. That was my point.
Prankster
+1  A: 

How will end users retrieve the ProductDetail? If the answer is that they will likely navigate to it from the Product, then the ProductDetail is simply a property of Product, or part of a collection of ProductDetails that is a property of the Product.

So in your object-oriented code, the ProductDetail objects won't need to have a reference to the parent Product.

Now in your database, you'll likely have a master table for Product with an Id column. You then have a child table for ProductDetail that has a foreign key to the Id of the Product it is related to.

It's very important to remember that Domain-Driven Design principles apply only to your OO code. Relational databases and relational data modeling are a different thing altogether.

dthrasher
How do you wire the two models together using an ORM? The ORM will make you define the Foriegn Keys in the ProductDetail class? Are there any ORM's out there that can handle the domain model without defining the ProductId. Is Raw sql the only method?
I've always rolled my own data access layer, rather than using an ORM. Most good ORM tools, like NHibernate, should allow you to create child tables in the database that have a foreign key reference to the parent table. If you configure the ORM properly, your Repository can retrieve both parent and child as part of the same DDD Aggregate.
dthrasher