From c897bd60c42210a50b88e6f7959b8667b4ef70dc Mon Sep 17 00:00:00 2001 From: Mauro Servienti Date: Sun, 13 Dec 2020 11:34:07 +0100 Subject: [PATCH] Refactoring (#195) * Minor tweaks * TODOs * Minor tweaks * switch to switch :) * no this qualifier --- .../CompositionHandler.cs | 3 ++ .../CompositionOverControllersActionFilter.cs | 5 ++- .../DynamicViewModel.cs | 39 +++++++------------ 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/ServiceComposer.AspNetCore/CompositionHandler.cs b/src/ServiceComposer.AspNetCore/CompositionHandler.cs index c6be8230..6a483984 100644 --- a/src/ServiceComposer.AspNetCore/CompositionHandler.cs +++ b/src/ServiceComposer.AspNetCore/CompositionHandler.cs @@ -105,12 +105,14 @@ await Task.WhenAll(context.RequestServices.GetServices request.SetModel(viewModel); var handlers = handlerTypes.Select(type => context.RequestServices.GetRequiredService(type)).ToArray(); + //TODO: if handlers == none we could shortcut to 404 here foreach (var subscriber in handlers.OfType()) { subscriber.Subscribe(viewModel); } + //TODO: if handlers == none we could shortcut again to 404 here var pending = handlers.OfType() .Select(handler => handler.Handle(request)) .ToList(); @@ -128,6 +130,7 @@ await Task.WhenAll(context.RequestServices.GetServices } catch (Exception ex) { + //TODO: refactor to Task.WhenAll var errorHandlers = handlers.OfType(); foreach (var handler in errorHandlers) { diff --git a/src/ServiceComposer.AspNetCore/CompositionOverControllersActionFilter.cs b/src/ServiceComposer.AspNetCore/CompositionOverControllersActionFilter.cs index f595985b..4d9bfd36 100644 --- a/src/ServiceComposer.AspNetCore/CompositionOverControllersActionFilter.cs +++ b/src/ServiceComposer.AspNetCore/CompositionOverControllersActionFilter.cs @@ -1,4 +1,5 @@ #if NETCOREAPP3_1 || NET5_0 +using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -21,9 +22,9 @@ public CompositionOverControllersActionFilter(CompositionOverControllersRoutes c public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { - var endpoint = context.HttpContext.GetEndpoint() as RouteEndpoint; - if (endpoint != null) + if (context.HttpContext.GetEndpoint() is RouteEndpoint endpoint) { + Debug.Assert(endpoint.RoutePattern.RawText != null, "endpoint.RoutePattern.RawText != null"); var rawTemplate = _compositionOverControllersOptions.UseCaseInsensitiveRouteMatching ? endpoint.RoutePattern.RawText.ToLowerInvariant() : endpoint.RoutePattern.RawText; diff --git a/src/ServiceComposer.AspNetCore/DynamicViewModel.cs b/src/ServiceComposer.AspNetCore/DynamicViewModel.cs index bda58386..cc01327f 100644 --- a/src/ServiceComposer.AspNetCore/DynamicViewModel.cs +++ b/src/ServiceComposer.AspNetCore/DynamicViewModel.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Dynamic; +using System.Linq; using System.Threading.Tasks; namespace ServiceComposer.AspNetCore @@ -18,9 +19,9 @@ class DynamicViewModel : DynamicObject, IPublishCompositionEvents, ICompositionE public DynamicViewModel(string requestId, RouteData routeData, HttpRequest httpRequest) { - this._requestId = requestId; - this._routeData = routeData; - this._httpRequest = httpRequest; + _requestId = requestId; + _routeData = routeData; + _httpRequest = httpRequest; } public void CleanupSubscribers() => _subscriptions.Clear(); @@ -59,19 +60,17 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o { result = null; - if (binder.Name == "RaiseEvent") + switch (binder.Name) { - result = RaiseEventImpl(args[0]); - return true; + case "RaiseEvent": + result = RaiseEventImpl(args[0]); + return true; + case "Merge": + result = MergeImpl((IDictionary) args[0]); + return true; + default: + return false; } - - if (binder.Name == "Merge") - { - result = MergeImpl((IDictionary) args[0]); - return true; - } - - return false; } public override IEnumerable GetDynamicMemberNames() @@ -89,22 +88,14 @@ Task RaiseEventImpl(object @event) { if (_subscriptions.TryGetValue(@event.GetType(), out var handlers)) { - var tasks = new List(); - foreach (var handler in handlers) - { - tasks.Add(handler.Invoke(_requestId, this, @event, _routeData, _httpRequest)); - } + var tasks = handlers.Select(handler => handler.Invoke(_requestId, this, @event, _routeData, _httpRequest)).ToList(); return Task.WhenAll(tasks); } if (_compositionEventsSubscriptions.TryGetValue(@event.GetType(), out var compositionHandlers)) { - var tasks = new List(); - foreach (var handler in compositionHandlers) - { - tasks.Add(handler.Invoke(@event, _httpRequest)); - } + var tasks = compositionHandlers.Select(handler => handler.Invoke(@event, _httpRequest)).ToList(); return Task.WhenAll(tasks); }