triggers vs model updates
I have a domain model with these two entities :
Parts and Contracts.
a Contract has multiple parts, and a part might belong to many contracts.
I represent it this way :
class Contract
{
ICollection<Part> Parts{get;set;}
DateTime LastModifiedDate{get;set;}
}
class Part
{
ICollection<Contract> Parts{get;set;}
}
Every time a Part on a contract is changed, the LastModifiedDate of the contract has to reflect the date at which the change occured.
which way is the better way to handle this ? :
1 -using database triggers
(this allows me to catch modifications that are made to parts without the modification having to be made through my application)
2 -or using the model in such a way that whenever a change is made at the part level, I manually change the contract's LastModifiedDate ?
3 -Is there a 3rd way that would allow to use both ways of doing things ?
4 -Is there anything else I'm missing ?
N.B : I'm开发者_开发知识库 using Entity Framework CTP4 for DB layer.
I'm much more familiar with NHibernate than entity-framework, but the usual way to do this is with event handling: you register a delegate to run before the object is saved and use this delegate to update the metadata.
You could create an interface named IHasUpdatedDate
if you end up with multiple classes with such metadata. In my projects all my entities have WhenUpdated & WhenCreated fields.
It seems like entity framework does support event handling:
http://msdn.microsoft.com/en-us/library/cc716714.aspx
精彩评论