File tree Expand file tree Collapse file tree 4 files changed +53
-8
lines changed
src/NimblePros.SampleToDo.Core/ContributorAggregate
tests/NimblePros.SampleToDo.UnitTests/Core/ContributorAggregate Expand file tree Collapse file tree 4 files changed +53
-8
lines changed Original file line number Diff line number Diff line change 1
- namespace NimblePros . SampleToDo . Core . ContributorAggregate ;
1
+ using NimblePros . SampleToDo . Core . ContributorAggregate . Events ;
2
+
3
+ namespace NimblePros . SampleToDo . Core . ContributorAggregate ;
2
4
3
5
public class Contributor : EntityBase , IAggregateRoot
4
6
{
@@ -11,6 +13,9 @@ public Contributor(ContributorName name)
11
13
12
14
public void UpdateName ( ContributorName newName )
13
15
{
16
+ if ( Name . Equals ( newName ) ) return ;
14
17
Name = newName ;
18
+ this . RegisterDomainEvent ( new ContributorNameUpdatedEvent ( this ) ) ;
15
19
}
16
20
}
21
+
Original file line number Diff line number Diff line change 4
4
/// A domain event that is dispatched whenever a contributor is deleted.
5
5
/// The DeleteContributorService is used to dispatch this event.
6
6
/// </summary>
7
- internal class ContributorDeletedEvent : DomainEventBase
7
+ internal class ContributorDeletedEvent ( int contributorId ) : DomainEventBase
8
8
{
9
- public int ContributorId { get ; set ; }
10
-
11
- public ContributorDeletedEvent ( int contributorId )
12
- {
13
- ContributorId = contributorId ;
14
- }
9
+ public int ContributorId { get ; set ; } = contributorId ;
15
10
}
Original file line number Diff line number Diff line change
1
+ namespace NimblePros . SampleToDo . Core . ContributorAggregate . Events ;
2
+
3
+ internal class ContributorNameUpdatedEvent ( Contributor contributor ) : DomainEventBase
4
+ {
5
+ public Contributor Contributor { get ; private set ; } = contributor ;
6
+
7
+ }
Original file line number Diff line number Diff line change @@ -20,3 +20,41 @@ public void InitializesName()
20
20
Assert . Equal ( _testName , _testContributor . Name . Value ) ;
21
21
}
22
22
}
23
+
24
+ public class ContributorUpdateName
25
+ {
26
+ private readonly string _testName = "new name" ;
27
+ private Contributor ? _testContributor ;
28
+
29
+ private Contributor CreateContributor ( )
30
+ {
31
+ return new Contributor ( ContributorName . From ( _testName ) ) ;
32
+ }
33
+
34
+ [ Fact ]
35
+ public void DoesNothingGivenSameName ( )
36
+ {
37
+ _testContributor = CreateContributor ( ) ;
38
+ var initialEvents = _testContributor . DomainEvents . Count ;
39
+
40
+ var initialHash = _testContributor . GetHashCode ( ) ;
41
+
42
+ _testContributor . UpdateName ( ContributorName . From ( _testName ) ) ;
43
+
44
+ Assert . Equal ( initialHash , _testContributor . GetHashCode ( ) ) ;
45
+ Assert . Equal ( initialEvents , _testContributor . DomainEvents . Count ) ;
46
+ }
47
+
48
+ [ Fact ]
49
+ public void UpdatesNameAndRegistersEventGivenNewName ( )
50
+ {
51
+ _testContributor = CreateContributor ( ) ;
52
+ var initialEvents = _testContributor . DomainEvents . Count ;
53
+ string newName = "A whole new name" ;
54
+
55
+ _testContributor . UpdateName ( ContributorName . From ( newName ) ) ;
56
+
57
+ Assert . Equal ( newName , _testContributor . Name . Value ) ;
58
+ Assert . Equal ( 1 , _testContributor . DomainEvents . Count ) ;
59
+ }
60
+ }
You can’t perform that action at this time.
0 commit comments