Okay, scratch the last question. Using FluentConfiguration is the
best way to go!!! I love this! This is probably the most flexible
validation framework i've ever seen!
I've already implemented this into my base classes. I'll share the
code incase anyone else has to do something similar.
My one and only instantiation of the validation engine that is
available for the entire lifecycle of the application:
if (_validatorEngine == null) //added this to class so
we can have validation, when a new NHibernationSessionManager is
created the validation engine also gets instantiated one and only one
time.
{
var provider = new
NHibernateSharedEngineProvider();
NHibernate.Validator.Cfg.Environment.SharedEngineProvider = provider;
_validatorEngine =
NHibernate.Validator.Cfg.Environment.SharedEngineProvider.GetEngine();
var configure = new FluentConfiguration();
//There are two ways to configure NHibernate
validator, one is with Fluent (which is WAY more flexible) and the
other is without fluent.
//without fluent you are pretty limited on the
types of checking you can do on your entitys as it is all defined with
static attributes
//on the entity classes. Here we use the
FluentConfiguration way.
configure
.Register(Assembly.GetExecutingAssembly().GetTypes().Where(t
=> t.Namespace.Equals("ItemBankCoreNH.Validators"))
.ValidationDefinitions()) //This type lookup
trick was found from the nHibernate validators EntityValidation
example
.SetDefaultValidatorMode(ValidatorMode.UseExternal)
.IntegrateWithNHibernate.AvoidingListenersRegister(); //
We don't want to nHibernate event listerners to be activated, we
always call IsValid() manually to verify entity validity
_validatorEngine.Configure(configure);
}
A sample validation definition:
public class ContentCodeValidationDef : ValidationDef<ContentCode>
{
public ContentCodeValidationDef()
{
//Example that replaces the NotNullNotEmpty attribute you
would normally use without FluentConfiguration
Define(e =>
e.ExamType).NotNullableAndNotEmpty().WithMessage("My Custom Message");
//Example where you could begin to lookup something from
the database perhaps to validate integrity on the entity object.
//For instance where you might want only one object in the
database to have a flag of "Active" or something like that.
//Whats awesome is you can then do more stuff based on the
return type of the lambda expression, in this case its returning
//a boolean so theres not much you can, but if it was a
string you could do NotNullableAndNotEmpty like the above
//example.
ValidateInstance.By((instance, context) =>
{
//Do lookup from database and validate something
bool isValid = true;
return isValid;
});
}
}
Thanks Fabio, your help has been invaluable in learning nHibernate and
the validator framework!
On Aug 20, 11:23 am, MattO <[email protected]> wrote:
> One more quick question, can you use a mixture of the
> FluentConfiguration of a validationengine and also still have it check
> the attribute flags as well (such as [NotNull] attributes), or are you
> forced to go entirely via the fluent configuration and use Defines,
> Satisfy, etc instead of Attributes if you create a validationEngine
> using fluentconfiguration?
>
> Reason why I ask is that lamda's still mystify me :-) It's like black
> magic.
>
> On Aug 20, 10:48 am, Fabio Maulo <[email protected]> wrote:
>
>
>
> > On Fri, Aug 20, 2010 at 12:44 PM, MattO <[email protected]> wrote:
> > > Well this has gone over my head... Do you need to be using Fluent
> > > nHibernate to use this? I'm using hbm XML files for everything...
>
> >http://fabiomaulo.blogspot.com/search/label/Validator
>
> > --
> > Fabio Maulo- Hide quoted text -
>
> - Show quoted text -
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.