EasySignWorkFlow is a C# library designed to provide a flexible and easy-to-use workflow management system. It allows you to define and manage state transitions, perform actions during transitions, and set custom rules for workflows. With features like state validation, transition conditions, and customizable behaviors, EasySignWorkFlow is perfect for implementing approval processes, request management, or any workflow-based application.
- Handle the exception in
GetNextUsersAsyncif the state does not exist in the map. - Add an optional parameter (action) in the
OnCreatemethod. - Change the property name from
StatusestoStates(Breaking change).
- State Management: Define states and manage transitions seamlessly.
- Flexible Conditions: Add conditional logic to transitions with
IfandIfAsyncmethods. - Custom Actions: Execute custom logic during transitions.
- Extensibility: Easily extend and customize workflows with user-defined states and transitions.
- Date Providers: Use
DateTimeProviderto control how dates are handled (UTC or local). - Database Integration: Works well with EF Core to persist requests and their states.
To install the library, add the following package to your project:
Install-Package EasySignWorkFlowOr use the .NET CLI:
dotnet add package EasySignWorkFlowCreate a class that implements the IRequest<TKey, TStatus> interface:
publicclassTestRequest:IRequest<Guid,TestStatus>{publicGuidId{get;set;}publicstring?Title{get;set;}publicboolFlag{get;set;}publicList<State<Guid,TestStatus>>States{get;}=new();publicState<Guid,TestStatus>?CurrentState{get;set;}}Use the FlowMachine class to define states and transitions:
publicclassTestRequestService{privatereadonlyFlowMachine<TestRequest,Guid,TestStatus>_flowMachine;privatereadonlyDemoDBContext_context;publicTestRequestService(DemoDBContextcontext){_context=context;_flowMachine=FlowMachine<TestRequest,Guid,TestStatus>.Create(TestStatus.Draft,(request,current,next)=>{Console.WriteLine($"Transitioning from {current} to {next}");returnTask.CompletedTask;});_flowMachine.SetCancelState(TestStatus.Canceled).SetRefuseState(TestStatus.Refused).SetDateTimeProvider(DateTimeProvider.Now);// Define transitions_flowMachine.When(TestStatus.Draft).If(request =>request.Flag).Set(TestStatus.WaitingForManager1).SetNextUsers((request,_)=>newList<Guid>{Guid.NewGuid()}).OnExecute((request,current,next,nextUserIds)=>{Console.WriteLine($"{request.Title} moved from {current} to {next}");});_flowMachine.When(TestStatus.WaitingForManager1).If(request =>!request.Flag).Set(TestStatus.Accepted).OnExecute((request,current,next,nextUserIds)=>{Console.WriteLine($"{request.Title} moved from {current} to {next}");});}publicstringPrintWorkflow()=>_flowMachine.ToString();}Execute the workflow by calling the FireAsync method:
varrequest=newTestRequest{Id=Guid.NewGuid(),Title="Sample Request",Flag=true};varisSuccess=await_flowMachine.FireAsync(request,TestStatus.Draft);if(isSuccess){Console.WriteLine("Transition successful.");}else{Console.WriteLine("Transition failed.");}You can integrate EasySignWorkFlow with EF Core to persist requests and their states:
publicclassDemoDBContext:DbContext{publicDemoDBContext(DbContextOptions<DemoDBContext>options):base(options){}publicDbSet<TestRequest>TestRequests{get;set;}protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<TestRequest>().OwnsOne("CurrentState", x =>x.CurrentState);modelBuilder.Entity<TestRequest>().OwnsMany("States", x =>x.States);base.OnModelCreating(modelBuilder);}}Fetch requests based on their current state:
publicasyncTask<List<TestRequest>>GetDraftRequests(){returnawait_context.TestRequests.Where(r =>r.CurrentState.Status==TestStatus.Draft).ToListAsync();}Defines a request with a collection of states and a current state.
The core class for defining and managing workflows.
Handles individual state transitions, including conditions and custom actions.
Represents a state in the workflow, including metadata like date and user.
Contributions are welcome! Feel free to open issues or submit pull requests.
This project is licensed under the MIT License. See the LICENSE file for details.