Skip to content

Instantly share code, notes, and snippets.

@cjrpriest
Created February 15, 2018 21:45
Show Gist options
  • Save cjrpriest/cc0642e6f4b3bdfa9680d836687358be to your computer and use it in GitHub Desktop.
Save cjrpriest/cc0642e6f4b3bdfa9680d836687358be to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using AzureFromTheTrenches.Commanding.Abstractions;
using AzureFromTheTrenches.Commanding.Abstractions.Model;
using Microsoft.Extensions.DependencyInjection;
namespace AsyncCommandDispatcher
{
// This gets registered somehow as the default command dispatcher
public class SpecialCommandDispatcher : ICommandDispatcher
{
private readonly ICommandDispatcher _commandDispatcher;
private readonly IAwsLamdbaExecuter _awsLamdbaExecuter;
public AsyncCommandDispatcher(
ICommandDispatcher commandDispatcher, // the passed in ICommandDispatcher is the 'normal' command dispatcher
IAwsLamdbaExecuter awsLamdbaExecuter
)
{
_commandDispatcher = commandDispatcher;
_awsLamdbaExecuter = awsLamdbaExecuter;
}
public Task<CommandResult<TResult>> DispatchAsync<TResult>(ICommand<TResult> command, CancellationToken cancellationToken = new CancellationToken())
{
throw new NotImplementedException();
}
public async Task<CommandResult> DispatchAsync(ICommand command, CancellationToken cancellationToken = new CancellationToken())
{
if (Context == Context.LocallyRunning)
{
await _commandDispatcher.DispatchAsync(command, cancellationToken);
}
else if (Context == Context.RunningInAwsLambda)
{
await _awsLamdbaExecuter.DispatchAsync(command, cancellationToken);
}
throw new NotImplementedException();
}
// I don't know what this is for
public ICommandExecuter AssociatedExecuter { get; }
}
class FunctionACommand : ICommand { }
class FunctionBCommand : ICommand { }
internal class FunctionAHandler : ICommandHandler<FunctionACommand>
{
private readonly FunctionA _functionA;
public FunctionAHandler(FunctionA functionA)
{
_functionA = functionA;
}
public async Task ExecuteAsync(FunctionACommand command)
{
await _functionA.Invoke();
}
}
internal class FunctionBHandler : ICommandHandler<FunctionBCommand>
{
private readonly FunctionB _functionB;
public FunctionBHandler(FunctionB functionB)
{
_functionB = functionB;
}
public async Task ExecuteAsync(FunctionBCommand command)
{
await _functionB.Invoke();
}
}
// This lambda is triggered by a CloudWatch Event
public class LambdaFunctionA
{
private readonly IServiceProvider _serviceProvider;
// Lambda constructires need to be parameterless
public LambdaFunctionA()
{
// haven't yet figured out how this will be possible
_serviceProvider = MagicallyBuildServiceProvider();
}
// Called by Lambda
public async Task Invoke(ILambdaContext lambdaContext)
{
await _serviceProvider.GetService<FunctionA>().Invoke();
}
}
// This lambda is only triggered by FunctionA
public class LambdaFunctionB
{
private readonly IServiceProvider _serviceProvider;
// Lambda constructires need to be parameterless
public LambdaFunctionB()
{
// haven't yet figured out how this will be possible
_serviceProvider = MagicallyBuildServiceProvider();
}
// Called by Lambda
public async Task Invoke(ILambdaContext lambdaContext)
{
await _serviceProvider.GetService<FunctionB>().Invoke();
}
}
class FunctionA
{
private readonly ICommandDispatcher _commandDispatcher;
FunctionA(ICommandDispatcher commandDispatcher)
{
_commandDispatcher = commandDispatcher;
}
public async Task Invoke()
{
await _commandDispatcher.DispatchAsync(new FunctionBCommand());
await DoSomeLongRunningCoolStuff();
}
}
class FunctionB
{
public async Task Invoke()
{
await DoSomeInterestingStuffInParallelWithTheLongRunningCoolStuff();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment