Exemplo n.º 1
0
        public Task <T> FindAsync(IKey key)
        {
            Ensure.Condition.NotEmptyKey(key);

            // Try to find snapshot.
            ISnapshot snapshot = snapshotStore.Find(key);

            // If snapshot exists, load only newer events; otherwise load all of them.
            IEnumerable <EventModel> eventModels = null;

            if (snapshot == null)
            {
                eventModels = store.Get(key);
            }
            else
            {
                eventModels = store.Get(key, snapshot.Version);
            }

            IEnumerable <object> events = eventModels.Select(e => formatter.DeserializeEvent(Type.GetType(e.EventKey.Type), e.Payload));

            // If snapshot exists, create instance with it and newer events; otherwise create instance using all events.
            T instance = null;

            if (snapshot == null)
            {
                instance = factory.Create(key, events);
            }
            else
            {
                instance = factory.Create(key, snapshot, events);
            }

            // Return the aggregate.
            return(Task.FromResult(instance));
        }