Created
June 16, 2011 15:20
-
-
Save alecwhittington/1029465 to your computer and use it in GitHub Desktop.
Fluent NHibernate - Data Annotation Conventions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace YourProjectName.Here.NHibernateMaps.Conventions | |
{ | |
using System; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
using DataAnnotationsExtensions; | |
using FluentNHibernate.Conventions.Instances; | |
public class PropertyConvention : FluentNHibernate.Conventions.IPropertyConvention | |
{ | |
public void Apply(IPropertyInstance instance) | |
{ | |
if (instance == null) return; | |
// bracket every property to ensure resevered work problems go away. | |
instance.Column(string.Format("[{0}]", instance.Name)); | |
instance.Access.CamelCaseField(); | |
if (instance.Type == typeof(string)) | |
{ | |
ProcessStringTypes(instance); | |
} | |
if (instance.Type == typeof(DateTime)) | |
{ | |
ProcessDateTypes(instance); | |
} | |
} | |
private static void ProcessDateTypes(IPropertyInstance instance) | |
{ | |
if (instance == null) return; | |
HasNullAttribute(instance); | |
if (instance.Name.ToUpper().Contains("START") || instance.Name.ToUpper().Contains("CREATEDON")) | |
{ | |
instance.Default("(getdate())"); | |
instance.Not.Nullable(); | |
} | |
} | |
private static void ProcessStringTypes(IPropertyInstance instance) | |
{ | |
if (instance == null) return; | |
HasKeyAttribute(instance); | |
HasNullAttribute(instance); | |
var max = HasMaxAttributes(instance); | |
if (max > 0) | |
{ | |
instance.CustomSqlType(string.Format("varchar({0})", max)); | |
return; | |
} | |
max = HasLengthAttributes(instance); | |
if (max > 0) | |
{ | |
instance.CustomSqlType(string.Format("varchar({0})", max)); | |
return; | |
} | |
ConventionBasedLengthAssignment(instance); | |
} | |
private static void ConventionBasedLengthAssignment(IPropertyInstance instance) | |
{ | |
if (instance == null) return; | |
if (instance.Name.ToUpper().Contains("SHORT")) | |
{ | |
instance.CustomSqlType("varchar(10)"); | |
return; | |
} | |
switch (instance.Name) | |
{ | |
case "Name": | |
instance.CustomSqlType("varchar(50)"); | |
break; | |
case "Description": | |
instance.CustomSqlType("varchar(150)"); | |
break; | |
default: | |
instance.CustomSqlType("varchar(255)"); | |
break; | |
} | |
} | |
private static void HasNullAttribute(IPropertyInstance instance) | |
{ | |
if (instance == null) return; | |
if (instance.Property.MemberInfo.IsDefined(typeof(RequiredAttribute), false)) | |
{ | |
instance.Not.Nullable(); | |
} | |
} | |
private static long HasLengthAttributes(IPropertyInstance instance) | |
{ | |
if (instance == null) return 0; | |
return (from attribute | |
in instance.Property.MemberInfo.GetCustomAttributes(typeof(StringLengthAttribute), false) | |
select (StringLengthAttribute)attribute into result | |
select result.MaximumLength).FirstOrDefault(); | |
} | |
private static void HasKeyAttribute(IPropertyInstance instance) | |
{ | |
if (instance == null) return; | |
if (instance.Property.MemberInfo.IsDefined(typeof(KeyAttribute), false)) | |
{ | |
instance.Unique(); | |
} | |
} | |
private static long HasMaxAttributes(IPropertyInstance instance) | |
{ | |
if (instance == null) return 0; | |
var me = (from attribute | |
in instance.Property.MemberInfo.GetCustomAttributes(typeof(MaxAttribute), false) | |
select (MaxAttribute)attribute into result | |
select result.Max).FirstOrDefault(); | |
long length = 0; | |
if (me != null && long.TryParse(me.ToString(), out length)) | |
{ | |
return length; | |
} | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment