I agree, as such Order and OrderItem are poor choices for the example.
Lets use the other Company based example.
In my system there is a Company and that Company will have many contacts.
So in our database we have:
Company - CompanyID, Name, Effective Date, End Date
Contact - ContactID, First Name, Last Name, DOB, etc...
CompanyContact - CompanyContactID, CompanyID, ContactID, Effective Date, End Date
The above generates the object model:
IEffectiveDate
- Effective Date
Company : IEffectiveDate
- CompanyID
- Name
- Effective Date
- End Date
- CompanyContact (1:n)
CompanyContact : IEffectiveDate
- CompanyContactID
- CompanyID
- Company (m:1)
- ContactID
- Contact (m:1)
- Effective Date
- End Date
Contact
- ContactID
- CompanyContact (1:n)
- CompanyCollectionViaCompanyContact (m:n)
- First Name
- Last Name
- DOB
So far so good?
So at runtime we create our Company and begin to add CompanyContacts to our collection.
The collection has a property that designates, for lack of a better term, a parent.
The containingEntity property of the collection allows you to say "CompanyContacts [b]belongs to[/b] Company".
IMHO, we should be able to do the same with Entity objects as well. When we add a CompanyContact to the collection there should be some way to mark the CompanyContact to indicate it also belongs to the Company.
I believe this would be helpful in routines, like the OnRelatedEntitySet, where you have the ability to overflow the stack because of endless loops produced.
The above model, and the code below would cause an endless loop:
public partial class CommonEntityBase
{
protected override void OnRelatedEntitySet(IEntity2 relatedEntity, string fieldName)
{
base.OnRelatedEntitySet(relatedEntity, fieldName);
IEffectiveDate currentEntity = this as IEffectiveDate;
if (currentEntity != null)
{
// We only want the values set 1 time as a "Default"
if (this.IsNew && !Fields["EffectiveDate"].IsChanged)
{
IEffectiveDate entity = relatedEntity as IEffectiveDate;
if (entity != null)
{
currentEntity.EffectiveDate = entity.EffectiveDate;
}
}
}
}
}
We could easily fix the endless loop if we could say :
if(currentEntity != null && this.ContainingObject != relatedEntity)
What do you think?