Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: fluent builder pattern #845

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ToDoItem> _items = new();
private readonly List<ToDoItem> _items = [];
public IEnumerable<ToDoItem> Items => _items.AsReadOnly();
public ProjectStatus Status => _items.All(i => i.IsDone) ? ProjectStatus.Complete : ProjectStatus.InProgress;

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,31 @@ 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)
{
IsDone = true;

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()
Expand Down
27 changes: 17 additions & 10 deletions src/Clean.Architecture.Core/ContributorAggregate/Contributor.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
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;
Expand Down
Loading