0

I have a scenario where I would like to simplify the process of creating and outputting a document. A document is created from different sources and it can be output to printer or send by email.

The fluent Api might look something like this:

var printJob = new PrintJob()
    .Initialize()
    .CreatePdf()
    .Print().When(output == PrintOutput.Printer)
    .Email().When(output == PrintOutput.Email);

The implementation would be like this:

public interface IPrintJob
{
    IPrintJob Initialize();
    IPrintJob CreatePdf();
    IPrintJob Print();
    IPrintJob Email();
    IPrintJob When();
}

public class PrintJob : IPrintJob
{
    public IPrintJob Initialize()
    { return this; }

    public IPrintJob CreatePdf()
    { return this; }

    public IPrintJob Print()
    { return this; }

    public IPrintJob Email()
    { return this; }

    public IPrintJob When()
    { return this; }
}

How would I implement a When or If condition?

Furthermore, how are the methods executed? Should they be added to a List<Action> and then add a Run method to the class which executes these actions sequentially?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
  • 1
    It can be done, but I would just create a PrintWhen method. – Silvermind Dec 20 '18 at 08:03
  • can you elaborate a bit more on how the document is created and how the output is set? At the moment seem like you are using IPrintJob to setup both the print job and the document? I would imagine you have a separate Document class, a PrintJob that you can set different outputs? – Phuong Nguyen Dec 20 '18 at 08:30

0 Answers0