A common validation scenario is to make sure a user has a unique username and/or email.
NHV Mapping
Code:
<property name="HasUniqueUsername">
<asserttrue message="Username is already in use" />
</property>
Validator Property
Code:
public virtual bool HasUniqueUsername {
get {
bool flag = true;
if (!string.IsNullOrEmpty(this.Username)) {
using (ISession session = DataGlobal.NewSession()) {
.. assume I can use existing session that fired this or open a new one
Member tmp = .. Just assume data is being retrieved from new session
if (this.ID == 0)
flag = (tmp == null);
else
flag = (tmp == null || tmp.ID == this.ID);
}
}
return flag;
}
}
With the above, my question is, is this even a good practice? I am trying to merge all my business logic into the business object so it seems correct to me. I don't like the idea of opening a new session on validation for reasons such as an existing session being in a transaction (your query in this instance will become deadlocked). If I use an existing session, it usually leads to all sorts of weirdness in NH so I tend to stay away from doing that.
NHV guys, please let me know how you approach validation scenarios such as this.