Pipeline Pattern is helpful in dividing the problem into smaller reusable code components. This is a simple yet powerful structural pattern to organize a complex logic into smaller reusable components, which can be added/removed/modified independently.
- The logic is complex.
- The simplicity and readability of the code is critical.
- The logic can be divided into multiple modules.
- The modules are potentially reusable for different usecases.
Common solution for pipeline processing
You can install CommandPipeline via NuGet:
Install-Package CommandPipeline
Install it if you want to use Ninject as dependencies resolver for commands
You can install CommandPipeline.Ninject via NuGet:
Install-Package CommandPipeline.Ninject
The complete ready-to-use example is available in the CommandPipeline.Example project
public class ExtractHtmlFromUrl : NonParameterizedCommand
{
public OutArgument<HtmlDocument> HtmlPage { get; set; }
public InArgument<Request> Request { get; set; }
public IWebPageDownloader Downloader { get; private set; }
public ExtractHtmlFromUrl(IWebPageDownloader downloader)
{
this.Downloader = downloader;
}
public override void Execute()
{
var request = Ensure.That(this.Request, "Request").Is(p => p.IsNotNull());
var webPage = this.Downloader.DownloadWebPage(request.Url);
var htmlPage = new HtmlDocument { Content = webPage };
this.HtmlPage.Set(htmlPage);
}
}
var container = new NinjectContainer();
container.Bind<IWebPageDownloader>().To<WebPageDownloader>();
container.Bind<IMarkdownConveter>().To<MarkdownConveter>();
container.Bind<IDataContainerService>().To<DataContainerServiceFake>();
var context = new ParametersContext<ICommand>();
context.Set<ExtractHtmlFromUrl, Request>(x => x.Request, new Request { Url = link });
builder = new PipelineBuilder()
var pipeline = builder.Create(container, context)
.Register<ExtractHtmlFromUrl>()
.Register<ConvertHtmlToMardown>()
.Register<UploadToDataContainer>();
pipeline.Handle();