Ejemplo n.º 1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="EfAggregateMemento" /> class.
        /// </summary>
        /// <param name="aggregate">The aggregate instance.</param>
        protected EfAggregateMemento(EventSourcedAggregateRoot aggregate) : this()
        {
            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }

            AggregateRootId   = aggregate.Id;
            LastEventSequence = aggregate.LastEventSequence;
            Version           = aggregate.Version;
            Payload           = JsonObjectSerializer.New().Serialize(aggregate);
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="EfDomainEvent" /> class.
        /// </summary>
        /// <param name="eventWrapper">The domain eventWrapper.</param>
        protected EfDomainEvent(DomainEventEnvelope eventWrapper)
        {
            Check.NotNull(eventWrapper, nameof(eventWrapper));

            var jsonObjectSerializer = new JsonObjectSerializer();

            EventId          = eventWrapper.EventId;
            Name             = eventWrapper.EventName;
            AggregateId      = eventWrapper.AggregateId;
            Sequence         = eventWrapper.Sequence;
            CreatedOnUtc     = eventWrapper.CreatedOnUtc;
            Payload          = jsonObjectSerializer.Serialize(eventWrapper.Event);
            AggregateVersion = eventWrapper.AggregateVersion;
        }
        public Tuple <TAggregateRoot, int> GetLatestSnapshot(Guid aggregateId)
        {
            var lastEventSequence = 0;

            var snapshotSet = Context.Set <TAggregateMemento>();
            var snapshot    =
                snapshotSet.AsNoTracking()
                .OrderByDescending(s => s.LastEventSequence)
                .FirstOrDefault(x => x.AggregateRootId == aggregateId);

            if (snapshot == null)
            {
                return(null);
            }

            lastEventSequence = snapshot.LastEventSequence;

            var stringSerializer = new JsonObjectSerializer();
            var aggregateRoot    = stringSerializer.Deserialize <TAggregateRoot>(snapshot.Payload);

            return(new Tuple <TAggregateRoot, int>(aggregateRoot, lastEventSequence));
        }