From 4d353d6cef47fd116063d939805b8f99910af9e8 Mon Sep 17 00:00:00 2001 From: drr00t Date: Thu, 5 Aug 2021 22:58:16 -0300 Subject: [PATCH 1/3] start thinking about Aggregation Factory --- .../Aggregates/IAggregateFactory.cs | 18 +++++++++++ src/DFlow.Domain/Command/BaseCommand.cs | 22 +++++++++++++ src/DFlow.Domain/Command/ICommand.cs | 16 ++++++++++ .../Business/Cqrs/CommandHandlerTests.cs | 1 + tests/DFlow.Tests/DFlow.Tests.csproj | 4 --- tests/DFlow.Tests/Domain/AggregatesTests.cs | 17 +++++----- .../Supporting/AddEntityCommandHandler.cs | 1 + .../AddEntityEventBasedCommandHandler.cs | 1 + .../Commands/AddEntityCommand.cs | 20 +++++++++--- .../Commands/UpdateEntityCommand.cs | 0 .../EventBasedAggregateFactory.cs | 31 +++++++++++++++++++ .../EventStreamBusinessEntityAggregateRoot.cs | 19 +++--------- 12 files changed, 119 insertions(+), 31 deletions(-) create mode 100644 src/DFlow.Domain/Aggregates/IAggregateFactory.cs create mode 100644 src/DFlow.Domain/Command/BaseCommand.cs create mode 100644 src/DFlow.Domain/Command/ICommand.cs rename tests/DFlow.Tests/Supporting/{ => DomainObjects}/Commands/AddEntityCommand.cs (56%) rename tests/DFlow.Tests/Supporting/{ => DomainObjects}/Commands/UpdateEntityCommand.cs (100%) create mode 100644 tests/DFlow.Tests/Supporting/DomainObjects/EventBasedAggregateFactory.cs diff --git a/src/DFlow.Domain/Aggregates/IAggregateFactory.cs b/src/DFlow.Domain/Aggregates/IAggregateFactory.cs new file mode 100644 index 0000000..eda730d --- /dev/null +++ b/src/DFlow.Domain/Aggregates/IAggregateFactory.cs @@ -0,0 +1,18 @@ +// Copyright (C) 2020 Road to Agility +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +using DFlow.Domain.Command; + +namespace DFlow.Domain.Aggregates +{ + public interface IAggregateFactory + where TCommandCreate: BaseCommand + { + TAggregate Create(TCommandCreate command); + + TAggregate ReconstructFrom(TReconstruct aggregationRoot); + } +} \ No newline at end of file diff --git a/src/DFlow.Domain/Command/BaseCommand.cs b/src/DFlow.Domain/Command/BaseCommand.cs new file mode 100644 index 0000000..ff80ab8 --- /dev/null +++ b/src/DFlow.Domain/Command/BaseCommand.cs @@ -0,0 +1,22 @@ +// Copyright (C) 2020 Road to Agility +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + + +using System; +using DFlow.Domain.Validation; + +namespace DFlow.Domain.Command +{ + public class BaseCommand: BaseValidation, ICommand + { + public DateTime When { get; } + + public BaseCommand() + { + When = DateTime.Now; + } + } +} \ No newline at end of file diff --git a/src/DFlow.Domain/Command/ICommand.cs b/src/DFlow.Domain/Command/ICommand.cs new file mode 100644 index 0000000..a5f7da3 --- /dev/null +++ b/src/DFlow.Domain/Command/ICommand.cs @@ -0,0 +1,16 @@ +// Copyright (C) 2020 Road to Agility +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + + +using System; + +namespace DFlow.Domain.Command +{ + public interface ICommand + { + DateTime When { get; } + } +} \ No newline at end of file diff --git a/tests/DFlow.Tests/Business/Cqrs/CommandHandlerTests.cs b/tests/DFlow.Tests/Business/Cqrs/CommandHandlerTests.cs index b404e25..c5e2824 100644 --- a/tests/DFlow.Tests/Business/Cqrs/CommandHandlerTests.cs +++ b/tests/DFlow.Tests/Business/Cqrs/CommandHandlerTests.cs @@ -10,6 +10,7 @@ using DFlow.Domain.Events; using DFlow.Tests.Supporting; using DFlow.Tests.Supporting.Commands; +using DFlow.Tests.Supporting.DomainObjects.Commands; using DFlow.Tests.Supporting.DomainObjects.Events; using NSubstitute; using Xunit; diff --git a/tests/DFlow.Tests/DFlow.Tests.csproj b/tests/DFlow.Tests/DFlow.Tests.csproj index c878fef..2d24817 100644 --- a/tests/DFlow.Tests/DFlow.Tests.csproj +++ b/tests/DFlow.Tests/DFlow.Tests.csproj @@ -36,8 +36,4 @@ - - - - diff --git a/tests/DFlow.Tests/Domain/AggregatesTests.cs b/tests/DFlow.Tests/Domain/AggregatesTests.cs index 4a238c8..7aa5707 100644 --- a/tests/DFlow.Tests/Domain/AggregatesTests.cs +++ b/tests/DFlow.Tests/Domain/AggregatesTests.cs @@ -13,6 +13,7 @@ using DFlow.Domain.DomainEvents; using DFlow.Domain.Events; using DFlow.Tests.Supporting.DomainObjects; +using DFlow.Tests.Supporting.DomainObjects.Commands; using Xunit; namespace DFlow.Tests.Domain @@ -54,9 +55,8 @@ public void Aggregate_EventBased_create_a_valid() [Fact] public void Aggregate_EventBased_create_an_invalid() { - var name = Name.Empty(); - var email = Email.Empty(); - var agg = EventStreamBusinessEntityAggregateRoot.Create(EntityTestId.GetNext(), name, email); + var factory = new EventBasedAggregateFactory(); + var agg = factory.Create(new AddEntityCommand("", "")); Assert.False(agg.IsValid); } @@ -65,12 +65,13 @@ public void Aggregate_EventBased_valid_Entity_create() { var fixture = new Fixture() .Customize(new AutoNSubstituteCustomization{ ConfigureMembers = true }); - fixture.Register(()=> Name.From(fixture.Create())); - fixture.Register(()=> Email.From("email@de.com")); + + var name = fixture.Create(); + var email = fixture.Create(); + + var factory = new EventBasedAggregateFactory(); + var agg = factory.Create(new AddEntityCommand(name, email)); - var name = fixture.Create(); - var email = fixture.Create(); - var agg = EventStreamBusinessEntityAggregateRoot.Create(EntityTestId.GetNext(), name, email); var change = agg.GetChange(); Assert.True(agg.IsValid); Assert.True(change.IsValid); diff --git a/tests/DFlow.Tests/Supporting/AddEntityCommandHandler.cs b/tests/DFlow.Tests/Supporting/AddEntityCommandHandler.cs index ff2ecd5..565b728 100644 --- a/tests/DFlow.Tests/Supporting/AddEntityCommandHandler.cs +++ b/tests/DFlow.Tests/Supporting/AddEntityCommandHandler.cs @@ -25,6 +25,7 @@ using DFlow.Domain.Events; using DFlow.Tests.Supporting.Commands; using DFlow.Tests.Supporting.DomainObjects; +using DFlow.Tests.Supporting.DomainObjects.Commands; namespace DFlow.Tests.Supporting { diff --git a/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs b/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs index b3ffa01..f875f44 100644 --- a/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs +++ b/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs @@ -26,6 +26,7 @@ using DFlow.Domain.Events; using DFlow.Tests.Supporting.Commands; using DFlow.Tests.Supporting.DomainObjects; +using DFlow.Tests.Supporting.DomainObjects.Commands; using FluentValidation.Results; namespace DFlow.Tests.Supporting diff --git a/tests/DFlow.Tests/Supporting/Commands/AddEntityCommand.cs b/tests/DFlow.Tests/Supporting/DomainObjects/Commands/AddEntityCommand.cs similarity index 56% rename from tests/DFlow.Tests/Supporting/Commands/AddEntityCommand.cs rename to tests/DFlow.Tests/Supporting/DomainObjects/Commands/AddEntityCommand.cs index a080731..191a793 100644 --- a/tests/DFlow.Tests/Supporting/Commands/AddEntityCommand.cs +++ b/tests/DFlow.Tests/Supporting/DomainObjects/Commands/AddEntityCommand.cs @@ -17,11 +17,23 @@ // -namespace DFlow.Tests.Supporting.Commands +using System.Collections.Immutable; +using DFlow.Domain.Command; +using DFlow.Domain.Validation; + +namespace DFlow.Tests.Supporting.DomainObjects.Commands { - public class AddEntityCommand + public class AddEntityCommand: BaseCommand { - public string Name { get; set; } - public string Mail { get; set; } + public AddEntityCommand(string name, string email) + { + Name = Name.From(name); + Mail = Email.From(email); + + AppendValidationResult(Name.ValidationStatus.Errors.ToImmutableList()); + AppendValidationResult(Mail.ValidationStatus.Errors.ToImmutableList()); + } + public Name Name { get; set; } + public Email Mail { get; set; } } } \ No newline at end of file diff --git a/tests/DFlow.Tests/Supporting/Commands/UpdateEntityCommand.cs b/tests/DFlow.Tests/Supporting/DomainObjects/Commands/UpdateEntityCommand.cs similarity index 100% rename from tests/DFlow.Tests/Supporting/Commands/UpdateEntityCommand.cs rename to tests/DFlow.Tests/Supporting/DomainObjects/Commands/UpdateEntityCommand.cs diff --git a/tests/DFlow.Tests/Supporting/DomainObjects/EventBasedAggregateFactory.cs b/tests/DFlow.Tests/Supporting/DomainObjects/EventBasedAggregateFactory.cs new file mode 100644 index 0000000..2407f9c --- /dev/null +++ b/tests/DFlow.Tests/Supporting/DomainObjects/EventBasedAggregateFactory.cs @@ -0,0 +1,31 @@ +// Copyright (C) 2020 Road to Agility +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +using DFlow.Domain.Aggregates; +using DFlow.Domain.BusinessObjects; +using DFlow.Tests.Supporting.DomainObjects.Commands; + +namespace DFlow.Tests.Supporting.DomainObjects +{ + public class EventBasedAggregateFactory: + IAggregateFactory> + { + public EventStreamBusinessEntityAggregateRoot Create(AddEntityCommand command) + { + return new EventStreamBusinessEntityAggregateRoot(command.Name, + command.Mail, + VersionId.New()); + } + + public EventStreamBusinessEntityAggregateRoot ReconstructFrom(EventStream eventStream) + { + return new EventStreamBusinessEntityAggregateRoot( + EventStream.From(eventStream.AggregationId, + eventStream.Name,VersionId.Next(eventStream.Version), + eventStream.Events)); + } + } +} \ No newline at end of file diff --git a/tests/DFlow.Tests/Supporting/DomainObjects/EventStreamBusinessEntityAggregateRoot.cs b/tests/DFlow.Tests/Supporting/DomainObjects/EventStreamBusinessEntityAggregateRoot.cs index 497883b..68913d3 100644 --- a/tests/DFlow.Tests/Supporting/DomainObjects/EventStreamBusinessEntityAggregateRoot.cs +++ b/tests/DFlow.Tests/Supporting/DomainObjects/EventStreamBusinessEntityAggregateRoot.cs @@ -7,12 +7,12 @@ namespace DFlow.Tests.Supporting.DomainObjects { public sealed class EventStreamBusinessEntityAggregateRoot:EventBasedAggregationRoot { - private EventStreamBusinessEntityAggregateRoot(EntityTestId aggregationId, Name name, Email email, VersionId version) - :base(aggregationId,version, AggregationName.From(nameof(EventStreamBusinessEntityAggregateRoot))) + internal EventStreamBusinessEntityAggregateRoot(Name name, Email email, VersionId version) + :base(EntityTestId.GetNext(),version, AggregationName.From(nameof(EventStreamBusinessEntityAggregateRoot))) { if (name.ValidationStatus.IsValid && email.ValidationStatus.IsValid) { - var change = TestEntityAggregateAddedDomainEvent.From(aggregationId, name, email, version); + var change = TestEntityAggregateAddedDomainEvent.From(AggregateId, name, email, version); Apply(change); // it is always new @@ -23,7 +23,7 @@ private EventStreamBusinessEntityAggregateRoot(EntityTestId aggregationId, Name AppendValidationResult(email.ValidationStatus.Errors.ToImmutableList()); } - private EventStreamBusinessEntityAggregateRoot(EventStream eventStream) + internal EventStreamBusinessEntityAggregateRoot(EventStream eventStream) : base(eventStream.AggregationId, eventStream.Version, AggregationName.From(nameof(EventStreamBusinessEntityAggregateRoot))) { @@ -43,16 +43,5 @@ public void UpdateName(EntityTestId aggregateId, Name name) AppendValidationResult(name.ValidationStatus.Errors.ToImmutableList()); } - - public static EventStreamBusinessEntityAggregateRoot Create(EntityTestId aggregateId, Name name, Email email) - { - return new EventStreamBusinessEntityAggregateRoot(aggregateId, name, email, VersionId.New()); - } - - public static EventStreamBusinessEntityAggregateRoot ReconstructFrom(EventStream eventStream) - { - return new EventStreamBusinessEntityAggregateRoot(EventStream.From(eventStream.AggregationId, - eventStream.Name,VersionId.Next(eventStream.Version), eventStream.Events)); - } } } \ No newline at end of file From 9c397a5c1d4ebdb9a2867a8768c8cd8e4d07a353 Mon Sep 17 00:00:00 2001 From: drr00t Date: Tue, 10 Aug 2021 21:12:06 -0300 Subject: [PATCH 2/3] factory for aggregation create --- src/DFlow.Business.Cqrs/CommandHandler.cs | 5 +++- .../Aggregates/EventBasedAggregationRoot.cs | 2 +- .../Aggregates/IAggregateFactory.cs | 10 +++---- .../Business/Cqrs/CommandHandlerTests.cs | 1 - tests/DFlow.Tests/Domain/AggregatesTests.cs | 5 +--- .../Supporting/AddEntityCommandHandler.cs | 1 - .../AddEntityEventBasedCommandHandler.cs | 1 - .../BusinessEntityAggregateRoot.cs | 2 +- .../Commands/UpdateEntityCommand.cs | 5 ++-- .../EventBasedAggregateFactory.cs | 10 +++---- .../ObjectBasedAggregateFactory.cs | 27 +++++++++++++++++++ .../UpdateEntityEventBasedCommandHandler.cs | 23 +++++++++++----- 12 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 tests/DFlow.Tests/Supporting/DomainObjects/ObjectBasedAggregateFactory.cs diff --git a/src/DFlow.Business.Cqrs/CommandHandler.cs b/src/DFlow.Business.Cqrs/CommandHandler.cs index 0297d1d..40bc1a4 100644 --- a/src/DFlow.Business.Cqrs/CommandHandler.cs +++ b/src/DFlow.Business.Cqrs/CommandHandler.cs @@ -5,15 +5,18 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. using System; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using DFlow.Business.Cqrs.CommandHandlers; using DFlow.Domain.Events; +using DFlow.Domain.Validation; using Microsoft.Extensions.Logging; namespace DFlow.Business.Cqrs { - public abstract class CommandHandler : ICommandHandler + public abstract class CommandHandler: + ICommandHandler { protected IDomainEventBus Publisher { get; } diff --git a/src/DFlow.Domain/Aggregates/EventBasedAggregationRoot.cs b/src/DFlow.Domain/Aggregates/EventBasedAggregationRoot.cs index 9daf056..79bfd1e 100644 --- a/src/DFlow.Domain/Aggregates/EventBasedAggregationRoot.cs +++ b/src/DFlow.Domain/Aggregates/EventBasedAggregationRoot.cs @@ -55,7 +55,7 @@ protected void Raise(IDomainEvent @event) public EventStream GetChange() { - return EventStream.From(AggregateId,Name ,Version, _changes.ToImmutableList()); + return EventStream.From(AggregateId,Name ,Version, _currentStream.ToImmutableList()); } public IReadOnlyList GetEvents() diff --git a/src/DFlow.Domain/Aggregates/IAggregateFactory.cs b/src/DFlow.Domain/Aggregates/IAggregateFactory.cs index eda730d..c9644f9 100644 --- a/src/DFlow.Domain/Aggregates/IAggregateFactory.cs +++ b/src/DFlow.Domain/Aggregates/IAggregateFactory.cs @@ -4,15 +4,13 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -using DFlow.Domain.Command; +using DFlow.Domain.Validation; namespace DFlow.Domain.Aggregates { - public interface IAggregateFactory - where TCommandCreate: BaseCommand + public interface IAggregateFactory + where TCreateFrom: BaseValidation { - TAggregate Create(TCommandCreate command); - - TAggregate ReconstructFrom(TReconstruct aggregationRoot); + TAggregate Create(TCreateFrom source); } } \ No newline at end of file diff --git a/tests/DFlow.Tests/Business/Cqrs/CommandHandlerTests.cs b/tests/DFlow.Tests/Business/Cqrs/CommandHandlerTests.cs index c5e2824..bdc8043 100644 --- a/tests/DFlow.Tests/Business/Cqrs/CommandHandlerTests.cs +++ b/tests/DFlow.Tests/Business/Cqrs/CommandHandlerTests.cs @@ -9,7 +9,6 @@ using AutoFixture.AutoNSubstitute; using DFlow.Domain.Events; using DFlow.Tests.Supporting; -using DFlow.Tests.Supporting.Commands; using DFlow.Tests.Supporting.DomainObjects.Commands; using DFlow.Tests.Supporting.DomainObjects.Events; using NSubstitute; diff --git a/tests/DFlow.Tests/Domain/AggregatesTests.cs b/tests/DFlow.Tests/Domain/AggregatesTests.cs index 7aa5707..5411974 100644 --- a/tests/DFlow.Tests/Domain/AggregatesTests.cs +++ b/tests/DFlow.Tests/Domain/AggregatesTests.cs @@ -5,13 +5,9 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. using System; -using System.Collections.Generic; -using System.Collections.Immutable; using AutoFixture; using AutoFixture.AutoNSubstitute; using DFlow.Domain.BusinessObjects; -using DFlow.Domain.DomainEvents; -using DFlow.Domain.Events; using DFlow.Tests.Supporting.DomainObjects; using DFlow.Tests.Supporting.DomainObjects.Commands; using Xunit; @@ -47,6 +43,7 @@ public void Aggregate_EventBased_create_a_valid() var name = fixture.Create(); var email = fixture.Create(); + var agg = EventStreamBusinessEntityAggregateRoot.Create(EntityTestId.GetNext(), name, email); Assert.Equal(nameof(EventStreamBusinessEntityAggregateRoot),agg.GetChange().Name.Value); Assert.True(agg.IsValid); diff --git a/tests/DFlow.Tests/Supporting/AddEntityCommandHandler.cs b/tests/DFlow.Tests/Supporting/AddEntityCommandHandler.cs index 565b728..ad79b1f 100644 --- a/tests/DFlow.Tests/Supporting/AddEntityCommandHandler.cs +++ b/tests/DFlow.Tests/Supporting/AddEntityCommandHandler.cs @@ -23,7 +23,6 @@ using DFlow.Business.Cqrs; using DFlow.Business.Cqrs.CommandHandlers; using DFlow.Domain.Events; -using DFlow.Tests.Supporting.Commands; using DFlow.Tests.Supporting.DomainObjects; using DFlow.Tests.Supporting.DomainObjects.Commands; diff --git a/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs b/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs index f875f44..1e0588d 100644 --- a/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs +++ b/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs @@ -24,7 +24,6 @@ using DFlow.Business.Cqrs; using DFlow.Business.Cqrs.CommandHandlers; using DFlow.Domain.Events; -using DFlow.Tests.Supporting.Commands; using DFlow.Tests.Supporting.DomainObjects; using DFlow.Tests.Supporting.DomainObjects.Commands; using FluentValidation.Results; diff --git a/tests/DFlow.Tests/Supporting/DomainObjects/BusinessEntityAggregateRoot.cs b/tests/DFlow.Tests/Supporting/DomainObjects/BusinessEntityAggregateRoot.cs index a8eba1b..22ff00a 100644 --- a/tests/DFlow.Tests/Supporting/DomainObjects/BusinessEntityAggregateRoot.cs +++ b/tests/DFlow.Tests/Supporting/DomainObjects/BusinessEntityAggregateRoot.cs @@ -7,7 +7,7 @@ namespace DFlow.Tests.Supporting.DomainObjects { public sealed class BusinessEntityAggregateRoot:ObjectBasedAggregationRoot { - private BusinessEntityAggregateRoot(BusinessEntity businessEntity) + internal BusinessEntityAggregateRoot(BusinessEntity businessEntity) { if (businessEntity.IsValid) { diff --git a/tests/DFlow.Tests/Supporting/DomainObjects/Commands/UpdateEntityCommand.cs b/tests/DFlow.Tests/Supporting/DomainObjects/Commands/UpdateEntityCommand.cs index 2342d07..a5a89f6 100644 --- a/tests/DFlow.Tests/Supporting/DomainObjects/Commands/UpdateEntityCommand.cs +++ b/tests/DFlow.Tests/Supporting/DomainObjects/Commands/UpdateEntityCommand.cs @@ -18,10 +18,11 @@ using System; +using DFlow.Domain.Validation; -namespace DFlow.Tests.Supporting.Commands +namespace DFlow.Tests.Supporting.DomainObjects.Commands { - public class UpdateEntityCommand + public class UpdateEntityCommand: BaseValidation { public string Name { get; set; } public Guid AggregateId { get; set; } diff --git a/tests/DFlow.Tests/Supporting/DomainObjects/EventBasedAggregateFactory.cs b/tests/DFlow.Tests/Supporting/DomainObjects/EventBasedAggregateFactory.cs index 2407f9c..0c6bdd7 100644 --- a/tests/DFlow.Tests/Supporting/DomainObjects/EventBasedAggregateFactory.cs +++ b/tests/DFlow.Tests/Supporting/DomainObjects/EventBasedAggregateFactory.cs @@ -11,7 +11,8 @@ namespace DFlow.Tests.Supporting.DomainObjects { public class EventBasedAggregateFactory: - IAggregateFactory> + IAggregateFactory, + IAggregateFactory> { public EventStreamBusinessEntityAggregateRoot Create(AddEntityCommand command) { @@ -20,12 +21,9 @@ public EventStreamBusinessEntityAggregateRoot Create(AddEntityCommand command) VersionId.New()); } - public EventStreamBusinessEntityAggregateRoot ReconstructFrom(EventStream eventStream) + public EventStreamBusinessEntityAggregateRoot Create(EventStream source) { - return new EventStreamBusinessEntityAggregateRoot( - EventStream.From(eventStream.AggregationId, - eventStream.Name,VersionId.Next(eventStream.Version), - eventStream.Events)); + return new EventStreamBusinessEntityAggregateRoot(source); } } } \ No newline at end of file diff --git a/tests/DFlow.Tests/Supporting/DomainObjects/ObjectBasedAggregateFactory.cs b/tests/DFlow.Tests/Supporting/DomainObjects/ObjectBasedAggregateFactory.cs new file mode 100644 index 0000000..1485dab --- /dev/null +++ b/tests/DFlow.Tests/Supporting/DomainObjects/ObjectBasedAggregateFactory.cs @@ -0,0 +1,27 @@ +// Copyright (C) 2020 Road to Agility +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +using DFlow.Domain.Aggregates; +using DFlow.Domain.BusinessObjects; +using DFlow.Tests.Supporting.DomainObjects.Commands; + +namespace DFlow.Tests.Supporting.DomainObjects +{ + public class ObjectBasedAggregateFactory: + IAggregateFactory, + IAggregateFactory + { + public BusinessEntityAggregateRoot Create(AddEntityCommand command) + { + return new BusinessEntityAggregateRoot(BusinessEntity.New()); + } + + public BusinessEntityAggregateRoot Create(BusinessEntity source) + { + return new BusinessEntityAggregateRoot(source); + } + } +} \ No newline at end of file diff --git a/tests/DFlow.Tests/Supporting/UpdateEntityEventBasedCommandHandler.cs b/tests/DFlow.Tests/Supporting/UpdateEntityEventBasedCommandHandler.cs index 049aba4..0553298 100644 --- a/tests/DFlow.Tests/Supporting/UpdateEntityEventBasedCommandHandler.cs +++ b/tests/DFlow.Tests/Supporting/UpdateEntityEventBasedCommandHandler.cs @@ -23,29 +23,38 @@ using System.Threading.Tasks; using DFlow.Business.Cqrs; using DFlow.Business.Cqrs.CommandHandlers; +using DFlow.Domain.Aggregates; using DFlow.Domain.BusinessObjects; using DFlow.Domain.Events; -using DFlow.Tests.Supporting.Commands; using DFlow.Tests.Supporting.DomainObjects; +using DFlow.Tests.Supporting.DomainObjects.Commands; namespace DFlow.Tests.Supporting { public sealed class UpdateEntityEventBasedCommandHandler : CommandHandler> { - public UpdateEntityEventBasedCommandHandler(IDomainEventBus publisher) + private IAggregateReconstructFactory> + _aggregationRoot; + + public UpdateEntityEventBasedCommandHandler(IDomainEventBus publisher, + IAggregateReconstructFactory> aggregateFactory) :base(publisher) { + _aggregationRoot = aggregateFactory; } protected override Task> ExecuteCommand(UpdateEntityCommand command, CancellationToken cancellationToken) { // FIXME: remove this after clear my mind, i do need port the persistence event sourcing infrastructure now :) - var aggOld = EventStreamBusinessEntityAggregateRoot.Create(EntityTestId.GetNext(), - Name.From("My name"), Email.From("my@mail.com")); + // var aggNew = _aggregationRoot + // .ReconstructFrom(EntityTestId.GetNext(), Name.From("My name"), Email.From("my@mail.com")); - var currentstream = aggOld.GetChange(); - var agg = EventStreamBusinessEntityAggregateRoot.ReconstructFrom(currentstream); - + // var currentstream = aggOld.GetChange(); + // var agg = EventStreamBusinessEntityAggregateRoot.Reconstruct(currentstream); + + var agg = _aggregationRoot + .ReconstructFrom(EventStream.From(EntityTestId.Empty(), + new AggregationName(), VersionId.Empty(), new ImmutableArray())); var isSucceed = agg.IsValid; var okId = Guid.Empty; From d1eb68edc1643d8379e2b2ce4f7bbe3c8313f74d Mon Sep 17 00:00:00 2001 From: drr00t Date: Wed, 11 Aug 2021 21:34:17 -0300 Subject: [PATCH 3/3] aggregate factory based classes --- tests/DFlow.Tests/Domain/AggregatesTests.cs | 15 +++++++----- .../AddEntityEventBasedCommandHandler.cs | 11 ++++++--- .../UpdateEntityEventBasedCommandHandler.cs | 24 +++++++------------ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/tests/DFlow.Tests/Domain/AggregatesTests.cs b/tests/DFlow.Tests/Domain/AggregatesTests.cs index 5411974..35e7019 100644 --- a/tests/DFlow.Tests/Domain/AggregatesTests.cs +++ b/tests/DFlow.Tests/Domain/AggregatesTests.cs @@ -28,7 +28,9 @@ public void Aggregate_create_a_valid() public void Aggregate_reconstruct_a_valid() { var be = BusinessEntity.From(EntityTestId.GetNext(), VersionId.New()); - var agg = BusinessEntityAggregateRoot.ReconstructFrom(be); + + var factory = new ObjectBasedAggregateFactory(); + var agg = factory.Create(be); Assert.True(agg.IsValid); } @@ -41,10 +43,10 @@ public void Aggregate_EventBased_create_a_valid() fixture.Register(()=> Name.From(fixture.Create())); fixture.Register(()=> Email.From("email@de.com")); - var name = fixture.Create(); - var email = fixture.Create(); - - var agg = EventStreamBusinessEntityAggregateRoot.Create(EntityTestId.GetNext(), name, email); + var addEntity = fixture.Create(); + + var factory = new EventBasedAggregateFactory(); + var agg = factory.Create(addEntity); Assert.Equal(nameof(EventStreamBusinessEntityAggregateRoot),agg.GetChange().Name.Value); Assert.True(agg.IsValid); } @@ -62,7 +64,8 @@ public void Aggregate_EventBased_valid_Entity_create() { var fixture = new Fixture() .Customize(new AutoNSubstituteCustomization{ ConfigureMembers = true }); - + fixture.Register(()=> "email@de.com"); + var name = fixture.Create(); var email = fixture.Create(); diff --git a/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs b/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs index 1e0588d..70fafda 100644 --- a/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs +++ b/tests/DFlow.Tests/Supporting/AddEntityEventBasedCommandHandler.cs @@ -23,6 +23,8 @@ using System.Threading.Tasks; using DFlow.Business.Cqrs; using DFlow.Business.Cqrs.CommandHandlers; +using DFlow.Domain.Aggregates; +using DFlow.Domain.BusinessObjects; using DFlow.Domain.Events; using DFlow.Tests.Supporting.DomainObjects; using DFlow.Tests.Supporting.DomainObjects.Commands; @@ -32,15 +34,18 @@ namespace DFlow.Tests.Supporting { public sealed class AddEntityEventBasedCommandHandler : CommandHandler> { - public AddEntityEventBasedCommandHandler(IDomainEventBus publisher) + private IAggregateFactory _aggregateFactory; + + public AddEntityEventBasedCommandHandler(IDomainEventBus publisher, + IAggregateFactory aggregateFactory) :base(publisher) { + _aggregateFactory = aggregateFactory; } protected override Task> ExecuteCommand(AddEntityCommand command, CancellationToken cancellationToken) { - var agg = EventStreamBusinessEntityAggregateRoot.Create(EntityTestId.GetNext(), - Name.From(command.Name), Email.From(command.Mail)); + var agg = _aggregateFactory.Create(command); var isSucceed = false; var okId = Guid.Empty; diff --git a/tests/DFlow.Tests/Supporting/UpdateEntityEventBasedCommandHandler.cs b/tests/DFlow.Tests/Supporting/UpdateEntityEventBasedCommandHandler.cs index 0553298..99b3707 100644 --- a/tests/DFlow.Tests/Supporting/UpdateEntityEventBasedCommandHandler.cs +++ b/tests/DFlow.Tests/Supporting/UpdateEntityEventBasedCommandHandler.cs @@ -17,7 +17,6 @@ // using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -33,28 +32,23 @@ namespace DFlow.Tests.Supporting { public sealed class UpdateEntityEventBasedCommandHandler : CommandHandler> { - private IAggregateReconstructFactory> - _aggregationRoot; + private IAggregateFactory> + _aggregateFactory; public UpdateEntityEventBasedCommandHandler(IDomainEventBus publisher, - IAggregateReconstructFactory> aggregateFactory) + IAggregateFactory> aggregateFactory) :base(publisher) { - _aggregationRoot = aggregateFactory; + _aggregateFactory = aggregateFactory; } protected override Task> ExecuteCommand(UpdateEntityCommand command, CancellationToken cancellationToken) { - // FIXME: remove this after clear my mind, i do need port the persistence event sourcing infrastructure now :) - // var aggNew = _aggregationRoot - // .ReconstructFrom(EntityTestId.GetNext(), Name.From("My name"), Email.From("my@mail.com")); - - // var currentstream = aggOld.GetChange(); - // var agg = EventStreamBusinessEntityAggregateRoot.Reconstruct(currentstream); - - var agg = _aggregationRoot - .ReconstructFrom(EventStream.From(EntityTestId.Empty(), - new AggregationName(), VersionId.Empty(), new ImmutableArray())); + var agg = _aggregateFactory.Create( + EventStream.From(EntityTestId.Empty(), + new AggregationName(), + VersionId.Empty(), new ImmutableArray()) + ); var isSucceed = agg.IsValid; var okId = Guid.Empty;