Skip to content

Commit 09bace6

Browse files
Craig W. SelbertCraig W. Selbert
authored andcommitted
Working on a pipeline pattern
1 parent 3cbb0a7 commit 09bace6

File tree

8 files changed

+49
-159
lines changed

8 files changed

+49
-159
lines changed

ConsoleApplication/Program.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System;
2-
using System.Diagnostics.Eventing.Reader;
32
using Ninject;
43
using UMV.Reference.ClassLibrary.Interfaces;
54
using UMV.Reference.ClassLibrary.Ninject;
65
using UMV.Reference.Patterns;
6+
using UMV.Reference.Patterns.Aspects;
77
using UMV.Reference.Patterns.Models;
8-
using UMV.Reference.Patterns.Operations;
98

109
namespace UMV.Reference.ConsoleApplication
1110
{
@@ -20,30 +19,21 @@ static void Main(string[] args)
2019
Console.ReadLine();
2120
}
2221

23-
24-
2522
private static void PipelineExample()
2623
{
2724
var context = new Member();
2825

29-
context = new Pipeline<Member>()
30-
.Register(new TimeToExecuteOperation<Member>())
31-
.Register(new TimeToExecuteOperation<Member>())
32-
.Register(new TimeToExecuteOperation<Member>())
33-
.Execute(context);
34-
}
26+
// Build up your pipeline
27+
var pipeline = new Pipeline<Member>()
28+
.Register(member => { member.FirstName = "Craig"; throw new Exception("Test"); return member; });
3529

36-
private static void PipelineBuilderExample()
37-
{
38-
var context = new Member();
30+
// Add aspects around the pipline
31+
var exceptionLogginAspect = new ExceptionLoggingAspect<Member>(pipeline.Execute);
3932

40-
var pipeline = new PipelineBuilder<Member>()
41-
.Register(new TimeToExecuteOperation<Member>())
42-
.Register(new TimeToExecuteOperation<Member>())
43-
.Register(new TimeToExecuteOperation<Member>())
44-
.Build();
33+
// Execute
34+
context = exceptionLogginAspect.Execute(context);
4535

46-
context = pipeline.Execute(context);
36+
Console.WriteLine(context.FirstName);
4737
}
4838

4939
private static void IoCExample()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
3+
namespace UMV.Reference.Patterns.Aspects
4+
{
5+
public class ExceptionLoggingAspect<T>
6+
{
7+
public ExceptionLoggingAspect(Func<T, T> action)
8+
{
9+
Handle = action;
10+
}
11+
12+
public T Execute(T input)
13+
{
14+
try
15+
{
16+
return Handle(input);
17+
}
18+
catch (Exception ex)
19+
{
20+
Console.WriteLine(ex.ToString());
21+
return (T)Activator.CreateInstance(typeof(T));
22+
}
23+
}
24+
25+
private Func<T, T> Handle { get; set; }
26+
27+
}
28+
}

Patterns/Pipeline/Operations/IOperation.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

Patterns/Pipeline/Operations/Operation.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

Patterns/Pipeline/Operations/TimeToExecuteOperation.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

Patterns/Pipeline/Pipeline.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
13
using System.Threading.Tasks;
2-
using UMV.Reference.Patterns.Operations;
34

45
namespace UMV.Reference.Patterns
56
{
67
public class Pipeline<T>
78
{
8-
private IOperation<T> _root;
9+
private readonly List<Func<T,T>> _operations = new List<Func<T,T>>();
910

10-
public Pipeline<T> Register(IOperation<T> operation)
11+
public Pipeline<T> Register(Func<T,T> operation)
1112
{
12-
if (_root == null)
13-
{
14-
_root = operation;
15-
}
16-
else
17-
{
18-
_root.Register(operation);
19-
}
20-
13+
_operations.Add(operation);
2114
return this;
2215
}
2316

2417
public T Execute(T context)
2518
{
26-
return _root.Execute(context);
19+
foreach (var operation in _operations)
20+
{
21+
context = operation.Invoke(context);
22+
}
23+
return context;
2724
}
2825
}
29-
}
26+
}

Patterns/Pipeline/PipelineBuilder.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

Patterns/UMV.Reference.Patterns.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@
4444
<Compile Include="Pipeline\Models\Address.cs" />
4545
<Compile Include="Pipeline\Models\Member.cs" />
4646
<Compile Include="Pipeline\Models\Message.cs" />
47-
<Compile Include="Pipeline\Operations\TimeToExecuteOperation.cs" />
48-
<Compile Include="Pipeline\Operations\Operation.cs" />
49-
<Compile Include="Pipeline\Operations\IOperation.cs" />
50-
<Compile Include="Pipeline\PipelineBuilder.cs" />
47+
<Compile Include="Pipeline\Aspects\ExceptionLoggingAspect.cs" />
5148
<Compile Include="Pipeline\Pipeline.cs" />
5249
<Compile Include="Properties\AssemblyInfo.cs" />
5350
</ItemGroup>

0 commit comments

Comments
 (0)