Dynamic Workflow

Dynamic Workflow

I tried to implement Dynamic Workflow and how we can create dynamic workflow functions and execute it in sequence, I used Inversion of control to achieve this .

here is the idea code:

using System;
using System.Collections.Generic;

namespace a1
{
    class Program
    {
        static void Main(string[] args)
        {
            WorkFlow wf = new WorkFlow();
            wf._worklist.AddLast(new CreateEmpList());
            wf._worklist.AddLast(new CreateEmpList());

            wf._worklist.AddLast(new PrintEmpList());

            wf._worklist.AddLast(new CreateEmpList());
            wf._worklist.AddLast(new CreateEmpList());

            wf._worklist.AddLast(new PrintEmpList());
            wf.execute();
            Console.ReadKey();
        }
    }
    public interface IFunction
    {
        void execute();
    }
    public interface IValidator
    {
        bool isValid();
    }
    public interface ICriteria
    {
        
    }

    public class DbModel<T>  where T : class
    {
        private static List<T> _list = new List<T>();

        public static T Add(T m)
        {
            _list.Add(m);
            return m;    
        }

        public static void Delete(T m)
        {
            _list.Remove(m);
        }

        public static List<T> Select(ICriteria c)
        {
            //---- TODO 
            return _list;
        }

        public static T Update(T m)
        {
            //---- TODO 
            return m;
        }
    }

    public class Emp : DbModel<Emp> , IValidator
    {
        public string name { set; get; }
        public string email { set; get; }
        public string mobile { set; get; }

        public bool isValid()
        {
            return true;
        }
    }
    public class CreateEmpList : IFunction
    {
        public void execute()
        {
            Emp _emp = new Emp() { name = "Ahmed Essam", email = "[email protected]", mobile = "966666666" };
            Emp.Add(_emp);
        }
    }
    public class PrintEmpList : IFunction
    {
        public void execute()
        {
            foreach (Emp _emp in Emp.Select(null)) Console.WriteLine("Name: " + _emp.name + " , Email: " + _emp.email + " , Mobile: " + _emp.mobile);
        }
    }

    public class WorkFlow : IFunction
    {
        public LinkedList<IFunction> _worklist { get; set; }
        public WorkFlow ()
        {
            _worklist = new LinkedList<IFunction>();
        }
        public void execute()
        {
            foreach (IFunction _func in _worklist) _func.execute();
        }
    }
}

The Output

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.