{"id":24360229,"url":"https://github.com/dreamnucleus/commands","last_synced_at":"2025-04-10T07:47:36.759Z","repository":{"id":38151168,"uuid":"67281825","full_name":"dreamnucleus/Commands","owner":"dreamnucleus","description":null,"archived":false,"fork":false,"pushed_at":"2022-12-08T10:55:13.000Z","size":325,"stargazers_count":5,"open_issues_count":7,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-30T11:19:26.212Z","etag":null,"topics":["c-sharp","command-processor","commands","dotnet","dotnet-standard","entity-framework"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dreamnucleus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-03T09:20:56.000Z","updated_at":"2023-06-29T11:40:35.000Z","dependencies_parsed_at":"2023-01-25T10:45:38.146Z","dependency_job_id":null,"html_url":"https://github.com/dreamnucleus/Commands","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreamnucleus%2FCommands","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreamnucleus%2FCommands/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreamnucleus%2FCommands/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreamnucleus%2FCommands/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dreamnucleus","download_url":"https://codeload.github.com/dreamnucleus/Commands/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248181341,"owners_count":21060885,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-sharp","command-processor","commands","dotnet","dotnet-standard","entity-framework"],"created_at":"2025-01-18T21:18:58.249Z","updated_at":"2025-04-10T07:47:36.723Z","avatar_url":"https://github.com/dreamnucleus.png","language":"C#","readme":"[![Build Status](https://mckendry.visualstudio.com/Commands/_apis/build/status/Commands)](https://mckendry.visualstudio.com/Commands/_build/latest?definitionId=8) [![NuGet](https://img.shields.io/nuget/v/DreamNucleus.Commands.svg)](https://www.nuget.org/packages/DreamNucleus.Commands/)\n\n\n# Commands\n\nThe aim of this library to help in writing a business layer for applications. It includes a pipeline and notifcations (specific to a type of command) to wrap incoming commands, outgoing results and exceptions.\n\nThe base components are:\n\n* Command\n* Command Handler\n* Command Processor\n* Result Processor\n* Executing Pipeline\n* Executed Pipeline\n* Exception Pipeline\n* Executing Notification\n* Executed Notification\n* Exception Notification\n\n\nBelow is a simple example of executing the GetBlogCommand and processing the result\n\n```cs\n\nreturn await resultProcessor.For(new GetBlogCommand(blogId))\n    .When(o =\u003e o.NotFound()).Return(r =\u003e new HttpResult(404))\n    .When(o =\u003e o.Success()).Return(r =\u003e new HttpResult(200))\n    .ExecuteAsync();\n    \n```\n\n## NuGet\n\nPackages availble for .NETFramework 4.5 and .NETStandard 1.6\n\nhttps://www.nuget.org/packages/DreamNucleus.Commands/\n\n```\nInstall-Package DreamNucleus.Commands\n```\n```\ndotnet add package DreamNucleus.Commands\n```\n\n\nhttps://www.nuget.org/packages/DreamNucleus.Commands.Extensions/\n\n```\nInstall-Package DreamNucleus.Commands.Extensions\n```\n```\ndotnet add package DreamNucleus.Commands.Extensions\n```\n\n\nhttps://www.nuget.org/packages/DreamNucleus.Commands.Autofac/\n\n```\nInstall-Package DreamNucleus.Commands.Autofac\n```\n```\ndotnet add package DreamNucleus.Commands.Autofac\n```\n\n\nhttps://www.nuget.org/packages/DreamNucleus.Commands.Extensions.Redis/\n\n```\nInstall-Package DreamNucleus.Commands.Extensions.Redis\n```\n```\ndotnet add package DreamNucleus.Commands.Extensions.Redis\n```\n\n# Example\n\n## Get Blog Command\n\n```cs\n\n// the interfaces let us know what will be returned or what exceptions can be thrown\npublic class GetBlogCommand : ISuccessResult\u003cGetBlogCommand, BlogData\u003e, INotFoundResult\n{\n    public int BlogId { get; }\n\n    public GetBlogCommand(int blogId)\n    {\n        BlogId = blogId;\n    }\n}\n\npublic class BlogData\n{\n    public int BlogId { get; set; }\n    public string Url { get; set; }\n}\n\n```\n\n\n## Get Blog Command Handler\n\n```cs\n\npublic class GetBlogCommandHandler : IAsyncCommandHandler\u003cGetBlogCommand, BlogData\u003e\n{\n    private readonly BloggingContext context;\n\n    public GetBlogCommandHandler(BloggingContext context)\n    {\n        this.context = context;\n    }\n\n    public async Task\u003cBlogData\u003e ExecuteAsync(GetBlogCommand command)\n    {\n        var blog = await context.Blogs.SingleOrDefaultAsync(b =\u003e b.BlogId == command.BlogId);\n\n        if (blog == null)\n        {\n            throw new NotFoundException();\n        }\n\n        return new BlogData\n        {\n            BlogId = blog.BlogId,\n            Url = blog.Url\n        };\n    }\n}\n\n```\n\n\n## Audit Log Pipeline\n\n```cs\n// this could be used to write all incoming commands to a audit log\npublic class AuditLogPipeline : Pipeline\n{\n    private readonly BloggingContext context;\n\n    public AuditLogPipeline(BloggingContext context)\n    {\n        this.context = context;\n    }\n    \n    public override Task ExecutingAsync(IAsyncCommand command)\n    {\n        // log the incoming command\n        return base.ExecutingAsync(command);\n    }\n\n    public override async Task ExecutedAsync(IAsyncCommand command, object result)\n    {\n        // log the completed command and result\n        return base.ExecutedAsync(command, result);\n    }\n    \n    public override Task ExceptionAsync(IAsyncCommand command, Exception exception)\n    {\n        // log an exception in the command\n        return base.ExceptionAsync(command, exception);\n    }\n\n}\n```\n\n\n## Executed Notification\n\n```cs\n// this could be used to send an email to the owner... maybe good if you get one visitor a day\npublic class ExecutedNotification : IExecutedNotification\u003cGetBlogCommand, BlogData\u003e\n{\n    public Task OnExecutedAsync(GetBlogCommand command, BlogData result)\n    {\n        // add email request to a queue\n        return Task.FromResult(0);\n    }\n}\n```\n\n\n## Using Command Processor (with Autofac)\n\n```cs\n\nvar containerBuilder = new ContainerBuilder();\n\ncontainerBuilder.RegisterType\u003cBloggingContext\u003e().InstancePerLifetimeScope();\n\n// these can be found and regsiters automatically\ncontainerBuilder.RegisterType\u003cGetBlogCommandHandler\u003e().As\u003cIAsyncCommandHandler\u003cGetBlogCommand, BlogData\u003e\u003e();\n\nvar container = containerBuilder.Build();\n\nvar commandProcessor = new CommandProcessor(new LifetimeScopeService(container.BeginLifetimeScope()));\n\ntry\n{\n    var blog = await commandProcessor.ProcessAsync(new GetBlogCommand(1));\n}\ncatch (Exception)\n{\n    // ignore\n}\n\n```\n\n\n## Using Result Processor (with Autofac)\n\n```cs\n\nvar containerBuilder = new ContainerBuilder();\n\ncontainerBuilder.RegisterType\u003cBloggingContext\u003e().InstancePerLifetimeScope();\n\n// these can be found and regsiters automatically\ncontainerBuilder.RegisterType\u003cGetBlogCommandHandler\u003e().As\u003cIAsyncCommandHandler\u003cGetBlogCommand, BlogData\u003e\u003e();\n\nvar container = containerBuilder.Build();\n\n// these are default handlers, local handlers are looked for first \nvar resultRegister = new ResultRegister\u003cHttpResult\u003e();\nresultRegister.When\u003cNotFoundException\u003e().Return(r =\u003e new HttpResult(404));\n\nvar resultProcessor = new ResultProcessor\u003cHttpResult\u003e(resultRegister.Emit(),\n    new LifetimeScopeService(container.BeginLifetimeScope()));\n\n// exceptions are caught and processed using the handlers\nvar result = await resultProcessor.For(new GetBlogCommand(1))\n    .When(o =\u003e o.NotFound()).Return(r =\u003e new HttpResult(404))\n    .When(o =\u003e o.Success()).Return(r =\u003e new HttpResult(200))\n    .ExecuteAsync();\n\n```\n\n## Using Command Processor (with Autofac and Redis as transport)\n\n```cs\n\nvar containerBuilder = new ContainerBuilder();\n\ncontainerBuilder.RegisterType\u003cBloggingContext\u003e().InstancePerLifetimeScope();\n\n// these can be found and regsiters automatically\ncontainerBuilder.RegisterType\u003cGetBlogCommandHandler\u003e().As\u003cIAsyncCommandHandler\u003cGetBlogCommand, BlogData\u003e\u003e();\n\nvar container = containerBuilder.Build();\n\nvar commandProcessor = new CommandProcessor(new LifetimeScopeService(container.BeginLifetimeScope()));\n\nvar connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(\"localhost\");\nvar redisCommandTransportClient = new RedisCommandTransportClient(connectionMultiplexer, \"commands\", \"results\");\nvar redisCommandTransportServer = new RedisCommandTransportServer(connectionMultiplexer, \"commands\", \"group\", \"consumer\");\n\nvar commandProcessorClient = new CommandProcessorClient(redisCommandTransportClient);\nvar commandProcessorServer = new CommandProcessorServer(commandProcessor, redisCommandTransportServer);\n\nawait commandProcessorServer.StartAsync();\n\ntry\n{\n    // this command will be executed using redis as the transport between the client and server\n    var blog = await commandProcessorClient.ProcessAsync(new GetBlogCommand(1));\n}\ncatch (Exception)\n{\n    // ignore\n}\n\nawait commandProcessorServer.StopAsync();\n\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdreamnucleus%2Fcommands","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdreamnucleus%2Fcommands","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdreamnucleus%2Fcommands/lists"}