c# – Alternative to not mixing sync and async when the nature of the program requires it


Let’s say I’m building a simple console app which has four commands:

  1. Create category.
  2. Download recipe from API to category.
  3. Display all recipes in a category.

Assuming the app will grow, I use the Command design pattern to handle console commands. I also use a factory design pattern to create the commands from the user input. Correct me if this is not a good approach so far.

1 and 3 are synchronous commands. 2 is asynchronous. I should be able to initiate multiple downloads before they finish.

So in these models, if I create a Command interface to handle these requests and write something back to the console, how do I do this without violating this “rule”?

The goal would be to have a simple loop that parses the lines, transforms them into Commands and executes them. These methods don’t know what they are executing as the logic is hidden in the concrete Commands.

Something alone these lines:

For (...)
{
    ...
    ICommand command = CommandFactory.From(string)

    Task t = command.executeAsync()
    tasks.Add(t)
}

Task.Waitall(tasks)

This works but it feels weird to have a executeAsync even when the command is sync. Although it works by doing Task.CompletedTask