Beispiel #1
0
        public void capture_events_to_an_existing_stream_and_fetch_the_events_back_in_another_database_schema(DocumentTracking sessionType)
        {
            var store = InitStore("event_store");

            var id = Guid.NewGuid();
            var started = new QuestStarted();

            using (var session = store.OpenSession(sessionType))
            {
                session.Events.StartStream<Quest>(id, started);
                session.SaveChanges();
            }

            using (var session = store.OpenSession(sessionType))
            {
                // SAMPLE: append-events
                var joined = new MembersJoined { Members = new string[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                session.Events.Append(id, joined, departed);

                session.SaveChanges();
                // ENDSAMPLE

                var streamEvents = session.Events.FetchStream(id);

                streamEvents.Count().ShouldBe(3);
                streamEvents.ElementAt(0).Data.ShouldBeOfType<QuestStarted>();
                streamEvents.ElementAt(0).Version.ShouldBe(1);
                streamEvents.ElementAt(1).Data.ShouldBeOfType<MembersJoined>();
                streamEvents.ElementAt(1).Version.ShouldBe(2);
                streamEvents.ElementAt(2).Data.ShouldBeOfType<MembersDeparted>();
                streamEvents.ElementAt(2).Version.ShouldBe(3);
            }
        }
Beispiel #2
0
        public void ForNewQuestStream(string name, DateTime date)
        {
            var started = new QuestStarted { Name = name };
            using (var session = _store.LightweightSession())
            {
                _lastStream = session.Events.StartStream<Quest>(started);

                _streams[name] = _lastStream;

                session.SaveChanges();
            }
        }
        public void live_aggregate_equals_inlined_aggregate_without_hidden_contracts(DocumentTracking sessionType)
        {
            var store = InitStore("event_store");
            var questId = Guid.NewGuid();

            using (var session = store.OpenSession())
            {
                //Note Id = questId, is we remove it from first message then AggregateStream will return party.Id=default(Guid) that is not equals to Load<QuestParty> result
                var started = new QuestStarted { /*Id = questId,*/ Name = "Destroy the One Ring" };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream<Quest>(questId, started, joined1);
                session.SaveChanges();
            }

            using (var session = store.OpenSession())
            {
                var liveAggregate = session.Events.AggregateStream<QuestParty>(questId);
                var inlinedAggregate = session.Load<QuestParty>(questId);
                liveAggregate.Id.ShouldBe(inlinedAggregate.Id);
                inlinedAggregate.ToString().ShouldBe(liveAggregate.ToString());
            }
        }
        // ENDSAMPLE

        // SAMPLE: using_live_transformed_events
        public void using_live_transformed_events(IDocumentSession session)
        {
            var started = new QuestStarted { Name = "Find the Orb" };
            var joined = new MembersJoined { Day = 2, Location = "Faldor's Farm", Members = new string[] { "Garion", "Polgara", "Belgarath" } };
            var slayed1 = new MonsterSlayed { Name = "Troll" };
            var slayed2 = new MonsterSlayed { Name = "Dragon" };

            MembersJoined joined2 = new MembersJoined { Day = 5, Location = "Sendaria", Members = new string[] { "Silk", "Barak" } };


            session.Events.StartStream<Quest>(started, joined, slayed1, slayed2);
            session.SaveChanges();

            // Our MonsterDefeated documents are created inline
            // with the SaveChanges() call above and are available
            // for querying
            session.Query<MonsterDefeated>().Count()
                .ShouldBe(2);
        }
        public void capture_events()
        {
// SAMPLE: event-store-quickstart            
var store = DocumentStore.For(_ =>
{
    _.Connection(ConnectionSource.ConnectionString);
    _.Events.InlineProjections.AggregateStreamsWith<QuestParty>();
});

var questId = Guid.NewGuid();

using (var session = store.OpenSession())
{
    var started = new QuestStarted {Name = "Destroy the One Ring"};
    var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

    // Start a brand new stream and commit the new events as 
    // part of a transaction
    session.Events.StartStream<Quest>(questId, started, joined1);
    session.SaveChanges();

    // Append more events to the same stream
    var joined2 = new MembersJoined(3, "Buckland", "Merry", "Pippen");
    var joined3 = new MembersJoined(10, "Bree", "Aragorn");
    var arrived = new ArrivedAtLocation { Day = 15, Location = "Rivendell" };
    session.Events.Append(questId, joined2, joined3, arrived);
    session.SaveChanges();
}
// ENDSAMPLE

// SAMPLE: events-fetching-stream
using (var session = store.OpenSession())
{
    var events = session.Events.FetchStream(questId);
    events.Each(evt =>
    {
        Console.WriteLine($"{evt.Version}.) {evt.Data}");
    });
}
// ENDSAMPLE

// SAMPLE: events-aggregate-on-the-fly
using (var session = store.OpenSession())
{
    // questId is the id of the stream
    var party = session.Events.AggregateStream<QuestParty>(questId);
    Console.WriteLine(party);

    var party_at_version_3 = session.Events
        .AggregateStream<QuestParty>(questId, 3);


    var party_yesterday = session.Events
        .AggregateStream<QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
}
// ENDSAMPLE


using (var session = store.OpenSession())
{
    var party = session.Load<QuestParty>(questId);
    Console.WriteLine(party);
}


        }
Beispiel #6
0
 private void Apply(QuestStarted started)
 {
     Name = started.Name;
 }
Beispiel #7
0
        public void open_persisted_stream_in_new_store_with_same_settings(DocumentTracking sessionType, TenancyStyle tenancyStyle, string[] tenants)
        {
            var store   = InitStore(tenancyStyle);
            var questId = Guid.NewGuid();

            When.CalledForEach(tenants, (tenantId, index) =>
            {
                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    //Note "Id = questId" @see live_aggregate_equals_inlined_aggregate...
                    var started = new QuestStarted {
                        Id = questId, Name = "Destroy the One Ring"
                    };
                    var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                    session.Events.StartStream <Quest>(questId, started, joined1);
                    session.SaveChanges();
                }

                // events-aggregate-on-the-fly - works with same store
                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    // questId is the id of the stream
                    var party = session.Events.AggregateStream <QuestParty>(questId);

                    party.Id.ShouldBe(questId);
                    party.ShouldNotBeNull();

                    var party_at_version_3 = session.Events
                                             .AggregateStream <QuestParty>(questId, 3);

                    party_at_version_3.ShouldNotBeNull();

                    var party_yesterday = session.Events
                                          .AggregateStream <QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                    party_yesterday.ShouldBeNull();
                }

                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    var party = session.Load <QuestParty>(questId);
                    party.Id.ShouldBe(questId);
                }

                var newStore = InitStore(tenancyStyle, false);

                //Inline is working
                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    var party = session.Load <QuestParty>(questId);
                    SpecificationExtensions.ShouldNotBeNull(party);
                }
                //GetAll
                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    var parties = session.Events.QueryRawEventDataOnly <QuestParty>().ToArray();
                    foreach (var party in parties)
                    {
                        SpecificationExtensions.ShouldNotBeNull(party);
                    }
                }
                //This AggregateStream fail with NPE
                using (var session = newStore.OpenSession(tenantId, sessionType))
                {
                    // questId is the id of the stream
                    var party = session.Events.AggregateStream <QuestParty>(questId);//Here we get NPE
                    party.Id.ShouldBe(questId);

                    var party_at_version_3 = session.Events
                                             .AggregateStream <QuestParty>(questId, 3);
                    party_at_version_3.Id.ShouldBe(questId);

                    var party_yesterday = session.Events
                                          .AggregateStream <QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                    party_yesterday.ShouldBeNull();
                }
            }).ShouldThrowIf(
                (tenancyStyle == TenancyStyle.Single && tenants.Length > 1) || (tenancyStyle == TenancyStyle.Conjoined && tenants.SequenceEqual(SameTenants))
                );
        }
        public void open_persisted_stream_in_new_store_with_same_settings()
        {
            var store   = InitStore("event_store");
            var questId = Guid.NewGuid();

            using (var session = store.OpenSession())
            {
                //Note "Id = questId" @see live_aggregate_equals_inlined_aggregate...
                var started = new QuestStarted {
                    Id = questId, Name = "Destroy the One Ring"
                };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream(questId, started, joined1);
                session.SaveChanges();
            }

            // events-aggregate-on-the-fly - works with same store
            using (var session = store.OpenSession())
            {
                // questId is the id of the stream
                var party = session.Events.AggregateStream <QuestParty>(questId);

                party.Id.ShouldBe(questId);
                SpecificationExtensions.ShouldNotBeNull(party);

                var party_at_version_3 = session.Events
                                         .AggregateStream <QuestParty>(questId, 3);

                SpecificationExtensions.ShouldNotBeNull(party_at_version_3);

                var party_yesterday = session.Events
                                      .AggregateStream <QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                party_yesterday.ShouldBeNull();
            }

            using (var session = store.OpenSession())
            {
                var party = session.Load <QuestParty>(questId);
                party.Id.ShouldBe(questId);
            }

            var newStore = InitStore("event_store", false);

            //Inline is working
            using (var session = store.OpenSession())
            {
                var party = session.Load <QuestParty>(questId);
                SpecificationExtensions.ShouldNotBeNull(party);
            }
            //GetAll
            using (var session = store.OpenSession())
            {
                var parties = session.Events.QueryRawEventDataOnly <QuestParty>().ToArray <QuestParty>();
                foreach (var party in parties)
                {
                    SpecificationExtensions.ShouldNotBeNull(party);
                }
            }
            //This AggregateStream fail with NPE
            using (var session = newStore.OpenSession())
            {
                // questId is the id of the stream
                var party = session.Events.AggregateStream <QuestParty>(questId);//Here we get NPE
                party.Id.ShouldBe(questId);

                var party_at_version_3 = session.Events
                                         .AggregateStream <QuestParty>(questId, 3);
                party_at_version_3.Id.ShouldBe(questId);

                var party_yesterday = session.Events
                                      .AggregateStream <QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                party_yesterday.ShouldBeNull();
            }
        }
 public void Apply(QuestStarted started)
 {
     Name = started.Name;
 }
Beispiel #10
0
 protected bool Equals(QuestStarted other)
 {
     return(Name == other.Name && Id.Equals(other.Id));
 }
        public void assert_on_max_event_id_on_event_stream_append(
            DocumentTracking sessionType)
        {
            var store = InitStore("event_store");

            var id = Guid.NewGuid();
            var started = new QuestStarted();
            
            using (var session = store.OpenSession(sessionType))
            {
                // SAMPLE: append-events-assert-on-eventid
                session.Events.StartStream<Quest>(id, started);
                session.SaveChanges();

                var joined = new MembersJoined { Members = new[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                // Events are appended into the stream only if the maximum event id for the stream
                // would be 3 after the append operation.
                session.Events.Append(id, 3, joined, departed);

                session.SaveChanges();
                // ENDSAMPLE
            }
        }
        public async Task aggregate_stream_async_has_the_id()
        {
            var store = InitStore("event_store");
            var questId = Guid.NewGuid();

            using (var session = store.OpenSession())
            {
                var parties = await session.Query<QuestParty>().ToListAsync();
                parties.Count.ShouldBeLessThanOrEqualTo(0);
            }

            //This SaveChanges will fail with missing method (ro collection configured?)
            using (var session = store.OpenSession())
            {
                var started = new QuestStarted { Name = "Destroy the One Ring" };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream<Quest>(questId, started, joined1);
                await session.SaveChangesAsync();

                var party = await session.Events.AggregateStreamAsync<QuestParty>(questId);
                party.Id.ShouldBe(questId);
            }
        }
        public void query_before_saving(DocumentTracking sessionType)
        {
            var store = InitStore("event_store");
            var questId = Guid.NewGuid();

            using (var session = store.OpenSession())
            {
                var parties = session.Query<QuestParty>().ToArray();
                parties.Length.ShouldBeLessThanOrEqualTo(0);
            }

            //This SaveChanges will fail with missing method (ro collection configured?)
            using (var session = store.OpenSession())
            {
                var started = new QuestStarted { Name = "Destroy the One Ring" };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream<Quest>(questId, started, joined1);
                session.SaveChanges();

                var party = session.Events.AggregateStream<QuestParty>(questId);
                party.Id.ShouldBe(questId);
            }
        }
        public void open_persisted_stream_in_new_store_with_same_settings(DocumentTracking sessionType)
        {
            var store = InitStore("event_store");
            var questId = Guid.NewGuid();

            using (var session = store.OpenSession())
            {
                //Note "Id = questId" @see live_aggregate_equals_inlined_aggregate...
                var started = new QuestStarted { Id = questId, Name = "Destroy the One Ring" };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream<Quest>(questId, started, joined1);
                session.SaveChanges();
            }

            // events-aggregate-on-the-fly - works with same store
            using (var session = store.OpenSession())
            {
                // questId is the id of the stream
                var party = session.Events.AggregateStream<QuestParty>(questId);

                party.Id.ShouldBe(questId);
                party.ShouldNotBeNull();

                var party_at_version_3 = session.Events
                    .AggregateStream<QuestParty>(questId, 3);

                party_at_version_3.ShouldNotBeNull();

                var party_yesterday = session.Events
                    .AggregateStream<QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                party_yesterday.ShouldNotBeNull();
            }

            using (var session = store.OpenSession())
            {
                var party = session.Load<QuestParty>(questId);
                party.Id.ShouldBe(questId);
            }

            var newStore = InitStore("event_store", false);

            //Inline is working
            using (var session = store.OpenSession())
            {
                var party = session.Load<QuestParty>(questId);
                party.ShouldNotBeNull();
            }
            //GetAll
            using (var session = store.OpenSession())
            {
                var parties = session.Events.QueryRawEventDataOnly<QuestParty>().ToArray();
                foreach (var party in parties)
                {
                    party.ShouldNotBeNull();
                }
            }
            //This AggregateStream fail with NPE
            using (var session = newStore.OpenSession())
            {
                // questId is the id of the stream
                var party = session.Events.AggregateStream<QuestParty>(questId);//Here we get NPE
                party.Id.ShouldBe(questId);

                var party_at_version_3 = session.Events
                    .AggregateStream<QuestParty>(questId, 3);
                party_at_version_3.Id.ShouldBe(questId);

                var party_yesterday = session.Events
                    .AggregateStream<QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                party_yesterday.Id.ShouldBe(questId);
            }
        }
Beispiel #15
0
        public void get_correct_events_from_single_stream()
        {
            var listener = new StubDocumentSessionListener();
            var store = InitStore(listener);

            var id = Guid.NewGuid();
            var started = new QuestStarted();

            using (var session = store.LightweightSession())
            {
                session.Events.StartStream<Quest>(id, started);
                session.SaveChanges();

                var events = listener.AfterCommitSession.LastCommit
                    .GetEvents()
                    .ToList();

                events.Count.ShouldBe(1);
                events.ElementAt(0).Data.ShouldBeOfType<QuestStarted>();
            }

            using (var session = store.LightweightSession())
            {
                var joined = new MembersJoined { Members = new string[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                session.Events.Append(id, joined, departed);

                session.SaveChanges();

                var events = listener.AfterCommitSession.LastCommit
                    .GetEvents()
                    .ToList();

                events.Count.ShouldBe(2);
                events.ElementAt(0).Data.ShouldBeOfType<MembersJoined>();
                events.ElementAt(1).Data.ShouldBeOfType<MembersDeparted>();
            }
        }