Created
February 20, 2018 07:03
-
-
Save cjrpriest/73679d8242ed64aba39b6a7c23f04725 to your computer and use it in GitHub Desktop.
AzureFromTheTrenches.Commanding custom CommandDispatcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AzureFromTheTrenches.Commanding.Abstractions; | |
namespace ConsoleApp4 | |
{ | |
public class Command : ICommand | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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