Ejemplo n.º 1
0
 public void Apply(QuestStarted started)
 {
     Name = started.Name;
 }
Ejemplo n.º 2
0
        public async Task capture_events()
        {
            #region sample_event-store-quickstart
            var store = DocumentStore.For(_ =>
            {
                _.Connection(ConnectionSource.ConnectionString);
                _.Projections.SelfAggregate <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", "Sam");

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

                // 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);
                await session.SaveChangesAsync();
            }
            #endregion


            #region sample_event-store-start-stream-with-explicit-type
            using (var session = store.OpenSession())
            {
                var started = new QuestStarted {
                    Name = "Destroy the One Ring"
                };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Sam");

                // Start a brand new stream and commit the new events as
                // part of a transaction
                session.Events.StartStream(typeof(Quest), questId, started, joined1);
            }
            #endregion

            #region sample_event-store-start-stream-with-no-type
            using (var session = store.OpenSession())
            {
                var started = new QuestStarted {
                    Name = "Destroy the One Ring"
                };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Sam");

                // Start a brand new stream and commit the new events as
                // part of a transaction
                // no stream type will be stored in database
                session.Events.StartStream(questId, started, joined1);
            }
            #endregion

            #region sample_events-fetching-stream
            using (var session = store.OpenSession())
            {
                var events = await session.Events.FetchStreamAsync(questId);

                events.Each(evt =>
                {
                    Console.WriteLine($"{evt.Version}.) {evt.Data}");
                });
            }
            #endregion

            #region 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 = await session.Events
                                         .AggregateStreamAsync <QuestParty>(questId, 3);

                var party_yesterday = await session.Events
                                      .AggregateStreamAsync <QuestParty>(questId, timestamp : DateTime.UtcNow.AddDays(-1));
            }
            #endregion

            using (var session = store.OpenSession())
            {
                var party = session.Load <QuestParty>(questId);
                Console.WriteLine(party);
            }
        }
Ejemplo n.º 3
0
        public void SampleCopyAndTransformStream()
        {
            // SAMPLE: scenario-copyandtransformstream-setup
            var started = new QuestStarted {
                Name = "Find the Orb"
            };
            var joined = new MembersJoined {
                Day = 2, Location = "Faldor's Farm", Members = new[] { "Garion", "Polgara", "Belgarath" }
            };
            var slayed1 = new MonsterSlayed {
                Name = "Troll"
            };
            var slayed2 = new MonsterSlayed {
                Name = "Dragon"
            };

            using (var session = theStore.OpenSession())
            {
                session.Events.StartStream <Quest>(started.Name, started, joined, slayed1, slayed2);
                session.SaveChanges();
            }
            // ENDSAMPLE

            // SAMPLE: scenario-copyandtransformstream-transform
            using (var session = theStore.OpenSession())
            {
                var events = session.Events.FetchStream(started.Name);

                var transformedEvents = events.SelectMany(x =>
                {
                    switch (x.Data)
                    {
                    case MonsterSlayed monster:
                        {
                            // Trolls we remove from our transformed stream
                            return(monster.Name.Equals("Troll") ? new object[] { } : new[] { monster });
                        }

                    case MembersJoined members:
                        {
                            // MembersJoined events we transform into a series of events
                            return(MemberJoined.From(members));
                        }
                    }

                    return(new[] { x.Data });
                }).Where(x => x != null).ToArray();

                var moveTo = $"{started.Name} without Trolls";
                // We copy the transformed events to a new stream
                session.Events.StartStream <Quest>(moveTo, transformedEvents);
                // And additionally mark the old stream as moved. Furthermore, we assert on the new expected stream version to guard against any racing updates
                session.Events.Append(started.Name, events.Count + 1, new StreamMovedTo
                {
                    To = moveTo
                });

                // Transactionally update the streams.
                session.SaveChanges();
            }
            // ENDSAMPLE
        }
Ejemplo n.º 4
0
        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", "Sam");

                // 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);
            }
        }
Ejemplo n.º 5
0
 public static void OnQuestStarted(QuestInfo quest)
 {
     QuestStarted?.Invoke(quest);
 }
Ejemplo n.º 6
0
 public void Apply(QuestStarted started)
 {
     Name = started.Name;
 }
Ejemplo n.º 7
0
 public void InvokeQuestStarted(TrainCarType unlockedTrainCar)
 {
     QuestStarted?.Invoke(unlockedTrainCar);
     Debug.Log($"Quest started, {unlockedTrainCar}");
 }