Created
July 1, 2011 06:28
-
-
Save nelsonlaquet/1057983 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.Reflection; | |
using FluentNHibernate.Automapping; | |
using FluentNHibernate.Cfg; | |
using FluentNHibernate.Conventions; | |
using FluentNHibernate.Conventions.Helpers; | |
using FluentNHibernate.Conventions.Instances; | |
using FluentNHibernate.MappingModel; | |
using Honeycomb.Base; | |
namespace Honeycomb.Services.Mappings | |
{ | |
public class AutoMapper | |
{ | |
private readonly Assembly assemblyToMap; | |
private readonly Assembly overrideAssembly; | |
private readonly IEnumerable<string> namespaces; | |
public AutoMapper(Assembly assemblyToMap, Assembly overrideAssembly, IEnumerable<string> namespaces) | |
{ | |
this.assemblyToMap = assemblyToMap; | |
this.overrideAssembly = overrideAssembly; | |
this.namespaces = namespaces; | |
} | |
public void Configure(MappingConfiguration config) | |
{ | |
config.AutoMappings.Add(AutoMap.Assembly(assemblyToMap, new AutoMapperConventions(namespaces)).Conventions.Add( | |
new IConvention[] | |
{ | |
ConventionBuilder.Class.Always | |
( c => | |
c.Table(GetTableName(c)) | |
), | |
ConventionBuilder.Id.When | |
( c => | |
c.Expect(i => ((PropertyInfo)i.Property.MemberInfo).PropertyType == typeof(Guid)), | |
c => | |
c.GeneratedBy.Assigned() | |
), | |
ConventionBuilder.Id.When | |
( c => | |
c.Expect(i => ((PropertyInfo)i.Property.MemberInfo).PropertyType == typeof(int)), | |
c => | |
c.GeneratedBy.Identity() | |
), | |
ConventionBuilder.HasMany.When | |
( c => | |
c.Expect(i => i.Member.GetCustomAttributes(typeof(OwnsAttribute), false).Any()), | |
c => | |
c.Cascade.AllDeleteOrphan() | |
), | |
ConventionBuilder.HasMany.When | |
( c => | |
c.Expect(i => RequiresCustomPropertyName(i.EntityType)), | |
c => | |
c.Key.Column(GetCustomPropertyName(c.Relationship.EntityType)) | |
), | |
ConventionBuilder.Reference.When | |
( c => | |
c.Expect(i => RequiresCustomPropertyName(i.EntityType)), | |
c => | |
c.Column(GetCustomPropertyName(c.EntityType)) | |
) | |
}).UseOverridesFromAssembly(overrideAssembly)); | |
} | |
private static bool RequiresCustomPropertyName(Type entityType) | |
{ | |
return entityType.GetCustomAttributes(typeof (UseTableNameOfAttribute), false).Any(); | |
} | |
private static string GetCustomPropertyName(Type entityType) | |
{ | |
var attr = (UseTableNameOfAttribute)entityType.GetCustomAttributes(typeof (UseTableNameOfAttribute), false).Single(); | |
return attr.UsesTableNameOf.Name + "_id"; | |
} | |
private static string GetTableName(IClassInstance classInstance) | |
{ | |
var entityType = classInstance.EntityType; | |
var overridenType = entityType.GetCustomAttributes(typeof(UseTableNameOfAttribute), false).FirstOrDefault() as UseTableNameOfAttribute; | |
if (overridenType != null) | |
entityType = overridenType.UsesTableNameOf; | |
return string.Join("_", entityType.FullName.Split('.').Skip(1)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment