Exemple #1
0
        public static void Main(string[] args)
        {
            var es = new Eventstore <InMemoryEventRepository>();
            var a  = new A();
            var b  = new B();
            var c  = new C();

            es.Record(null, a);
            es.Record(a.Id, c);
            es.Record(c.Id, b);

            EventArchive.Write("myarchive.json", es.Replay());

            var events = EventArchive.Read("myarchive.json");

            var     es2 = new Eventstore <InMemoryEventRepository>();
            EventId id  = null;

            events.ToList().ForEach(delegate(IEvent e)
            {
                es2.Record(id, e);
                id = e.Id;
            });

            Console.WriteLine(es2.Replay().ToList().Count);
        }
Exemple #2
0
        public void Replay_in_the_right_order_with_many_events()
        {
            const string PATH = nameof(FolderEventstore_tests) + "_" + nameof(Replay_in_the_right_order_with_many_events);

            if (Directory.Exists(PATH))
            {
                Directory.Delete(PATH, true);
            }
            var sut = new Eventstore <FilesInFolderEventRepository>(PATH);

            IEvent e0 = null;

            for (var i = 0; i < 50; i++)
            {
                IEvent ePrevious = e0;
                e0 = new TestEvent {
                    Foo = i.ToString()
                };
                sut.Record(ePrevious == null ? null : ePrevious.Id, e0);
            }

            var result = sut.Replay();

            var prev = -1;

            foreach (var v in result.Select(x => int.Parse(((TestEvent)x).Foo)))
            {
                Assert.Equal(1, v - prev);
                prev = v;
            }
        }
Exemple #3
0
        public void Basic_recording_and_replaying()
        {
            const string PATH = nameof(FolderEventstore_tests) + "_" + nameof(Basic_recording_and_replaying);

            if (Directory.Exists(PATH))
            {
                Directory.Delete(PATH, true);
            }
            var sut = new Eventstore <FilesInFolderEventRepository>(PATH);

            IEvent e0 = new TestEvent {
                Foo = "a"
            };

            sut.Record(null, e0);
            IEvent e1 = new TestEvent {
                Foo = "b"
            };
            IEvent e2 = new TestEvent {
                Foo = "c"
            };

            sut.Record(e0.Id, new[] { e1, e2 });

            var result = sut.Replay();

            Assert.Equal(new[] { "a", "b", "c" }, result.Select(e => ((TestEvent)e).Foo).ToArray());
        }
Exemple #4
0
        public void Replaying_a_subset()
        {
            const string PATH = nameof(FolderEventstore_tests) + "_" + nameof(Replaying_a_subset);

            if (Directory.Exists(PATH))
            {
                Directory.Delete(PATH, true);
            }
            var sut = new Eventstore <FilesInFolderEventRepository>(PATH);

            IEvent e0 = new TestEvent {
                Foo = "a"
            };
            IEvent e1 = new AnotherTestEvent {
                Bar = 1
            };
            IEvent e2 = new AnotherTestEvent {
                Bar = 2
            };
            IEvent e3 = new TestEvent {
                Foo = "b"
            };
            IEvent e4 = new AnotherTestEvent {
                Bar = 3
            };

            sut.Record(null, new IEvent[] { e0, e1, e2, e3, e4 });

            var result = sut.Replay();

            Assert.Equal(e4.Id, result.Last().Id);
            Assert.Equal(5, result.Count());
        }
Exemple #5
0
        public void InMemoryEventstore_acceptance_test()
        {
            using (var sut = new Eventstore <InMemoryEventRepository>())
            {
                IEvent e0 = new TodoAdded("do dishes");
                sut.Record(null, e0);
                IEvent e1 = new TodoAdded("walk dog");
                sut.Record(e0.Id, e1);

                IEvent e2 = new TodoAdded("write report");
                IEvent e3 = new TodoCategorized(e2.Id.ToString(), "work");
                sut.Record(e1.Id, new IEvent[] { e2, e3 });
                IEvent e4 = new TodoDone(e0.Id.ToString());
                sut.Record(e3.Id, e4);

                var result = sut.Replay();
                var todos  = result.Aggregate(new Dictionary <string, ToDoItem>(), Map);

                Assert.Equal(2, todos.Count);
                Assert.Equal("write report", todos[e2.Id.ToString()].Subject);
                Assert.Contains("work", todos[e2.Id.ToString()].Categories);


                Dictionary <string, ToDoItem> Map(Dictionary <string, ToDoItem> items, IEvent e)
                {
                    switch (e)
                    {
                    case TodoAdded a:
                        items[a.Id.ToString()] = new ToDoItem {
                            Id = a.Id.ToString(), Subject = a.Subject
                        };
                        break;

                    case TodoDone d:
                        items.Remove(d.EntityId);
                        break;

                    case TodoCategorized c:
                        foreach (var cat in c.Categories)
                        {
                            items[c.EntityId].Categories.Add(cat);
                        }
                        break;
                    }

                    return(items);
                }
            }
        }
Exemple #6
0
        public void Replaying_from_event_id()
        {
            const string PATH = nameof(FolderEventstore_tests) + "_" + nameof(Replaying_from_event_id);

            if (Directory.Exists(PATH))
            {
                Directory.Delete(PATH, true);
            }
            var sut = new Eventstore <FilesInFolderEventRepository>(PATH);

            IEvent e0 = new TestEvent {
                Foo = "a"
            };
            IEvent e1 = new AnotherTestEvent {
                Bar = 1
            };
            IEvent e2 = new AnotherTestEvent {
                Bar = 2
            };
            IEvent e3 = new TestEvent {
                Foo = "b"
            };
            IEvent e4 = new AnotherTestEvent {
                Bar = 3
            };

            sut.Record(null, new IEvent[] { e0, e1, e2, e3, e4 });

            Replay_from_somewhere_in_the_middle();
            Replay_from_before_the_beginning();
            Replay_from_after_the_end();

            void Replay_from_somewhere_in_the_middle()
            {
                var result = sut.Replay(e2.Id);

                Assert.Equal(3, result.Count());
                Assert.Equal(2, ((AnotherTestEvent)result.ElementAt(0)).Bar);
                Assert.Equal("b", ((TestEvent)result.ElementAt(1)).Foo);
                Assert.Equal(3, ((AnotherTestEvent)result.ElementAt(2)).Bar);

                result = sut.Replay(e4.Id);
                Assert.Single(result);

                result = sut.Replay(e0.Id);
                Assert.Equal(5, result.Count());
            }

            void Replay_from_before_the_beginning()
            {
                var result = sut.Replay();

                Assert.Equal(5, result.Count());
            }

            void Replay_from_after_the_end()
            {
                var result = sut.Replay(e4.Id);

                Assert.Single(result);
            }
        }