Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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();
                }
            });
        }