GithubHelp home page GithubHelp logo

bubdm / commandrouter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mrsmoke/commandrouter

0.0 0.0 0.0 36 KB

Simple package to help route certain commands to functions, similar to routing in ASP.

C# 100.00%

commandrouter's Introduction

Command Router

Build status NuGet

Simple package to help route certain commands to functions, similar to routing in ASP.

Installation

PM> Install-Package CommandRouter

Or for integration with MVC

PM> Install-Package CommandRouter.Integration.AspNetCore

Usage

Setup

Add services.AddCommandRouter() to your Startup.cs.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddCommandRouter();
}

Next create a command. All commands must extend the Command class.

public class TestCommand : Command
{
    [Command("hello")]
    public ICommandResult Test()
    {
        return StringResult("Hello world!");
    }
}

All commands must be decorated with the Command attribute and must also return either ICommandResult or void (async is also supported).

To run a command, call RunAsync on ICommandRunner

var result = await _commandRunner.RunAsync(command);

ICommandResult

The command result is similar to the IActionResult. It has only one method on it

Task ExecuteAsync(Stream resultStream)

resultStream will be written to when this function is called.

Context items

Request level context items can also be passed in the second parameter of RunAsync

await _commandRunner.RunAsync(command, new Dictionary<string, object>
{
    {"content", "Hello world!" }
});

These will be available from within the Command

[Command("hello")]
public ICommandResult Test()
{
    return StringResult(Context.Items["content"].ToString());
}

Parameters

By default, parameters are tokenized based on space. Eg command parameter. Parameters are passed based on the order in the Command action.

The below will return The id is: 88 for the command hello 88

[Command("hello")]
public ICommandResult Test(int id)
{
    return StringResult("The id is: " + id);
}

Custom parameter converters

You can add your own parameter converters to support more complex models by implementing IPropertyConverter

public class JsonConverter : IPropertyConverter
{
    public bool CanConvert(Type propertyType, object value)
    {
        throw new NotImplementedException();
    }

    public object Convert(Type propertyType, object value)
    {
        throw new NotImplementedException();
    }
}

Additional converters can be added to your CommandRouter in the Startup.cs file

services.AddCommandRouter(options =>
{
    options.PropertyConverters.Add(new JsonConverter());
});

Custom object activation

If you are using some form of custom DI which doesn't add services into the default IServiceProvider, you can implement your own ICommandActivator and register it in the default IServiceProvider.

public class SimpleInjectorCommandActivator : ICommandActivator
{
    private readonly Container _container;

    public SimpleInjectorCommandActivator(Container container)
    {
        _container = container;
    }

    public object Create(Type type)
    {
        return _container.GetInstance(type);
    }
}

Examples

MVC

[Route("{command}")]
public async Task<IActionResult> Index(string command)
{
    var result = await _commandRunner.RunAsync(command, new Dictionary<string, object>
    {
        {"content", "Hello world!" }
    });

    return new CommandRouterResult(result);
}

commandrouter's People

Contributors

mrsmoke avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.