/// <summary> /// Saves the provided <paramref name="snapshot"/> into storage. /// </summary> /// <param name="snapshot">the <see cref="Snapshot"/> to save and store.</param> public override void Save(Snapshot snapshot) { Logger.LogDebug("Persisting aggregate root snapshot", string.Format("{0}\\Save", GetType().Name)); try { using (DocumentClient client = AzureDocumentDbSnapshotStoreConnectionStringFactory.GetSnapshotStoreConnectionClient()) { Database database = AzureDocumentDbHelper.CreateOrReadDatabase(client, AzureDocumentDbSnapshotStoreConnectionStringFactory.GetSnapshotStoreConnectionDatabaseName()).Result; string collectionName = AzureDocumentDbSnapshotStoreConnectionStringFactory.GetSnapshotStoreConnectionCollectionName(); DocumentCollection collection = AzureDocumentDbHelper.CreateOrReadCollection(client, database, collectionName, UniqueIndexProperties).Result; Logger.LogDebug("Creating document for snapshot asynchronously", string.Format("{0}\\Save", GetType().Name)); AzureDocumentDbHelper.ExecuteFaultTollerantFunction ( () => { Task <ResourceResponse <Document> > work = client.CreateDocumentAsync ( collection.SelfLink, BuildEventData(snapshot) ); work.ConfigureAwait(false); work.Wait(); } ); } } finally { Logger.LogDebug("Persisting aggregate root snapshot... Done", string.Format("{0}\\Save", GetType().Name)); } }
/// <summary> /// Get the latest <see cref="Snapshot"/> from storage. /// </summary> /// <returns>The most recent <see cref="Snapshot"/> of</returns> protected virtual async Task <Snapshot> GetAsync(Type aggregateRootType, string streamName) { using (DocumentClient client = AzureDocumentDbSnapshotStoreConnectionStringFactory.GetSnapshotStoreConnectionClient()) { Database database = AzureDocumentDbHelper.CreateOrReadDatabase(client, AzureDocumentDbSnapshotStoreConnectionStringFactory.GetSnapshotStoreConnectionDatabaseName()).Result; string collectionName = AzureDocumentDbSnapshotStoreConnectionStringFactory.GetSnapshotStoreConnectionCollectionName(); DocumentCollection collection = AzureDocumentDbHelper.CreateOrReadCollection(client, database, collectionName, UniqueIndexProperties).Result; IOrderedQueryable <EventData> query = client.CreateDocumentQuery <EventData>(collection.SelfLink); IEnumerable <EventData> results = query.Where(snapshot => snapshot.AggregateId == streamName); return(AzureDocumentDbHelper.ExecuteFaultTollerantFunction(() => results .ToList() .OrderByDescending(eventData => eventData.Version) .Take(1) .Select(EventDeserialiser.Deserialise) .SingleOrDefault() )); } }