Skip to content

Instantly share code, notes, and snippets.

View MarcoNicolodi's full-sized avatar

Marco Nicolodi MarcoNicolodi

View GitHub Profile
@MarcoNicolodi
MarcoNicolodi / Proposal.cs
Created October 15, 2019 01:07
Proposal using double dispatch to call domain service within a single operation
public class Proposal : Aggregate
{
public void Accept(IDomainService domainService)
{
ChangeStatus(ProposalStatus.Accepted);
base.EventsRaised.Add(new ProposalAcceptedEvent(proposal.Id, proposal.AcceptedAt))
domainService.DenyOtherCompaniesProposals(this);
}
}
@MarcoNicolodi
MarcoNicolodi / Program.cs
Last active November 29, 2019 16:44
Proposal with access to other aggregates lacks boundaries
var proposal = _proposalRepository.Find(id);
proposal.Candidate.Ban();
proposal.JobListing.AddPreRequisite(PreRequisite.Clojure);
proposalRepository.Save(proposal);
@MarcoNicolodi
MarcoNicolodi / Program.cs
Last active October 14, 2019 23:01
Proposal with access with other aggregates lacks boundaries
var proposal = _proposalRepository.Find(id);
proposal.Candidate.Ban();
proposalRepository.Save(proposal);
@MarcoNicolodi
MarcoNicolodi / Proposal.cs
Last active October 14, 2019 22:53
Proposal aggregate with access to other aggregates
public class Proposal : Aggregate
{
public Candidate CandidateId { get; }
public JobListing JobListingId { get; }
}
@MarcoNicolodi
MarcoNicolodi / Proposal.cs
Last active November 29, 2019 13:43
Proposal before being
public class Proposal : Entity
{
public Guid CandidateId { get; }
public Guid JobListingId { get; }
public ProposaStatus Status { get; private set; }
public DateTime? ClosedAt { get; private set; }
public Proposal(Guid candidateId, Guid jobListingId)
{
CandidateId = candidateId;
@MarcoNicolodi
MarcoNicolodi / Program.cs
Created October 13, 2019 20:28
Showing the only way to save comments
var proposal = _proprosalRepository.Find(proposalId);
proposal.Discuss(candidateId, "I would like to know more about benefits");
_proposalRepository.Save(proposal);
@MarcoNicolodi
MarcoNicolodi / Proposal.cs
Last active October 13, 2019 20:13
Proposal aggregate root relationship with Comment entity
public class Proposal
{
private List<Comments> _comments;
public ReadOnlyList<Comments> Comments => _comments.AsReadOnly();
public void Discuss(Guid userId, string content)
{
if(Status != ProposalStatus.Pending)
throw new DomainException("Can't discuss about an closed proposal");
@MarcoNicolodi
MarcoNicolodi / Proposal.cs
Created October 13, 2019 17:33
Anemic proposal with public setters
public class Proposal
{
public DateTime SentAt { get; set; }
public DateTime AcceptedAt { get; set; }
public ProposalStatus Status { get; set; }
public Guid CandidateId { get; set; }
}
@MarcoNicolodi
MarcoNicolodi / ProposalService.cs
Created October 11, 2019 01:30
Using a Domain Service so our domain logic doesnt leak to the Application Service
public class ProposalService
{
public void AcceptProposal(Guid proposalId)
{
var proposal = _repository.Find(proposalId);
proposal.Accept();
_repository.Save(proposal);
_domainService.DenyOtherCompaniesProposals(proposal);
public class DomainService
{
public class DenyOtherCompaniesProposals(Proposal proposal)
{
var proposalsToDeny = _repository.FindOpenForCandidate(proposal.CandidateId);
foreach(var proposal in proposalsToDeny)
proposal.Deny();
_repository.Save(proposalsToDeny);
}