I am building a command-line .exe that can apply several operations on a PDF file (add text, images, resize, crop, etc).
Currently, my Program.cs looks a bit like this (it uses CommandLineParser):
switch (invokedVerb)
{
case "barcode":
Operations.BarcodeOperations.AddBarCode(options.AddBarcodeVerb);
break;
case "addblankpage":
Operations.PageOperations.AddBlankPage(options.AddBlankPageVerb);
break;
}
As you can see, I have split the operations into several XXXOperations
classes, each of them having very similar instructions:
public static void AddStuff(StuffOptions options)
{
Logging.Log("here is a bunch of logging");
// here sometimes there is some action-specific code but not often
using (DocWrapper doc = new DocWrapper(options.File)) // this is in all actions
{
foreach (int page in doc.GetPagesToModify(options.Pages)) // this is in most actions
{
// call some stuff on the doc instance
}
doc.Save(options.OutputFile); // this is in all actions
}
}
So, all actions create a new instance of DocWrapper
, most of them loop on its pages (but I could modify the actions so that all of them do), and all of them save, but each of them do a different set of actions inside it.
How could I refactor this code so that the DocWrapper
instantiation, the pages loop and the save are in a single place, but I can specify custom code inside the loop?
I'm thinking of using delegates to define my actions, but I have no idea where to start, since I'm not very familiar with them. I'm also considering inheriting from an abstract class that implements the loop and abstracts a DoWork
method, called inside the loop and implemented in the XXXOperations
class, but since I don't know delegates, I'm not sure which is more adapted to the situation.
BarCodeOperations
andPageOperations
both inherit from a baseOperations
class, or is Operations a namespace? – RubberDuck 23 hours agooptions.AddBarcodeVerb
andoptions.AddBlankPageVerb
? Are they constant or do they change according to something? – TopinFrassi 22 hours agoOperations
is just a namespace.options.StuffVerb
are of typeStuffOptions
, and implementIProgramOptions
, an interface describing parameters common to all verbs. Note that when I asked my question, it did not yet implementIProgramOptions
. See more info here: github.com/gsscoder/commandline/wiki/Verb-Commands – thomasbtv 5 hours ago