Skip to content

Instantly share code, notes, and snippets.

@JulianMay
Created March 19, 2017 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JulianMay/daa5607b13d8a6b9c9b220a14a3d1fb2 to your computer and use it in GitHub Desktop.
Save JulianMay/daa5607b13d8a6b9c9b220a14a3d1fb2 to your computer and use it in GitHub Desktop.
Domain Service Example
using System;
using Website.Domain;
namespace Website.Domain.Customers
{
public class ContactRegistrationService : IRegisterContactCommandHandler, IActivateExistingContactCommandHandler
{
private readonly ITransactionProvider _transactor;
private readonly ISelfRegistrationValidator _validator;
private readonly ICompanyRepository _companyRepo;
private readonly ICompanyNotifier _notifier;
public ContactRegistrationService(ITransactionProvider transactor, ISelfRegistrationValidator validator,
ICompanyRepository companyRepo, ICompanyNotifier notifier)
{
if (transactor == null) throw new ArgumentNullException("transactor");
if (validator == null) throw new ArgumentNullException("validator");
if (companyRepo == null) throw new ArgumentNullException("companyRepo");
if (notifier == null) throw new ArgumentNullException("notifier");
_transactor = transactor;
_validator = validator;
_companyRepo = companyRepo;
_notifier = notifier;
}
/// <summary>
/// Registers and activates a new contact
/// </summary>
/// <param name="command"></param>
public void RegisterContact(RegisterContactCommand command)
{
if (command == null) throw new ArgumentNullException("command");
using (var transaction = _transactor.NewTransaction())
{
var company = GetValidCompanyId(command.CompanyAgreementCode);
AssertEmailIsAvailable(command.Email);
int createdContactId = _companyRepo.CreateOnlineActiveContact(
new ContactCreationDTO
{
FirstName = command.FirstName,
Surname = command.Surname,
Mobile = command.Mobile,
Email = command.Email,
CompanyCustomerId = company.CustomerNumber,
CompanyName = company.Name});
_notifier.OnContactOnlineRegistered(command.Email, createdContactId, company.CustomerNumber);
transaction.Complete();
}
}
/// <summary>
/// Activates specified contact which belongs to Corporate structure identified by companyAgreementCode
/// </summary>
/// <param name="contactCustomerNumber"></param>
/// <param name="companyAgreementCode"></param>
public void ActivateExistingContact(int contactCustomerNumber, string companyAgreementCode)
{
using (var transaction = _transactor.NewTransaction())
{
var activation = _companyRepo.ActivateExistingContact(contactCustomerNumber, companyAgreementCode);
_notifier.OnContactOnlineActivated(activation.Email, contactCustomerNumber, activation.CompanyCustomerNumber);
}
}
private CompanyInfo GetValidCompanyId(string code)
{
var result = _validator.VerifyCompanyAgreementCode(code);
if(!result.HasMatch)
{
throw new DomainConstraintViolatedException("InvalidCompanyAgreementCode", "The specified company agreement code is not valid");
}
return new CompanyInfo
{
Name = result.MatchedCompany,
CustomerNumber = result.CompanyCustomerId.Value
};
}
private void AssertEmailIsAvailable(string email)
{
if(_companyRepo.ContactEmailIsInUse(email))
{
throw new DomainConstraintViolatedException("EmailIsNotAvailable", "The specified email is already in use");
}
}
private class CompanyInfo
{
public string Name;
public int CustomerNumber;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment