public void Projection_write_speed_without_unit_of_work() { var eventsRead = 0; var projector1 = Projector.Create <IEvent>(e => { using (var db = new ReadModelDbContext()) { db.Set <ProductInventory>().Add(new ProductInventory { ProductName = Guid.NewGuid().ToString(), QuantityInStock = Any.Int(1, 5), QuantityReserved = 0 }); db.SaveChanges(); eventsRead++; } }).Named(MethodBase.GetCurrentMethod().Name + ":projector1"); using (var catchup = new ReadModelCatchup(projector1) { StartAtEventId = startAtEventId }) { catchup.Run(); } Console.WriteLine(new { eventsRead }); // TODO: (Write_speed_without_unit_of_work) write test Assert.Fail("Test not written yet."); }
public void Report_projections_can_be_created_using_anonymous_projectors_over_multiple_event_types() { var projector = Projector.Combine( Projector.CreateFor <Placed>(e => { using (var db1 = new ReadModelDbContext()) { db1.OrderTallyByStatus(OrderTally.OrderStatus.Pending).Count++; db1.SaveChanges(); } }), Projector.CreateFor <Cancelled>(e => { using (var db2 = new ReadModelDbContext()) { db2.OrderTallyByStatus(OrderTally.OrderStatus.Canceled).Count++; db2.OrderTallyByStatus(OrderTally.OrderStatus.Pending).Count--; db2.SaveChanges(); } }), Projector.CreateFor <Delivered>(e => { using (var db3 = new ReadModelDbContext()) { db3.OrderTallyByStatus(OrderTally.OrderStatus.Delivered).Count++; db3.OrderTallyByStatus(OrderTally.OrderStatus.Pending).Count--; db3.SaveChanges(); } })).Named("Order status report"); Events.Write(20, _ => new Order.Cancelled()); Events.Write(20, _ => new Order.Delivered()); Events.Write(50, _ => new Order.Placed()); using (var catchup = CreateReadModelCatchup(projector)) { catchup.Run(); } using (var db = new ReadModelDbContext()) { db.Set <OrderTally>() .Single(t => t.Status == "Canceled") .Count .Should() .Be(20); db.Set <OrderTally>() .Single(t => t.Status == "Delivered") .Count .Should() .Be(20); db.Set <OrderTally>() .Single(t => t.Status == "Pending") .Count .Should() .Be(10); } }
public override void SetUp() { Console.WriteLine("ReportingProjectorTests.SetUp"); base.SetUp(); // reset order tallies using (var db = new ReadModelDbContext()) { var tallies = db.Set <OrderTally>(); tallies.ToArray().ForEach(t => tallies.Remove(t)); db.SaveChanges(); } }
public OrderTallyProjector() { On <Placed>(e => { using (var db = new ReadModelDbContext()) { db.OrderTallyByStatus(OrderTally.OrderStatus.Pending).Count++; db.SaveChanges(); } }); On <Cancelled>(e => { using (var db = new ReadModelDbContext()) { db.OrderTallyByStatus(OrderTally.OrderStatus.Canceled).Count++; db.OrderTallyByStatus(OrderTally.OrderStatus.Pending).Count--; db.SaveChanges(); } }); On <Delivered>(e => { using (var db = new ReadModelDbContext()) { db.OrderTallyByStatus(OrderTally.OrderStatus.Delivered).Count++; db.OrderTallyByStatus(OrderTally.OrderStatus.Pending).Count--; db.SaveChanges(); } }); On("Order.Fulfilled", e => { using (var db = new ReadModelDbContext()) { db.OrderTallyByStatus(OrderTally.OrderStatus.Fulfilled).Count++; db.SaveChanges(); } }); On("Order.Misdelivered", e => { using (var db = new ReadModelDbContext()) { db.OrderTallyByStatus(OrderTally.OrderStatus.Misdelivered).Count++; db.SaveChanges(); } }); }
private void ResetReadModelInfo() { using (var db = new ReadModelDbContext()) { foreach (var info in db.Set <ReadModelInfo>()) { info.InitialCatchupStartTime = null; info.InitialCatchupEndTime = null; info.BatchRemainingEvents = 0; info.BatchTotalEvents = 0; info.BatchStartTime = null; } db.SaveChanges(); } }
public async Task When_not_using_Update_then_failed_writes_do_not_interrupt_catchup() { // arrange // preload some events for the catchup. replay will hit the barrier on the last one. var order = new Order(); var productName = Any.Paragraph(3); Action addEvent = () => order.Apply(new AddItem { Quantity = 1, ProductName = productName, Price = .01m }); Enumerable.Range(1, 30).ForEach(_ => addEvent()); var repository = new SqlEventSourcedRepository <Order>(new FakeEventBus()); await repository.Save(order); var count = 0; var projector = new Projector1 { OnUpdate = (work, e) => { using (var db = new ReadModelDbContext()) { // throw one exception in the middle if (count++ == 15) { throw new Exception("drat!"); } db.SaveChanges(); } } }; // act using (var catchup = CreateReadModelCatchup(projector)) { await catchup.Run(); } // assert count.Should().Be(30); }
public void Update(IDictionary <NatureReadModel, DbAction> entities) { foreach (var entity in entities.Keys) { var existingEntity = _dbContext.NatureReadModels .SingleOrDefault(l => l.ApplicationDbId == entity.ApplicationDbId); if (existingEntity != null) { UpdateExistingEntity(existingEntity, entity); } else { _dbContext.NatureReadModels.Add(entity); } } _dbContext.SaveChanges(); }
public void Update(IDictionary <PokemonVarietyReadModel, DbAction> entities) { foreach (var entity in entities.Keys) { var existingEntity = _dbContext.PokemonVarietyReadModels .Where(e => e.ApplicationDbId == entity.ApplicationDbId) .IncludeOptimized(e => e.Varieties) .IncludeOptimized(e => e.Forms) .IncludeOptimized(e => e.Urls) .IncludeOptimized(e => e.PrimaryEvolutionAbilities) .IncludeOptimized(e => e.SecondaryEvolutionAbilities) .IncludeOptimized(e => e.HiddenEvolutionAbilities) .IncludeOptimized(e => e.HuntingConfigurations) .IncludeOptimized(e => e.DefenseAttackEffectivities) .IncludeOptimized(e => e.Spawns) .IncludeOptimized(e => e.Spawns.Select(s => s.Seasons)) .IncludeOptimized(e => e.Spawns.Select(s => s.TimesOfDay)) .IncludeOptimized(e => e.Evolutions) .IncludeOptimized(e => e.LearnableMoves) .IncludeOptimized(e => e.LearnableMoves.Select(lm => lm.LearnMethods)) .IncludeOptimized(e => e.Builds) .IncludeOptimized(e => e.Builds.Select(b => b.MoveOptions)) .IncludeOptimized(e => e.Builds.Select(b => b.ItemOptions)) .IncludeOptimized(e => e.Builds.Select(b => b.NatureOptions)) .IncludeOptimized(e => e.Builds.Select(b => b.OffensiveCoverage)) .AsSingleQuery() .SingleOrDefault(); if (existingEntity != null) { UpdateExistingEntity(existingEntity, entity); } else { _dbContext.PokemonVarietyReadModels.Add(entity); } } _dbContext.SaveChanges(); }
public void Update(IDictionary <EntityTypeReadModel, DbAction> entities) { foreach (var entity in entities.Keys) { var existingEntity = _dbContext.EntityTypeReadModels .SingleOrDefault(e => e.ResourceName.Equals(entity.ResourceName) && e.EntityType == entity.EntityType); if (existingEntity != null) { existingEntity.ResourceName = entity.ResourceName; existingEntity.EntityType = entity.EntityType; } else { _dbContext.EntityTypeReadModels.Add(entity); } } _dbContext.SaveChanges(); }
public void Update(IDictionary <ItemReadModel, DbAction> entities) { var allEntities = _dbContext.ItemReadModels .Include(e => e.PlacedItems) .ToList(); foreach (var entity in entities.Keys) { var existingEntity = allEntities .SingleOrDefault(e => e.ApplicationDbId == entity.ApplicationDbId); if (existingEntity != null) { UpdateExistingEntity(existingEntity, entity); } else { _dbContext.ItemReadModels.Add(entity); } } _dbContext.SaveChanges(); }
public void SaveChanges() { _context.SaveChanges(); }
public void Projection_write_speed_without_unit_of_work() { var eventsRead = 0; var projector1 = Projector.Create<IEvent>(e => { using (var db = new ReadModelDbContext()) { db.Set<ProductInventory>().Add(new ProductInventory { ProductName = Guid.NewGuid().ToString(), QuantityInStock = Any.Int(1, 5), QuantityReserved = 0 }); db.SaveChanges(); eventsRead++; } }).Named(MethodBase.GetCurrentMethod().Name + ":projector1"); using (var catchup = new ReadModelCatchup(projector1) { StartAtEventId = startAtEventId }) { catchup.Run(); } Console.WriteLine(new { eventsRead }); // TODO: (Write_speed_without_unit_of_work) write test Assert.Fail("Test not written yet."); }