Skip to content

Commit

Permalink
Refactoring (#195)
Browse files Browse the repository at this point in the history
* Minor tweaks

* TODOs

* Minor tweaks

* switch to switch :)

* no this qualifier
  • Loading branch information
mauroservienti authored Dec 13, 2020
1 parent af242c6 commit c897bd6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/ServiceComposer.AspNetCore/CompositionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ await Task.WhenAll(context.RequestServices.GetServices<IViewModelPreviewHandler>
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<ICompositionEventsSubscriber>())
{
subscriber.Subscribe(viewModel);
}

//TODO: if handlers == none we could shortcut again to 404 here
var pending = handlers.OfType<ICompositionRequestsHandler>()
.Select(handler => handler.Handle(request))
.ToList();
Expand All @@ -128,6 +130,7 @@ await Task.WhenAll(context.RequestServices.GetServices<IViewModelPreviewHandler>
}
catch (Exception ex)
{
//TODO: refactor to Task.WhenAll
var errorHandlers = handlers.OfType<ICompositionErrorsHandler>();
foreach (var handler in errorHandlers)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if NETCOREAPP3_1 || NET5_0
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
Expand All @@ -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;
Expand Down
39 changes: 15 additions & 24 deletions src/ServiceComposer.AspNetCore/DynamicViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;

namespace ServiceComposer.AspNetCore
Expand All @@ -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();
Expand Down Expand Up @@ -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<string, object>) args[0]);
return true;
default:
return false;
}

if (binder.Name == "Merge")
{
result = MergeImpl((IDictionary<string, object>) args[0]);
return true;
}

return false;
}

public override IEnumerable<string> GetDynamicMemberNames()
Expand All @@ -89,22 +88,14 @@ Task RaiseEventImpl(object @event)
{
if (_subscriptions.TryGetValue(@event.GetType(), out var handlers))
{
var tasks = new List<Task>();
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<Task>();
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);
}
Expand Down

0 comments on commit c897bd6

Please sign in to comment.