Skip to content

Instantly share code, notes, and snippets.

@jermdavis
Created February 15, 2019 14:38
Show Gist options
  • Save jermdavis/89f08945923af2f3a4702c559250f805 to your computer and use it in GitHub Desktop.
Save jermdavis/89f08945923af2f3a4702c559250f805 to your computer and use it in GitHub Desktop.
An alternative example of a generic pipeline which includes logging behaviour
using System;
namespace LoggingPipeline
{
public interface IPipelineStep<INPUT, OUTPUT>
{
OUTPUT Process(INPUT input);
}
public interface IPipeline<INPUT, OUTPUT> : IPipelineStep<INPUT, OUTPUT>
{
Func<INPUT, OUTPUT> PipelineSteps { get; }
}
public abstract class Pipeline<INPUT, OUTPUT> : IPipeline<INPUT, OUTPUT>
{
public Func<INPUT, OUTPUT> PipelineSteps { get; protected set; }
public OUTPUT Process(INPUT input)
{
return PipelineSteps(input);
}
}
}
namespace LoggingPipeline
{
public class ToStringStepWithLog : ILoggingPipelineStep<int, string>
{
public ILoggingPipeline ParentPipeline { get; set; }
public string Process(int input)
{
ParentPipeline.Log.Message(this, $"Calling the example step with input data {input}");
var output = input.ToString();
ParentPipeline.Log.Message(this, $"And the result is: {output}");
return output;
}
}
public class DoubleStepWithLog : ILoggingPipelineStep<int, int>
{
public ILoggingPipeline ParentPipeline { get; set; }
public int Process(int input)
{
ParentPipeline.Log.Message(this, $"Doubling the input value {input}");
var output = input * 2;
ParentPipeline.Log.Message(this, $"Geting the output {output}");
return output;
}
}
public class ExampleLoggingPipeline : LoggingPipeline<int, string>
{
public ExampleLoggingPipeline(ILog log) : base(log)
{
PipelineSteps = input => input
.Step(this, new DoubleStepWithLog())
.Step(this, new ToStringStepWithLog());
}
}
}
using System;
namespace LoggingPipeline
{
public interface ILog
{
void Message(object sender, string message);
}
public class ConsoleLog : ILog
{
public void Message(object sender, string message)
{
Console.WriteLine($"[{sender.GetType().Name}] {message}");
}
}
}
namespace LoggingPipeline
{
public interface ILoggingPipelineStep<INPUT, OUTPUT> : IPipelineStep<INPUT, OUTPUT>
{
ILoggingPipeline ParentPipeline { get; set; }
}
public interface ILoggingPipeline
{
ILog Log { get; }
}
public abstract class LoggingPipeline<INPUT, OUTPUT> : Pipeline<INPUT, OUTPUT>, ILoggingPipeline
{
public ILog Log { get; private set; }
public LoggingPipeline(ILog log)
{
Log = log;
}
}
public static class LoggingPipelineStepExtensions
{
public static OUTPUT Step<INPUT, OUTPUT>(this INPUT input, ILoggingPipeline pipeline, ILoggingPipelineStep<INPUT, OUTPUT> step)
{
step.ParentPipeline = pipeline;
return step.Process(input);
}
}
}
using System;
namespace LoggingPipeline
{
class Program
{
static void Main(string[] args)
{
var log = new ConsoleLog();
var pipeline = new ExampleLoggingPipeline(log);
var input = 35;
var output = pipeline.Process(input);
Console.WriteLine($"Transformed {input}[{input.GetType().Name}] to {output}[{output.GetType().Name}]");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment