It seems to be firing off in the correct order for me. Here is my sample code:
[AttributeUsage(AttributeTargets.Class)]
public class ValidationAttribute : HandlerAttribute
{
public ValidationAttribute(int order = 1)
{
Order = order;
}
public override ICallHandler CreateHandler(IUnityContainer container)
{
var handler = container.Resolve<ValidationHandler>();
handler.Order = Order;
return handler;
}
}
public class TransactionAttribute : HandlerAttribute
{
public TransactionAttribute(int order = 2)
{
Order = order;
}
public override ICallHandler CreateHandler(IUnityContainer container)
{
var handler = container.Resolve<TransactionHandler>();
handler.Order = Order;
return handler;
}
}
public class NotifyAttribute : HandlerAttribute
{
public NotifyAttribute(int order = 3)
{
Order = order;
}
public override ICallHandler CreateHandler(IUnityContainer container)
{
var handler = container.Resolve<NotifyHandler>();
handler.Order = Order;
return handler;
}
}
public class ValidationHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.WriteLine("Validation!");
return getNext().Invoke(input, getNext);
}
public int Order
{
get;
set;
}
}
public class TransactionHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.WriteLine("Transaction!");
return getNext().Invoke(input, getNext);
}
public int Order
{
get;
set;
}
}
public class NotifyHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.WriteLine("Notify!");
return getNext().Invoke(input, getNext);
}
public int Order
{
get;
set;
}
}
[Validation]
public class TestClass
{
public virtual int TestProperty
{
get;
[Transaction]
[Notify]
set;
}
}
I made the TestProperty property virtual so I could use the VirtualMethodInterceptor. Here is the registration and test code:
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<ValidationHandler>(new ContainerControlledLifetimeManager());
container.RegisterType<TransactionHandler>(new ContainerControlledLifetimeManager());
container.RegisterType<NotifyHandler>(new ContainerControlledLifetimeManager());
container.RegisterType<TestClass>(
new InterceptionBehavior<PolicyInjectionBehavior>(),
new Interceptor<VirtualMethodInterceptor>());
var testClass = container.Resolve<TestClass>();
testClass.TestProperty = 5;
}
}
When I run this code I see:
Validation!
Transaction!
Notify!
If I change the order argument to the ValidationAttribute constructor to 9 ([Validation(9)]
) then I see Validation happening last as expected.