-
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.
Lets define some aspects:
public class InterceptionAspectImpl: ActionInterceptionAspect
{
public override void OnInvoke(ActionInterceptionArgs args) {
Console.WriteLine("OnInvoke");
base.OnInvoke(args);
}
}
public class AnotherInterceptionAspectImpl : ActionInterceptionAspect
{
public override void OnInvoke(ActionInterceptionArgs args) {
Console.WriteLine("OnInvoke of AnotherInterceptionAspectImpl");
base.OnInvoke(args);
}
}
public class OnMethodBoundaryAspectImpl : OnActionBoundaryAspect
{
public override void OnEntry(ActionExecutionArgs args) {
Console.WriteLine("OnEntry");
base.OnEntry(args);
}
public override void OnSuccess(ActionExecutionArgs args) {
Console.WriteLine("OnSuccess");
base.OnSuccess(args);
}
public override void OnException(ActionExecutionArgs args) {
Console.WriteLine("OnException");
base.OnException(args);
}
public override void OnExit(ActionExecutionArgs args) {
Console.WriteLine("OnExit");
base.OnExit(args);
}
}
Lets annotate the method with multiple aspects of type MethodInterceptionAspect
[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IDeveloper
{
[MethodInterceptionAspect(typeof(AnotherInterceptionAspectImpl))]
[MethodInterceptionAspect(typeof(InterceptionAspectImpl))]
void Code();
}
So for the code below:
[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IDeveloper
{
[MethodInterceptionAspect(typeof(AnotherInterceptionAspectImpl))]
[MethodInterceptionAspect(typeof(InterceptionAspectImpl))]
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");
base.OnInvoke(args);
}
}
public class AnotherInterceptionAspectImpl : ActionInterceptionAspect
{
public override void OnInvoke(ActionInterceptionArgs args) {
Console.WriteLine("OnInvoke of AnotherInterceptionAspectImpl");
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"
"OnInvoke of AnotherInterceptionAspeImpl"
"C# coding"
For different Method Aspects types
[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IDeveloper
{
[MethodInterceptionAspect(typeof(InterceptionAspectImpl))]
[OnMethodBoundaryAspect(typeof(OnMethodBoundaryAspectImpl))]
void Code();
}
And for the code below:
[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IDeveloper
{
[MethodInterceptionAspect(typeof(InterceptionAspectImpl))]
[OnMethodBoundaryAspect(typeof(OnMethodBoundaryAspectImpl))]
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");
base.OnInvoke(args);
}
}
public class OnMethodBoundaryAspectImpl : OnActionBoundaryAspect
{
public override void OnEntry(ActionExecutionArgs args) {
Console.WriteLine("OnEntry");
base.OnEntry(args);
}
public override void OnSuccess(ActionExecutionArgs args) {
Console.WriteLine("OnSuccess");
base.OnSuccess(args);
}
public override void OnException(ActionExecutionArgs args) {
Console.WriteLine("OnException");
base.OnException(args);
}
public override void OnExit(ActionExecutionArgs args) {
Console.WriteLine("OnExit");
base.OnExit(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:
"OnEntry"
"OnInvoke"
"C# coding"
"OnSuccess"
"OnExit"
You can also apply multiple aspects on Property Aspects and Event Aspects