Created
April 12, 2011 21:32
-
-
Save ruslander/916468 to your computer and use it in GitHub Desktop.
Pipeline steps in complex processes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Pipe<T> | |
{ | |
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | |
private readonly List<Type> _operations = new List<Type>(); | |
public Pipe<T> Register<TOperation>() where TOperation : IOperation<T> | |
{ | |
_operations.Add(typeof(TOperation)); | |
IoC.Register<TOperation>(); | |
return this; | |
} | |
public void Execute<T>(T current) | |
{ | |
Log.Info("Starting #" + current); | |
foreach (var operationType in _operations){ | |
var operation = (IOperation<T>)IoC.Resolve(operationType); | |
var operationName = operationType.Name; | |
try{ | |
Log.Info(operationName); | |
operation.Execute(current); | |
} | |
catch (Exception e){ | |
throw new OperationAbortException(string.Format("[{0}] - {1}", operationName, e.Message), e); | |
} | |
} | |
Log.Info("Completing #" + current); | |
} | |
} | |
public interface IOperation<T> | |
{ | |
void Execute(T drawing); | |
} | |
public class ComplexContextProcessingPipeline : Pipe<ComplexContext> | |
{ | |
public ComplexContextProcessingPipeline() | |
{ | |
Register<Initialize>(); | |
Register<Run>(); | |
Register<ExportResult>(); | |
Register<Notification>(); | |
Register<Cleanup>(); | |
} | |
} | |
// caller | |
foreach (var context in complexContexts) | |
{ | |
try | |
{ | |
_complexContextProcessingPipeline.Execute(drawing); | |
} | |
catch (OperationAbortException exception) | |
{ | |
Log.Error(exception.Message, exception); | |
continue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment