I had this discussion with Igloo coder before, he suggested that repository.SaveOrUpdate(T) should always return void. I couldn’t agree with him with 2 major reasons:
- We need new generated Id from db
- We need updated time-stamp to do concurrency check for following updates
His answer was:
We shouldn’t use auto-gen Id for business usage, and fetching updated timestamp should be in another repository method like FecthById(). Update() should only do one thing, SRP.
Because we are using his NHibernateWCF framework to implement our WCF service, the NHibernate session/transaction is commit or rollback only when WCF service finished, this means to return T in repository.Update() really won’t get anything new because at that time nothing has been commit yet!
A seperated fetch to get new time-stamp has be used, OK, SRP.
But, if we can not switch from auto-gen id overnight, how to get the new generated Id?
Here are some possible solutions:
- Use a fetch by key fields right after new insert. This won’t be very reliable,unless we use guid for accurate search.
- Organize our own Unit-Of-Work, do commit in repository.Save() then we can get new gen id right way and return it. It works, but the pattern and code will be ugly.
- Try to follow “new->save->back to list->update” work flow, don’t use the same screen to add new and afterward update.
Anyway, this must be another example of anti “persistence ignorance” design.