tags:

views:

121

answers:

2

I know about the not-null attribute. Is there one for enforcing the minimum length of a string property? I don't want empty strings in my database.

+2  A: 

I don't know of anything in the mapping file that will let you do this (and I don't see anything in the schema). You could probably define a custom type using NHibernate.IUserType and build your logic into that type (if the string is empty save null). Here is an example of building an IUserType (it would be easy to change this example code to work for you)

The other option is to take advantage of NHibernate.Validations and to handle the validation logic before getting to the point where you are saving the entity to the database.

James Avery
A: 

You are looking for NHibernate Validator! There's a blog post here showing some of its goodness.

mookid8000
And there's my question here (http://stackoverflow.com/questions/696359/recommended-castle-windsor-nhibernate-stack), explaining why I'm not using it yet! Seriously, though, I'll check it out.
Roger Lipscombe
Ah, I see! But checking out and building the respective projects is definitely worth the effort!
mookid8000
Oh yeah, and James' suggestion of writing an IUserType - please don't do that when what you need is validation! Of course you could be sure that you would never save empty string in the db, but what about handling errors? NHibernate Validator also gives you the ability to validate other stuff which would probably be beneficial in the long run!
mookid8000