コード例 #1
0
        /// <inheritdoc />
        public object Invoke(object handlerAction, params object[] args)
        {
            HandlerActionInvokerCacheEntry handlerActionInvoker = this.invokerCache.GetCachedHandlerAction(handlerAction);

            if (handlerActionInvoker == null)
            {
                throw new ArgumentNullException(nameof(handlerActionInvoker));
            }

            var targetHandler = handlerActionInvoker.HandlerFactory(handlerActionInvoker.HandlerType);

            if (targetHandler == null)
            {
                throw new ArgumentNullException(nameof(targetHandler));
            }

            object handlerResult = null;

            try
            {
                var handlerActionParameters = this.PrepareParameters(args, handlerActionInvoker.HandlerExecutor);

                handlerResult = handlerActionInvoker.HandlerExecutor.Execute(targetHandler, handlerActionParameters);
            }
            catch
            {
                throw;
            }
            finally
            {
                handlerActionInvoker.HandlerReleaser(targetHandler);
            }

            return(handlerResult);
        }
コード例 #2
0
        /// <summary>
        /// Gets a <see cref="HandlerActionInvokerCacheEntry"/> based on a handler action.
        /// </summary>
        /// <remarks>
        /// <see cref="HandlerActionInvokerCacheEntry"/> contains the handler factory creator
        /// and the handler action executor.
        /// </remarks>
        /// <param name="handlerAction">Handler action.</param>
        /// <returns>
        /// Existing <see cref="HandlerActionInvokerCacheEntry"/> in cache;
        /// if the entry doesn't exist, it creates a new <see cref="HandlerActionInvokerCacheEntry"/>,
        /// caches it and returns it.
        /// </returns>
        public HandlerActionInvokerCacheEntry GetCachedHandlerAction(object handlerAction)
        {
            if (!this.cache.TryGetValue(handlerAction, out HandlerActionInvokerCacheEntry cacheEntry))
            {
                HandlerActionModel handlerActionModel = this.handlerCache.GetHandlerAction(handlerAction);

                if (handlerActionModel == null)
                {
                    throw new ArgumentNullException(nameof(handlerActionModel));
                }

                object[] defaultHandlerActionParameters = handlerActionModel.Method.GetMethodParametersDefaultValues();

                cacheEntry = new HandlerActionInvokerCacheEntry(
                    handlerActionModel.HandlerTypeInfo.AsType(),
                    this.handlerFactory.CreateHandler,
                    this.handlerFactory.ReleaseHandler,
                    new HandlerExecutor(handlerActionModel.HandlerTypeInfo, handlerActionModel.Method, defaultHandlerActionParameters));

                this.cache.Add(handlerAction, cacheEntry);
            }

            return(cacheEntry);
        }