Skip to content

Commit

Permalink
initial cqrs with es version added
Browse files Browse the repository at this point in the history
  • Loading branch information
wojteksuwala committed Feb 8, 2019
1 parent b83d2ce commit 1f4bb9e
Show file tree
Hide file tree
Showing 23 changed files with 1,225 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CqrsWithEs.Tests/TestData/CarsTestData.cs
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
);
}
}
}
46 changes: 46 additions & 0 deletions CqrsWithEs.Tests/TestData/OffersTestData.cs
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;
}
}
}
18 changes: 18 additions & 0 deletions CqrsWithEs.Tests/TestData/PersonsTestData.cs
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"
);
}
}
}
23 changes: 23 additions & 0 deletions CqrsWithEs.Tests/TestData/PolicyTestData.cs
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;
}
}*/
}
25 changes: 25 additions & 0 deletions CqrsWithEs.Tests/TestData/ProductsTestData.cs
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")
}
);
}
}
}
17 changes: 17 additions & 0 deletions CqrsWithEs/Commands/CreatePolicyCommand.cs
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; }
}
}
27 changes: 27 additions & 0 deletions CqrsWithEs/Commands/CreatePolicyHandler.cs
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
});
}
}
}
40 changes: 40 additions & 0 deletions CqrsWithEs/Domain/AggregateRoot.cs
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 { }
}
21 changes: 21 additions & 0 deletions CqrsWithEs/Domain/Car.cs
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);
}
}
}
25 changes: 25 additions & 0 deletions CqrsWithEs/Domain/Cover.cs
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()
{
}
}


}
14 changes: 14 additions & 0 deletions CqrsWithEs/Domain/IOfferRepository.cs
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);
}
}
14 changes: 14 additions & 0 deletions CqrsWithEs/Domain/IProductRepository.cs
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();
}
}
Loading

0 comments on commit 1f4bb9e

Please sign in to comment.