protected override Task <TestResult> Handle(IExecutionContextReader context, TestQuery request) { return(new TestResult { Desc = $"{request.Name}FromSecondHandler" }.WrapInTask()); }
public override Task <IEnumerable <IError> > Validate(IExecutionContextReader context, TestRequest request) { return(RuleChecker.For(request) .Should(x => x.Id.HasValue(), "Id", "Id is required") .Done() .WrapInTask()); }
public static async Task <TResult> GetAsync <TResult>(IServiceProvider sp, IExecutionContextReader context, ILogger logger, bool failSafe) { var handler = sp.GetServices <IResponseHandlerAsync <TResult> >() ?.Where(h => h.IsApplicable(context)).FirstOrDefault(); if (handler == null) { if (failSafe) { logger.LogWarning($"No handler found that impement IResponseProvider<{typeof(TResult).FullName}>"); return(default(TResult)); } throw new RequestBusException($"No response handler defined for type {typeof(IResponseHandlerAsync<TResult>)}"); } #if DEBUG var timer = Timer.Start(logger, handler); #endif var response = await handler.Handle(context); #if DEBUG timer.Completed(); #endif if (response == null) { return(default(TResult)); } await sp.FilterAsync(context, response, logger); return(response.Result); }
protected override Task <TestResponse> Handle(IExecutionContextReader context, TestRequest request) { return(new TestResponse { Name = "Main" }.WrapInTask()); }
public override Task Filter(IExecutionContextReader context, IResponse <Customer> response) { if (response.IsSucceed) { response.Result.Name = response.Result.Name + "Filtered"; } return(Task.FromResult(0)); }
protected override Task <ShowroomViewModel> Handle(IExecutionContextReader context, ShowroomRequest request) { var tenantName = _tenant.Name; var provider = _providers.FirstOrDefault(x => x.ForTenants.Any(tenant => tenantName.IsSame(tenant))); return(provider == null ? Task.FromResult <ShowroomViewModel>(null) : Task.FromResult(provider.Get())); }
public override Task <IEnumerable <IError> > Validate(IExecutionContextReader context, TestQuery request) { return(RuleChecker .For(request) .Should(r => !string.IsNullOrWhiteSpace(r.Id), "Id", "Id is required", "TESTQUERY_SECOND_ID_REQUIRED") .Should(r => !string.IsNullOrWhiteSpace(r.Name), "Name", "Name is required", "TESTQUERY_SECOND_NAME_REQUIRED") .Done() .WrapInTask()); }
protected override async Task Handle(IExecutionContextReader context, RegisterUser request) { var id = await _userService.Create(request); await _emailSender.SendAsync(request); await _authenticator.Authenticate(new UserAuthRequest { Email = request.Email, Id = id }); }
async Task <IResponse> IRequestHandlerAsync <LoginRequest> .Handle(IExecutionContextReader context, LoginRequest request) { var user = await _userService.Retrieve(request); if (user == null) { return(FailedResponse()); } await _authenticator.Authenticate(new UserAuthRequest { Id = user.Id, Email = user.Email }); return(Response.Succeed()); }
protected override async Task <ProductDetailsViewModel> Handle(IExecutionContextReader context, LoadProductDetails request) { var product = await _productApiProxy.GetAsync(request.Id); if (product == null) { return(null); } var discount = _discountCalculator.Calculate(product.Price); return(new ProductDetailsViewModel { Title = product.Title, PriceText = product.Price.ToString("C"), DiscountText = discount > 0 ? discount.ToString("C") : string.Empty }); }
public static Task PublishAsync <TEvent>(IServiceProvider sp, IExecutionContextReader context, ILogger logger, TEvent evnt, bool failSafe) { var handlers = sp.GetServices <IEventHandlerAsync <TEvent> >() ?.Where(h => h.IsApplicable(context, evnt)); if (handlers == null) { return(Task.FromResult(0)); } var tasks = handlers.Select(async h => { if (!failSafe) { #if DEBUG var timer = Timer.Start(logger, h); #endif await h.Handle(context, evnt); #if DEBUG timer.Completed(); #endif } else { try { #if DEBUG var timer = Timer.Start(logger, h); #endif await h.Handle(context, evnt); #if DEBUG timer.Completed(); #endif } catch (Exception e) { logger.LogError(0, e, e.Message); } } }); return(Task.WhenAll(tasks)); }
public async Task <IExecutionContextReader> GetAsync(IServiceProvider serviceProvider) { if (_context != null) { return(_context); } var populators = serviceProvider.GetServices <IExecutionContextPopulatorAsync>(); var context = new ExecutionContext(); if (populators != null) { var tasks = populators.Select(p => p.Init(context)); await Task.WhenAll(tasks); } _context = context; return(_context); }
protected abstract Task Handle(IExecutionContextReader context, TRequest request);
private static async Task <IResponseUnit <TResult> > HandleResponseHandler <TRequest, TResult>(IExecutionContextReader context, IResponseHandlerAsync <TRequest, TResult> h, TRequest request, ILogger logger) { #if DEBUG var timer = Timer.Start(logger, h); #endif IResponse <TResult> rsp; if (h.ExecutionHint != ExecutionHintType.Main) { rsp = await h.Handle(context, request); } else { try { rsp = await h.Handle(context, request); } catch (Exception e) { rsp = Response.Failed <TResult>(); logger.LogError(0, e, $"ResponseHandler {h.GetType().FullName} failed with message {e.Message}"); } } #if DEBUG timer.Completed(); #endif return(Convert(rsp, h.ExecutionHint)); }
Task <IResponse> IRequestHandlerAsync <TRequest> .Handle(IExecutionContextReader context, TRequest request) { return(Handle(context, request) .ContinueWith(t => Response.Succeed())); }
public static async Task <IEnumerable <IResponseUnit <TResult> > > GetAllAsync <TRequest, TResult>(IServiceProvider sp, IExecutionContextReader context, ILogger logger, TRequest request) { var errors = await sp.ValidateAsync(context, request, logger); var result = new List <IResponseUnit <TResult> >(); if (errors.Any()) { result.Add(new ResponseUnit <TResult> { Errors = errors, IsMainResponse = true, IsSucceed = false }); return(result); } var allApplicableHandlers = sp.GetServices <IResponseHandlerAsync <TRequest, TResult> >() ?.Where(t => t.IsApplicable(context, request)) ?? Enumerable.Empty <IResponseHandlerAsync <TRequest, TResult> >(); var mainHandlers = allApplicableHandlers.Where(t => t.ExecutionHint == ExecutionHintType.Independent || t.ExecutionHint == ExecutionHintType.Main); var mainHandlerTasks = mainHandlers .Select(h => HandleResponseHandler(context, h, request, logger)); await Task.WhenAll(mainHandlerTasks); var isMainFailed = false; foreach (var task in mainHandlerTasks) { if (task.Result == null) { continue; } if (task.Result.IsMainResponse) { isMainFailed = !task.Result.IsSucceed; } result.Add(task.Result); } if (isMainFailed) { return(result); } var dependentHandlers = allApplicableHandlers.Where(h => h.ExecutionHint == ExecutionHintType.Dependent); var dependentHandlerTasks = dependentHandlers .Select(h => HandleResponseHandler(context, h, request, logger)); await Task.WhenAll(dependentHandlerTasks); foreach (var task in dependentHandlerTasks) { if (task.Result == null) { continue; } result.Add(task.Result); } var filters = sp.GetServices <IResponseCollectionFilterAsync <TRequest, TResult> >() ?.Where(x => x.IsApplicable(context, request)) ?? Enumerable.Empty <IResponseCollectionFilterAsync <TRequest, TResult> >(); foreach (var filter in filters) { #if DEBUG var timer = Timer.Start(logger, filter); #endif await filter.Filter(context, request, result); #if DEBUG timer.Completed(); #endif } return(result); }
public override bool IsApplicable(IExecutionContextReader context, TestQuery request) { return(context.Get <string>("handler").IsSame("second")); }
public override bool IsApplicable(IExecutionContextReader context, TestRequest request) { var control = context.Get <string>("control"); return(control.IsSame("a")); }
public abstract Task Handle(IExecutionContextReader context, TEvent evnt);
protected abstract Task <TResult> Handle(IExecutionContextReader context);
public static T Get <T>(this IExecutionContextReader source, string key) { return((T)(source.Get(key))); }
internal static async Task FilterAsync <TRequest, TResult>(this IServiceProvider service, IExecutionContextReader context, TRequest request, IResponse <TResult> response, ILogger logger) { if (response == null || !response.IsSucceed || response.Result == null) { return; } var filters = service.GetServices <IResponseFilterAsync <TRequest, TResult> >() ?.Where(h => h.IsApplicable(context, request)); if (filters == null) { return; } foreach (var filter in filters) { #if DEBUG var timer = Timer.Start(logger, filter); #endif await filter.Filter(context, request, response); #if DEBUG timer.Completed(); #endif } }
Task <IResponse <TResult> > IResponseHandlerAsync <TResult> .Handle(IExecutionContextReader context) { return(Handle(context).ContinueWith(h => Response.Succeed(h.Result))); }
public Task <IResponse <TestResponse> > Handle(IExecutionContextReader context, TestRequest request) { return(Response.Failed <TestResponse>().WrapInTask()); }
public bool IsApplicable(IExecutionContextReader context, TestRequest request) { return(context.Get <string>("control").IsSame("mainfailed")); }
public abstract Task <IEnumerable <IError> > Validate(IExecutionContextReader context, TRequest request);
public virtual bool IsApplicable(IExecutionContextReader context, TRequest request) => true;
internal static async Task <IEnumerable <IError> > ValidateAsync <TRequest>(this IServiceProvider serviceProvider, IExecutionContextReader context, TRequest request, ILogger logger) { var validators = serviceProvider.GetServices <IValidatorAsync <TRequest> >() ?.Where(s => s.IsApplicable(context, request)) ?? Enumerable.Empty <IValidatorAsync <TRequest> >(); foreach (var val in validators) { IEnumerable <IError> errors; #if DEBUG var timer = Timer.Start(logger, val); #endif errors = await val.Validate(context, request); #if DEBUG timer.Completed(); #endif if (errors != null && errors.Any()) { return(errors); } } return(Enumerable.Empty <IError>()); }
internal static async Task <IResponse <TResult> > SendAsync <TRequest, TResult>(IServiceProvider serviceProvider, IExecutionContextReader context, ILogger logger, TRequest request, bool failSafe) { var errors = await serviceProvider.ValidateAsync(context, request, logger); if (errors != null && errors.Any()) { return(Response.Failed <TResult>(errors)); } var handler = serviceProvider.GetServices <IRequestHandlerAsync <TRequest, TResult> >() ?.Where(h => h.IsApplicable(context, request)).FirstOrDefault(); if (handler == null) { if (failSafe) { return(Response.NoHandler <TResult>()); } throw new RequestBusException($"No request handler found that implement IRequestHandlerAsync<{typeof(TRequest).FullName},{typeof(TResult).FullName}>"); } #if DEBUG var timer = Timer.Start(logger, handler); #endif var result = await handler.Handle(context, request); #if DEBUG timer.Completed(); #endif await serviceProvider.FilterAsync(context, request, result, logger); return(result); }
public override bool IsApplicable(IExecutionContextReader context, TestRequest request) { return(context.Get <string>("control").IsSame("ValidationFailed")); }