From 0b48008f4b916fe33bdaa9b4e486fdb8525110e9 Mon Sep 17 00:00:00 2001 From: aminsharifi Date: Wed, 16 Oct 2024 19:24:03 +0330 Subject: [PATCH] Fluent builder pattern --- .../ContributorAggregate/Contributor.cs | 9 ++----- .../ProjectAggregate/Project.cs | 12 +++++---- .../ProjectAggregate/ToDoItem.cs | 9 ++++--- .../ContributorAggregate/Contributor.cs | 27 ++++++++++++------- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/sample/src/NimblePros.SampleToDo.Core/ContributorAggregate/Contributor.cs b/sample/src/NimblePros.SampleToDo.Core/ContributorAggregate/Contributor.cs index 14e9dfb7e..09036785d 100644 --- a/sample/src/NimblePros.SampleToDo.Core/ContributorAggregate/Contributor.cs +++ b/sample/src/NimblePros.SampleToDo.Core/ContributorAggregate/Contributor.cs @@ -6,15 +6,10 @@ public class Contributor : EntityBase, IAggregateRoot public Contributor(string name) { - SetName(name); + UpdateName(name); } - public void UpdateName(string newName) - { - SetName(newName); - } - - public Contributor SetName(string newName) + public Contributor UpdateName(string newName) { this.Name = Guard.Against.NullOrEmpty(newName, nameof(newName)); return this; diff --git a/sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/Project.cs b/sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/Project.cs index 26d539de3..bec6a66ef 100644 --- a/sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/Project.cs +++ b/sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/Project.cs @@ -4,9 +4,9 @@ namespace NimblePros.SampleToDo.Core.ProjectAggregate; public class Project : EntityBase, IAggregateRoot { - public string Name { get; private set; } + public string Name { get; private set; } = default!; - private readonly List _items = new(); + private readonly List _items = []; public IEnumerable Items => _items.AsReadOnly(); public ProjectStatus Status => _items.All(i => i.IsDone) ? ProjectStatus.Complete : ProjectStatus.InProgress; @@ -15,21 +15,23 @@ public class Project : EntityBase, IAggregateRoot public Project(string name, Priority priority) { - Name = Guard.Against.NullOrEmpty(name); + UpdateName(name); Priority = priority; } - public void AddItem(ToDoItem newItem) + public Project AddItem(ToDoItem newItem) { Guard.Against.Null(newItem); _items.Add(newItem); var newItemAddedEvent = new NewItemAddedEvent(this, newItem); base.RegisterDomainEvent(newItemAddedEvent); + return this; } - public void UpdateName(string newName) + public Project UpdateName(string newName) { Name = Guard.Against.NullOrEmpty(newName); + return this; } } diff --git a/sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/ToDoItem.cs b/sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/ToDoItem.cs index caa228bef..5d687b262 100644 --- a/sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/ToDoItem.cs +++ b/sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/ToDoItem.cs @@ -9,7 +9,7 @@ public class ToDoItem : EntityBase public int? ContributorId { get; private set; } // tasks don't have anyone assigned when first created public bool IsDone { get; private set; } - public void MarkComplete() + public ToDoItem MarkComplete() { if (!IsDone) { @@ -17,20 +17,23 @@ public void MarkComplete() RegisterDomainEvent(new ToDoItemCompletedEvent(this)); } + return this; } - public void AddContributor(int contributorId) + public ToDoItem AddContributor(int contributorId) { Guard.Against.Null(contributorId); ContributorId = contributorId; var contributorAddedToItem = new ContributorAddedToItemEvent(this, contributorId); base.RegisterDomainEvent(contributorAddedToItem); + return this; } - public void RemoveContributor() + public ToDoItem RemoveContributor() { ContributorId = null; + return this; } public override string ToString() diff --git a/src/Clean.Architecture.Core/ContributorAggregate/Contributor.cs b/src/Clean.Architecture.Core/ContributorAggregate/Contributor.cs index b9e9136cd..fce73fe79 100644 --- a/src/Clean.Architecture.Core/ContributorAggregate/Contributor.cs +++ b/src/Clean.Architecture.Core/ContributorAggregate/Contributor.cs @@ -3,22 +3,29 @@ namespace Clean.Architecture.Core.ContributorAggregate; -public class Contributor(string name) : EntityBase, IAggregateRoot +public class Contributor : EntityBase, IAggregateRoot { - // Example of validating primary constructor inputs - // See: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/primary-constructors#initialize-base-class - public string Name { get; private set; } = Guard.Against.NullOrEmpty(name, nameof(name)); + public Contributor(string name) + { + UpdateName(name); + } + public string Name { get; private set; } = default!; public ContributorStatus Status { get; private set; } = ContributorStatus.NotSet; public PhoneNumber? PhoneNumber { get; private set; } + public Contributor SetPhoneNumber(string phoneNumber) + { + PhoneNumber = new PhoneNumber(string.Empty, phoneNumber, string.Empty); + return this; + } - public void SetPhoneNumber(string phoneNumber) => PhoneNumber = new PhoneNumber(string.Empty, phoneNumber, string.Empty); - - public void UpdateName(string newName) => Name = Guard.Against.NullOrEmpty(newName, nameof(newName)); + public Contributor UpdateName(string newName) + { + Name = Guard.Against.NullOrEmpty(newName, nameof(newName)); + return this; + } } -public class PhoneNumber(string countryCode, - string number, - string? extension) : ValueObject +public class PhoneNumber(string countryCode, string number, string? extension) : ValueObject { public string CountryCode { get; private set; } = countryCode; public string Number { get; private set; } = number;