I try to explain it again.
Don't rely on the fact that there is a database. Validation should not
occur before changes are persisted, but when they are done (in memory,
by the business logic).
Making changes in memory but not storing them is NOT the solution. How
would to reliably validate entities if you have invalid data in
memory? How would you calculate the price of an order when you allow
any part of the software changing the price of a product in memory?
Your memory needs to be as consistent as the database.
Automatic dirty checking means, that all changes will be persisted. It
is a problem of your application if you don't know which part of the
application changes which entities and therefore you don't know where
to put validation logic.
By the way, to prevent others to perform changes on entities they
shouldn't, you can implement immutable interfaces which only consist
of references to others by immutable interfaces. the problem here is,
that C# does not have the const keyword as C has. This is missed in
many places which is also not related to NH.
When working with DTO's (what most people in large projects do), this
is not an issue anyway. You'll have a "SafeProduct" method, which
takes a DTO as argument. This method applies only the values that are
changeable to the entities, and validates it.
There is a feature by NH that allows validation within the entity at
the point in time before it gets persisted. Just implement the
interface IValidatable
(see
http://knol.google.com/k/fabio-maulo/nhibernate-chapter-4/1nr4enxv3dpeq/7#4%282E%295%282E%29%28C2%29%28A0%29IValidatable_callback).
(This is of course only meaningful for validation within the
aggregation boundary.)
If your application depends on an existing dirty check mechanism, you
could write an interceptor which calls it.
http://www.surcombe.com/nhibernate-1.2/api/html/M_NHibernate_IInterceptor_FindDirty.htm
You could also validate in a OnFlushDirty: check all the entities
which are going to be stored, and throw an exception if they shouldn't
change. It would be a programming error. Not sure if this is worth the
effort.
On 24 Aug., 08:52, Oskar Berggren <
oskar.bergg...@gmail.com> wrote:
> 2009/8/22 kurtharriger <
kurtharri...@gmail.com>:
>
>
>
> > I also would like to go on the record as saying that I too believe
> > dirty checking is a a bad idea and probably should be considered a bug
> > because it violates encapsulation boundaries.
>
> I disagree with your terminology. I consider a bug to be a "violation
> of specification or intended behaviour". Auto dirty checking is
> intended. What we have here I would call a feature request, since the
> ability to completely turn of auto dirty checking would be a new
> feature.
>
> > The leakage of mutable
> > references makes it difficult to verify that business rules always
> > remain valid when changes are persisted. You can read more on my blog
> > here:
>
> >
http://kurtharriger.wordpress.com/2009/08/21/how-nhibernates-dirty-ch...