Skip to content

Commit

Permalink
#146 - Using ObjectSender in CQRS assemblies.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Feb 15, 2019
1 parent cb9cfcf commit f91a02e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 63 deletions.
4 changes: 3 additions & 1 deletion Neptuo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ Global
{8EC49C59-5F97-4877-95CA-F1BB2254F332}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8EC49C59-5F97-4877-95CA-F1BB2254F332}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EC49C59-5F97-4877-95CA-F1BB2254F332}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EC49C59-5F97-4877-95CA-F1BB2254F332}.Release|Any CPU.Build.0 = Release|Any CPU
{57E49676-4746-41FD-B439-B4F41F8AE837}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57E49676-4746-41FD-B439-B4F41F8AE837}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57E49676-4746-41FD-B439-B4F41F8AE837}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -264,6 +265,7 @@ Global
{980DAF38-FF5E-40CB-9B34-1888EACC0E3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{980DAF38-FF5E-40CB-9B34-1888EACC0E3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{980DAF38-FF5E-40CB-9B34-1888EACC0E3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{980DAF38-FF5E-40CB-9B34-1888EACC0E3C}.Release|Any CPU.Build.0 = Release|Any CPU
{EDA4C50E-2873-4237-A84B-F73A15C27CD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EDA4C50E-2873-4237-A84B-F73A15C27CD6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDA4C50E-2873-4237-A84B-F73A15C27CD6}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -441,7 +443,7 @@ Global
{F9377571-8686-4079-8074-DBF0C73B03BC} = {A7CE8ABF-9770-49F4-8969-8AB6957A87E6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C551EE44-C191-455D-BEFA-6366FEC51A7D}
EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.2\lib\NET35
SolutionGuid = {C551EE44-C191-455D-BEFA-6366FEC51A7D}
EndGlobalSection
EndGlobal
12 changes: 6 additions & 6 deletions src/Neptuo.Commands.Http/Handlers/HttpCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
namespace Neptuo.Commands.Handlers
{
/// <summary>
/// Implementation of <see cref="ICommandHandler{TCommand}"/> that transfers commands over HTTP.
/// An implementation of <see cref="ICommandHandler{TCommand}"/> that transfers commands over HTTP.
/// </summary>
/// <typeparam name="TCommand">Type of command.</typeparam>
/// <typeparam name="TCommand">A type of the command.</typeparam>
public class HttpCommandHandler<TCommand> : ICommandHandler<TCommand>
{
private readonly HttpCommandDispatcher dispatcher;

/// <summary>
/// Creates new instance that routes commands of type <typeparamref name="TCommand"/> to the <paramref name="route"/>.
/// Creates new instance that sends commands of type <typeparamref name="TCommand"/> throught <paramref name="objectSender"/>.
/// </summary>
/// <param name="route">Route definition.</param>
public HttpCommandHandler(RouteDefinition route)
/// <param name="objectSender">An object sender.</param>
public HttpCommandHandler(ObjectSender objectSender)
{
dispatcher = new HttpCommandDispatcher(new DefaultRouteTable().Add(typeof(TCommand), route));
dispatcher = new HttpCommandDispatcher(objectSender);
}

public Task HandleAsync(TCommand command) => dispatcher.HandleAsync(command);
Expand Down
35 changes: 9 additions & 26 deletions src/Neptuo.Commands.Http/HttpCommandDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,26 @@
namespace Neptuo.Commands
{
/// <summary>
/// Implementation of <see cref="ICommandDispatcher"/> that transfers commands over HTTP.
/// An implementation of <see cref="ICommandDispatcher"/> that transfers commands over HTTP.
/// </summary>
public class HttpCommandDispatcher : ICommandDispatcher
{
private readonly IRouteTable routeTable;
private readonly HttpClientAdapter httpAdapter;
private readonly ObjectSender objectSender;

/// <summary>
/// Creates new instance with absolute URLs defined in <paramref name="routeTable"/>.
/// Creates a new instance which sends objects using <paramref name="objectSender"/>.
/// </summary>
/// <param name="routeTable">Route table with absolute URLs.</param>
public HttpCommandDispatcher(IRouteTable routeTable)
/// <param name="objectSender">An object sender.</param>
public HttpCommandDispatcher(ObjectSender objectSender)
{
Ensure.NotNull(routeTable, "routeTable");
this.routeTable = routeTable;
this.httpAdapter = new HttpClientAdapter(routeTable);
Ensure.NotNull(objectSender, "objectSender");
this.objectSender = objectSender;
}

public async Task HandleAsync<TCommand>(TCommand command)
public Task HandleAsync<TCommand>(TCommand command)
{
Ensure.NotNull(command, "command");
Type commandType = command.GetType();
RouteDefinition route;
if (routeTable.TryGet(commandType, out route))
{
using (HttpClient httpClient = httpAdapter.PrepareHttpClient(route))
{
// Prepare content and send request.
ObjectContent objectContent = httpAdapter.PrepareObjectContent(command, route);
HttpResponseMessage response = await httpAdapter.Execute(httpClient, objectContent, route);

//TODO: Process HTTP status errors.
return;
}
}

throw Ensure.Exception.InvalidOperation("Unnable to preces command without registered URL route, command type is '{0}'.", commandType.FullName);
return objectSender.SendAsync(command, default);
}
}
}
10 changes: 6 additions & 4 deletions src/Neptuo.Queries.Http/Handlers/HttpQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Neptuo.Net.Http.Routing;
using Neptuo;
using Neptuo.Net.Http.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -21,10 +22,11 @@ public class HttpQueryHandler<TQuery, TResult> : IQueryHandler<TQuery, TResult>
/// <summary>
/// Creates new instance that routes queries of type <typeparamref name="TQuery"/> to the <paramref name="route"/>.
/// </summary>
/// <param name="route">Route definition.</param>
public HttpQueryHandler(RouteDefinition route)
/// <param name="objectSender">An object sender.</param>
public HttpQueryHandler(ObjectSender objectSender)
{
dispatcher = new HttpQueryDispatcher(new DefaultRouteTable().Add(typeof(TQuery), route));
Ensure.NotNull(objectSender, "objectSender");
dispatcher = new HttpQueryDispatcher(objectSender);
}

public Task<TResult> HandleAsync(TQuery query, CancellationToken cancellationToken) => dispatcher.QueryAsync<TQuery, TResult>(query, cancellationToken);
Expand Down
34 changes: 8 additions & 26 deletions src/Neptuo.Queries.Http/HttpQueryDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,27 @@
namespace Neptuo.Queries
{
/// <summary>
/// Implementation of <see cref="IQueryDispatcher"/> that transfers queries over HTTP.
/// An implementation of <see cref="IQueryDispatcher"/> that transfers queries over HTTP.
/// </summary>
public class HttpQueryDispatcher : IQueryDispatcher
{
private readonly IRouteTable routeTable;
private readonly HttpClientAdapter httpAdapter;
private readonly ObjectSender objectSender;

/// <summary>
/// Creates new instance with absolute URLs defined in <paramref name="routeTable"/>.
/// Creates a new instance which sends objects using <paramref name="objectSender"/>.
/// </summary>
/// <param name="routeTable">Route table with absolute URLs.</param>
public HttpQueryDispatcher(IRouteTable routeTable)
/// <param name="objectSender">An object sender.</param>
public HttpQueryDispatcher(ObjectSender objectSender)
{
Ensure.NotNull(routeTable, "routeTable");
this.routeTable = routeTable;
this.httpAdapter = new HttpClientAdapter(routeTable);
Ensure.NotNull(objectSender, "objectSender");
this.objectSender = objectSender;
}

public async Task<TResult> QueryAsync<TQuery, TResult>(TQuery query, CancellationToken cancellationToken = default)
where TQuery : IQuery<TResult>
{
Ensure.NotNull(query, "query");
Type queryType = query.GetType();
RouteDefinition route;
if (routeTable.TryGet(queryType, out route))
{
using (HttpClient httpClient = httpAdapter.PrepareHttpClient(route))
{
// Prepare content and send request.
ObjectContent objectContent = httpAdapter.PrepareObjectContent(query, route);
HttpResponseMessage response = await httpAdapter.Execute(httpClient, objectContent, route);

// Parse output.
TResult output = await response.Content.ReadAsAsync<TOutput>();
return output;
}
}

throw Ensure.Exception.InvalidOperation("Unnable to preces query without registered URL route, query type is '{0}'.", queryType.FullName);
return (TResult) await objectSender.SendAsync(query, cancellationToken);
}
}
}

0 comments on commit f91a02e

Please sign in to comment.