Ejemplo n.º 1
0
        private static async Task RaiseStateEventWithInMemoryState <T>(MonthlyInvoiceStateMachine stateMachine, Event <T> stateEvent, T data) where T : AccountDomainEvent
        {
            var state = InMemoryStateDictionary.GetValueOrDefault(Guid.Parse(data.StreamId), null);

            if (state == null)
            {
                state = new MonthlyInvoiceState();
                InMemoryStateDictionary.Add(Guid.Parse(data.StreamId), state);
            }

            if (state.AccountStreamVersion > data.EventNumber)
            {
                return;
            }

            state.AccountStreamVersion = data.EventNumber;

            await stateMachine.RaiseEvent(state, stateEvent, data);
        }
Ejemplo n.º 2
0
        private static async Task RaiseStateEventWithSqlState <T>(MonthlyInvoiceStateMachine stateMachine, Event <T> stateEvent, T data) where T : AccountDomainEvent
        {
            using (var context = new StateDbContext())
            {
                var state = await context.MonthlyInvoiceStates.SingleOrDefaultAsync(s => s.AccountId == Guid.Parse(data.StreamId));

                if (state == null)
                {
                    state = new MonthlyInvoiceState();
                    await context.MonthlyInvoiceStates.AddAsync(state);
                }

                if (state.AccountStreamVersion > data.EventNumber)
                {
                    return;
                }

                state.AccountStreamVersion = data.EventNumber;

                await stateMachine.RaiseEvent(state, stateEvent, data);

                await context.SaveChangesAsync();
            }
        }