Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:31:13+00:00 2026-05-28T23:31:13+00:00

I have some fairly simple state needs (for now). I think I would like

  • 0

I have some fairly simple state needs (for now). I think I would like to model these using the Stateless api. (But I don’t really know much about state machines, so I could be wrong.)

But I am getting caught up in the terminology (Specifically State and Trigger)

Here is an example: I have an order class. It is setup with several states. They are: New, Filled, Shipping, Completed, Cancelled.

A few simple state rules I would like is that these state transitions are allowed:

  • New (is the default)
  • New -> Filled
  • New -> Cancelled
  • Filled -> Shipping
  • Filled -> Cancelled
  • Filled -> Shipping
  • Shipping -> Complete

So where I am getting tripped up here is what is my “Trigger”?

Just in case a more specific example is needed, say I want a method like this:

public bool UpdateOrderStatus(int OrderId, OrderStatusEnum NewOrderStatus)

that will return true if the status updated successfully. How can setup and use Stateless to make this happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:31:14+00:00Added an answer on May 28, 2026 at 11:31 pm

    The machine is in only one state at a time; the state it is in at any
    given time is called the current state. It can change from one state
    to another when initiated by a triggering event or condition, this is
    called a transition.
    from Finite-state machine on Wiki

    I believe, the trigger is this triggering event.

    Update:

    Of course trigger name sometimes can be equal to some of state names.

    New (initial state)
    New -> Filled (trigger "Filled")
    New -> Cancelled (trigger "Cancelled")
    Filled -> Shipping (trigger "ToBeShipped")
    Filled -> Cancelled (trigger "Cancelled")
    Shipping -> Complete (trigger "Completed").
    

    Update:

    stateless is really nice framework!
    I’ve tried to implemented the functionality.

    States:

    public enum State
    {
        New,
        Filled,
        Shipping,
        Cancelled,
        Completed
    }
    

    Triggers:

    public enum Trigger
    {
        Filled,
        Cancelled,
        ToBeShipped,
        Completed
    }
    

    Order class:

    public class Order
    {
        private readonly StateMachine<State, Trigger> _stateMachine;
    
        public Order()
        {
            _stateMachine = CreateStateMachine();
        }
    
        public bool TryUpdateOrderStatus(Trigger trigger)
        {
            if (!_stateMachine.CanFire(trigger))
                return false;
    
            _stateMachine.Fire(trigger);
            return true;
        }
    
        public State Status
        {
            get
            {
                return _stateMachine.State;
            }
        }
    
        private StateMachine<State, Trigger> CreateStateMachine()
        {
            StateMachine<State, Trigger> stateMachine = new StateMachine<State, Trigger>(State.New);
            stateMachine.Configure(State.New)
                .Permit(Trigger.Filled, State.Filled)
                .Permit(Trigger.Cancelled, State.Cancelled);
    
            stateMachine.Configure(State.Filled)
                .Permit(Trigger.ToBeShipped, State.Shipping)
                .Permit(Trigger.Cancelled, State.Cancelled);
    
            stateMachine.Configure(State.Shipping)
                .Permit(Trigger.Completed, State.Completed);
    
            stateMachine.OnUnhandledTrigger((state, trigger) =>
                {
                    Console.WriteLine("Unhandled: '{0}' state, '{1}' trigger!");
                });
            return stateMachine;
        }
    }
    

    Tester for Order class:

    Order order = new Order();
    bool result = order.TryUpdateOrderStatus(Trigger.Completed);
    Console.WriteLine("Attemp to complete order: {0}", result);
    Console.WriteLine("Order status: {0}", order.Status);
    
    result = order.TryUpdateOrderStatus(Trigger.ToBeShipped);
    Console.WriteLine("Attemp to ship order: {0}", result);
    Console.WriteLine("Order status: {0}", order.Status);
    
    result = order.TryUpdateOrderStatus(Trigger.Cancelled);
    Console.WriteLine("Attemp to cancel order: {0}", result);
    Console.WriteLine("Order status: {0}", order.Status);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple Windows Forms application that I would like to add
I'm doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out
I have a fairly simple const struct in some C code that simply holds
This sounds like it would be a simple task, but I'm running into some
I have a fairly simple SQL statement to get some results from a couple
I have a fairly simple model: public class Delivery { public int DeliveryId {
I have what I think should be a fairly simple implementation, but I'm running
We have a couple of sites which have some memory problems and fairly often
I'm relatively new to java (specifically swing) and have recently been making some fairly
I have some classes layed out like this class A { public virtual void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.