コード例 #1
0
        public void TwoConcurrentTransactionsSerialized()
        {
            var store = TestDocumentStore.Create();

            using (var session = store.OpenSession())
            {
                var accountA = new Account("A", 750);

                session.Store(accountA);
                session.SaveChanges();
            }

            using (var session1 = store.DirtyTrackedSession(IsolationLevel.Serializable))
                using (var session2 = store.DirtyTrackedSession(IsolationLevel.Serializable))
                {
                    var session1AcountA = session1.Load <Account>("A");
                    session1AcountA.Substract(500);

                    var session2AcountA = session2.Load <Account>("A");
                    session2AcountA.Substract(350);

                    session1.SaveChanges();
                    session2.SaveChanges();
                }
        }
コード例 #2
0
        public void CreateAnInlineViewModelBasedOnDomainEvents()
        {
            var store = TestDocumentStore.Create();

            var streamId = CombGuidIdGeneration.NewGuid();

            using (var session = store.OpenSession())
            {
                session.Events.StartStream <Route>(streamId,
                                                   new RoutePlanned(streamId, DateTime.Now),
                                                   new StopCreated(Guid.NewGuid(), streamId, "Home", TestData.PositionHome),
                                                   new StopCreated(Guid.NewGuid(), streamId, "Visug", TestData.PositionVisug),
                                                   new StopCreated(Guid.NewGuid(), streamId, "Home", TestData.PositionHome)
                                                   );

                session.SaveChanges();
            }

            using (var session = store.OpenSession())
            {
                var events = session.Events.FetchStream(streamId);

                _testOutputHelper.WriteAsJson(events);
            }
        }
コード例 #3
0
        public void EventSourcingWithInlineProjection()
        {
            var store = TestDocumentStore.Create(optionsHandler: options =>
            {
                options.Events.InlineProjections.Add(new RouteDetailsViewProjection());
            });

            var routeId = CombGuidIdGeneration.NewGuid();

            using (var session = store.OpenSession())
            {
                var route = new Route(routeId);
                route.Plan(DateTime.Now.AddDays(1));
                route.AddSource(new StopName("Home"), new TimeOfDay(17, 30), new Position(51.197894m, 4.481736m));
                route.AddDestination(new StopName("Visug"), new TimeOfDay(18, 30), new Position(50.828417m, 4.400963m));
                route.AddStop(new StopName("Filling station"), new TimeOfDay(17, 45), new Position(50.828417m, 4.400963m));
                route.Drive(new DateTime(2016, 05, 20, 17, 32, 0));

                var events = route.GetChanges();
                //_testOutputHelper.WriteAsJson(events);

                session.Events.StartStream <Route>(route.Id);
                session.Events.Append(route.Id, events);

                session.SaveChanges();
            }

            //load the view by route id
            using (var session = store.OpenSession())
            {
                var view = session.Load <RouteDetails>(routeId);

                _testOutputHelper.WriteAsJson(view);
            }
        }
コード例 #4
0
        public void QuerySingleInclude()
        {
            var store = TestDocumentStore.Create(testOutputHelper: _testOutputHelper);

            using (var session = store.OpenSession())
            {
                var customer = new Customer("customer-1");
                session.Store(customer);

                var order = new Order(customer.Id);
                order.Complete();

                session.Store(order);
                session.SaveChanges();
            }

            _testOutputHelper.BeginTest("QuerySingleInclude");

            using (var session = store.QuerySession())
            {
                Customer customer = null;

                var firstOrder = session
                                 .Query <Order>()
                                 .Include <Customer>(order => order.CustomerId, value => customer = value)
                                 .FirstOrDefault(route => route.Status == OrderStatus.Completed);

                _testOutputHelper.WriteLine(firstOrder.ToString());
                _testOutputHelper.WriteLine(customer.ToString());
            }
        }
コード例 #5
0
        public void EventSourcingWithInlinieAggregation()
        {
            var store = TestDocumentStore.Create(optionsHandler: options =>
            {
                options.Events.AggregateStreamsInlineWith <Route>();
            });

            var routeId = CombGuidIdGeneration.New();

            using (var session = store.OpenSession())
            {
                var route = new Route(routeId);
                route.Plan(DateTime.Now.AddDays(1));
                route.AddSource(new StopName("Home"), new TimeOfDay(17, 30), new Position(51.197894m, 4.481736m));
                route.AddDestination(new StopName("WooRank"), new TimeOfDay(18, 30), new Position(50.828417m, 4.400963m));
                route.AddStop(new StopName("Filling station"), new TimeOfDay(17, 45), new Position(50.828417m, 4.400963m));
                route.Drive(new DateTime(2016, 05, 20, 17, 32, 0));

                session.Events.StartStream <Route>(route.Id, route.GetChanges());

                session.SaveChanges();
            }

            using (var session = store.OpenSession())
            {
                var route = session.Load <Route>(routeId);

                _testOutputHelper.WriteAsJson(route);
            }
        }
コード例 #6
0
        public void EventSourcingShowTheEvents()
        {
            var store   = TestDocumentStore.Create();
            var routeId = CombGuidIdGeneration.New();

            using (var session = store.OpenSession())
            {
                var route = new Route(routeId);
                route.Plan(DateTime.Now.AddDays(1));
                route.AddSource(new StopName("Home"), new TimeOfDay(17, 30), new Position(51.197894m, 4.481736m));
                route.AddDestination(new StopName("WooRank"), new TimeOfDay(18, 30), new Position(50.828417m, 4.400963m));
                route.AddStop(new StopName("Filling station"), new TimeOfDay(17, 45), new Position(50.828417m, 4.400963m));
                route.Drive(new DateTime(2016, 05, 20, 17, 32, 0));

                var events = route.GetChanges();
                //_testOutputHelper.WriteAsJson(events);

                session.Events.StartStream <Route>(route.Id);
                session.Events.Append(route.Id, events);

                session.SaveChanges();
            }

            using (var session = store.OpenSession())
            {
                var eventStream = session.Events.FetchStream(routeId);

                _testOutputHelper.WriteAsJson(eventStream);
            }
        }
コード例 #7
0
        public void LoadAccountA()
        {
            var store = TestDocumentStore.Create();

            using (var session = store.OpenSession())
            {
                var accountA = session.Load <Account>("A");

                _testOutputHelper.WriteAsJson(accountA);
            }
        }
コード例 #8
0
        public void FindAggregateAsJson()
        {
            var store = TestDocumentStore.Create();
            var id    = AddRoute(store);

            using (var session = store.QuerySession())
            {
                var json = session.FindJsonById <Route>(id);

                _testOutputHelper.WriteFormattedJson(json);
            }
        }
コード例 #9
0
        public void QueryCompiledJson()
        {
            var store = TestDocumentStore.Create();

            store.AddRoutes(100);

            using (var session = store.QuerySession())
            {
                var routes = session.Query(new RoutesPlannedAfterJson(DateTime.Now.AddDays(5))).ToList();

                _testOutputHelper.WriteLine(routes.AsString());
            }
        }
コード例 #10
0
        public void QueryExplaind()
        {
            var store = TestDocumentStore.Create();

            store.AddRoutes(100);

            using (var session = store.QuerySession())
            {
                var queryPlan = session.Query <Route>()
                                .Where(route => route.Status == RouteStatus.Planned)
                                .Explain();

                _testOutputHelper.WriteAsJson(queryPlan);
            }
        }
コード例 #11
0
        public void InvalidForeignKey()
        {
            var store = TestDocumentStore.Create(optionsHandler: options =>
            {
                options.Schema.For <Order>()
                .ForeignKey <Customer>(order => order.CustomerId);
            });

            using (var session = store.OpenSession())
            {
                var order1 = new Order(customerId: new Guid());

                session.Store(order1);
                session.SaveChanges();
            }
        }
コード例 #12
0
        public void QueryExplaindWithIndex()
        {
            var store = TestDocumentStore.Create(optionsHandler: options =>
            {
                options.Schema.For <Route>().Index(route => route.Status);
            });

            store.AddRoutes(100);

            using (var session = store.QuerySession())
            {
                var queryPlan = session.Query <Route>()
                                .Where(route => route.Status == RouteStatus.Planned)
                                .Explain();

                _testOutputHelper.WriteAsJson(queryPlan);
            }
        }
コード例 #13
0
        public void LoadAggregateById()
        {
            var slaveStore = TestDocumentStore.Create(ConnectionString.Slave,
                                                      optionsHandler: options =>
            {
                options.UpsertType = PostgresUpsertType.Legacy;
                options.AutoCreateSchemaObjects = AutoCreate.None;
            }, clear: false);
            var masterStore = TestDocumentStore.Create(ConnectionString.Master, optionsHandler: options => options.UpsertType = PostgresUpsertType.Legacy);

            var tempRouteId = CombGuidIdGeneration.New();
            var routeId     = CombGuidIdGeneration.New();

            using (var massWriter = new Writer(masterStore))
                using (var listenerMaster = new Listener("master", _testOutputHelper, masterStore, routeId, tempRouteId))
                    using (var listenerSlave = new Listener("slave", _testOutputHelper, slaveStore, routeId, tempRouteId))
                        using (var session = masterStore.OpenSession())
                        {
                            session.Store(new Route {
                                ID = tempRouteId
                            });
                            session.SaveChanges();

                            massWriter.Start();

                            Thread.Sleep(5000);

                            listenerSlave.Stored();
                            listenerMaster.Stored();

                            session.Store(new Route {
                                ID = routeId
                            });
                            session.SaveChanges();

                            listenerMaster.Start();
                            listenerSlave.Start();


                            _testOutputHelper.WriteLine($"Route stored");

                            Thread.Sleep(500);
                        }
        }
コード例 #14
0
        public void QueryWithProjection()
        {
            var store = TestDocumentStore.Create(testOutputHelper: _testOutputHelper);

            store.AddRoutes(100);

            using (var session = store.QuerySession())
            {
                var routes = session.Query <Route>()
                             .Where(route => route.Status == RouteStatus.Planned)
                             .Select(route => new { route.Id, route.Date })
                             .ToList();

                foreach (var route in routes)
                {
                    _testOutputHelper.WriteLine($"ID: {route.Id} Date: {route.Date}");
                }
            }
        }
コード例 #15
0
        public void CreateAnInlineViewModelBasedOnDomainEvents()
        {
            var store = TestDocumentStore.Create(optionsHandler: options =>
            {
                options.Events.InlineTransformation(new RouteDetailsViewProjection());
            });

            var routeId = CreateRoute(store);

            AddStop(store, routeId, "Home", new Domain.Position(51.197894m, 4.481736m));
            AddStop(store, routeId, "WooRank", new Domain.Position(50.828417m, 4.400963m));

            //load the view by route id
            using (var session = store.OpenSession())
            {
                var json = session.FindJsonById <RouteDetails>(routeId);

                _testOutputHelper.WriteFormattedJson(json);
            }
        }
コード例 #16
0
        public void QueryMultiInclude()
        {
            var store = TestDocumentStore.Create();

            store.AddProductsAndOrders();

            _testOutputHelper.BeginTest("QueryMultiInclude");

            using (var session = store.QuerySession())
            {
                var customers = new Dictionary <Guid, Customer>();

                var orders = session
                             .Query <Order>()
                             .Include(order => order.CustomerId, customers)
                             .Where(route => route.Status == OrderStatus.Completed)
                             .ToList();

                _testOutputHelper.WriteLine(orders.AsString());
                _testOutputHelper.WriteLine(customers.AsString());
            }
        }
コード例 #17
0
        public void QueryComplex()
        {
            var john     = "John";
            var pageSize = 20;
            var page     = 2;

            var store = TestDocumentStore.Create(testOutputHelper: _testOutputHelper);

            store.AddDinners(200);

            _testOutputHelper.BeginTest("QueryComplex");

            using (var session = store.QuerySession())
            {
                QueryStatistics stats;

                var dinners = session.Query <Dinner>()
                              .Stats(out stats)
                              .Where(x => x.HostedBy == john)
                              .OrderBy(x => x.EventDate)
                              .Skip(pageSize * page)
                              .Take(pageSize)
                              .Select(dinner => new
                {
                    dinner.Id,
                    dinner.Title,
                    Date = dinner.EventDate,
                    dinner.Address,
                    Host = dinner.HostedBy
                })
                              .ToList();

                _testOutputHelper
                .Write("TotalResults: " + stats.TotalResults)
                .Write("Dinners: " + dinners.Count)
                .NewLine()
                .WriteAsJson(dinners);
            }
        }
コード例 #18
0
        public void StoreAndLoadDocument()
        {
            var store = TestDocumentStore.Create(testOutputHelper: _testOutputHelper);

            var route = new Route {
                Date = DateTime.Now
            };

            route.Stops.Add(new Stop {
                Name = "Home", Position = new Position {
                    Latitude = 51.197894m, Longitude = 4.481736m
                }
            });
            route.Stops.Add(new Stop {
                Name = "Visug", Position = new Position {
                    Latitude = 51.142128m, Longitude = 4.43787m
                }
            });
            route.Stops.Add(new Stop {
                Name = "Home", Position = new Position {
                    Latitude = 51.197894m, Longitude = 4.481736m
                }
            });

            using (var session = store.OpenSession())
            {
                session.Store(route);
                session.SaveChanges();
            }

            using (var session = store.QuerySession())
            {
                var json = session.Load <Route>(route.Id);

                _testOutputHelper.WriteAsJson(json);
            }
        }