Fluent NHibernate with System.ComponentModel.DataAnnotations
What I REALLY wanted was to use the Data Annotations with Fluent NHibernate for my DB mapping files. This presents a problem, right? WRONG! Because Fluent NHibernate is so extensible, I can easily create a convention to use the Attributes. Then all I have to do is add the convention to my Fluent NHibernate AutoMapper instance and the properties with the attributes are just taken care of. Isn’t that neat!!
Required is a common notion with domains. Something HAS to be defined for an entity to be considered a reputable entry. Here’s the convention to use the Required attribute in Fluent NHibernate.
public class RequiredConvention : AttributePropertyConvention<RequiredAttribute>
protected override void Apply(RequiredAttribute attribute, IPropertyInstance instance)
As you can see… there’s really only 1 line of code!
I just showed you one out of the handful of attributes in the DataAnnotations library. To use the other attributes, you just basically follow the same basic outline of the example above. Not bad, eh :)