diff --git a/README.md b/README.md index a4784772f..3ac64b85d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Read the docs at [https://docs.squidex.io/](https://docs.squidex.io/) (work in p ## Status -Current Version 1.0-beta2 +Current Version 1.0-beta3. Roadmap: https://trello.com/b/KakM4F3S/squidex-roadmap ## Prerequisites diff --git a/src/Squidex.Infrastructure/CQRS/Commands/AggregateHandler.cs b/src/Squidex.Infrastructure/CQRS/Commands/AggregateHandler.cs index 654a4f331..45f3ebddb 100644 --- a/src/Squidex.Infrastructure/CQRS/Commands/AggregateHandler.cs +++ b/src/Squidex.Infrastructure/CQRS/Commands/AggregateHandler.cs @@ -44,7 +44,7 @@ namespace Squidex.Infrastructure.CQRS.Commands Guard.NotNull(context, nameof(context)); var aggregateCommand = GetCommand(context); - var aggregate = (T)domainObjectFactory.CreateNew(typeof(T), aggregateCommand.AggregateId); + var aggregate = domainObjectFactory.CreateNew(aggregateCommand.AggregateId); await creator(aggregate); diff --git a/src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectFactory.cs b/src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectFactory.cs index 1308de330..e61372ba1 100644 --- a/src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectFactory.cs +++ b/src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectFactory.cs @@ -23,19 +23,18 @@ namespace Squidex.Infrastructure.CQRS.Commands this.serviceProvider = serviceProvider; } - public IAggregate CreateNew(Type type, Guid id) + public T CreateNew(Guid id) where T : IAggregate { - var factoryFunctionType = typeof(DomainObjectFactoryFunction<>).MakeGenericType(type); - var factoryFunction = (Delegate)serviceProvider.GetService(factoryFunctionType); + var factoryFunction = (DomainObjectFactoryFunction)serviceProvider.GetService(typeof(DomainObjectFactoryFunction)); - var aggregate = (IAggregate)factoryFunction.DynamicInvoke(id); + var domainObject = factoryFunction.Invoke(id); - if (aggregate.Version != -1) + if (domainObject.Version != -1) { throw new InvalidOperationException("Must have a version of -1"); } - return aggregate; + return domainObject; } } } diff --git a/src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectRepository.cs b/src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectRepository.cs index 27e6c582e..6d4620d50 100644 --- a/src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectRepository.cs +++ b/src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectRepository.cs @@ -38,22 +38,22 @@ namespace Squidex.Infrastructure.CQRS.Commands this.nameResolver = nameResolver; } - public async Task GetByIdAsync(Guid id, long? expectedVersion = null) where TDomainObject : class, IAggregate + public async Task GetByIdAsync(Guid id, long? expectedVersion = null) where T : class, IAggregate { - var streamName = nameResolver.GetStreamName(typeof(TDomainObject), id); + var streamName = nameResolver.GetStreamName(typeof(T), id); var events = await eventStore.GetEventsAsync(streamName); if (events.Count == 0) { - throw new DomainObjectNotFoundException(id.ToString(), typeof(TDomainObject)); + throw new DomainObjectNotFoundException(id.ToString(), typeof(T)); } - var domainObject = (TDomainObject)factory.CreateNew(typeof(TDomainObject), id); + var domainObject = factory.CreateNew(id); foreach (var storedEvent in events) { - var envelope = ParseOrNull(storedEvent); + var envelope = ParseKnownCommand(storedEvent); if (envelope != null) { @@ -63,7 +63,7 @@ namespace Squidex.Infrastructure.CQRS.Commands if (expectedVersion != null && domainObject.Version != expectedVersion.Value) { - throw new DomainObjectVersionException(id.ToString(), typeof(TDomainObject), domainObject.Version, expectedVersion.Value); + throw new DomainObjectVersionException(id.ToString(), typeof(T), domainObject.Version, expectedVersion.Value); } return domainObject; @@ -90,7 +90,7 @@ namespace Squidex.Infrastructure.CQRS.Commands } } - private Envelope ParseOrNull(StoredEvent storedEvent) + private Envelope ParseKnownCommand(StoredEvent storedEvent) { try { diff --git a/src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectFactory.cs b/src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectFactory.cs index c290697a1..bcaff2c43 100644 --- a/src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectFactory.cs +++ b/src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectFactory.cs @@ -12,6 +12,6 @@ namespace Squidex.Infrastructure.CQRS.Commands { public interface IDomainObjectFactory { - IAggregate CreateNew(Type type, Guid id); + T CreateNew(Guid id) where T : IAggregate; } } diff --git a/src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectRepository.cs b/src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectRepository.cs index 2aa0ee679..82590593d 100644 --- a/src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectRepository.cs +++ b/src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectRepository.cs @@ -15,7 +15,7 @@ namespace Squidex.Infrastructure.CQRS.Commands { public interface IDomainObjectRepository { - Task GetByIdAsync(Guid id, long? expectedVersion = null) where TDomainObject : class, IAggregate; + Task GetByIdAsync(Guid id, long? expectedVersion = null) where T : class, IAggregate; Task SaveAsync(IAggregate domainObject, ICollection> events, Guid commitId); } diff --git a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/AggregateHandlerTests.cs b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/AggregateHandlerTests.cs index 34a384bb8..6b8712a56 100644 --- a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/AggregateHandlerTests.cs +++ b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/AggregateHandlerTests.cs @@ -91,7 +91,7 @@ namespace Squidex.Infrastructure.CQRS.Commands [Fact] public async Task Create_async_should_create_domain_object_and_save() { - A.CallTo(() => factory.CreateNew(typeof(MyDomainObject), domainObject.Id)) + A.CallTo(() => factory.CreateNew(domainObject.Id)) .Returns(domainObject); A.CallTo(() => repository.SaveAsync(domainObject, A>>.Ignored, A.Ignored)) @@ -115,7 +115,7 @@ namespace Squidex.Infrastructure.CQRS.Commands [Fact] public async Task Create_sync_should_create_domain_object_and_save() { - A.CallTo(() => factory.CreateNew(typeof(MyDomainObject), domainObject.Id)) + A.CallTo(() => factory.CreateNew(domainObject.Id)) .Returns(domainObject); A.CallTo(() => repository.SaveAsync(domainObject, A>>.Ignored, A.Ignored)) diff --git a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs index b6c6914bd..32bd9f342 100644 --- a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs +++ b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs @@ -46,7 +46,7 @@ namespace Squidex.Infrastructure.CQRS.Commands var id = Guid.NewGuid(); - var domainObject = sut.CreateNew(typeof(DO), id); + var domainObject = sut.CreateNew(id); Assert.Equal(id, domainObject.Id); Assert.Equal(-1, domainObject.Version); @@ -65,7 +65,7 @@ namespace Squidex.Infrastructure.CQRS.Commands var sut = new DefaultDomainObjectFactory(serviceProvider); - Assert.Throws(() => sut.CreateNew(typeof(DO), Guid.NewGuid())); + Assert.Throws(() => sut.CreateNew(Guid.NewGuid())); } } } diff --git a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs index 3aa78fc4c..c5fa1507a 100644 --- a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs +++ b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs @@ -34,9 +34,11 @@ namespace Squidex.Infrastructure.CQRS.Commands { domainObject = new MyDomainObject(aggregateId, 123); - A.CallTo(() => streamNameResolver.GetStreamName(A.Ignored, aggregateId)).Returns(streamName); + A.CallTo(() => streamNameResolver.GetStreamName(A.Ignored, aggregateId)) + .Returns(streamName); - A.CallTo(() => factory.CreateNew(typeof(MyDomainObject), aggregateId)).Returns(domainObject); + A.CallTo(() => factory.CreateNew(aggregateId)) + .Returns(domainObject); sut = new DefaultDomainObjectRepository(factory, eventStore, streamNameResolver, eventDataFormatter); }