-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: NHibernate Validator Best Practice
PostPosted: Fri Sep 05, 2008 9:46 am 
Beginner
Beginner

Joined: Wed Mar 14, 2007 12:35 pm
Posts: 24
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.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 06, 2008 3:45 pm 
Regular
Regular

Joined: Tue Dec 25, 2007 3:41 pm
Posts: 57
Location: Argentina
Hi

You can create a custom validator in order to do this, is cleaner than use NHibernate inside your entity.

See the examples.

_________________
Dario Quintana


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 4:50 pm 
Beginner
Beginner

Joined: Wed Mar 14, 2007 12:35 pm
Posts: 24
Thank you for your response.

1) Making this validator instead of a property validation actually creates more issues for me than solving them. CustomValidator does not allow me to read any other variable from the object being validated which in this case I need because I do different validation based on inserting vs. updating.

2) ClassValidator may be able to read all the properties, but I can't (correct me if I'm wrong) change the validation string based on what property is wrong in the class unless I create several class validators (which I also believe cannot be done).

Several features that seem appropriate out of the following use case are:
1) if each validator had access to the entire object if need be
2) if each validator was passed the action that is being taken. Insert or update.


My other part of this question was how do you handle data access on these types of properties? Opening a new session seems to be the only thing that works for me without messing up the whole persistance action.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 7:56 pm 
Regular
Regular

Joined: Tue Dec 25, 2007 3:41 pm
Posts: 57
Location: Argentina
But I don't see why can't be translate it to a Custom validator.
I don't get properly the point 2)

The only value I'm see you're using is UserName, ID and a ISession (which is taken from a global process). So use a Bean Validator for this, like:

[UniqueUsername]
public class Customer
{
}

Then the complete object is passed to the IsValid method.

BTW the documentation of NHibernate Validator is here:
http://nhforge.org/doc/nhv/en/

Tell me if it's what you need.

Best regards

_________________
Dario Quintana


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2008 2:12 pm 
Beginner
Beginner

Joined: Wed Mar 14, 2007 12:35 pm
Posts: 24
I chose the wrong term for #2, I meant to say BeanValidator, not ClassValidator. My apologies for the confusion.

Anyhow, I wanted a bit more of a general purpose validator, namely a unique constraint validator. It is easy to pass arguments to property validators via xml & attributes. However, for bean validators, I can only find <attributename>STRING</attributename>. I can easily define [UniqueConstraintValidator(PROPERTY_NAME)] as an attribute, but I would like to use the XML mechanism if at all possible.

The documentation doesn't say very much about bean level validation.


Top
 Profile  
 
 Post subject: Example of a general purpose object uniqueness validator
PostPosted: Tue Jan 13, 2009 2:38 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 12:13 pm
Posts: 44
Location: Denver, CO
a.ritchie,

I have included a general purpose object uniqueness checker within S#arp Architecture. The class level validator takes into account all properties decorated with a [DomainSignature] attribute for finding duplicate records.

S#arp Architecture may be downloaded from http://code.google.com/p/sharp-architecture/ (be sure to grab from the trunk!)

The following files show the validator and how it is used in the example code:
* http://sharp-architecture.googlecode.co ... mployee.cs
* http://sharp-architecture.googlecode.co ... Checker.cs
* http://sharp-architecture.googlecode.co ... lidator.cs

Hope this helps!

Billy McCafferty


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.