Why My repository.Update() should not return T?

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:

  1. We need new generated Id from db
  2. 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:

  1. Use a fetch by key fields right after new insert. This won’t be very reliable,unless we use guid for accurate search.
  2. 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.
  3. 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s