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

Added self-delegation functions in Customer Entity #47

Open
wants to merge 3 commits into
base: master
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
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
using SampleProject.Domain.Customers;

namespace SampleProject.Application.Customers.DomainServices
{
class CustomerEmailChecker : ICustomerEmailChecker
{
private readonly string _customerEmail;

public CustomerEmailChecker(string customerEmail)
{
_customerEmail = customerEmail;
}

public bool IsValid(string customerEmail)
{
try
{
var emailAddress = new MailAddress(customerEmail);
}
catch
{
return false;
}

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@ public class RegisterCustomerCommandHandler : ICommandHandler<RegisterCustomerCo
private readonly ICustomerRepository _customerRepository;
private readonly ICustomerUniquenessChecker _customerUniquenessChecker;
private readonly IUnitOfWork _unitOfWork;
private readonly ICustomerEmailChecker _customerEmailChecker;

public RegisterCustomerCommandHandler(
ICustomerRepository customerRepository,
ICustomerUniquenessChecker customerUniquenessChecker,
ICustomerUniquenessChecker customerUniquenessChecker,
ICustomerEmailChecker customerEmailChecker,
IUnitOfWork unitOfWork)
{
this._customerRepository = customerRepository;
_customerUniquenessChecker = customerUniquenessChecker;
_customerEmailChecker = customerEmailChecker;
_unitOfWork = unitOfWork;
}

public async Task<CustomerDto> Handle(RegisterCustomerCommand request, CancellationToken cancellationToken)
{
var customer = Customer.CreateRegistered(request.Email, request.Name, this._customerUniquenessChecker);
var customer = Customer.CreateRegistered(request.Email, request.Name, this._customerUniquenessChecker, this._customerEmailChecker);

if (customer == null)
{
// What is the best way to return error to user?
return new CustomerDto();
}

await this._customerRepository.AddAsync(customer);

Expand Down
54 changes: 47 additions & 7 deletions src/SampleProject.Domain/Customers/Customer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Mail;
using SampleProject.Domain.Customers.Orders;
using SampleProject.Domain.Customers.Orders.Events;
using SampleProject.Domain.Customers.Rules;
Expand Down Expand Up @@ -30,9 +33,9 @@ private Customer()
private Customer(string email, string name)
{
this.Id = new CustomerId(Guid.NewGuid());
_email = email;
_name = name;
_welcomeEmailWasSent = false;
SetCustomerEmail(email);
SetCustomerName(name);
SetWelcomeEmailSentStatus(false);
_orders = new List<Order>();

this.AddDomainEvent(new CustomerRegisteredEvent(this.Id));
Expand All @@ -41,10 +44,32 @@ private Customer(string email, string name)
public static Customer CreateRegistered(
string email,
string name,
ICustomerUniquenessChecker customerUniquenessChecker)
ICustomerUniquenessChecker customerUniquenessChecker,
ICustomerEmailChecker customerEmailChecker = null)
{
CheckRule(new CustomerEmailMustBeUniqueRule(customerUniquenessChecker, email));

try
{
if (customerEmailChecker != null)
{
CheckRule(new CustomerEmailValidRule(customerEmailChecker, email));
}
}
catch (BusinessRuleValidationException ex)
{
return null;
}

try
{

CheckRule(new CustomerEmailMustBeUniqueRule(customerUniquenessChecker, email));
}

catch(BusinessRuleValidationException ex)
{
//FIXME: Maybe throw duplicate email exception? Or log the error?
return null;
}
return new Customer(email, name);
}

Expand Down Expand Up @@ -91,7 +116,22 @@ public void RemoveOrder(OrderId orderId)

public void MarkAsWelcomedByEmail()
{
this._welcomeEmailWasSent = true;
SetWelcomeEmailSentStatus(true);
}

private void SetCustomerEmail(string email)
{
_email = email;
}

private void SetCustomerName(string name)
{
_name = name;
}

private void SetWelcomeEmailSentStatus(bool wasSent)
{
_welcomeEmailWasSent = wasSent;
}
}
}
7 changes: 7 additions & 0 deletions src/SampleProject.Domain/Customers/ICustomerEmailChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SampleProject.Domain.Customers
{
public interface ICustomerEmailChecker
{
bool IsValid(string customerEmail);
}
}
27 changes: 27 additions & 0 deletions src/SampleProject.Domain/Customers/Rules/CustomerEmailValidRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using SampleProject.Domain.SeedWork;

namespace SampleProject.Domain.Customers.Rules
{
internal class CustomerEmailValidRule : IBusinessRule
{
private readonly ICustomerEmailChecker _customerEmailChecker;

private readonly string _email;

public CustomerEmailValidRule(
ICustomerEmailChecker customerEmailChecker,
string email)
{
_customerEmailChecker = customerEmailChecker;
_email = email;
}

public bool IsBroken()
{
return !_customerEmailChecker.IsValid(_email);
}


public string Message => "Customer with this email already exists.";
}
}
4 changes: 4 additions & 0 deletions src/SampleProject.Infrastructure/Domain/DomainModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ protected override void Load(ContainerBuilder builder)
.As<ICustomerUniquenessChecker>()
.InstancePerLifetimeScope();

builder.RegisterType<ICustomerEmailChecker>()
.As<ICustomerEmailChecker>()
.InstancePerLifetimeScope();

builder.RegisterType<ForeignExchange>()
.As<IForeignExchange>()
.InstancePerLifetimeScope();
Expand Down