Skip to content

Commit e246702

Browse files
Craig W. SelbertCraig W. Selbert
authored andcommitted
Working on a pipeline pattern implementation
1 parent 322e366 commit e246702

26 files changed

+330
-92
lines changed

ConsoleApplication/Program.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,58 @@
44
using UMV.Reference.ClassLibrary.Ninject;
55
using UMV.Reference.Patterns;
66
using UMV.Reference.Patterns.Aspects;
7+
using UMV.Reference.Patterns.Base.Interfaces;
78
using UMV.Reference.Patterns.Models;
8-
using UMV.Reference.Patterns.Operations;
9+
using UMV.Reference.Patterns.Ninject;
10+
using UMV.Reference.Patterns.Operations.Interfaces;
911

1012
namespace UMV.Reference.ConsoleApplication
1113
{
1214
class Program
1315
{
1416
static void Main(string[] args)
17+
{
18+
PipelineExample();
19+
20+
Console.ReadLine();
21+
}
22+
23+
private static void TestChangeTracking()
1524
{
1625
var member = new Member();
1726

1827
member.FirstName = "Craig";
1928

20-
member.Initialize();
29+
member.InitializeChangeState();
2130

2231
member.FirstName = "CRAIG";
2332

24-
var changes = member.GetChanges();
25-
26-
Console.ReadLine();
33+
var changes = member.GetChangeState();
2734
}
2835

2936
private static void PipelineExample()
3037
{
38+
var kernel = new StandardKernel(new PatternsModule());
3139
var context = new Member();
3240

33-
IOperation<Member> m = null;
41+
var trackChanges = kernel.Get<IOperation<IChangeTrackable>>("TrackChanges");
42+
var addAuditInformation = kernel.Get<IOperation<IAuditible>>("AddAuditInformation");
43+
var updateMemberNameToCraigOperation = kernel.Get<IOperation<Member>>("UpdateMemberNameToCraigOperation");
44+
var initializeChangeTracking = kernel.Get<IOperation<IChangeTrackable>>("InitializeChangeTracking");
3445

3546
// Build up your pipeline
36-
var pipeline = new Pipeline<Member>()
37-
.Register(m)
38-
.Register(new AddMemberNameOperation());
47+
var pipeline = new Pipeline<IChangeTrackable>()
48+
.Register(initializeChangeTracking)
49+
.Register(addAuditInformation)
50+
.Register(updateMemberNameToCraigOperation)
51+
.Register(trackChanges)
52+
;
3953

4054
// Add aspects around the pipline
41-
var exceptionLogginAspect = new ExceptionLoggingAspect<Member>(pipeline.Execute);
55+
var exceptionLogginAspect = new ExceptionLoggingAspect<IChangeTrackable>(pipeline.Execute);
4256

4357
// Execute
44-
context = exceptionLogginAspect.Execute(context);
58+
context = exceptionLogginAspect.Execute(context) as Member;
4559

4660
Console.WriteLine(context.FirstName);
4761
}

Patterns/Base/ChangeTrackable.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using UMV.Reference.Patterns.Base.Interfaces;
6+
using UMV.Reference.Patterns.Base.Models;
7+
8+
namespace UMV.Reference.Patterns.Base
9+
{
10+
public abstract class ChangeTrackable : Auditible, IChangeTrackable
11+
{
12+
private readonly List<PropertyInfo> _properties;
13+
private readonly List<PropertyInfo> _propertiesToIgnore;
14+
private readonly Dictionary<string, object> _originalValues = new Dictionary<string, object>();
15+
16+
protected ChangeTrackable()
17+
{
18+
_propertiesToIgnore = typeof(Auditible).GetProperties().ToList();
19+
_properties = GetType().GetProperties().Where(x => !_propertiesToIgnore.Contains(x)).ToList();
20+
}
21+
22+
public void InitializeChangeState()
23+
{
24+
// Save the current value of the properties to our dictionary.
25+
foreach (var property in _properties)
26+
_originalValues.Add(property.Name, property.GetValue(this));
27+
}
28+
29+
public ChangeState GetChangeState()
30+
{
31+
// Filter properties by only getting what has changed
32+
var changedProperties = _properties.Where(p => !Equals(p.GetValue(this, null), _originalValues[p.Name])).ToArray();
33+
34+
return new ChangeState
35+
{
36+
CreateDate = CreateDate,
37+
UpdateDate = UpdateDate,
38+
UpdateUser = UpdateUser,
39+
CreateUser = CreateUser,
40+
ChangedProperties = changedProperties.Select(x => new ChangedProperty
41+
{
42+
Name = x.Name,
43+
CurrentValue = x.GetValue(this),
44+
OriginalValue = _originalValues[x.Name]
45+
})
46+
};
47+
}
48+
}
49+
}

Patterns/Base/ChangeTrackingBase.cs

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

Patterns/Base/Enums/ObjectState.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace UMV.Reference.Patterns.Base.Enums
2+
{
3+
public enum ObjectState
4+
{
5+
New,
6+
Changed,
7+
NoChange
8+
}
9+
}

Patterns/Base/Interfaces/IAuditible.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System;
2+
using UMV.Reference.Patterns.Base.Enums;
23

34
namespace UMV.Reference.Patterns.Base.Interfaces
45
{
5-
public interface IAuditible
6+
public interface IAuditible : IIdentifiable
67
{
8+
ObjectState ObjectState { get; set; }
9+
710
string CreateUser { get; set; }
811

912
DateTime CreateDate { get; set; }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using UMV.Reference.Patterns.Base.Models;
2+
3+
namespace UMV.Reference.Patterns.Base.Interfaces
4+
{
5+
public interface IChangeTrackable : IAuditible
6+
{
7+
ChangeState GetChangeState();
8+
9+
void InitializeChangeState();
10+
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace UMV.Reference.Patterns.Base.Interfaces
2+
{
3+
public interface IIdentifiable
4+
{
5+
int Id { get; set; }
6+
}
7+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
2+
using UMV.Reference.Patterns.Base.Enums;
23
using UMV.Reference.Patterns.Base.Interfaces;
34

4-
namespace UMV.Reference.Patterns.Base
5+
namespace UMV.Reference.Patterns.Base.Models
56
{
6-
public abstract class Auditible : IAuditible
7+
public abstract class Auditible : Identifiable, IAuditible
78
{
89
public string CreateUser { get; set; }
910

@@ -12,5 +13,7 @@ public abstract class Auditible : IAuditible
1213
public string UpdateUser { get; set; }
1314

1415
public DateTime UpdateDate { get; set; }
16+
17+
public ObjectState ObjectState { get; set; }
1518
}
1619
}

Patterns/Base/Models/ChangeState.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UMV.Reference.Patterns.Base.Enums;
4+
using UMV.Reference.Patterns.Base.Interfaces;
5+
6+
namespace UMV.Reference.Patterns.Base.Models
7+
{
8+
public class ChangeState : IAuditible
9+
{
10+
public int Id { get; set; }
11+
12+
public string CreateUser { get; set; }
13+
14+
public DateTime CreateDate { get; set; }
15+
16+
public string UpdateUser { get; set; }
17+
18+
public DateTime UpdateDate { get; set; }
19+
20+
public ObjectState ObjectState { get; set; }
21+
22+
public IEnumerable<ChangedProperty> ChangedProperties { get; set; }
23+
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace UMV.Reference.Patterns.Base.Models
2+
{
3+
public class ChangedProperty
4+
{
5+
public string Name { get; set; }
6+
7+
public object OriginalValue { get; set; }
8+
9+
public object CurrentValue { get; set; }
10+
}
11+
}

0 commit comments

Comments
 (0)