private Task <bool> SaveAsync(IUpdateEntry entry, CancellationToken cancellationToken)
        {
            var entityType     = entry.EntityType;
            var documentSource = GetDocumentSource(entityType);
            var collectionId   = documentSource.GetCollectionId();
            var state          = entry.EntityState;

            if (entry.SharedIdentityEntry != null)
            {
                if (entry.EntityState == EntityState.Deleted)
                {
                    return(Task.FromResult(false));
                }

                if (state == EntityState.Added)
                {
                    state = EntityState.Modified;
                }
            }

            switch (state)
            {
            case EntityState.Added:
                var newDocument = documentSource.CreateDocument(entry);
                return(_cosmosClient.CreateItemAsync(collectionId, newDocument, GetPartitionKey(entry), cancellationToken));

            case EntityState.Modified:
                var document = documentSource.GetCurrentDocument(entry);
                if (document != null)
                {
                    if (documentSource.UpdateDocument(document, entry) == null)
                    {
                        return(Task.FromResult(false));
                    }
                }
                else
                {
                    document = documentSource.CreateDocument(entry);

                    var propertyName = entityType.GetDiscriminatorProperty()?.GetPropertyName();
                    if (propertyName != null)
                    {
                        document[propertyName] =
                            JToken.FromObject(entityType.GetDiscriminatorValue(), CosmosClientWrapper.Serializer);
                    }
                }

                return(_cosmosClient.ReplaceItemAsync(
                           collectionId, documentSource.GetId(entry.SharedIdentityEntry ?? entry), document, GetPartitionKey(entry),
                           cancellationToken));

            case EntityState.Deleted:
                return(_cosmosClient.DeleteItemAsync(
                           collectionId, documentSource.GetId(entry), GetPartitionKey(entry), cancellationToken));

            default:
                return(Task.FromResult(false));
            }
        }
Beispiel #2
0
        private Task <bool> SaveAsync(IUpdateEntry entry, CancellationToken cancellationToken)
        {
            var entityType     = entry.EntityType;
            var documentSource = GetDocumentSource(entityType);
            var collectionId   = documentSource.GetCollectionId();
            var state          = entry.EntityState;

            if (entry.SharedIdentityEntry != null)
            {
                if (entry.EntityState == EntityState.Deleted)
                {
                    return(Task.FromResult(false));
                }

                if (state == EntityState.Added)
                {
                    state = EntityState.Modified;
                }
            }

            switch (state)
            {
            case EntityState.Added:
                var newDocument = documentSource.CreateDocument(entry);
                newDocument["__partitionKey"] = "0";
                return(_cosmosClient.CreateItemAsync(collectionId, newDocument, cancellationToken));

            case EntityState.Modified:
                var jObjectProperty = entityType.FindProperty(StoreKeyConvention.JObjectPropertyName);
                var document        = jObjectProperty != null
                        ? (JObject)(entry.SharedIdentityEntry ?? entry).GetCurrentValue(jObjectProperty)
                        : null;
                if (document != null)
                {
                    if (documentSource.UpdateDocument(document, entry) == null)
                    {
                        return(Task.FromResult(false));
                    }
                }
                else
                {
                    document = documentSource.CreateDocument(entry);
                    document["__partitionKey"] = "0";

                    document[entityType.Cosmos().DiscriminatorProperty.Cosmos().PropertyName] =
                        JToken.FromObject(entityType.Cosmos().DiscriminatorValue, CosmosClientWrapper.Serializer);
                }

                return(_cosmosClient.ReplaceItemAsync(
                           collectionId, documentSource.GetId(entry.SharedIdentityEntry ?? entry), document, cancellationToken));

            case EntityState.Deleted:
                return(_cosmosClient.DeleteItemAsync(collectionId, documentSource.GetId(entry), cancellationToken));

            default:
                return(Task.FromResult(false));
            }
        }