Browse Source

Roadmap linked.

pull/95/head
Sebastian Stehle 9 years ago
parent
commit
50c12081ad
  1. 2
      README.md
  2. 2
      src/Squidex.Infrastructure/CQRS/Commands/AggregateHandler.cs
  3. 11
      src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectFactory.cs
  4. 14
      src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectRepository.cs
  5. 2
      src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectFactory.cs
  6. 2
      src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectRepository.cs
  7. 4
      tests/Squidex.Infrastructure.Tests/CQRS/Commands/AggregateHandlerTests.cs
  8. 4
      tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs
  9. 6
      tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs

2
README.md

@ -10,7 +10,7 @@ Read the docs at [https://docs.squidex.io/](https://docs.squidex.io/) (work in p
## Status ## Status
Current Version 1.0-beta2 Current Version 1.0-beta3. Roadmap: https://trello.com/b/KakM4F3S/squidex-roadmap
## Prerequisites ## Prerequisites

2
src/Squidex.Infrastructure/CQRS/Commands/AggregateHandler.cs

@ -44,7 +44,7 @@ namespace Squidex.Infrastructure.CQRS.Commands
Guard.NotNull(context, nameof(context)); Guard.NotNull(context, nameof(context));
var aggregateCommand = GetCommand(context); var aggregateCommand = GetCommand(context);
var aggregate = (T)domainObjectFactory.CreateNew(typeof(T), aggregateCommand.AggregateId); var aggregate = domainObjectFactory.CreateNew<T>(aggregateCommand.AggregateId);
await creator(aggregate); await creator(aggregate);

11
src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectFactory.cs

@ -23,19 +23,18 @@ namespace Squidex.Infrastructure.CQRS.Commands
this.serviceProvider = serviceProvider; this.serviceProvider = serviceProvider;
} }
public IAggregate CreateNew(Type type, Guid id) public T CreateNew<T>(Guid id) where T : IAggregate
{ {
var factoryFunctionType = typeof(DomainObjectFactoryFunction<>).MakeGenericType(type); var factoryFunction = (DomainObjectFactoryFunction<T>)serviceProvider.GetService(typeof(DomainObjectFactoryFunction<T>));
var factoryFunction = (Delegate)serviceProvider.GetService(factoryFunctionType);
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"); throw new InvalidOperationException("Must have a version of -1");
} }
return aggregate; return domainObject;
} }
} }
} }

14
src/Squidex.Infrastructure/CQRS/Commands/DefaultDomainObjectRepository.cs

@ -38,22 +38,22 @@ namespace Squidex.Infrastructure.CQRS.Commands
this.nameResolver = nameResolver; this.nameResolver = nameResolver;
} }
public async Task<TDomainObject> GetByIdAsync<TDomainObject>(Guid id, long? expectedVersion = null) where TDomainObject : class, IAggregate public async Task<T> GetByIdAsync<T>(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); var events = await eventStore.GetEventsAsync(streamName);
if (events.Count == 0) 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<T>(id);
foreach (var storedEvent in events) foreach (var storedEvent in events)
{ {
var envelope = ParseOrNull(storedEvent); var envelope = ParseKnownCommand(storedEvent);
if (envelope != null) if (envelope != null)
{ {
@ -63,7 +63,7 @@ namespace Squidex.Infrastructure.CQRS.Commands
if (expectedVersion != null && domainObject.Version != expectedVersion.Value) 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; return domainObject;
@ -90,7 +90,7 @@ namespace Squidex.Infrastructure.CQRS.Commands
} }
} }
private Envelope<IEvent> ParseOrNull(StoredEvent storedEvent) private Envelope<IEvent> ParseKnownCommand(StoredEvent storedEvent)
{ {
try try
{ {

2
src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectFactory.cs

@ -12,6 +12,6 @@ namespace Squidex.Infrastructure.CQRS.Commands
{ {
public interface IDomainObjectFactory public interface IDomainObjectFactory
{ {
IAggregate CreateNew(Type type, Guid id); T CreateNew<T>(Guid id) where T : IAggregate;
} }
} }

2
src/Squidex.Infrastructure/CQRS/Commands/IDomainObjectRepository.cs

@ -15,7 +15,7 @@ namespace Squidex.Infrastructure.CQRS.Commands
{ {
public interface IDomainObjectRepository public interface IDomainObjectRepository
{ {
Task<TDomainObject> GetByIdAsync<TDomainObject>(Guid id, long? expectedVersion = null) where TDomainObject : class, IAggregate; Task<T> GetByIdAsync<T>(Guid id, long? expectedVersion = null) where T : class, IAggregate;
Task SaveAsync(IAggregate domainObject, ICollection<Envelope<IEvent>> events, Guid commitId); Task SaveAsync(IAggregate domainObject, ICollection<Envelope<IEvent>> events, Guid commitId);
} }

4
tests/Squidex.Infrastructure.Tests/CQRS/Commands/AggregateHandlerTests.cs

@ -91,7 +91,7 @@ namespace Squidex.Infrastructure.CQRS.Commands
[Fact] [Fact]
public async Task Create_async_should_create_domain_object_and_save() public async Task Create_async_should_create_domain_object_and_save()
{ {
A.CallTo(() => factory.CreateNew(typeof(MyDomainObject), domainObject.Id)) A.CallTo(() => factory.CreateNew<MyDomainObject>(domainObject.Id))
.Returns(domainObject); .Returns(domainObject);
A.CallTo(() => repository.SaveAsync(domainObject, A<ICollection<Envelope<IEvent>>>.Ignored, A<Guid>.Ignored)) A.CallTo(() => repository.SaveAsync(domainObject, A<ICollection<Envelope<IEvent>>>.Ignored, A<Guid>.Ignored))
@ -115,7 +115,7 @@ namespace Squidex.Infrastructure.CQRS.Commands
[Fact] [Fact]
public async Task Create_sync_should_create_domain_object_and_save() public async Task Create_sync_should_create_domain_object_and_save()
{ {
A.CallTo(() => factory.CreateNew(typeof(MyDomainObject), domainObject.Id)) A.CallTo(() => factory.CreateNew<MyDomainObject>(domainObject.Id))
.Returns(domainObject); .Returns(domainObject);
A.CallTo(() => repository.SaveAsync(domainObject, A<ICollection<Envelope<IEvent>>>.Ignored, A<Guid>.Ignored)) A.CallTo(() => repository.SaveAsync(domainObject, A<ICollection<Envelope<IEvent>>>.Ignored, A<Guid>.Ignored))

4
tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs

@ -46,7 +46,7 @@ namespace Squidex.Infrastructure.CQRS.Commands
var id = Guid.NewGuid(); var id = Guid.NewGuid();
var domainObject = sut.CreateNew(typeof(DO), id); var domainObject = sut.CreateNew<DO>(id);
Assert.Equal(id, domainObject.Id); Assert.Equal(id, domainObject.Id);
Assert.Equal(-1, domainObject.Version); Assert.Equal(-1, domainObject.Version);
@ -65,7 +65,7 @@ namespace Squidex.Infrastructure.CQRS.Commands
var sut = new DefaultDomainObjectFactory(serviceProvider); var sut = new DefaultDomainObjectFactory(serviceProvider);
Assert.Throws<InvalidOperationException>(() => sut.CreateNew(typeof(DO), Guid.NewGuid())); Assert.Throws<InvalidOperationException>(() => sut.CreateNew<DO>(Guid.NewGuid()));
} }
} }
} }

6
tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs

@ -34,9 +34,11 @@ namespace Squidex.Infrastructure.CQRS.Commands
{ {
domainObject = new MyDomainObject(aggregateId, 123); domainObject = new MyDomainObject(aggregateId, 123);
A.CallTo(() => streamNameResolver.GetStreamName(A<Type>.Ignored, aggregateId)).Returns(streamName); A.CallTo(() => streamNameResolver.GetStreamName(A<Type>.Ignored, aggregateId))
.Returns(streamName);
A.CallTo(() => factory.CreateNew(typeof(MyDomainObject), aggregateId)).Returns(domainObject); A.CallTo(() => factory.CreateNew<MyDomainObject>(aggregateId))
.Returns(domainObject);
sut = new DefaultDomainObjectRepository(factory, eventStore, streamNameResolver, eventDataFormatter); sut = new DefaultDomainObjectRepository(factory, eventStore, streamNameResolver, eventDataFormatter);
} }

Loading…
Cancel
Save