Beispiel #1
0
        public void Init()
        {
            WarehouseAggregate[] aggregates = new[] {
                CreateWarehouseAggregate(Guid.Parse("6df8744a-d464-4826-91d1-08095ab49d93"), "Naboo"),
                CreateWarehouseAggregate(Guid.Parse("6df8744a-d464-4826-91d1-08095ab49d94"), "Tatooine")
            };

            var events = aggregates.ToDictionary(agg => agg.Id, agg => agg.PendingEvents.ToList());

            Func <Dictionary <Guid, List <IEvent> > > transactionFunc = () => events;
            Action transactionPostProcessFunc = () => events.SelectMany(agg => agg.Value).ToList().ForEach(@event => readStorageSyncEventBus.Send(@event));

            int attempts_count = 0;

            while (attempts_count <= 20)
            {
                try
                {
                    repository.Transaction(transactionFunc, transactionPostProcessFunc);
                    attempts_count = 21;
                }
                catch (Exception)
                {
                    attempts_count += 1;
                    Console.WriteLine($"### Retry connect to Rabbit MQ attempt {attempts_count}");
                }
                Thread.Sleep(TimeSpan.FromSeconds(10));
            }
        }
        public void Handle(CreateOrder command)
        {
            var createOrderEvents = new Dictionary <Guid, List <IEvent> >();

            var order = new OrderAggregate(command.OrderId, command.CustomerId);

            createOrderEvents.Add(order.Id, order.PendingEvents.ToList());

            Dictionary <Guid, List <IEvent> > orderLinesEvents = AddOrderLines(order, command.Lines);

            foreach (KeyValuePair <Guid, List <IEvent> > lineAgg in orderLinesEvents)
            {
                AddAggEvents(createOrderEvents, lineAgg.Key, lineAgg.Value);
            }

            Func <Dictionary <Guid, List <IEvent> > > transactionFunc = () => createOrderEvents;
            Action transactionPostProcessFunc = () => createOrderEvents.SelectMany(agg => agg.Value).ToList().ForEach(@event => readStorageSyncEventBus.Send(@event));

            repository.Transaction(transactionFunc, transactionPostProcessFunc);
        }