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?