Ejemplo n.º 1
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.º 2
0
        public ProductIntegrationTestBase()
        {
            var esConnection = Defaults
                               .GetEsConnection("admin", "changeit", "tcp://*****:*****@127.0.0.1:1113").GetAwaiter().GetResult();

            _getBucket = Defaults.GetCouchbaseBucket(nameof(ProductContext), "Administrator", "password",
                                                     "http://localhost:8091");

            var services = new ServiceCollection();

            services.AddMediatR(typeof(ProductCommandHandlers).Assembly);

            services.AddMediatR(typeof(ProductCommandHandlers).Assembly);
            services.AddSingleton(p => esConnection);
            services.AddTransient <IEventDeserializer, DefaultEventDeserializer>();
            services.AddScoped <ConcurrentUnitOfWork>();
            services.AddScoped <AsyncRepository <Product> >();
            services.AddTransient(p => new EventReaderConfiguration(
                                      new SliceSize(500),
                                      p.GetService <IEventDeserializer>(),
                                      new TypedStreamNameResolver(typeof(Product),
                                                                  p.GetService <GetStreamName>()),
                                      new NoStreamUserCredentialsResolver()));

            services.AddScoped <AsyncSnapshotableRepository <Product> >();
            services.AddTransient <IAsyncSnapshotReader, AsyncSnapshotReader>();
            services.AddTransient(p => new SnapshotReaderConfiguration(
                                      new DefaultSnapshotDeserializer(),
                                      new SnapshotableStreamNameResolver(typeof(Product),
                                                                         p.GetService <GetSnapshotStreamName>()),
                                      new NoStreamUserCredentialsResolver()));

            services.AddSingleton(s_getStreamName);
            services.AddSingleton(s_getSnapshotStreamName);
            services.AddSingleton(s_now);
            services.AddTransient(provider => Product.Factory);

            _serviceProvider = services.BuildServiceProvider();
            Mediator         = The <IMediator>();

            Func <string, Task <Aggregate> > getProductAggregate = async streamId =>
            {
                var productRepository = new AsyncRepository <Product>(
                    Product.Factory,
                    new ConcurrentUnitOfWork(),
                    esConnection,
                    new EventReaderConfiguration(
                        new SliceSize(500),
                        new DefaultEventDeserializer(),
                        new PassThroughStreamNameResolver(),
                        new NoStreamUserCredentialsResolver()));

                await productRepository.GetAsync(streamId);

                productRepository.UnitOfWork.TryGet(streamId, out var aggregate);
                return(aggregate);
            };

            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 % 1 == 0,
                    stream => $"{stream}-Snapshot",
                    s_now
                    )
                )
            .Projections(
                ProjectorDefiner.For <ProductProjection>()
                ).Activate(_getBucket).GetAwaiter().GetResult();
        }
        protected async Task InitializeFixture()
        {
            var nodeBuilder = EmbeddedVNodeBuilder.AsSingleNode()
                              .OnDefaultEndpoints()
                              .RunInMemory();
            var node  = nodeBuilder.Build();
            var ready = await node.StartAsync(true);

            var connection = EmbeddedEventStoreConnection.Create(ready);

            //var esConnection = await Defaults.GetEsConnection("admin", "changeit", "tcp://*****:*****@127.0.0.1:1113");
            var esConnection = connection;

            _getBucket = Defaults.GetCouchbaseBucket(nameof(ProductContext), "Administrator", "password", "http://localhost:8091");

            var services = new ServiceCollection();

            services.AddMediatR(typeof(ProductCommandHandlers).Assembly);
            services.AddSingleton(p => esConnection);
            services.AddTransient <IEventDeserializer, DefaultEventDeserializer>();
            services.AddTransient <ConcurrentUnitOfWork>();
            services.AddTransient <AsyncRepository <Product> >();
            services.AddTransient <AsyncSnapshotableRepository <Product> >();
            services.AddTransient <IAsyncSnapshotReader, AsyncSnapshotReader>();
            services.AddSingleton(s_getStreamName);
            services.AddSingleton(s_getSnapshotStreamName);
            services.AddSingleton(s_now);
            services.AddTransient(provider => Product.Factory);
            services.AddTransient(p => new SnapshotReaderConfiguration(
                                      new DefaultSnapshotDeserializer(),
                                      new SnapshotableStreamNameResolver(typeof(Product),
                                                                         p.GetService <GetSnapshotStreamName>()),
                                      new NoStreamUserCredentialsResolver()));
            services.AddTransient(p => new EventReaderConfiguration(
                                      new SliceSize(500),
                                      p.GetService <IEventDeserializer>(),
                                      new TypedStreamNameResolver(typeof(Product),
                                                                  p.GetService <GetStreamName>()),
                                      new NoStreamUserCredentialsResolver()));

            _serviceProvider = services.BuildServiceProvider();

            Mediator = The <IMediator>();

            async Task <Aggregate> GetProductAggregate(string streamId)
            {
                var productRepository = new AsyncRepository <Product>(
                    Product.Factory, new ConcurrentUnitOfWork(),
                    esConnection, new EventReaderConfiguration(new SliceSize(500),
                                                               new DefaultEventDeserializer(),
                                                               new PassThroughStreamNameResolver(),
                                                               new NoStreamUserCredentialsResolver()));

                await productRepository.GetAsync(streamId);

                productRepository.UnitOfWork.TryGet(streamId, out Aggregate aggregate);
                return(aggregate);
            }

            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 % 1 == 0,
                    stream => $"{stream}-Snapshot",
                    s_now
                    )
                )
            .Projections(
                ProjectorDefiner.For <ProductProjection>()
                ).Activate(_getBucket);
        }