Exemplo n.º 1
0
        public static ReadModel Load()
        {
            var model = new ReadModel();

            foreach (var @event in EventStore.AllEvents())
            {
                model.Update(@event);
            }

            return(model);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //Rebuild the read model and hook it up to
            //receive future events
            var readModel = ReadModel.Load();

            MessageBus.Subscribe <Event>(e => readModel.Update(e));

            //Simulate a command
            var id = CreateEmployee("bart", "*****@*****.**");

            //Recreate an aggregate from events
            var bart = Load(id);

            Console.WriteLine(bart.Name);
            Console.WriteLine(bart.Email);

            //Issue another command
            ChangeEmail(id, "*****@*****.**");

            bart = Load(id);
            Console.WriteLine(bart.Name);
            Console.WriteLine(bart.Email);

            //Generate some data so we have something to query
            CreateManyEmployees();

            //Simulate a query that accesses the readmodel
            var page         = 4;
            var itemsPerPage = 10;

            readModel.EmployeesByEmail
            .Values
            .Skip((page - 1) * itemsPerPage)
            .Take(itemsPerPage)
            .ToList()
            .ForEach(e => Console.WriteLine(e.Email));
        }