a) evict the item
b) use long running sessions, and make the user fix the problem before
committing the transaction. Won't work in an over 18 case, but will
work for bad formatted email addresses
c) change the entity back to the way it was prior to the request
hmm.. I'm sure there are others. I'd look into the long running
sessions. If you're using rhino tools, there's even a way to have
multiple long running sessions concurrently for the same user.
-Will
1) Long running transactions don't/shouldn't require domain code. Your
domain throws an exception, something else catches it and doesn't
commit the transaction. You don't have to discard the transaction,
just make the user fix it before save.
2) If you REALLY want the "don't save if I don't say so" then evict
items right after loading them. Of course then lazy loading etc won't
work....
-Will
I'm developing a web application, and I'm using a UnitOfWork to create
me a session/tx on begin_request and it flushes/commits on
end_request.
On an edit screen for one of my persisted objects, I get the object
from NH, set the properties with the POSTed data from the edit form,
then validate the object to make sure no business rules are violated.
In pseudo code:
var person12 = Session.Linq<Person>().Query().Where(x => x.Id = 12);
person12.Age = int.parse(form["Age"]);
if(person12.Age >= 18)
Session.Update(person12);
else
throw new ValidationException("You must be at least 18");
The problem is that if the validation fails, the person object is
still saved when the transaction is committed on end_request.
What should I do about this?
It seems very strange to me that dirty objects should get saved
without me telling the session to update it. Can I just turn this
behaviour off? If so, how? It seems strange that there is a
Session.Update(object) method, if you cant turn it off.