예제 #1
0
 /// <summary>
 /// Executes after the handling request.
 /// </summary>
 /// <param name="context">
 /// The context.
 /// </param>
 public void AfterHandlingRequest(RequestProcessingContext context)
 {
     if (context != null && context.Response != null)
     {
         try
         {
             if (context.Response.Exception == null)
             {
                 if (_validationFailures.Count > 0)
                 {
                     if (context.Response is IDtoResponse)
                     {
                         var dto = (context.Response as IDtoResponse).GetDto();
                         MapFailures(_validationFailures, dto);
                     }
                     else
                     {
                         Logger.Error(
                             "Validation failed but no failures mapped to data transfer object, because the response type ({0}) is not derived from {1}",
                             context.Response.GetType().Name,
                             typeof(IDtoResponse).Name);
                     }
                 }
             }
         }
         catch (Exception e)
         {
             var response = context.Response;
             response.Exception     = new ExceptionInfo(e);
             response.ExceptionType = ExceptionType.Unknown;
         }
     }
 }
예제 #2
0
파일: TestObjects.cs 프로젝트: siimv/Agatha
        public virtual void BeforeHandlingRequest(RequestProcessingContext context)
        {
            var testRequest = (SpyRequest)context.Request;

            testRequest.BeforeProcessingTimeStamp = SystemClock.Now();
            Thread.Sleep(50);
        }
예제 #3
0
        private void HandleRequest(RequestProcessingContext requestProcessingState)
        {
            var request = requestProcessingState.Request;

            if (request is OneWayRequest)
            {
                using (var handler = (IOneWayRequestHandler)IoC.Container.Resolve(GetOneWayRequestHandlerTypeFor(request)))
                {
                    try
                    {
                        ExecuteHandler((OneWayRequest)request, handler);
                    }
                    finally
                    {
                        IoC.Container.Release(handler);
                    }
                }
            }
            else
            {
                using (var handler = (IRequestHandler)IoC.Container.Resolve(GetRequestHandlerTypeFor(request)))
                {
                    try
                    {
                        var response = GetResponseFromHandler(request, handler);
                        requestProcessingState.MarkAsProcessed(response);
                    }
                    finally
                    {
                        IoC.Container.Release(handler);
                    }
                }
            }
        }
예제 #4
0
파일: TestObjects.cs 프로젝트: siimv/Agatha
        public void AfterHandlingRequest(RequestProcessingContext context)
        {
            var testRequest = (SpyRequest)context.Request;

            testRequest.AfterProcessingTimeStamp = SystemClock.Now();
            testRequest.ResponseFromContext      = context.Response;
            Thread.Sleep(50);
        }
        public void DealWithPreviouslyOccurredExceptions(RequestProcessingContext context)
        {
            var response = CreateResponse(context);

            response.Exception     = new ExceptionInfo(new Exception(ExceptionType.EarlierRequestAlreadyFailed.ToString()));
            response.ExceptionType = ExceptionType.EarlierRequestAlreadyFailed;
            context.MarkAsProcessed(response);
        }
예제 #6
0
 public void AfterHandlingRequest(RequestProcessingContext context)
 {
     if (ResponseCanBeCached(context))
     {
         context.Response.IsCached = true;
         cacheManager.StoreInCache(context.Request, context.Response);
     }
 }
예제 #7
0
 public void AfterHandlingRequest(RequestProcessingContext context)
 {
     if (ResponseCanBeCached(context))
     {
         context.Response.IsCached = true;
         cacheManager.StoreInCache(context.Request, context.Response);
     }
 }
        public void DealWithException(RequestProcessingContext context, Exception exception)
        {
            var response = CreateResponse(context);

            response.Exception = new ExceptionInfo(exception);
            SetExceptionType(response, exception);
            context.MarkAsProcessed(response);
        }
        private Type TryBasedOnConventions(RequestProcessingContext context)
        {
            var conventions = IoC.Container.TryResolve <IConventions>();

            if (conventions != null)
            {
                return(conventions.GetResponseTypeFor(context.Request));
            }
            return(null);
        }
예제 #10
0
 protected override void Given()
 {
     IoC.Container = new Container();
     errorHandler  = new RequestProcessingErrorHandler(new ServiceLayerConfiguration(IoC.Container));
     request       = new RequestA();
     Context       = new RequestProcessingContext(request);
     cacheManager  = Stubbed <ICacheManager>();
     Container.RegisterInstance(cacheManager);
     EstablishContext();
 }
예제 #11
0
 public void BeforeHandlingRequest(RequestProcessingContext context)
 {
     if (CachingIsEnabledForThisRequest(context))
     {
         var response = cacheManager.GetCachedResponseFor(context.Request);
         if (response != null)
         {
             context.MarkAsProcessed(response);
         }
     }
 }
예제 #12
0
 public void BeforeHandlingRequest(RequestProcessingContext context)
 {
     if (CachingIsEnabledForThisRequest(context))
     {
         var response = cacheManager.GetCachedResponseFor(context.Request);
         if (response != null)
         {
             context.MarkAsProcessed(response);
         }
     }
 }
예제 #13
0
 private Type TryBasedOnRequestHandler(RequestProcessingContext context)
 {
     try
     {
         var handler = (IRequestHandler)IoC.Container.Resolve(GetRequestHandlerTypeFor(context.Request));
         return(handler.CreateDefaultResponse().GetType());
     }
     catch
     {
         return(null);
     }
 }
예제 #14
0
 /// <summary>
 /// Befores the handling request.
 /// </summary>
 /// <param name="context">The context.</param>
 public void BeforeHandlingRequest(RequestProcessingContext context)
 {
     if (context != null && context.Request != null)
     {
         var request  = context.Request;
         var resource = new ResourceRequest {
             request.GetType().FullName
         };
         if (!_accessControlManager.CanAccess(resource))
         {
             throw new InvalidOperationException("You do not have permission to access: " + resource);
         }
     }
 }
예제 #15
0
 public override void BeforeHandlingRequest(RequestProcessingContext context)
 {
     try
     {
         Validate.Object(context.Request as dynamic);
     }
     catch (ValidationException exc)
     {
         var response = CreateDefaultResponseFor(context.Request);
         response.Exception     = new ExceptionInfo(exc);
         response.ExceptionType = ExceptionType.Unknown;
         context.MarkAsProcessed(response);
     }
 }
        private Type TryBasedOnCachedResponse(RequestProcessingContext context)
        {
            var cacheManager = IoC.Container.Resolve <ICacheManager>();

            if (cacheManager.IsCachingEnabledFor(context.Request.GetType()))
            {
                var response = cacheManager.GetCachedResponseFor(context.Request);
                if (response != null)
                {
                    return(response.GetType());
                }
            }
            return(null);
        }
예제 #17
0
        private IEnumerable <Exception> RunInvokedInterceptorsSafely(RequestProcessingContext requestProcessingState, IList <IRequestHandlerInterceptor> invokedInterceptors)
        {
            var exceptionsFromInterceptor = new List <Exception>();

            foreach (var interceptor in invokedInterceptors.Reverse())
            {
                try
                {
                    interceptor.AfterHandlingRequest(requestProcessingState);
                }
                catch (Exception exc)
                {
                    exceptionsFromInterceptor.Add(exc);
                }
            }

            return(exceptionsFromInterceptor);
        }
        private Type DetermineResponseType(RequestProcessingContext context)
        {
            var strategies = new Func <RequestProcessingContext, Type>[]
            {
                TryBasedOnConventions,
                TryBasedOnCachedResponse,
                TryBasedOnRequestHandler
            };

            foreach (var strategy in strategies)
            {
                var responseType = strategy(context);
                if (responseType != null)
                {
                    return(responseType);
                }
            }
            return(typeof(Response));
        }
예제 #19
0
        private bool InvokeRequestHandler(RequestProcessingContext requestProcessingState)
        {
            var request = requestProcessingState.Request;

            BeforeResolvingRequestHandler(request);

            using (var handler = (IRequestHandler)IoC.Container.Resolve(GetRequestHandlerTypeFor(request)))
            {
                try
                {
                    var response = GetResponseFromHandler(request, handler);
                    requestProcessingState.MarkAsProcessed(response);
                    return(response.ExceptionType != ExceptionType.None);
                }
                finally
                {
                    IoC.Container.Release(handler);
                }
            }
        }
        private Type TryBasedOnRequestHandler(RequestProcessingContext context)
        {
            IRequestHandler handler = null;

            try
            {
                handler = (IRequestHandler)IoC.Container.Resolve(GetRequestHandlerTypeFor(context.Request));
                return(handler.CreateDefaultResponse().GetType());
            }
            catch
            {
                return(null);
            }
            finally
            {
                if (handler != null)
                {
                    IoC.Container.Release(handler);
                }
            }
        }
예제 #21
0
 private void InvokeRequestHandler(RequestProcessingContext requestProcessingState)
 {
     BeforeResolvingRequestHandler(requestProcessingState.Request);
     HandleRequest(requestProcessingState);
 }
예제 #22
0
 public abstract void AfterHandlingRequest(RequestProcessingContext context);
예제 #23
0
 public abstract void BeforeHandlingRequest(RequestProcessingContext context);
예제 #24
0
 private bool ResponseCanBeCached(RequestProcessingContext context)
 {
     return CachingIsEnabledForThisRequest(context) && context.Response.ExceptionType == ExceptionType.None;
 }
예제 #25
0
 /// <summary>
 /// Executes before the handling request.
 /// </summary>
 /// <param name="context">
 /// The context.
 /// </param>
 public void BeforeHandlingRequest(RequestProcessingContext context)
 {
     DomainEvent.Register <RuleViolationEvent> (failure => _validationFailures.AddRange(failure.RuleViolations));
 }
예제 #26
0
 public abstract void BeforeHandlingRequest(RequestProcessingContext context);
예제 #27
0
 public abstract void AfterHandlingRequest(RequestProcessingContext context);
예제 #28
0
파일: TestObjects.cs 프로젝트: siimv/Agatha
        public override void AfterHandlingRequest(RequestProcessingContext context)
        {
            base.AfterHandlingRequest(context);

            throw new InvalidOperationException("I will fail.");
        }
예제 #29
0
        private void DispatchRequestsToHandlers(List <Response> responses, params Request[] requests)
        {
            bool exceptionsPreviouslyOccurred = false;

            foreach (var request in requests)
            {
                var requestProcessingState = new RequestProcessingContext(request);
                IList <IRequestHandlerInterceptor> interceptors = new List <IRequestHandlerInterceptor>();
                try
                {
                    IList <IRequestHandlerInterceptor> invokedInterceptors = new List <IRequestHandlerInterceptor>();
                    if (!exceptionsPreviouslyOccurred)
                    {
                        interceptors = ResolveInterceptors();

                        foreach (var interceptor in interceptors)
                        {
                            interceptor.BeforeHandlingRequest(requestProcessingState);
                            invokedInterceptors.Add(interceptor);
                            if (requestProcessingState.IsProcessed)
                            {
                                responses.Add(requestProcessingState.Response);
                                break;
                            }
                        }
                    }

                    if (!requestProcessingState.IsProcessed)
                    {
                        var skipHandler = false;
                        var cachingIsEnabledForThisRequest = _cacheManager.IsCachingEnabledFor(request.GetType());

                        if (cachingIsEnabledForThisRequest)
                        {
                            var cachedResponse = _cacheManager.GetCachedResponseFor(request);

                            if (cachedResponse != null)
                            {
                                if (exceptionsPreviouslyOccurred)
                                {
                                    var dummyResponse = Activator.CreateInstance(cachedResponse.GetType()) as Response;
                                    responses.Add(SetStandardExceptionInfoWhenEarlierRequestsFailed(dummyResponse));
                                    requestProcessingState.MarkAsProcessed(dummyResponse);
                                }
                                else
                                {
                                    responses.Add(cachedResponse);
                                    requestProcessingState.MarkAsProcessed(cachedResponse);
                                }

                                skipHandler = true;
                            }
                        }
                        if (!skipHandler)
                        {
                            BeforeResolvingRequestHandler(request);

                            using (var handler = (IRequestHandler)IoC.Container.Resolve(GetRequestHandlerTypeFor(request)))
                            {
                                try
                                {
                                    if (!exceptionsPreviouslyOccurred)
                                    {
                                        var response = GetResponseFromHandler(request, handler);
                                        exceptionsPreviouslyOccurred = response.ExceptionType != ExceptionType.None;
                                        responses.Add(response);
                                        requestProcessingState.MarkAsProcessed(response);

                                        if (response.ExceptionType == ExceptionType.None && cachingIsEnabledForThisRequest)
                                        {
                                            _cacheManager.StoreInCache(request, response);
                                        }
                                    }
                                    else
                                    {
                                        var response = handler.CreateDefaultResponse();
                                        responses.Add(SetStandardExceptionInfoWhenEarlierRequestsFailed(response));
                                        requestProcessingState.MarkAsProcessed(response);
                                    }
                                }
                                finally
                                {
                                    IoC.Container.Release(handler);
                                }
                            }
                        }
                    }

                    foreach (var interceptor in invokedInterceptors.Reverse())
                    {
                        interceptor.AfterHandlingRequest(requestProcessingState);
                    }
                    invokedInterceptors.Clear();
                }
                finally
                {
                    SaveDisposeInterceptors(interceptors);
                }
            }
        }
예제 #30
0
 private bool CachingIsEnabledForThisRequest(RequestProcessingContext context)
 {
     return cacheManager.IsCachingEnabledFor(context.Request.GetType());
 }
예제 #31
0
파일: TestObjects.cs 프로젝트: siimv/Agatha
 public virtual void AfterHandlingRequest(RequestProcessingContext context)
 {
     ((SpyRequest)context.Request).SubSequentInterceptorAfterProcessingTimeStamp = SystemClock.Now();
     Thread.Sleep(50);
 }
예제 #32
0
 private bool ResponseCanBeCached(RequestProcessingContext context)
 {
     return(CachingIsEnabledForThisRequest(context) && context.Response.ExceptionType == ExceptionType.None);
 }
예제 #33
0
파일: TestObjects.cs 프로젝트: siimv/Agatha
 public void BeforeHandlingRequest(RequestProcessingContext context)
 {
     ((SpyRequest)context.Request).SubSequentInterceptorBeforeProcessingTimeStamp = SystemClock.Now();
     Thread.Sleep(50);
 }
        private Response CreateResponse(RequestProcessingContext context)
        {
            var responseType = DetermineResponseType(context);

            return((Response)Activator.CreateInstance(responseType));
        }
예제 #35
0
 private bool CachingIsEnabledForThisRequest(RequestProcessingContext context)
 {
     return(cacheManager.IsCachingEnabledFor(context.Request.GetType()));
 }
예제 #36
0
파일: TestObjects.cs 프로젝트: siimv/Agatha
 public override void BeforeHandlingRequest(RequestProcessingContext context)
 {
     context.MarkAsProcessed(new ResponseFromInterceptor());
 }