Example #1
0
        static void Main()
        {
            var store = DocumentStore.For(_ =>
            {
                _.Connection("host=localhost; database=event_store; password=postgres; username=postgres");
                _.Events.StreamIdentity = StreamIdentity.AsString;
                //.._.Events.AsyncProjections.AggregateStreamsWith<ExportDefinition>();
            });



            //using (var session = store.OpenSession())
            //{
            //	// questId is the id of the stream
            //	var party = session.Events.AggregateStream<ExportDefinition>("5307eb1f-ed92-44dd-b7d4-1cca35da3fc5");
            //	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));
            //}



            //var id = new Guid("5307eb1f-ed92-44dd-b7d4-1cca35da3fc5");
            var id    = Guid.NewGuid();
            var aggId = $@"ed-{id}";

            using (var unitOfWork = new UnitOfTwerk(store.OpenSession()))
            {
                var bus = new InMemoryBus.InMemoryBus("bus");

                var exportDefinitionRepo =
                    new AsyncRepository <ExportDefinition>(ExportDefinition.Factory, unitOfWork);

                var definingExportsMessageHandlersMartenHandler = new DefiningExportsMessageHandlersMarten(exportDefinitionRepo);
                bus.Subscribe <CreateExportDefinition>(definingExportsMessageHandlersMartenHandler);
                bus.Subscribe <AddExportRowToExportDefinition>(definingExportsMessageHandlersMartenHandler);

                bus.PublishAsync(new CreateExportDefinition(id, "newest ed")).Wait();
                bus.PublishAsync(new AddExportRowToExportDefinition(id, "11 row")).Wait();
                bus.PublishAsync(new AddExportRowToExportDefinition(id, "again row")).Wait();

                unitOfWork.SaveChanges();
            }

            using (var session = store.OpenSession())
            {
                // events are an array of little IEvent objects
                // that contain both the actual event object captured
                // previously and metadata like the Id and stream version
                //var events = session.Events.FetchStream("99ca96f2-a277-43c2-8af5-308189ef69fa");
                var events = session.Events.FetchStream(aggId);
                events.Each(evt =>
                {
                    Console.WriteLine($"{evt.Version}.) {evt.Data}");
                });


                var party_at_version_3 = session.Events
                                         .AggregateStream <ExportDefinition>(aggId, 3);
            }

            Console.ReadLine();
        }
Example #2
0
        static void Run()
        {
            var store = DocumentStore.For(_ =>
            {
                _.Connection("host=localhost; database=event_store; password=postgres; username=postgres");
                _.Events.StreamIdentity = StreamIdentity.AsString;
            });


            var gen = new PersonNameGenerator();

            Console.WriteLine("CQRS Test Harness. Type exit to quit.");

            while (true)
            {
                Console.WriteLine("Enter a code to add more lines to an existing or enter to create a new object:");

                var userInput = Console.ReadLine();

                if (userInput == "exit")
                {
                    break;
                }

                var isCreate = false;
                if (Guid.TryParse(userInput, out var id) == false)
                {
                    Console.WriteLine("No code input, creating new...");
                    id       = Guid.NewGuid();
                    isCreate = true;
                }

                using (var unitOfWork = new UnitOfTwerk(store.OpenSession()))
                {
                    var bus = new InMemoryBus.InMemoryBus("bus");

                    var exportDefinitionRepo =
                        new AsyncRepository <ExportDefinition>(ExportDefinition.Factory, unitOfWork);

                    var definingExportsMessageHandlersMartenHandler = new DefiningExportsMessageHandlersMarten(exportDefinitionRepo);
                    bus.Subscribe <CreateExportDefinition>(definingExportsMessageHandlersMartenHandler);
                    bus.Subscribe <AddExportRowToExportDefinition>(definingExportsMessageHandlersMartenHandler);

                    if (isCreate)
                    {
                        bus.PublishAsync(new CreateExportDefinition(id, gen.GenerateRandomLastName())).Wait();
                    }
                    bus.PublishAsync(new AddExportRowToExportDefinition(id, gen.GenerateRandomLastName())).Wait();
                    bus.PublishAsync(new AddExportRowToExportDefinition(id, gen.GenerateRandomLastName())).Wait();

                    unitOfWork.CommitChanges();
                }


                using (var session = store.OpenSession())
                {
                    // events are an array of little IEvent objects
                    // that contain both the actual event object captured
                    // previously and metadata like the Id and stream version
                    //var events = session.Events.FetchStream("99ca96f2-a277-43c2-8af5-308189ef69fa");
                    var events = session.Events.FetchStream($@"ed-{id}");
                    events.Each(evt =>
                    {
                        Console.WriteLine($"{evt.Version}.) {evt.Data}");
                    });
                }
            }
        }