{"id":15412025,"url":"https://github.com/bardin08/statemachine","last_synced_at":"2025-07-22T21:36:02.993Z","repository":{"id":104509920,"uuid":"344614386","full_name":"Bardin08/StateMachine","owner":"Bardin08","description":".NET state machine implementation","archived":false,"fork":false,"pushed_at":"2021-07-09T09:15:49.000Z","size":112,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T03:25:25.227Z","etag":null,"topics":["state","state-machine","states"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Bardin08.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-04T21:29:10.000Z","updated_at":"2021-07-09T09:15:52.000Z","dependencies_parsed_at":"2023-07-17T14:40:09.798Z","dependency_job_id":null,"html_url":"https://github.com/Bardin08/StateMachine","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Bardin08/StateMachine","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bardin08%2FStateMachine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bardin08%2FStateMachine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bardin08%2FStateMachine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bardin08%2FStateMachine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bardin08","download_url":"https://codeload.github.com/Bardin08/StateMachine/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bardin08%2FStateMachine/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266578680,"owners_count":23951150,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["state","state-machine","states"],"created_at":"2024-10-01T16:50:54.004Z","updated_at":"2025-07-22T21:36:02.968Z","avatar_url":"https://github.com/Bardin08.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# StateMachine\n.NET state-machine implementation. \n\n## What is a state-machine\nA state-machine (SM) is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number of states at any given time. \nThe SM can change from one state to another in response to some inputs; the change from one state to another is called a transition.\n\n## Installation:\nAdd StateMachine.dll to your project's references and include StateMachine namespace.\n\n## Usage Example\n\nTo use a state machine follow 3 simple steps:\n1. Create a state-machine\n2. Create states and transitions base types\n3. Add some states and transitions\n\n### State-Machine creation\nTo create a state-machine you need to inherit the`StateMachine.StateMachine` class or to implement a `StateMachine.Abstractions.IStateMachine` interface.\n`StateMachine.StateMachine` is a default implementation of this interface. Class StateMachine is abstract. It has several protected constructors, so you need \nto add at least one public constructor which will call a base one. \nAlso, it’s possible to add some properties and methods to the state-machine.\n\n```CSharp\npublic sealed class RegistrationStateMachine : StateMachine\u003cITransactionState, Models.RegistrationTransactionModel\u003e\n{\n    public RegistrationStateMachine() \n        : base(new Transactions.RegistrateTransaction.RegistrationBeginState())\n    {\n    }\n\n    public void LogMachineState(string transitionName)\n    {\n        Console.ForegroundColor = ConsoleColor.Green;\n        Console.WriteLine($\"{ this.GetType().Name } [ { CurrentState.GetType().Name } | { transitionName } ]: \");\n        Console.ResetColor();\n    }\n}\n```\nHere `ITransactionState` is a state base type. It will be used to find all states in assembly. `Models.RegistrationTransactionModel`\nis an input that every state will receive.\n\n\n### States and transitions base types creation\nStates base types must implement `StateMachine.Abstractions.IState` interface.\n```CSharp\npublic interface ITransactionState : StateMachine.Abstractions.IState\n{\n}\n```\n\nTransition base states must implement `StateMachine.Abstractions.ITransition\u003cTStateMachine, TState, TInputState, TInput, TOutoutState\u003e` interface.\nTInputState and TOutputState must implement or inherit a TState. \n```CSharp\npublic interface ITransactionTransition\u003cTInputState, TOutputState\u003e \n    : StateMachine.Abstractions.ITransition\u003cRegistrationStateMachine,\n                                            ITransactionState,\n                                            TInputState,\n                                            RegistrationTransactionModel,\n                                            TOutputState\u003e\n    where TInputState : ITransactionState\n    where TOutputState : ITransactionState\n{\n}\n```\n\n\n### States and transitions creation\nHere would be shown how to create a state and a transition. For a full example visit an example folder.\n\nEach state must implement a base type which defined for states for a current state-machine. All states can contain additional methods and properties which could be used while\ntransiting.\n```CSharp\npublic sealed class RegistrationBeginState : ITransactionState\n{\n    public void PrintStateMessage() \n    {\n        System.Console.WriteLine(\"Welcome to this wonderful application. To begin using this application you should register.\");\n    }\n}\n```\nTransition must implement a base type which defined for transitions for a current state-machine.\n```CSharp\npublic sealed class BeginToConfirmRegistrationTransition\n    : ITransactionTransition\u003cRegistrationBeginState, RegistrationBeginConfirmState\u003e\n{\n    public bool CanTransition(RegistrationStateMachine machine,\n                              RegistrationBeginState state,\n                              RegistrationTransactionModel input)\n    {\n        return true;\n    }\n\n    public RegistrationBeginConfirmState DoTransition(RegistrationStateMachine machine,\n                                                      RegistrationBeginState state,\n                                                      RegistrationTransactionModel input)\n    {\n        machine.LogMachineState(this.GetType().Name);\n\n        state.PrintStateMessage();\n\n        System.Console.WriteLine(\"Please, press 'Y' if you want to start a registration or 'N' to exit.\");\n        return new RegistrationBeginConfirmState();\n    }\n}\n```\n`CanTransition(TStateMachine machine, TState state, TInput input)` method should return a boolean value that would show if it's possible to do this transition.\n`DoTransition(TStateMachine machine, TState state, TInput input)` method should contain all logic that should be done while this transition.\n\n```CSharp\ninternal class Program\n{\n    public static Models.RegistrationTransactionModel RegistrationModel { get; set; } = new Models.RegistrationTransactionModel();\n\n    private static void Main()\n    {\n        var rsm = new RegistrationStateMachine();\n        while (rsm.CurrentState.GetType() != typeof(DataConfirmedState))\n        {\n            RegistrationModel.UserInput = Console.ReadLine();\n\n            rsm.TryTransition(RegistrationModel);\n        }\n\n        Console.WriteLine($\"Welcome, {RegistrationModel.UserModel.UserFirstName}!\");\n    }\n}\n```\n\nTo try to do a transition you should call a `TryTransition(TInput input)` method.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbardin08%2Fstatemachine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbardin08%2Fstatemachine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbardin08%2Fstatemachine/lists"}