I want to declare interception to Unity for a particular base type, and have that interception be honored for all derived types automatically.
I see two different SO posts that are the same topic, but neither has the answer I'm looking for:
- Microsoft Unity Base Class Interception
This one tells me what I already know; it is still not enough information. - Unity Interception in Derived Classes
This one is very detailed, but pertains to Unity configuration instead of the imperative form I am using.
So I'd like to show the code I have, and see if I can get an answer specific to my scenario.
I have these classes:
public abstract class RootController
{
[Report]
public abstract void Action();
}
public class MyController
{
public void Action()
{
Console.WriteLine("hey");
}
}
The [Report] annotation is my own custom attribute - it indicates AOP logging should be applied to the method. I arrange for it to be used with policy-based interception as follows:
container.AddNewExtension<Interception>();
container.RegisterInstance<InjectionPolicy>(typeof(ReportAttributePolicy).AssemblyQualifiedName, new ReportAttributePolicy());
container.RegisterType<RootController>(
new Interceptor<VirtualMethodInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>()
);
The ReportAttributePolicy
is my custom version of AttributeDrivenPolicy
. See my Unity Interception blog post for details.
Obviously the scenario I'm trying to make work is this:
var yup = container.Resolve<MyController>();
Even though the requested type merely derives from RootController
, I would like the resolved type to be instrumented with AOP logging, per my ReportAttributePolicy
.
When I execute the above code, none of the methods on my ReportAttributePolicy are executed. This means I don't have a chance to make magic happen. If I'm not dealing with an inheritance example, then it all works fine.
How do I make it work with inheritance?