예제 #1
0
        async Task <GangState <TStateData> > IGangStateStore
        .CommitAsync <TStateData>(string gangId, GangState <TStateData> state, GangAudit audit)
        {
            var version = state.Version - (uint)state.Uncommitted.Count;

            foreach (var eventData in state.Uncommitted)
            {
                var e       = GangStateEvent.From(eventData, audit);
                var wrapper = new GangStateEventWrapper(
                    eventData,
                    GangStateEventMap.GetName <TStateData>(eventData.GetType()),
                    audit.SetVersion(++version)
                    );

                _events.OnNext(e);

                var key = GetEventKey(++_sequenceNumber);

                await _eventStore.SetAsync(
                    key, wrapper,
                    overwrite : false
                    );
            }

            await _cache.SetAsync(gangId,
                                  new { state.Data, state.Version }
                                  );

            await _sequenceNumberStore.SetAsync(KEY_SEQUENCE_NUMBER, _sequenceNumber);

            return(GangState.Create(state.Data, state.Version));
        }
예제 #2
0
        /// <summary>
        /// Adds services for TStateData, including;
        ///
        /// Command Executor
        /// Command handlers in same assembly as TStateData
        ///
        /// </summary>
        /// <typeparam name="TStateData">State</typeparam>
        /// <typeparam name="TStore">Store implementation</typeparam>
        /// <param name="services">Services collection</param>
        /// <returns>Services collection</returns>
        public static IServiceCollection AddGangState <TStateData, TStore>(
            this IServiceCollection services
            )
            where TStateData : class
            where TStore : class, IGangStateStore
        {
            services.AddSingleton(_stateMap = _stateMap.Add <TStateData>());

            services.TryAddSingleton <IGangStateStore, TStore>();
            services.TryAddSingleton <IGangCommandExecutor <TStateData>, GangCommandExecutor <TStateData> >();

            return(services
                   .AddGangCommandHandlers <TStateData>());
        }
예제 #3
0
        public GangStateStore(
            IGangSerializationService serializer,
            GangStateEventMap eventMap,
            IGangStoreFactory storeFactory
            )
        {
            _events     = new Subject <IGangStateEvent>();
            _eventStore = storeFactory
                          .For <GangStateEventWrapper>()
                          .AddIndex(e => e.Audit.GangId)
                          .Create(STORE_EVENTS);
            _sequenceNumberStore = storeFactory
                                   .For <uint>()
                                   .Create(STORE_EVENTS);

            _cache = storeFactory
                     .For <object>()
                     .Create(STORE_CACHE);
            _serializer     = serializer;
            _eventMap       = eventMap;
            _sequenceNumber = _sequenceNumberStore
                              .TryGetAsync(KEY_SEQUENCE_NUMBER)
                              .GetAwaiter().GetResult();
        }