All my domain entities have an ID. Evan's description of entities in Domain Driven Design is really what it's all about. The ID is the unique reference for an entity apart from your domain model. In fact, I often have a generic abstract base called something like DomainEntity<IdType> that covers all my persitable objects.
The vast majority of my IDs are similar to surrogate keys. Some legacy tables (esp. in oracle) have a code (varchar) or some other field with business value as a primary key and I treat those as IDs in my mapping files (although they grate against the purist in me). In the objects themselves I'll have the Code and ID properties share the same field value, with neither having public setters (because the entity would no longer be itself if it's identity was changed).
Billy McCafferty has a good article that covers this and a number of other topics.
http://www.codeproject.com/aspnet/NHibe ... ctices.asp
To address your question more specifically, All your classes could have a property, ID, that has no business value. This is there to identify an entity and support your data repository only. An order has many orderitems and an orderitem has one product, right?
So...
Code:
public interface IDomainEntity<IdType>
{
IdType ID {get;set;}
}
//let's say order's PK is an int
public interface IOrder : IDomainEntity<int>
{
List<IOrderItem> OrderItems {get;set;}
}
//let's say OrderItem's PK is a string
public interface IOrderItem : IDomainEntity<string>
{
IProduct Product {get;set;}
}
public interface IProduct : IDomainEntity<int>
{
...
}
...forgive me, I LOVE interfaces!
On mapping, you could set any lazy loading off if you wanted. Order could have a set of OrderItems.
On DB end,
Order could have PK OrderID
Product could have PK ProductID
OrderItem could have PK OrderItemID
and FK to Order.OrderID
and FK to Product.ProductID
*This would only be in the DB and would be resolved in class map file, the classes don't need to be set up like the tables.
Make sense?
BTW, you can use interfaces instead of declaring all your props as virtual
;)