CommandQuery.GoogleCloudFunctions
4.0.0
dotnet add package CommandQuery.GoogleCloudFunctions --version 4.0.0
NuGet\Install-Package CommandQuery.GoogleCloudFunctions -Version 4.0.0
<PackageReference Include="CommandQuery.GoogleCloudFunctions" Version="4.0.0" />
paket add CommandQuery.GoogleCloudFunctions --version 4.0.0
#r "nuget: CommandQuery.GoogleCloudFunctions, 4.0.0"
// Install CommandQuery.GoogleCloudFunctions as a Cake Addin #addin nuget:?package=CommandQuery.GoogleCloudFunctions&version=4.0.0 // Install CommandQuery.GoogleCloudFunctions as a Cake Tool #tool nuget:?package=CommandQuery.GoogleCloudFunctions&version=4.0.0
CommandQuery.GoogleCloudFunctions ⚡
Command Query Separation for Google Cloud Functions
- Provides generic function support for commands and queries with HTTP functions
- Enables APIs based on HTTP
POST
andGET
Get Started
- Install Google.Cloud.Functions.Templates
- Create a new gcf-http project
- Install the
CommandQuery.GoogleCloudFunctions
package from NuGetPM>
Install-Package CommandQuery.GoogleCloudFunctions
- Create functions
- Preferably named
Command
andQuery
- Preferably named
- Create commands and command handlers
- Implement
ICommand
andICommandHandler<in TCommand>
- Or
ICommand<TResult>
andICommandHandler<in TCommand, TResult>
- Implement
- Create queries and query handlers
- Implement
IQuery<TResult>
andIQueryHandler<in TQuery, TResult>
- Implement
- Configure services in
Startup.cs
Commands
using CommandQuery.GoogleCloudFunctions;
using Google.Cloud.Functions.Framework;
using Google.Cloud.Functions.Hosting;
using Microsoft.AspNetCore.Http;
namespace CommandQuery.Sample.GoogleCloudFunctions
{
[FunctionsStartup(typeof(Startup))]
public class Command(ICommandFunction commandFunction) : IHttpFunction
{
public async Task HandleAsync(HttpContext context)
{
var commandName = context.Request.Path.Value!.Substring("/api/command/".Length);
await commandFunction.HandleAsync(commandName, context, context.RequestAborted);
}
}
}
- The function is requested via HTTP
POST
with the Content-Typeapplication/json
in the header. - The name of the command is the slug of the URL.
- The command itself is provided as JSON in the body.
- If the command succeeds; the response is empty with the HTTP status code
200
. - If the command fails; the response is an error message with the HTTP status code
400
or500
.
Commands with result:
- If the command succeeds; the response is the result as JSON with the HTTP status code
200
.
Queries
using CommandQuery.GoogleCloudFunctions;
using Google.Cloud.Functions.Framework;
using Google.Cloud.Functions.Hosting;
using Microsoft.AspNetCore.Http;
namespace CommandQuery.Sample.GoogleCloudFunctions
{
[FunctionsStartup(typeof(Startup))]
public class Query(IQueryFunction queryFunction) : IHttpFunction
{
public async Task HandleAsync(HttpContext context)
{
var queryName = context.Request.Path.Value!.Substring("/api/query/".Length);
await queryFunction.HandleAsync(queryName, context, context.RequestAborted);
}
}
}
- The function is requested via:
- HTTP
POST
with the Content-Typeapplication/json
in the header and the query itself as JSON in the body - HTTP
GET
and the query itself as query string parameters in the URL
- HTTP
- The name of the query is the slug of the URL.
- If the query succeeds; the response is the result as JSON with the HTTP status code
200
. - If the query fails; the response is an error message with the HTTP status code
400
or500
.
Configuration
Configuration in Startup.cs
:
using CommandQuery.GoogleCloudFunctions;
using CommandQuery.Sample.Contracts.Commands;
using CommandQuery.Sample.Contracts.Queries;
using CommandQuery.Sample.Handlers;
using CommandQuery.Sample.Handlers.Commands;
using CommandQuery.Sample.Handlers.Queries;
using Google.Cloud.Functions.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace CommandQuery.Sample.GoogleCloudFunctions
{
public class Startup : FunctionsStartup
{
public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) =>
services
//.AddSingleton(new JsonSerializerOptions(JsonSerializerDefaults.Web))
// Add commands and queries
.AddCommandFunction(typeof(FooCommandHandler).Assembly, typeof(FooCommand).Assembly)
.AddQueryFunction(typeof(BarQueryHandler).Assembly, typeof(BarQuery).Assembly)
// Add handler dependencies
.AddTransient<IDateTimeProxy, DateTimeProxy>()
.AddTransient<ICultureService, CultureService>();
public override void Configure(WebHostBuilderContext context, IApplicationBuilder app)
{
// Validation
app.ApplicationServices.GetService<ICommandProcessor>()!.AssertConfigurationIsValid();
app.ApplicationServices.GetService<IQueryProcessor>()!.AssertConfigurationIsValid();
}
}
}
The extension methods AddCommandFunction
and AddQueryFunction
will add functions and all command/query handlers in the given assemblies to the IoC container.
You can pass in a params
array of Assembly
arguments if your handlers are located in different projects.
If you only have one project you can use typeof(Startup).Assembly
as a single argument.
Testing
You can integration test your functions with the Google.Cloud.Functions.Testing package.
using System.Net;
using System.Net.Http.Json;
using CommandQuery.Sample.Contracts.Commands;
using FluentAssertions;
using Google.Cloud.Functions.Testing;
using NUnit.Framework;
namespace CommandQuery.Sample.GoogleCloudFunctions.Tests
{
public class CommandTests
{
[SetUp]
public void SetUp()
{
Server = new FunctionTestServer<Command>();
Client = Server.CreateClient();
}
[TearDown]
public void TearDown()
{
Client.Dispose();
Server.Dispose();
}
[Test]
public async Task should_handle_command()
{
var response = await Client.PostAsJsonAsync("/api/command/FooCommand", new FooCommand { Value = "Foo" });
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
[Test]
public async Task should_handle_errors()
{
var response = await Client.PostAsJsonAsync("/api/command/FooCommand", new FooCommand());
await response.ShouldBeErrorAsync("Value cannot be null or empty");
}
FunctionTestServer<Command> Server = null!;
HttpClient Client = null!;
}
}
Samples
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- CommandQuery (>= 4.0.0)
- CommandQuery.SystemTextJson (>= 4.0.0)
- Microsoft.AspNetCore.Http (>= 2.1.34)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on CommandQuery.GoogleCloudFunctions:
Repository | Stars |
---|---|
hlaueriksson/CommandQuery
Command Query Separation for 🌐ASP.NET Core ⚡AWS Lambda ⚡Azure Functions ⚡Google Cloud Functions
|
- Change Microsoft.AspNetCore.Http to 2.1.34
- Bump to Microsoft.Extensions.Logging.Abstractions 6.0.4
- Remove ILogger parameter from HandleAsync