-
Notifications
You must be signed in to change notification settings - Fork 1
/
DemoScript.txt
116 lines (92 loc) · 3.15 KB
/
DemoScript.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Domain Events Demo
-------------------------------------------------------------------------------
Create new Console Application
-------------------------------------------------------------------------------
Create new Console Solution
Add StructureMap via nuget (local)
Add JaySmith.DomainEvents.StructureMap via nuget (local)
Create BootStrapper class
public static class BootStrapper
public static void Initalize()
{
ObjectFactory.Initialize(x => x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AssembliesFromApplicationBaseDirectory();
scan.LookForRegistries();
}
}
Create DemoApplication
public class DemoApplication
{
public void Run()
{
//Where something interesting happens
Console.Writeline("Domain Event Demo");
}
}
Modify Program Main
static void Main(string[] args)
{
Bootstrapper.Initialize();
var application = ObjectFactory.GetInstance<DemoApplication>();
application.Run();
Console.Read();
}
Run Demo to make sure it is working so far
Add Domain Folder
Add Domain Object: Person
Constructor (string firstName, string lastName)
Properties
FirstName AUTO
LastName AUTO
Full Name Getter Only - return string.Format("{0} {1}", FirstName, LastName);
Stub out UpdateLastName(string newLastName)
public void UpdateLastName(string newLastName)
{
LastName = newLastName;
}
Modify DemoApplication Run
{
var person = new Person("Kristina", "Parker");
person.UpdateLastName = "Smith";
}
Talk about LastName Changed as an interesting event
Create DomainEvents Folder
Add PersonLastNameChangedEvent
public class PersonLastNameChangedEvent : IDomainEvent
{
public Person Person { get; set; }
public string OldValue { get; set; }
public string NewValue { get; set; }
public PersonLastNameChangedEvent(Person person, string oldValue, string newValue )
{
this.Person = person;
this.OldValue = oldValue;
this.NewValue = newValue;
}
}
Modify the Person class to raise the event when the last name is changed
public void UpdateLastName(string newLastName)
{
var oldLastName = LastName;
LastName = newLastName;
var lastNameChangedEvent = new PersonLastNameChangedEvent(this, oldLastName, newLastName);
DomainEventManager.Raise(lastNameChangedEvent);
}
The event gets raised but nothing is listening to for the event. So, lets do
something when this event happens, to do that we need an event handler.
Add DomainEventHandlers Folder
Add PersonLastNameCHangedEventHandler
public class PersonLastNameChangedEventHandler : IDomainEventHandler<PersonLastNameChangedEvent>
{
public void Handle(PersonLastNameChangedEvent args)
{
Console.WriteLine("Lastname changed from {0} to {1}", args.OldValue, args.NewValue);
}
}
Modify BootStrapper
scan.ConnectImplementationsToTypesClosing(typeof(IDomainEventHandler<>));
Run Program to show how the event fires.
Go over how domain events are found and executed