Skip to content

Commit 0b48008

Browse files
committed
Fluent builder pattern
1 parent b98c25d commit 0b48008

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

sample/src/NimblePros.SampleToDo.Core/ContributorAggregate/Contributor.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@ public class Contributor : EntityBase, IAggregateRoot
66

77
public Contributor(string name)
88
{
9-
SetName(name);
9+
UpdateName(name);
1010
}
1111

12-
public void UpdateName(string newName)
13-
{
14-
SetName(newName);
15-
}
16-
17-
public Contributor SetName(string newName)
12+
public Contributor UpdateName(string newName)
1813
{
1914
this.Name = Guard.Against.NullOrEmpty(newName, nameof(newName));
2015
return this;

sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/Project.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace NimblePros.SampleToDo.Core.ProjectAggregate;
44

55
public class Project : EntityBase, IAggregateRoot
66
{
7-
public string Name { get; private set; }
7+
public string Name { get; private set; } = default!;
88

9-
private readonly List<ToDoItem> _items = new();
9+
private readonly List<ToDoItem> _items = [];
1010
public IEnumerable<ToDoItem> Items => _items.AsReadOnly();
1111
public ProjectStatus Status => _items.All(i => i.IsDone) ? ProjectStatus.Complete : ProjectStatus.InProgress;
1212

@@ -15,21 +15,23 @@ public class Project : EntityBase, IAggregateRoot
1515

1616
public Project(string name, Priority priority)
1717
{
18-
Name = Guard.Against.NullOrEmpty(name);
18+
UpdateName(name);
1919
Priority = priority;
2020
}
2121

22-
public void AddItem(ToDoItem newItem)
22+
public Project AddItem(ToDoItem newItem)
2323
{
2424
Guard.Against.Null(newItem);
2525
_items.Add(newItem);
2626

2727
var newItemAddedEvent = new NewItemAddedEvent(this, newItem);
2828
base.RegisterDomainEvent(newItemAddedEvent);
29+
return this;
2930
}
3031

31-
public void UpdateName(string newName)
32+
public Project UpdateName(string newName)
3233
{
3334
Name = Guard.Against.NullOrEmpty(newName);
35+
return this;
3436
}
3537
}

sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/ToDoItem.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,31 @@ public class ToDoItem : EntityBase
99
public int? ContributorId { get; private set; } // tasks don't have anyone assigned when first created
1010
public bool IsDone { get; private set; }
1111

12-
public void MarkComplete()
12+
public ToDoItem MarkComplete()
1313
{
1414
if (!IsDone)
1515
{
1616
IsDone = true;
1717

1818
RegisterDomainEvent(new ToDoItemCompletedEvent(this));
1919
}
20+
return this;
2021
}
2122

22-
public void AddContributor(int contributorId)
23+
public ToDoItem AddContributor(int contributorId)
2324
{
2425
Guard.Against.Null(contributorId);
2526
ContributorId = contributorId;
2627

2728
var contributorAddedToItem = new ContributorAddedToItemEvent(this, contributorId);
2829
base.RegisterDomainEvent(contributorAddedToItem);
30+
return this;
2931
}
3032

31-
public void RemoveContributor()
33+
public ToDoItem RemoveContributor()
3234
{
3335
ContributorId = null;
36+
return this;
3437
}
3538

3639
public override string ToString()

src/Clean.Architecture.Core/ContributorAggregate/Contributor.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,29 @@
33

44
namespace Clean.Architecture.Core.ContributorAggregate;
55

6-
public class Contributor(string name) : EntityBase, IAggregateRoot
6+
public class Contributor : EntityBase, IAggregateRoot
77
{
8-
// Example of validating primary constructor inputs
9-
// See: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/primary-constructors#initialize-base-class
10-
public string Name { get; private set; } = Guard.Against.NullOrEmpty(name, nameof(name));
8+
public Contributor(string name)
9+
{
10+
UpdateName(name);
11+
}
12+
public string Name { get; private set; } = default!;
1113
public ContributorStatus Status { get; private set; } = ContributorStatus.NotSet;
1214
public PhoneNumber? PhoneNumber { get; private set; }
15+
public Contributor SetPhoneNumber(string phoneNumber)
16+
{
17+
PhoneNumber = new PhoneNumber(string.Empty, phoneNumber, string.Empty);
18+
return this;
19+
}
1320

14-
public void SetPhoneNumber(string phoneNumber) => PhoneNumber = new PhoneNumber(string.Empty, phoneNumber, string.Empty);
15-
16-
public void UpdateName(string newName) => Name = Guard.Against.NullOrEmpty(newName, nameof(newName));
21+
public Contributor UpdateName(string newName)
22+
{
23+
Name = Guard.Against.NullOrEmpty(newName, nameof(newName));
24+
return this;
25+
}
1726
}
1827

19-
public class PhoneNumber(string countryCode,
20-
string number,
21-
string? extension) : ValueObject
28+
public class PhoneNumber(string countryCode, string number, string? extension) : ValueObject
2229
{
2330
public string CountryCode { get; private set; } = countryCode;
2431
public string Number { get; private set; } = number;

0 commit comments

Comments
 (0)