Code Monkey home page Code Monkey logo

state-machine-1's Introduction

Jason State

Jason Statham

Jason State is a simple state machine implementation. It's configured by a JSON file.

Supported Platforms

Features

  • Dependency injection friendly (can also be used standalone, see below)

Table of Contents

  1. Why Jason State?
  2. Installation
  3. Usage
  4. License

Why Jason State?

  • No need to worry about implementing State Pattern. With Jason State, it is already implemented!
  • Because you use state pattern, your code is cleaner.
  • Let the business need change! It'll give you the flexibility to change the flow just by the JSON file you provide. No need for deployment!
  • Supports sync and async operations!

Installation

Package
JasonState NuGet
JasonState.Extension NuGet

Following commands can be used to install JasonState and JasonState.Extension, run the following command in the Package Manager Console

dotnet add package JasonState
dotnet add package JasonState.Extension

Or use dotnet cli

dotnet add package JasonState
dotnet add package JasonState.Extension

Usage

Json

First you need to provide a valid JSON file. This JSON file must contain a States array. This array should have BaseState objects.

  • Namespace: namespace of your state
  • Name: Just the name of your state file
  • NextState: Next State array contains Next State objects
    • Condition: The condition for the states execution.
    • State: State is next state's name. No need to provide namespace
  • ErrorState: Name of your state's error state. This state will be executed only if the current state has an exception that you don't handle. Error state can be different for each state.

An example of a valid JSON file can be found throug here

{
  "States": [
    {
      "Namespace": "TestClient.Impls.States",
      "Name": "InitialState",
      "NextState": [
        {
          "Condition": "!string.IsNullOrEmpty(FromEmail) && FromEmail.Equals(\"[email protected]\")",
          "State": "ValidatePaymentState"
        },
        {
          "Condition": "!string.IsNullOrEmpty(FromEmail) && FromEmail.Equals(\"[email protected]\")",
          "State": "FinalState"
        }
      ],
      "ErrorState": "ErrorState"
    },
    {
      "Namespace": "TestClient.Impls.States",
      "Name": "ErrorState",
      "NextState": [
        {
          "Condition": "true",
          "State": "FinalState"
        }
      ],
      "ErrorState": null
    },
    {
      "Namespace": "TestClient.Impls.States",
      "Name": "FinalState",
      "NextState": null,
      "ErrorState": null
    }
  ]
}

State Implementation

States must inherit from BaseState and implement Execute, or ExecuteAsync, method with your state context. You can use any dependency injection framework for construction injections. It will not break anything.

public class InitialState : BaseState<TestStateContext>
{
    public override void Execute(TestStateContext context)
    {
      // do the magic
    }
}

State Context

Jason State allows you to add any kind of object to the context. Everything you need during the state execution should be in the context.

public class TestStateContext
{
    public long CreditCardNumber { get; set; }

    public string CardHolderName { get; set; }

    public decimal Amount { get; set; }
}

public class InitialState : BaseState<TestStateContext>
{
    public override void Execute(TestStateContext context)
    {
        context.CreditCardNumber = "4545454545454545";
    }
}

Microsoft.Extensions.DependencyInjection Initialization

By referencing JasonState.Extension, register necessary dependencies to ServiceCollection as follows

serviceCollection.AddJasonState<TestStateContext>();

or

serviceCollection.AddAsyncJasonState<TestStateContext>();

Samples

TestClient can be found here
AsyncTestClient can be found here

License

Licensed under MIT, see LICENSE for the full text.

state-machine-1's People

Contributors

akselarzuman 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.