usingFluentNHibernate.Conventions;usingFluentNHibernate.Conventions.AcceptanceCriteria;usingFluentNHibernate.Conventions.Inspections;usingFluentNHibernate.Conventions.Instances;usingMarcusoft.FluentAutomapper.Domain.Conventions;usingFluentNHibernate.Automapping;usingMarcusoft.FluentAutomapper.Domain.Model;
using PrimaryKey = FluentNHibernate.Conventions.Helpers.PrimaryKey;
using ForeignKey = FluentNHibernate.Conventions.Helpers.ForeignKey;
using Table = FluentNHibernate.Conventions.Helpers.Table;
using Inf = Inflector.Net.Inflector;
namespaceMarcusoft.FluentAutomapper.Domain
{
public classFluentNHibernateHelper
{public static AutoPersistenceModelGetAutomappedPeristanceModel()
{
return AutoMap.AssemblyOf<Product>()
.Where(t => t.Namespace.EndsWith("Model") && t.IsAbstract == false)
.Conventions.Add(
PrimaryKey.Name.Is(pk => "ID"),
ForeignKey.EndsWith("ID"),
Table.Is(t => Inf.Pluralize(t.EntityType.Name)))
.Conventions.Add<CustomManyToManyTableNameConvention>()
.Conventions.Add<CustomHasManyConvention>();
}
}
}
namespaceMarcusoft.FluentAutomapper.Domain.Conventions
{
/// <summary>/// Setting the name of Many-to-Many tables/// </summary>public classCustomManyToManyTableNameConvention : ManyToManyTableNameConvention
{protected override stringGetBiDirectionalTableName(IManyToManyCollectionInspector collection, IManyToManyCollectionInspector otherSide)
{
return Inf.Pluralize(collection.EntityType.Name) + "_" + Inf.Pluralize(otherSide.EntityType.Name);
}
protected override stringGetUniDirectionalTableName(IManyToManyCollectionInspector collection)
{
return Inf.Pluralize(collection.EntityType.Name) + "_" + Inf.Pluralize(collection.ChildType.Name);
}
}
/// <summary>/// I use this convention to get cascade all on all the entities /// that implements IAggregateRoot (empty interface), so that I /// can set up a casacade-strategy/// </summary>/// <seealso cref="http://wiki.fluentnhibernate.org/Available_conventions#Interfaces"/>public classCustomHasManyConvention : IHasManyConvention, IHasManyConventionAcceptance
{public voidAccept(IAcceptanceCriteria<IOneToManyCollectionInspector> criteria)
{
// Hmmm - not to happy about the GetInterface(typeof(IAggregateRoot).Name)// but it will have to work for now
criteria.Expect(x => x.EntityType.GetInterface(typeof(IAggregateRoot).Name) != null);
}
public voidApply(IOneToManyCollectionInstance instance)
{
instance.Cascade.All();
}
}
}