Forked from stevehodgkiss/Rails Conventions for Fluent NHibernate.cs
Created
March 21, 2010 12:06
-
-
Save gshutler/339268 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public static class ConventionHelper | |
{ | |
public static string PluralUnderscore(string name) | |
{ | |
return Underscore(Inflector.Net.Inflector.Pluralize(name)); | |
} | |
public static string Underscore(string name) | |
{ | |
return Regex.Replace(name, "([a-z])([A-Z])", "$1_$2").ToLowerInvariant().Trim(); | |
} | |
} | |
public class CustomForeignKeyConvention : ForeignKeyConvention | |
{ | |
protected override string GetKeyName(Member property, Type type) | |
{ | |
if (property == null) | |
return ConventionHelper.Underscore(type.Name) + "_id"; // many-to-many, one-to-many, join | |
return ConventionHelper.Underscore(property.Name) + "_id"; // many-to-one | |
} | |
} | |
public class IdConvention : IIdConvention | |
{ | |
public void Apply(IIdentityInstance instance) | |
{ | |
instance.Column("id"); | |
} | |
} | |
public class PropertyConvention : IPropertyConvention | |
{ | |
public void Apply(IPropertyInstance instance) | |
{ | |
instance.Column(ConventionHelper.Underscore(instance.Property.Name)); | |
} | |
} | |
public class ReferenceConvention : IReferenceConvention | |
{ | |
public void Apply(IManyToOneInstance instance) | |
{ | |
instance.Column(ConventionHelper.Underscore(instance.Property.Name) + "_id"); | |
} | |
} | |
public class TableConvention : IClassConvention | |
{ | |
public void Apply(IClassInstance instance) | |
{ | |
instance.Table(ConventionHelper.PluralUnderscore(instance.EntityType.Name)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment