I'm sure the root of my problem stems from not using NH correctly, but I 
couldn't find an example I could relate to so I thought I'd post in here 
looking for advice.
My app is getting some DTOs (from deserialized JSON) that get mapped into 
(typically one) Domain Object, which then gets validated and if it passes 
validation it gets saved.

The flow is something like this:
0. Get the existing DO (or create new one)
var entity = Repository.FindRepo<MyEntity>().FindById(dto.Id);

1. Map simple properties DTO -> DO (using ValueInjector)
entity.InjectFrom(dto);

2. Restore complex properties performing lookups:
*var user = Repository.FindRepo<User>().GetById(dto.UserId);*
if (user != null) entity.Buyer = user;

3. Validate DO
4. Save DO.
Repository.FindRepo<MyEntity>.AddOrUpdate(entity);

I'm running into problems on Step #2 because GetById() is implemented 
something like this, on recommendation that Gets are within their own 
transactions (so that they benefit from L2 cache):
using (var tx = Session.BeginTransaction()) {
  result = Session.Get<T>(id);
  tx.Commit();
}

If the DO exists, it is returned by the Repo on Step #0. However, 
attempting to retriever the User is Step #2, causes the tx.Commit() to 
attempt to also commit the possibly incomplete DO, and SQL errors could be 
thrown that would've been caught by the validation in Step #3 (eg field 
length overruns).

Are we supposed to open up a transaction before Step #0, execute everything 
in that transaction - then Commit or Rollback depending on validation 
result?

Is there another pattern you would recommend?

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/qZCyHkvPmfUJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to