protected internal Task <string> GenerateDocumentKeyForStorageAsync(object entity) { if (entity is IDynamicMetaObjectProvider) { string id; if (GenerateEntityIdOnTheClient.TryGetIdFromDynamic(entity, out id)) { return(CompletedTask.With(id)); } else { return(GenerateKeyAsync(entity) .ContinueWith(task => { // If we generated a new id, store it back into the Id field so the client has access to to it if (task.Result != null) { GenerateEntityIdOnTheClient.TrySetIdOnDynamic(entity, task.Result); } return task.Result; })); } } return(GetOrGenerateDocumentKeyAsync(entity) .ContinueWith(task => { GenerateEntityIdOnTheClient.TrySetIdentity(entity, task.Result); return task.Result; })); }
private DocumentMetadata GetDocumentMetadata <T>(T instance) { DocumentMetadata value; if (entitiesAndMetadata.TryGetValue(instance, out value) == false) { string id; if (GenerateEntityIdOnTheClient.TryGetIdFromInstance(instance, out id) || (instance is IDynamicMetaObjectProvider && GenerateEntityIdOnTheClient.TryGetIdFromDynamic(instance, out id)) ) { AssertNoNonUniqueInstance(instance, id); var jsonDocument = GetJsonDocument(id); entitiesByKey[id] = instance; entitiesAndMetadata[instance] = value = new DocumentMetadata { ETag = UseOptimisticConcurrency ? (Guid?)Guid.Empty : null, Key = id, OriginalMetadata = jsonDocument.Metadata, Metadata = (RavenJObject)jsonDocument.Metadata.CloneToken(), OriginalValue = new RavenJObject() }; } else { throw new InvalidOperationException("Could not find the document key for " + instance); } } return(value); }