Пример #1
0
        public void ResultDescriptorConstructionWorksWithNoArguments()
        {
            // --- Act
            var descriptor = new MethodResultDescriptor(false, null, null);

            // --- Assert
            descriptor.OutputArgumentCount.ShouldEqual(0);
        }
Пример #2
0
        /// <summary>
        /// Executes the method call on the target object and wraps it into the execution of the
        /// specified object chain.
        /// </summary>
        /// <param name="target">Target object</param>
        /// <param name="arguments">Method call arguments</param>
        /// <param name="targetMethod"></param>
        /// <param name="aspects">Aspect chain to execute</param>
        /// <param name="methodInfo"></param>
        /// <returns>Result of the method execution</returns>
        public static IMethodResultDescriptor ExecuteWrappedCall <TService>(TService target, List <MethodArgument> arguments,
                                                                            MethodInfo methodInfo, Func <IMethodCallDescriptor, TService, IMethodResultDescriptor> targetMethod,
                                                                            AspectChain aspects)
        {
            // --- Use the aspects of the object, if that supports IMethodAspect
            if (aspects == null)
            {
                var typeAspects = GetAspects(methodInfo, target.GetType());
                if (typeAspects.Count > 0)
                {
                    aspects = new AspectChain(typeAspects);
                }
            }

            var targetAspect = target as IMethodAspect;

            if (targetAspect != null)
            {
                var aspectList = new List <IMethodAspect> {
                    targetAspect
                };
                if (aspects != null)
                {
                    aspectList.AddRange(aspects);
                }
                aspects = new AspectChain(aspectList);
            }

            // --- Obtain the method descriptor
            var args          = new MethodCallDescriptor(target, methodInfo, arguments);
            var onEntryResult = OnEntry(aspects, args);

            if (onEntryResult == null)
            {
                // --- Call original method body
                IMethodResultDescriptor result;
                try
                {
                    result = targetMethod(args, target);
                }
                catch (Exception ex)
                {
                    result = new MethodResultDescriptor(ex);
                }

                // --- Call OnExit
                OnExit(aspects, args, result);
                if (result.Exception == null)
                {
                    onEntryResult = OnSuccess(aspects, args, result);
                }
                else
                {
                    var newEx = OnException(aspects, args, result.Exception);
                    if (newEx == null)
                    {
                        throw new AspectInfrastructureException("OnException returned null.", null);
                    }
                    throw newEx;
                }
            }
            return(onEntryResult);
        }