I'll describe my specific situation, but this is also a general
question around the subject.

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.

If i cant turn it off, what am I supposed to do? I've had suggestions
of Evict()ing the object or calling setReadOnly, but they feel like
hacks. I don't want to rollback the transaction, because what if ive
got other updates pending that i do want to commit?

Am I missing some concept which is why automatic dirty object updating
feels wrong to me?

Thanks for any advice,

Andrew

Reply via email to