Ryan Bosinger
Ryan Bosinger

Reputation: 1862

How to use Fluent NHibernate Validator when using auto mapping?

I've just modelled a small database using Fluent nHibernate and the auto mapping feature. Now I'm wondering how I work with validation. In the past I've decorated my classes with attributes but the purpose of this by-convention automapping is to keep things clean.

I do have a couple override files which look like this:

public class EventMappingOverride : IAutoMappingOverride<Event>
{
    public void Override(AutoMapping<Event> mapping)
    {
        mapping.Map(x => x.EventType, "TypeID").CustomType(typeof(EventType));
        mapping.Map(x => x.EventStatus, "StatusID").CustomType(typeof(EventStatus));
        mapping.HasMany(x => x.EventDates).KeyColumn("EventID");
    }
}

Is this where I would put my validation rules? If so, what does that look like and is there really even a point to using the auto mapping (if my override files are going to be elaborate anyway)?

Thanks.

To clarify further:

My entities look like this as of now:

namespace Business.Data
{
       public class Event
       {
            public virtual int Id { get; set; }
            public virtual string Title { get; set; }
            public virtual EventStatus EventStatus { get; set; }
            public virtual EventType EventType { get; set; }
            public virtual IList<EventDate> EventDates { get; set; }
       }
}

I would like to keep them looking that like. Just plain objects so in the future we can potentially switch out or upgrade the ORM and still have these nice clean objects.

However, when it comes to using nHibernate Validator (part of NHContrib) I'm not sure how to incorporate it without littering the properties with attributes. I guess this is more of a question architecture. I could use a different validation framework as well but I want it to be tied in with nHibernate so that it won't insert/update invalid records. Any opinions appreciated!

Upvotes: 1

Views: 1550

Answers (1)

Kuqd
Kuqd

Reputation: 497

My opinion is :

Validation is part of the business at it depend from it and then the database scale to this need. So if you need a email string column in your db you should not rely on a db framework to do that especially as you said that may be later you will switch ORM then you will loose your work.

Keep validation in the business/high layer, and leave the db do simple query/insertion, remember NHibernate is already a bit complicate to hand on so keep it simple.

To answer your question, if you don't want to littering your entities use the xml validation as describe here.

http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation.aspx

Upvotes: 2

Related Questions