narsil/Tanshu.Accounts.SqlDAO/Fluent/CascadeConvention.cs

44 lines
1.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
using Tanshu.Accounts.Contracts;
using System.Reflection;
namespace Tanshu.Accounts.SqlDAO
{
public class CascadeConvention : IHasManyConvention, IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
var property = instance.Property;
if (!HasAttribute(property.MemberInfo)) return;
Console.WriteLine("CascadeAll on {0}.{1}", property.DeclaringType.Name, property.Name);
instance.Cascade.All();
}
public void Apply(IOneToManyCollectionInstance instance)
{
var property = instance.Member;
if (!HasAttribute(property)) return;
Console.WriteLine("CascadeAllDeleteOrphan on {0}.{1}", property.DeclaringType.Name, property.Name);
instance.Cascade.AllDeleteOrphan();
}
bool HasAttribute(ICustomAttributeProvider provider)
{
return provider.GetCustomAttributes(typeof(CascadeAttribute), false).Length == 1;
}
}
}