Skip to content

omidhosseini/SimpleStateMachine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SimpleStateMachine

  • Install package
dotnet add package OmidHosseini.StateMachineHandler --version 1.0.0
  • Setup your rules
SetRule<List<MyModel>>
  (
      myModel.GetMyModelList(), // your context
      (cfg, x) => new[] // your config
      {
          new StateMachineModel<MyStates>()
          {
              CurrentState = MyStates.New,
              CanBeChangedTo = new[] {MyStates.New, MyStates.Ordered, MyStates.Paid},
              CanBeChanged = x.Any(s => s.Age < 20)
          },
          new StateMachineModel<MyStates>()
          {
              CurrentState = MyStates.Ordered,
              CanBeChangedTo = new[] {MyStates.New, MyStates.Visited},
              CanBeChanged = true
          },
          new StateMachineModel<MyStates>()
          {
              CurrentState = MyStates.Sent,
              CanBeChangedTo = new[] {MyStates.New, MyStates.Ordered, MyStates.Paid},
              CanBeChanged = false
          }
      }
  );

In this case, I use my own states that defined as Enum :

// this is just a sample
 public enum MyStates
  {
      New,
      Visited,
      Ordered,
      Paid,
      Sent,
  }
  • Then you can check your states:
bool canInsertNewStateAfterOrderedState = stateService.CanInsert(MyStates.New, MyStates.Ordered);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages