Simple Workflow Engine Implementation in C#

Below is a simple implementation of work flow engine using interfaces and the concept of injecting overloaded class implementations via the dependent class method. The class constructor creates the List for storing the specific workflow implementations which are further executed.Interface – IWorkFlowActivities

namespace WFEngine
{
    public interface IWorkFlowActivities
    {
        void Execute();
    }
}



CarWorkflow

using System;
 
namespace WFEngine
{
    public class CarWorkflow : IWorkFlowActivities
    {
        public void Execute()
        {
            Console.WriteLine("\n\n>>>>Car WF<<<<<<\n");
            Console.WriteLine("Starting..");
            Console.WriteLine("Revving..");
            Console.WriteLine("driving..");
            Console.WriteLine("stopping:)");
        }
    }
}



EatWorkFlow

using System;
 
namespace WFEngine
{
    public class EatWorkFlow : IWorkFlowActivities
    {
        public void Execute()
        {
            Console.WriteLine("\n\n>>>>Eat WF<<<<<<");
            Console.WriteLine("Take Food..");
            Console.WriteLine( "Put in mouth..");
            Console.WriteLine("Chew..");
            Console.WriteLine("Swallow..");
            Console.WriteLine("Digest..:)");
        }
    }
}



Core Class – WorkFLowEngine

using System;

namespace WFEngine
{
    public class WorkFlowEngine
    {
        private readonly IList<IWorkFlowActivities> _workFlowActivities;
 
        public WorkFlowEngine()
        {
            _workFlowActivities = new List<IWorkFlowActivities>();
        }
 
        public void RegisterActivities(IWorkFlowActivities workFlowActivities)
        {
            _workFlowActivities.Add(workFlowActivities);
        }
 
        public void Run()
        {
            foreach (var i in _workFlowActivities)
            {
                i.Execute();
            }
        }
 
    }
}



Note how we create a private list field of type interface objects that can hold any specific implementation on work flows.

Main program

namespace WFEngine
{
    class Program
    {
        static void Main(string[] args)
        {
            var workFlowEngine = new WorkFlowEngine();
            workFlowEngine.RegisterActivities(new CarWorkflow());
            workFlowEngine.RegisterActivities(new EatWorkFlow());
            workFlowEngine.Run();
        }
    }
}



See how each specific implementation is being injected to the core class to run the specific work flows.