Ejemplo n.º 1
0
        public async Task AggregationWithWhenShouldGetTheCurrentState()
        {
            var invoiceId = Guid.NewGuid();

            var invoiceInitiated = new InvoiceInitiated(
                invoiceId,
                34.12,
                "INV/2021/11/01",
                new Person("Oscar the Grouch", "123 Sesame Street"),
                DateTime.UtcNow
                );
            var invoiceIssued = new InvoiceIssued(
                invoiceId,
                "Cookie Monster",
                DateTime.UtcNow
                );
            var invoiceSent = new InvoiceSent(
                invoiceId,
                InvoiceSendMethod.Email,
                DateTime.UtcNow
                );

            theSession.Events.Append(invoiceId, invoiceInitiated, invoiceIssued, invoiceSent);
            await theSession.SaveChangesAsync();

            #region sample_aggregate-stream-usage
            var invoice = await theSession.Events.AggregateStreamAsync <Invoice>(invoiceId);

            #endregion

            invoice.ShouldNotBeNull();
            invoice.Id.ShouldBe(invoiceInitiated.InvoiceId);
            invoice.Amount.ShouldBe(invoiceInitiated.Amount);
            invoice.Number.ShouldBe(invoiceInitiated.Number);
            invoice.Status.ShouldBe(InvoiceStatus.Sent);

            invoice.IssuedTo.ShouldBe(invoiceInitiated.IssuedTo);
            invoice.InitiatedAt.ShouldBe(invoiceInitiated.InitiatedAt);

            invoice.IssuedBy.ShouldBe(invoiceIssued.IssuedBy);
            invoice.IssuedAt.ShouldBe(invoiceIssued.IssuedAt);

            invoice.SentVia.ShouldBe(invoiceSent.SentVia);
            invoice.SentAt.ShouldBe(invoiceSent.SentAt);
        }
        public void AggregationWithWhenShouldGetTheCurrentState()
        {
            var invoiceInitiated = new InvoiceInitiated(
                34.12,
                "INV/2021/11/01",
                new Person("Oscar the Grouch", "123 Sesame Street"),
                DateTime.UtcNow
                );
            var invoiceIssued = new InvoiceIssued(
                "Cookie Monster",
                DateTime.UtcNow
                );
            var invoiceSent = new InvoiceSent(
                InvoiceSendMethod.Email,
                DateTime.UtcNow
                );

            // 1. Get all events and sort them in the order of appearance
            var events = new object[] { invoiceInitiated, invoiceIssued, invoiceSent };

            // 2. Construct empty Invoice object
            var invoice = new Invoice();

            // 3. Apply each event on the entity.
            foreach (var @event in events)
            {
                invoice.When(@event);
            }

            invoice.Id.Should().Be(invoiceInitiated.Number);
            invoice.Amount.Should().Be(invoiceInitiated.Amount);
            invoice.Number.Should().Be(invoiceInitiated.Number);
            invoice.Status.Should().Be(InvoiceStatus.Sent);

            invoice.IssuedTo.Should().Be(invoiceInitiated.IssuedTo);
            invoice.InitiatedAt.Should().Be(invoiceInitiated.InitiatedAt);

            invoice.IssuedBy.Should().Be(invoiceIssued.IssuedBy);
            invoice.IssuedAt.Should().Be(invoiceIssued.IssuedAt);

            invoice.SentVia.Should().Be(invoiceSent.SentVia);
            invoice.SentAt.Should().Be(invoiceSent.SentAt);
        }
 private void Apply(InvoiceSent @event)
 {
     SentVia = @event.SentVia;
     SentAt  = @event.SentAt;
     Status  = InvoiceStatus.Sent;
 }