コード例 #1
0
            public void GetAsyncThrowsForUnknownId()
            {
                var exception =
                    Assert.Throws <AggregateException>(() => { var _ = _sut.GetAsync(_model.UnknownIdentifier).Result; });

                Assert.That(exception.InnerExceptions, Has.Count.EqualTo(1));
                Assert.That(exception.InnerExceptions[0], Is.InstanceOf <AggregateNotFoundException>());
                var actualException = (AggregateNotFoundException)exception.InnerExceptions[0];

                Assert.That(actualException.Identifier, Is.EqualTo(_model.UnknownIdentifier));
                Assert.That(actualException.Type, Is.EqualTo(typeof(AggregateRootEntityStub)));
            }
コード例 #2
0
            public void GetAsyncThrowsForUnknownId()
            {
                var exception =
                    Assert.ThrowsAsync <AggregateNotFoundException>(async() => { var _ = await _sut.GetAsync(_model.UnknownIdentifier); });

                Assert.That(exception.Identifier, Is.EqualTo(_model.UnknownIdentifier));
                Assert.That(exception.ClrType, Is.EqualTo(typeof(AggregateRootEntityStub)));
            }
コード例 #3
0
        public async Task BasicCrud()
        {
            using (var trans = DataSource.BeginTransaction())
            {
                var repo = new AsyncRepository<Employee, int>(trans, EmployeeTableName);

                var emp1 = new Employee() { FirstName = "Tom", LastName = "Jones", Title = "President" };
                var echo1 = await repo.InsertAsync(emp1);

                Assert.AreNotEqual(0, echo1.EmployeeKey, "EmployeeKey was not set");
                Assert.AreEqual(emp1.FirstName, echo1.FirstName, "FirstName");
                Assert.AreEqual(emp1.LastName, echo1.LastName, "LastName");
                Assert.AreEqual(emp1.Title, echo1.Title, "Title");

                echo1.MiddleName = "G";
                await repo.UpdateAsync(echo1);

                var emp2 = new Employee() { FirstName = "Lisa", LastName = "Green", Title = "VP Transportation", ManagerKey = echo1.EmployeeKey };
                var echo2 = await repo.InsertAsync(emp2);
                Assert.AreNotEqual(0, echo2.EmployeeKey, "EmployeeKey was not set");
                Assert.AreEqual(emp2.FirstName, echo2.FirstName, "FirstName");
                Assert.AreEqual(emp2.LastName, echo2.LastName, "LastName");
                Assert.AreEqual(emp2.Title, echo2.Title, "Title");
                Assert.AreEqual(emp2.ManagerKey, echo2.ManagerKey, "ManagerKey");

                var list = await repo.GetAllAsync();
                Assert.IsTrue(list.Any(e => e.EmployeeKey == echo1.EmployeeKey), "Employee 1 is missing");
                Assert.IsTrue(list.Any(e => e.EmployeeKey == echo2.EmployeeKey), "Employee 2 is missing");

                var get1 = await repo.GetAsync(echo1.EmployeeKey.Value);
                Assert.AreEqual(echo1.EmployeeKey, get1.EmployeeKey);



                var whereSearch1 = await repo.QueryAsync("FirstName = @FN", new { FN = "Tom" });
                Assert.IsTrue(whereSearch1.Any(x => x.EmployeeKey == echo1.EmployeeKey), "Emp1 should have been returned");
                Assert.IsTrue(whereSearch1.All(x => x.FirstName == "Tom"), "Checking for incorrect return values");

                var whereSearch2 = await repo.QueryAsync(new { FirstName = "Tom" });
                Assert.IsTrue(whereSearch2.Any(x => x.EmployeeKey == echo1.EmployeeKey), "Emp1 should have been returned");
                Assert.IsTrue(whereSearch2.All(x => x.FirstName == "Tom"), "Checking for incorrect return values");


                await repo.DeleteAsync(echo2.EmployeeKey.Value);
                await repo.DeleteAsync(echo1.EmployeeKey.Value);

                var list2 = await repo.GetAllAsync();
                Assert.AreEqual(list.Count - 2, list2.Count);

            }
        }
コード例 #4
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);
        }
        public async Task HandleAsync(AddExportRowToExportDefinition message)
        {
            var exportDefinition = await _repository
                                   .GetAsync($@"ed-{message.ExportDefinitionId}")
                                   .ConfigureAwait(false);

            var exprtRowId = Guid.NewGuid();

            exportDefinition.AddRowToRowsCollection(exprtRowId, message.Name);

            var aggregates = _repository.UnitOfWork.GetChanges();

            foreach (var aggregate in aggregates)
            {
                var aggregateIdentifier      = aggregate.Identifier;
                var aggregateExpectedVersion = aggregate.ExpectedVersion;
                var changes = aggregate.Root.GetChanges().ToArray();

                _repository.AppendToStream(
                    aggregateIdentifier,
                    aggregateExpectedVersion,
                    changes);
            }
        }
コード例 #6
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();
        }
コード例 #7
0
        public async Task UpdateWithDictionary()
        {
            using (var trans = DataSource.BeginTransaction())
            {
                var repo = new AsyncRepository<Employee, int>(trans, EmployeeTableName);

                var emp1 = new Dictionary<string, object>() { { "FirstName", "Tom" }, { "LastName", "Jones" }, { "Title", "President" } };
                var echo1 = await repo.InsertAsync(emp1);

                Assert.AreNotEqual(0, echo1.EmployeeKey, "EmployeeKey was not set");
                Assert.AreEqual(emp1["FirstName"], echo1.FirstName, "FirstName");
                Assert.AreEqual(emp1["LastName"], echo1.LastName, "LastName");
                Assert.AreEqual(emp1["Title"], echo1.Title, "Title");

                var emp2 = new Dictionary<string, object>() { { "EmployeeKey", echo1.EmployeeKey }, { "LastName", "Brown" } };
                await repo.UpdateAsync(emp2);
                var echo2 = await repo.GetAsync(echo1.EmployeeKey.Value);

                //these were changed
                Assert.AreEqual(echo1.EmployeeKey, echo2.EmployeeKey, "EmployeeKey was not set");
                Assert.AreEqual(emp2["LastName"], echo2.LastName, "LastName");

                //these should be unchanged
                Assert.AreEqual(emp1["FirstName"], echo2.FirstName, "FirstName");
                Assert.AreEqual(emp1["Title"], echo2.Title, "Title");


                await repo.DeleteAsync(echo1.EmployeeKey.Value);

            }
        }
        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);
        }