Esempio n. 1
0
        /// <summary>
        /// save events to db then apply event to state
        /// snapshot will be created for every 10 events
        /// Returns id of the newly applied event(from db)
        /// </summary>
        public async Task <long> ApplyEvents(TEvent[] events, long?rootEventId = null, long?parentEventId = null)
        {
            var aggregateEvents = new AggregateEventBase[events.Length];

            for (int i = 0; i < events.Length; i++)
            {
                // get event type
                var type = events[i].GetType();
                // get event type name and version
                var eventIdentity = EventTypeHelper.GetEventIdentity(type);
                // serialize event for db
                var serialized = JsonSerializer.Serialize(events[i], type);
                // increment aggregate version
                _aggregateVersion++;
                // add aggregateeventbase
                aggregateEvents[i] = new AggregateEventBase
                {
                    AggregateId      = AggregateId,
                    AggregateVersion = _aggregateVersion,
                    Type             = eventIdentity.Name,
                    EventVersion     = eventIdentity.Version,
                    Data             = serialized,
                    RootEventId      = rootEventId,
                    ParentEventId    = parentEventId,
                    Created          = DateTime.UtcNow
                };
            }
            var eventId = await _repository.SaveEvents(
                _aggregateName,
                aggregateEvents
                );

            for (int i = 0; i < events.Length; i++)
            {
                // update state
                _aggregate.Apply(events[i]);

                // save snapshot if ShouldSaveSnapshot returns true
                if (_shouldSaveSnapshot(events[i], _aggregateVersion))
                {
                    await SaveSnapshot();
                }
            }

            // return id
            return(eventId);
        }
Esempio n. 2
0
        /// <summary>
        /// save event to db then apply event to state
        /// snapshot will be created for every 10 events
        /// Returns id of the newly applied event(from db)
        /// </summary>
        public async Task <long> ApplyEvent(TEvent @event, long?rootEventId = null, long?parentEventId = null)
        {
            // get event type
            var type = @event.GetType();
            // get event type name and version
            var eventIdentity = EventTypeHelper.GetEventIdentity(type);
            // serialize event for db
            var serialized = JsonSerializer.Serialize(@event, type);

            // increment aggregate version
            _aggregateVersion++;
            // save event to db
            var eventId = await _repository.SaveEvent(
                _aggregateName,
                new Persistance.AggregateEventBase
            {
                AggregateId      = AggregateId,
                AggregateVersion = _aggregateVersion,
                Type             = eventIdentity.Name,
                EventVersion     = eventIdentity.Version,
                Data             = serialized,
                RootEventId      = rootEventId,
                ParentEventId    = parentEventId,
                Created          = DateTime.UtcNow
            }
                );

            // update state
            _aggregate.Apply(@event);
            // save snapshot if ShouldSaveSnapshot returns true
            if (_shouldSaveSnapshot(@event, _aggregateVersion))
            {
                await SaveSnapshot();
            }

            // return id
            return(eventId);
        }