forked from asc-lab/dotnet-cqrs-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b83d2ce
commit 1f4bb9e
Showing
23 changed files
with
1,225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using CqrsWithEs.Domain; | ||
|
||
namespace CqrsWithEs.Tests | ||
{ | ||
public class CarsTestData | ||
{ | ||
public static Car OldFordFocus() | ||
{ | ||
return new Car | ||
( | ||
"Ford Focus", | ||
"WAW1010", | ||
2005 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using CqrsWithEs.Domain; | ||
using NodaMoney; | ||
using NodaTime; | ||
|
||
namespace CqrsWithEs.Tests | ||
{ | ||
public class OffersTestData | ||
{ | ||
public static Offer StandardOneYearOCOfferValidUntil(DateTime validityEnd) | ||
{ | ||
var product = ProductsTestData.StandardCarInsurance(); | ||
return new Offer | ||
( | ||
Guid.NewGuid(), | ||
"1", | ||
product, | ||
PersonsTestData.Kowalski(), | ||
PersonsTestData.Kowalski(), | ||
CarsTestData.OldFordFocus(), | ||
TimeSpan.FromDays(365), | ||
Money.Euro(500), | ||
validityEnd.AddDays(-30), | ||
new Dictionary<Cover, Money>() | ||
{ | ||
{product.CoverWithCode("OC"), Money.Euro(500) } | ||
} | ||
); | ||
} | ||
|
||
public static Offer RejectedOfferValidUntil(DateTime validityEnd) | ||
{ | ||
var offer = StandardOneYearOCOfferValidUntil(validityEnd); | ||
offer.Reject(); | ||
return offer; | ||
} | ||
|
||
public static Offer ConvertedOfferValidUntil(DateTime validityEnd) | ||
{ | ||
var offer = StandardOneYearOCOfferValidUntil(validityEnd); | ||
offer.Convert(); | ||
return offer; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using CqrsWithEs.Domain; | ||
|
||
namespace CqrsWithEs.Tests | ||
{ | ||
public class PersonsTestData | ||
{ | ||
public static Person Kowalski() | ||
{ | ||
return new Person | ||
( | ||
"Jan", | ||
"Kowalski", | ||
"1111111116" | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using CqrsWithEs.Domain; | ||
|
||
namespace CqrsWithEs.Tests | ||
{ | ||
/*public static class PolicyTestData | ||
{ | ||
public static Policy StandardOneYearPolicy(DateTime policyStartDate) | ||
{ | ||
var offer = OffersTestData.StandardOneYearOCOfferValidUntil(policyStartDate.AddDays(10)); | ||
return Policy.ConvertOffer(offer, "POL0001", policyStartDate.AddDays(-1), policyStartDate); | ||
} | ||
public static Policy StandardOneYearPolicyTerminated(DateTime policyStartDate, DateTime policyTerminationDate) | ||
{ | ||
var policy = StandardOneYearPolicy(policyStartDate); | ||
policy.TerminatePolicy(policyTerminationDate); | ||
policy.ConfirmChanges(2); | ||
return policy; | ||
} | ||
}*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using CqrsWithEs.Domain; | ||
|
||
namespace CqrsWithEs.Tests | ||
{ | ||
public class ProductsTestData | ||
{ | ||
public static Product StandardCarInsurance() | ||
{ | ||
return new Product | ||
( | ||
Guid.NewGuid(), | ||
"STD_CAR_INSURANCE", | ||
"Standard Car Insurance", | ||
new List<Cover> | ||
{ | ||
new Cover(Guid.NewGuid(), "OC", "Third party liability"), | ||
new Cover(Guid.NewGuid(), "AC", "Auto casco"), | ||
new Cover(Guid.NewGuid(), "ASSISTANCE", "Assistance") | ||
} | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using MediatR; | ||
|
||
namespace CqrsWithEs.Commands | ||
{ | ||
public class CreatePolicyCommand : IRequest<CreatePolicyResult> | ||
{ | ||
public string OfferNumber { get; set; } | ||
public DateTime PurchaseDate { get; set; } | ||
public DateTime PolicyStartDate { get; set; } | ||
} | ||
|
||
public class CreatePolicyResult | ||
{ | ||
public string PolicyNumber { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CqrsWithEs.Domain; | ||
using MediatR; | ||
|
||
namespace CqrsWithEs.Commands | ||
{ | ||
public class CreatePolicyHandler : IRequestHandler<CreatePolicyCommand, CreatePolicyResult> | ||
{ | ||
private IOfferRepository offerRepository; | ||
|
||
public CreatePolicyHandler(IOfferRepository offerRepository) | ||
{ | ||
this.offerRepository = offerRepository; | ||
} | ||
|
||
public Task<CreatePolicyResult> Handle(CreatePolicyCommand request, CancellationToken cancellationToken) | ||
{ | ||
var offer = offerRepository.WithNumber(request.OfferNumber); | ||
|
||
return Task.FromResult(new CreatePolicyResult | ||
{ | ||
PolicyNumber = null | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using CqrsWithEs.Dragons; | ||
|
||
namespace CqrsWithEs.Domain | ||
{ | ||
public class AggregateRoot | ||
{ | ||
private readonly List<Event> _changes = new List<Event>(); | ||
|
||
public Guid Id { get; protected set; } | ||
public int Version { get; internal set; } | ||
|
||
public IEnumerable<Event> GetUncommittedChanges() { | ||
return _changes; | ||
} | ||
|
||
public void MarkChangesAsCommitted() { | ||
_changes.Clear(); | ||
} | ||
|
||
public void LoadsFromHistory(IEnumerable<Event> history) { | ||
foreach (var e in history) ApplyChange(e, false); | ||
} | ||
|
||
protected void ApplyChange(Event @event) { | ||
ApplyChange(@event, true); | ||
} | ||
|
||
// push atomic aggregate changes to local history for further processing (EventStore.SaveEvents) | ||
private void ApplyChange(Event @event, bool isNew) { | ||
this.AsDynamic().Apply(@event); | ||
if (isNew) _changes.Add(@event); | ||
} | ||
} | ||
|
||
public interface Message { } | ||
public class Command : Message { } | ||
public class Event : Message { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace CqrsWithEs.Domain | ||
{ | ||
public class Car | ||
{ | ||
public string Make { get; private set; } | ||
public string PlateNumber { get; private set; } | ||
public int ProductionYear { get; private set; } | ||
|
||
public Car(string make, string plateNumber, int productionYear) | ||
{ | ||
Make = make; | ||
PlateNumber = plateNumber; | ||
ProductionYear = productionYear; | ||
} | ||
|
||
public Car Copy() | ||
{ | ||
return new Car(Make, PlateNumber, ProductionYear); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
|
||
namespace CqrsWithEs.Domain | ||
{ | ||
public class Cover | ||
{ | ||
public Guid Id { get; private set; } | ||
public string Code { get; private set; } | ||
public string Name { get; private set; } | ||
|
||
public Cover(Guid id, string code, string name) | ||
{ | ||
Id = id; | ||
Code = code; | ||
Name = name; | ||
} | ||
|
||
//required by EF | ||
protected Cover() | ||
{ | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace CqrsWithEs.Domain | ||
{ | ||
public interface IOfferRepository | ||
{ | ||
Task<Offer> WithNumber(string number); | ||
|
||
Task<IReadOnlyList<Offer>> All(); | ||
|
||
void Add(Offer offer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace CqrsWithEs.Domain | ||
{ | ||
public interface IProductRepository | ||
{ | ||
void Add(Product product); | ||
|
||
Task<Product> WithCode(string code); | ||
|
||
Task<IReadOnlyList<Product>> All(); | ||
} | ||
} |
Oops, something went wrong.