public async Task <TDomainObject> GetByIdAsync <TDomainObject>(Guid id, long?expectedVersion = null) where TDomainObject : class, IAggregate { var streamName = nameResolver.GetStreamName(typeof(TDomainObject), id); var events = await eventStore.GetEventsAsync(streamName).ToList(); if (events.Count == 0) { throw new DomainObjectNotFoundException(id.ToString(), typeof(TDomainObject)); } var domainObject = (TDomainObject)factory.CreateNew(typeof(TDomainObject), id); foreach (var storedEvent in events) { var envelope = ParseOrNull(storedEvent); if (envelope != null) { domainObject.ApplyEvent(envelope); } } if (expectedVersion != null && domainObject.Version != expectedVersion.Value) { throw new DomainObjectVersionException(id.ToString(), typeof(TDomainObject), domainObject.Version, expectedVersion.Value); } return(domainObject); }
public async Task LoadAsync(IAggregate domainObject, long?expectedVersion = null) { var streamName = nameResolver.GetStreamName(domainObject.GetType(), domainObject.Id); var events = await eventStore.GetEventsAsync(streamName); if (events.Count == 0) { throw new DomainObjectNotFoundException(domainObject.Id.ToString(), domainObject.GetType()); } foreach (var storedEvent in events) { var envelope = ParseKnownCommand(storedEvent); if (envelope != null) { domainObject.ApplyEvent(envelope); } } if (expectedVersion != null && domainObject.Version != expectedVersion.Value) { throw new DomainObjectVersionException(domainObject.Id.ToString(), domainObject.GetType(), domainObject.Version, expectedVersion.Value); } }
public PersistenceBatchTests() { A.CallTo(() => streamNameResolver.GetStreamName(None.Type, A <string> ._)) .ReturnsLazily(x => x.GetArgument <string>(1) !); sut = new Store <int>(snapshotStore, eventStore, eventDataFormatter, streamNameResolver); }
public ContentVersionLoaderTests() { A.CallTo(() => nameResolver.GetStreamName(typeof(ContentDomainObject), id)) .Returns(streamName); sut = new ContentVersionLoader(eventStore, nameResolver, formatter); }
public PersistenceEventSourcingTests() { A.CallTo(() => services.GetService(typeof(MyStatefulObject))) .Returns(statefulObject); A.CallTo(() => services.GetService(typeof(MyStatefulObjectWithSnapshot))) .Returns(statefulObjectWithSnapShot); A.CallTo(() => services.GetService(typeof(ISnapshotStore <object, string>))) .Returns(snapshotStore); A.CallTo(() => streamNameResolver.GetStreamName(typeof(MyStatefulObject), key)) .Returns(key); A.CallTo(() => streamNameResolver.GetStreamName(typeof(MyStatefulObjectWithSnapshot), key)) .Returns(key); sut = new StateFactory(pubSub, cache, eventStore, eventDataFormatter, services, streamNameResolver); sut.Initialize(); }
public PersistenceEventSourcingTests() { A.CallTo(() => services.GetService(typeof(ISnapshotStore <object, string>))) .Returns(snapshotStore); A.CallTo(() => streamNameResolver.GetStreamName(typeof(object), key)) .Returns(key); sut = new Store <string>(eventStore, eventDataFormatter, services, streamNameResolver); }
public DefaultDomainObjectRepositoryTests() { domainObject = new MyDomainObject(aggregateId, 123); A.CallTo(() => nameResolver.GetStreamName(A <Type> .Ignored, aggregateId)) .Returns(streamName); A.CallTo(() => factory.CreateNew <MyDomainObject>(aggregateId)) .Returns(domainObject); sut = new DefaultDomainObjectRepository(eventStore, nameResolver, formatter); }
public async Task <NamedContentData> LoadAsync(Guid appId, Guid id, long version) { var streamName = nameResolver.GetStreamName(typeof(ContentDomainObject), id); var events = await eventStore.GetEventsAsync(streamName); if (events.Count == 0 || events.Count < version - 1) { throw new DomainObjectNotFoundException(id.ToString(), typeof(ContentDomainObject)); } NamedContentData contentData = null; foreach (var storedEvent in events.Where(x => x.EventStreamNumber <= version)) { var envelope = ParseKnownEvent(storedEvent); if (envelope != null) { if (envelope.Payload is ContentCreated contentCreated) { if (contentCreated.AppId.Id != appId) { throw new DomainObjectNotFoundException(id.ToString(), typeof(ContentDomainObject)); } contentData = contentCreated.Data; } else if (envelope.Payload is ContentUpdated contentUpdated) { contentData = contentUpdated.Data; } } } return(contentData); }
private string GetStreamName() { return(streamNameResolver.GetStreamName(ownerType, ownerKey.ToString() !)); }
private string GetStreamName() { return(streamNameResolver.GetStreamName(typeof(TOwner), ownerKey.ToString())); }