Skip to content

Instantly share code, notes, and snippets.

@cjrpriest
Created February 20, 2018 07:03
Show Gist options
  • Save cjrpriest/73679d8242ed64aba39b6a7c23f04725 to your computer and use it in GitHub Desktop.
Save cjrpriest/73679d8242ed64aba39b6a7c23f04725 to your computer and use it in GitHub Desktop.
AzureFromTheTrenches.Commanding custom CommandDispatcher
using AzureFromTheTrenches.Commanding.Abstractions;
namespace ConsoleApp4
{
public class Command : ICommand
{
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AzureFromTheTrenches.Commanding" Version="6.2.0" />
<PackageReference Include="AzureFromTheTrenches.Commanding.MicrosoftDependencyInjection" Version="6.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
</ItemGroup>
</Project>
using System;
using System.Threading;
using System.Threading.Tasks;
using AzureFromTheTrenches.Commanding.Abstractions;
using AzureFromTheTrenches.Commanding.Abstractions.Model;
namespace ConsoleApp4
{
public class CustomCommandDispatcher : ICommandDispatcher
{
public CustomCommandDispatcher(ICommandExecuter commandExecuter)
{
AssociatedExecuter = commandExecuter;
}
public Task<CommandResult<TResult>> DispatchAsync<TResult>(ICommand<TResult> command, CancellationToken cancellationToken = new CancellationToken())
{
throw new NotImplementedException();
}
public Task<CommandResult> DispatchAsync(ICommand command, CancellationToken cancellationToken = new CancellationToken())
{
Console.WriteLine("CustomCommandDispatcher executing");
return Task.FromResult(new CommandResult(true));
}
public ICommandExecuter AssociatedExecuter { get; }
}
}
using System;
using System.Threading;
using System.Threading.Tasks;
using AzureFromTheTrenches.Commanding.Abstractions;
namespace ConsoleApp4
{
public class CustomCommandExecuter : ICommandExecuter
{
public Task<TResult> ExecuteAsync<TResult>(ICommand<TResult> command, CancellationToken cancellationToken = new CancellationToken())
{
Console.WriteLine("CustomCommandExecuter executing");
return Task.FromResult(default(TResult));
}
}
}
using System;
using System.Threading.Tasks;
using AzureFromTheTrenches.Commanding.Abstractions;
namespace ConsoleApp4
{
public class Handler : ICommandHandler<Command>
{
public async Task ExecuteAsync(Command command)
{
Console.WriteLine("Handler executing");
}
}
}
using System;
using AzureFromTheTrenches.Commanding;
using AzureFromTheTrenches.Commanding.Abstractions;
using Microsoft.Extensions.DependencyInjection;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
IServiceProvider serviceProvider = null;
var serviceCollection = new ServiceCollection()
.AddTransient<ICommandExecuter, CustomCommandExecuter>();
new CommandingDependencyResolver(
(type, instance) => serviceCollection.AddSingleton(type, instance),
(type, impl) => serviceCollection.AddTransient(type, impl),
type => serviceProvider.GetService(type)
)
.UseCommanding()
.Register<Handler>(dispatcherFactoryFunc: () => serviceProvider.GetService<CustomCommandDispatcher>());
serviceProvider = serviceCollection.BuildServiceProvider();
serviceProvider.GetService<ICommandDispatcher>().DispatchAsync(new Command());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment