-
Notifications
You must be signed in to change notification settings - Fork 7
Multiple Aspects
Sagi edited this page Nov 12, 2015
·
7 revisions
Please refer to Adding your first Aspect
If you haven't read it yet.
NCop supports the option to run multiple aspects on the same method.
You just need to annotate the same method multiple times with your desired aspect.
[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IDeveloper
{
[MethodInterceptionAspect(typeof(InterceptionAspectImpl))]
[MethodInterceptionAspect(typeof(AnotherInterceptionAspectImpl))]
void Code();
}
public class InterceptionAspectImpl: ActionInterceptionAspect
{
public override void OnInvoke(ActionInterceptionArgs args) {
Console.WriteLine("OnInvoke of AnInterceptionAspect");
base.OnInvoke(args);
}
}
public class AnotherInterceptionAspectImpl : ActionInterceptionAspect
{
public override void OnInvoke(ActionInterceptionArgs args) {
Console.WriteLine("OnInvoke of AnotherInterceptionAspect");
base.OnInvoke(args);
}
}
So for the code below:
[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IDeveloper
{
[MethodInterceptionAspect(typeof(InterceptionAspectImpl))]
[MethodInterceptionAspect(typeof(AnotherInterceptionAspectImpl))]
void Code();
}
public class CSharpDeveloperMixin : IDeveloper
{
public void Code() {
Console.WriteLine("C# coding");
}
}
public class InterceptionAspectImpl : ActionInterceptionAspect
{
public override void OnInvoke(ActionInterceptionArgs args) {
Console.WriteLine("OnInvoke of AnInterceptionAspect");
base.OnInvoke(args);
}
}
public class AnotherInterceptionAspectImpl : ActionInterceptionAspect
{
public override void OnInvoke(ActionInterceptionArgs args) {
Console.WriteLine("OnInvoke of AnotherInterceptionAspect");
base.OnInvoke(args);
}
}
class Program
{
static void Main(string[] args) {
IDeveloper developer = null;
var container = new CompositeContainer();
container.Configure();
developer = container.Resolve<IDeveloper>();
developer.Code();
}
}
The expected output should be:
"OnInvoke of AnInterceptionAspect"
"OnInvoke of AnotherInterceptionAspect"
"C# coding"
You can also mix different Method Aspects
[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IDeveloper
{
[MethodInterceptionAspect(typeof(InterceptionAspectImpl))]
[OnMethodBoundaryAspect(typeof(OnMethodBoundaryAspectImpl))]
void Code();
}
And apply multiple aspects on Property Aspects and Event Aspects