3

I have an abstract entity base class defined like this:

public abstract class SessionItem : Entity
{
    public virtual Session Session { get; set; }
}

In addition, I'm using auto mapping:

private AutoPersistenceModel CreateAutomappings()
{
    return AutoMap
        // configuration
        .Conventions.Add(DefaultCascade.All())
        // more configuration
}

SessionItem has several derived classes/tables, and I'd like to override the cascading policy for all of them. I tried the following:

public class SessionItemAutommapingOverride : IAutoMappingOverride<SessionItem>
{
    public void Override(AutoMapping<SessionItem> mapping)
    {
        mapping.References(x => x.Session).Cascade.None();
    }
}

But unfortunately the override is not called since SessionItem is abstract (and is not mapped). I prefer to avoid overriding it for each subclass (using IAutoMappingOverride).

Is there any way to override cascading for multiple types, without using IAutoMappingOverride<> for each one?

kshahar
  • 10,423
  • 9
  • 49
  • 73

2 Answers2

2

Apparently this is possible by using IReferenceConvention:

public class CascadeNoneOverrideConvention : IReferenceConvention
{
    public void Apply(IManyToOneInstance instance)
    {
        // override
    }
}
kshahar
  • 10,423
  • 9
  • 49
  • 73
  • @kshahar - +1. Your solution seems simpler, but it's not clear to me how you would use this convention with the classes you wished to apply it to. If you have time, could you expand on the sample code a little bit? – Tom Bushell Jan 12 '12 at 15:43
  • @TomBushell - thanks, as a matter of fact it's the same solution, I just omitted the `Accept` part. To check against more than one type, I keep a list of types and traverse them inside `Expect`. – kshahar Jan 16 '12 at 11:56
2
public class SessionReferenceCascadeNone : IReferenceConvention, IReferenceConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IManyToOneInspector> criteria)
    {
        criteria.Expect(x =>
            typeof(SessionItem).IsAssignableFrom(x.EntityType) &&
            x.Property.PropertyType == typeof(Session));
    }

    public void Apply(IManyToOneInstance instance)
    {
        instance.Cascade.None();
    }
}
Firo
  • 30,626
  • 4
  • 55
  • 94