AzureFromTheTrenches.Commanding

2025-12-07 0 230

AzureFromTheTrenches.Commanding

Note that full documentation including guides and an API reference can be found on the help site.

I also have a series on moving from \”onion layer\” architecture to making use of a mediated command pattern in a series of posts on my blog.

Introduction

AzureFromTheTrenches.Commanding is a configuration based asynchronous command mediator framework with a number of key design goals:

  • To provide a highly performant mediator for simple usage
  • To support evolution across a projects lifecycle allowing for easy decomposition from a modular-monolith to a fully distributed micro-service architecture
  • To provide a none-leaking abstraction over command dispatch and execution semantics
  • To reduce boilerplate code – simplistically less code means less errors and less to maintain

To support these goals the framework supports .NET Standard 2.0 (and higher) and so can be used in a wide variety of scenarios and a number of fully optional extension packages are available to enable:

  • Building a REST API directly from commands using a configuration based approach
  • Dispatching commands to queues (Service Bus Queues and Topics, and Azure Storage)
  • Dispatching commands to event hubs
  • Using queues as a source for executing commands
  • Caching commands based on signatures in local memory caches or Redis caches

You don\’t need to take advantage of that functionality but you can, if you want, adopt it over time without changing your core code.

Getting Started

Firstly install the nuget package for commanding:

Install-Package AzureFromTheTrenches.Commanding

As an example let\’s create a command that adds two numbers together and returns a result:

public class MathResult
{
    public int Value { get; set; }
}

public class AddCommand : ICommand<MathResult>
{
    public int FirstNumber { get; set; }

    public int SecondNumber { get; set; }
}

Commands are acted on by handlers and our add handler looks like this:

public AddCommandHandler : ICommandHandler<AddCommand, MathResult>
{
    public Task<MathResult> ExecuteAsync(AddCommand command, MathResult previousResult)
    {
        return new MathResult {
            Value = command.FirstNumber + command.SecondNumber
        };
    }
}

Having defined our command, result and handler, we need to register these with the commanding system. If you\’re just writing a console app you can do this in Program.cs but for more realistic usage you\’d do this where you configure your IoC container – it\’s handy to think of command registrations as just another part of your applications configuration, besides which you\’ll need access to the container. The example below demonstrates registration with the Microsoft ASP.Net Core Service Provider:

// First register the commanding framework with the IoC container
IServiceProvider serviceProvider = null;
IServiceCollection serviceCollection = new ServiceCollection();
CommandingDependencyResolverAdapter adapter = new CommandingDependencyResolverAdapter(
    (fromType, toInstance) => services.AddSingleton(fromType, toInstance),
    (fromType, toType) => services.AddTransient(fromType, toType),
    (resolveTo) => _serviceProvider.GetService(resolveTo));
ICommandRegistry registry = adapter.AddCommanding();
serviceProvider = serviceCollection.BuildServiceProvider();

// Now register our handler
registry.Register<AddCommandHandler>();

The CommandingDependencyResolverAdapter class is an adapter that allows the framework to be registed with any IoC container and the AddCommanding method registers the injectable commaning interfaces and returns an ICommandRegistry interface that allows you to register handlers – you only need to register a handler, the framework will figure out the rest and registration uses a fluent API style for concise readable code.

To dispatch our command we need to get hold of the ICommandDispatcher interface and send the command. We\’ll do that and output the result to the console:

ICommandDispatcher commandDispatcher = dependencyResolver.ServiceProvider.GetService<ICommandDispatcher>();
MathResult mathResult = await commandDispatcher.DispatchAsync(new AddCommand { FirstNumber = 5, SecondNumber = 6});
Console.WriteLine(mathResult.Value); // hopefully says 11

And for simple usage that\’s it. This example is a bit contrived as we\’re resolving dependencies by hand and theres a lot of boilerplate to add two numbers together but in real world scenarios all you really need to do is register your command handlers in the appropriate place, for example if you\’re using ASP.Net Core then all the dependency injection boilerplate is in place.

Samples

  1. Simple in memory command execution
    https://**github.com*/JamesRandall/AzureFromTheTrenches.Commanding/tree/master/Samples/InMemoryCommanding

  2. Dispatching commands over HTTP
    https://g*i*t*hub.com/JamesRandall/AzureFromTheTrenches.Commanding/tree/master/Samples/HttpCommanding.Client

  3. Executing commands in response to HTTP requests (ASP.Net Core)
    https://gith**ub.*com/JamesRandall/AzureFromTheTrenches.Commanding/tree/master/Samples/HttpCommanding.Web

  4. Dispatching to and executing from Azure Storage queues
    https://github.*c*om*/JamesRandall/AzureFromTheTrenches.Commanding/tree/master/Samples/AzureStorageQueueCommanding

  5. Azure storage auditing
    https://gith*ub.*c*om/JamesRandall/AzureFromTheTrenches.Commanding/tree/master/Samples/AzureStorageAuditing

  6. Azure event hub auditing
    https://git*hu*b*.com/JamesRandall/AzureFromTheTrenches.Commanding/tree/master/Samples/AzureEventHubAuditing

Advanced Usage

Further usage scenarios can be found in the wiki including:

  • Configuration Options
  • HTTP Execution and Dispatch
  • Azure Queue Dispatch and Execution
  • Auditing and Event Sourcing

Support

If you get stuck log a GitHub issue or catch me over on Twitter at @azuretrenches and I\’ll help if I can.

下载源码

通过命令行克隆项目:

git clone https://github.com/JamesRandall/AzureFromTheTrenches.Commanding.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 开发教程 AzureFromTheTrenches.Commanding https://www.zuozi.net/31399.html

dingpiaoxitong
上一篇: dingpiaoxitong
httpout
下一篇: httpout
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务