Question

Currently I am trying to develop a solution that will check if a method has been executed and if some time has passed since it was last executed, given that it was and the time has passed, I would like to skip from the OnEntry method to the OnExit without actually executing any code from the method itself.

Sort of :

public class CacheThisMethod : OnMethodBoundaryAspect
{
            public override void OnEntry(MethodExecutionArgs args)
            {
               if (isCached( args.Method.name)
                   {
                    args.MethodExecutionTag =  getReturnValue(args.Method.name)
                    //jump to OnExit
                   }
               else
                   {
                    //continue
                   }
            }
            public override void OnExit(MethodExecutionArgs args)
            {
               args.Method.ReturnValue = args.MethodExecutionTag;


             }
}

How can I achieve this? Thanks.

Was it helpful?

Solution

The following modifications to your code show how to get what you want.

public class CacheThisMethod : OnMethodBoundaryAspect
{
        public override void OnEntry(MethodExecutionArgs args)
        {
           if (isCached( args.Method.name)
               {
                args.MethodExecutionTag =  getReturnValue(args.Method.name)
                OnExit(args);
               }
           else
               {
                //continue
               }
        }
        public override void OnExit(MethodExecutionArgs args)
        {
           //args.Method.ReturnValue = args.MethodExecutionTag;
            args.ReturnValue = args.MethodExecutionTag;
            args.FlowBehavior = FlowBehavior.Return;
        }
}

However, if you are working on a per method name key, then you can use a simple property for your cached return value, as a separate instance of each aspect will be created for you when you attach your advice to a method.

If there is no reason to jump to the OnExit then just add the FlowBehaviour and return value setting to the OnEntry method at the point it makes the call to the OnExit method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top