Ejemplo n.º 1
0
        private static async Task AppendToStream <T>(ConcurrentUnitOfWork uow, IEventStoreConnection connection, Now now, IStreamNameResolver getStreamName) where T : IAggregateRootEntity
        {
            foreach (Aggregate aggregate in uow.GetChanges())
            {
                EventData[] changes = aggregate.Root.GetChanges()
                                      .Select(@event => new EventData(
                                                  Guid.NewGuid(),
                                                  @event.GetType().TypeQualifiedName(),
                                                  true,
                                                  Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(@event)),
                                                  Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new EventMetadata
                {
                    TimeStamp     = now(),
                    AggregateType = typeof(T).Name,
                    AggregateAssemblyQualifiedName = typeof(T).AssemblyQualifiedName,
                    IsSnapshot = false
                }))
                                                  )).ToArray();
                try
                {
                    await connection.AppendToStreamAsync(getStreamName.Resolve(aggregate.Identifier), aggregate.ExpectedVersion, changes);
                }
                catch (WrongExpectedVersionException)
                {
                    StreamEventsSlice page = await connection.ReadStreamEventsBackwardAsync(aggregate.Identifier, -1, 1, false);

                    throw new WrongExpectedStreamVersionException(
                              $"Failed to append stream {aggregate.Identifier} with expected version {aggregate.ExpectedVersion}. " +
                              $"{(page.Status == SliceReadStatus.StreamNotFound ? "Stream not found!" : $"Current Version: {page.LastEventNumber}")}");
                }
            }
        }
Ejemplo n.º 2
0
        private async Task InitProjections()
        {
            IEventStoreConnection esConnection = await GetEsConnection();

            Func <IBucket> getBucket = GetCouchbaseBucket();

            Func <string, Task <Aggregate> > getProductAggregate = async streamId =>
            {
                var defaultSerializer    = new DefaultEventDeserializer();
                var concurrentUnitOfWork = new ConcurrentUnitOfWork();

                var productRepository = new AsyncRepository <Product>(
                    Product.Factory,
                    concurrentUnitOfWork,
                    esConnection,
                    new EventReaderConfiguration(
                        new SliceSize(500),
                        defaultSerializer,
                        new TypedStreamNameResolver(typeof(Product), s_sGetStreamName),
                        new NoStreamUserCredentialsResolver()));

                await productRepository.GetAsync(streamId);

                return(concurrentUnitOfWork.GetChanges().First());
            };

            await ProjectionManagerBuilder.With
            .Connection(esConnection)
            .Deserializer(new DefaultEventDeserializer())
            .CheckpointStore(new CouchbaseCheckpointStore(getBucket))
            .Snaphotter(
                new EventStoreSnapshotter <Aggregate, ProductSnapshot>(
                    getProductAggregate,
                    () => esConnection,
                    e => e.Event.EventNumber > 0 && e.Event.EventNumber % 5 == 0,
                    stream => $"{stream}-Snapshot",
                    s_sNow))
            .Projections(
                ProjectorDefiner.For <ProductProjection>()
                ).Activate(getBucket);
        }
Ejemplo n.º 3
0
 public void GetChangesReturnsEmpty()
 {
     Assert.That(_sut.GetChanges(), Is.EquivalentTo(Enumerable.Empty <Aggregate>()));
 }
Ejemplo n.º 4
0
 public void GetChangesReturnsEmpty()
 {
     Assert.That(_sut.GetChanges(), Is.EquivalentTo(new[] { _aggregate1, _aggregate2 }));
 }