Skip to content

Commit ec2d7fc

Browse files
aminsharifiardalis
andauthored
refactor: fluent builder pattern (ardalis#845)
* Fluent builder pattern * Update sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/Project.cs * Update src/Clean.Architecture.Core/ContributorAggregate/Contributor.cs --------- Co-authored-by: Steve Smith <[email protected]>
1 parent 3243757 commit ec2d7fc

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ public class Contributor : EntityBase, IAggregateRoot
88

99
public Contributor(ContributorName name)
1010
{
11-
Name = name;
11+
Name = name;
1212
}
1313

14-
public void UpdateName(ContributorName newName)
14+
public Contributor UpdateName(ContributorName newName)
1515
{
1616
if (Name.Equals(newName)) return;
1717
Name = newName;
1818
this.RegisterDomainEvent(new ContributorNameUpdatedEvent(this));
19+
return this;
1920
}
2021
}
2122

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Project : EntityBase<Project, ProjectId>, IAggregateRoot
66
{
77
public ProjectName Name { get; private set; }
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,17 +15,19 @@ public Project(ProjectName name)
1515
Name = name;
1616
}
1717

18-
public void AddItem(ToDoItem newItem)
18+
public Project AddItem(ToDoItem newItem)
1919
{
2020
Guard.Against.Null(newItem);
2121
_items.Add(newItem);
2222

2323
var newItemAddedEvent = new NewItemAddedEvent(this, newItem);
2424
base.RegisterDomainEvent(newItemAddedEvent);
25+
return this;
2526
}
2627

27-
public void UpdateName(ProjectName newName)
28+
public Project UpdateName(ProjectName newName)
2829
{
2930
Name = newName;
31+
return this;
3032
}
3133
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,31 @@ public ToDoItem(Priority priority)
2020

2121
public Priority Priority { get; private set; }
2222

23-
24-
public void MarkComplete()
23+
public ToDoItem MarkComplete()
2524
{
2625
if (!IsDone)
2726
{
2827
IsDone = true;
2928

3029
RegisterDomainEvent(new ToDoItemCompletedEvent(this));
3130
}
31+
return this;
3232
}
3333

34-
public void AddContributor(int contributorId)
34+
public ToDoItem AddContributor(int contributorId)
3535
{
3636
Guard.Against.Null(contributorId);
3737
ContributorId = contributorId;
3838

3939
var contributorAddedToItem = new ContributorAddedToItemEvent(this, contributorId);
4040
base.RegisterDomainEvent(contributorAddedToItem);
41+
return this;
4142
}
4243

43-
public void RemoveContributor()
44+
public ToDoItem RemoveContributor()
4445
{
4546
ContributorId = null;
47+
return this;
4648
}
4749

4850
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
@@ -1,21 +1,28 @@
11
namespace Clean.Architecture.Core.ContributorAggregate;
22

3-
public class Contributor(string name) : EntityBase, IAggregateRoot
3+
public class Contributor : EntityBase, IAggregateRoot
44
{
5-
// Example of validating primary constructor inputs
6-
// See: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/primary-constructors#initialize-base-class
7-
public string Name { get; private set; } = Guard.Against.NullOrEmpty(name, nameof(name));
5+
public Contributor(string name)
6+
{
7+
UpdateName(name); // TODO: Replace with value object and use primary constructor to populate field.
8+
}
9+
public string Name { get; private set; } = default!;
810
public ContributorStatus Status { get; private set; } = ContributorStatus.NotSet;
911
public PhoneNumber? PhoneNumber { get; private set; }
12+
public Contributor SetPhoneNumber(string phoneNumber)
13+
{
14+
PhoneNumber = new PhoneNumber(string.Empty, phoneNumber, string.Empty);
15+
return this;
16+
}
1017

11-
public void SetPhoneNumber(string phoneNumber) => PhoneNumber = new PhoneNumber(string.Empty, phoneNumber, string.Empty);
12-
13-
public void UpdateName(string newName) => Name = Guard.Against.NullOrEmpty(newName, nameof(newName));
18+
public Contributor UpdateName(string newName)
19+
{
20+
Name = Guard.Against.NullOrEmpty(newName, nameof(newName));
21+
return this;
22+
}
1423
}
1524

16-
public class PhoneNumber(string countryCode,
17-
string number,
18-
string? extension) : ValueObject
25+
public class PhoneNumber(string countryCode, string number, string? extension) : ValueObject
1926
{
2027
public string CountryCode { get; private set; } = countryCode;
2128
public string Number { get; private set; } = number;

0 commit comments

Comments
 (0)