Need an idea on how to handle task group variables in a Winforms application

I'm struggling to separate the individual actions in my application. I basically have a form that the user chooses to perform actions. Some actions are performed separately and can easily get into the queue, which I performed in order. However, some actions require several related actions for which the main action requires other tasks:

Create Website
 - DoesWebsiteExists (Validation)
 - CreatePhyiscalDir 
 - CreateWebsite 
 - CreateVirDirectories

All these actions were divided and do not know about each other.

Initially, before I separate the validation from the action, I pass in the Settings object, which contains all the related / necessary settings for performing any actions and queues that I iterate over, and call the Execute () method for each action.

 Queue<IAction> Actions
    [0] = CreateDirectory.Execute()
    [1] = CreateWebsite.Execute()

, , , .

? , 1 ? , 4 (4 ), 4 ?

, , , , . , IIS, , .

?

+3
2

IAction ( Action) Command Pattern. , (.NET v1.0), Composite, . , Add, Execute .

public CompositeAction : IAction
{
    private ActiondCollection Action;

    void Add(Action action)
    {
       Action.Add(action);
    }

    void Execute()
    {
       foreach(Action action in Actions)
       { 
          action.Execute(); 
       }
    }
}

.

, Execute void, - ( IsValid DoWebSiteExist, )

, .

, , bool, , Func

, bool, . , , , .

namespace Test
{
    class Program
    {
        //Change this value to show how the Actions bail when you DoesWebsiteNotExists 
        //returns false
        static bool isvalid = true;
        static void Main(string[] args)
        {
            List<System.Func<bool>> MainActions = new List<System.Func<bool>>();

            List<System.Func<bool>> CompositeActions = new List<System.Func<bool>>();

            MainActions.Add(() => NonCompositeAction());

           //Probably want a builder here.
            CompositeActions.Add(() => DoesWebsiteNotExists());
            CompositeActions.Add(() => CreatePhyiscalDir());
            CompositeActions.Add(() => CreateVirDirectories());
            CompositeActions.Add(() => CreateWebsite());

            MainActions.Add(() => ExcuteCompositeActions(CompositeActions));

            foreach (Func<bool> action in MainActions)
                action.Invoke();



        }


        static bool ExcuteCompositeActions(List<System.Func<bool>> Actions)
        {



            bool Success = true;
            foreach (Func<bool> action in Actions)
            {
                if (!action.Invoke())
                {
                    Success = false;
                    break;
                }

            }
            return Success;
        }

        static bool NonCompositeAction()
        {
            Console.WriteLine("NonCompositeAction");
            return true;
        }

        static bool DoesWebsiteNotExists()
        {
            Console.WriteLine("DoesWebsiteExists");
            return isvalid;
        }
         static bool CreatePhyiscalDir()
        {
            Console.WriteLine("CreatePhyiscalDir");
            return true;
        }
        static bool CreateWebsite()
        {
            Console.WriteLine("CreateWebsite");
            return true;
        }
        static bool CreateVirDirectories()
        {
            Console.WriteLine("CreateVirDirectories");
            return true;
        }






        }

    }

, , , .

. List<Func<bool> . ,

public class CreatePhyiscalDir
    {

        public bool Execute()
        {
            Console.WriteLine("CreatePhyiscalDir");
            return true;
        }
    }

    CreatePhyiscalDir cpd = new CreatePhyiscalDir();
    CompositeActions.Add(() => DoesWebsiteExists());
    CompositeActions.Add(() => cpd.Execute());

, cpd.

, List<Func<bool>...

  • , bool, . , .
  • , , .

...

  • return true; , , .
  • .

, Chain of Responsiblity Pipeline, , , List<Func<bool>> Composite Actionl, SnOrfus. , , , , .

. , , , . , .

+3

, , :

- , , , .

, IAction , :

public abstract SystemAction
{
    public SystemAction nextAction { get; set; }

    public void Execute()
    {
        this.ExecuteAction();
        if (this.nextAction != null)
            this.nextAction.Execute();
    }

    protected abstract void ExecuteAction();

    public SystemAction ThenDo(SystemAction action) 
    { 
        this.nextAction = action;
        return this.nextAction;
    }            
}

. :

SystemAction setupSite = new CreatePhyiscalDir();
setupsSite
    .ThenDo(new CreateWebsite())
    .ThenDo(new CreateVirDirectories());

setupSite.Execute();

, , Conrad, . ( ).

+1

Source: https://habr.com/ru/post/1780940/


All Articles