Esempio n. 1
0
        /// <summary>
        /// Saves the specified document to a Couchbase server cluster. If change tracking is enabled via <see cref="BeginChangeTracking"/>
        /// then the document will be added to the modified list and then saved when <see cref="SubmitChanges()"/> is called.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of the document being saved.</typeparam>
        /// <param name="document">The document.</param>
        /// <exception cref="KeyAttributeMissingException">The document id could not be found.</exception>
        /// <exception cref="AmbiguousMatchException">More than one of the requested attributes was found.</exception>
        /// <exception cref="TypeLoadException">A custom attribute type cannot be loaded.</exception>
        /// <exception cref="CouchbaseWriteException">An exception wrapping the <see cref="IOperationResult"/> interface. Use this to determine what failed.</exception>
        public void Save <T>(T document)
        {
            var id = GetDocumentId(document);

            if (ChangeTrackingEnabled)
            {
                var documentWrapper = new NewDocumentWrapper
                {
                    Value    = document,
                    Metadata = new DocumentMetadata {
                        Id = id
                    },
                    IsDirty = true
                };
                documentWrapper.RegisterChangeTracking(this); //overkill with no effect

                var context = this as IChangeTrackableContext;
                context.Track(documentWrapper);
                context.Modified(documentWrapper);
            }
            else
            {
                var result = _bucket.Upsert(id, document);
                if (!result.Success)
                {
                    throw new CouchbaseWriteException(result);
                }

                AddToMutationState(result.Token);
            }
        }
        /// <summary>
        /// Saves the specified document to a Couchbase server cluster. If change tracking is enabled via <see cref="BeginChangeTracking"/>
        /// then the document will be added to the modified list and then saved when <see cref="SubmitChanges()"/> is called.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of the document being saved.</typeparam>
        /// <param name="document">The document.</param>
        /// <exception cref="KeyAttributeMissingException">The document id could not be found.</exception>
        /// <exception cref="AmbiguousMatchException">More than one of the requested attributes was found.</exception>
        /// <exception cref="TypeLoadException">A custom attribute type cannot be loaded.</exception>
        /// <exception cref="CouchbaseWriteException">An exception wrapping the <see cref="IOperationResult"/> interface. Use this to determine what failed.</exception>
        public void Save <T>(T document)
        {
            var id = GetDocumentId(document);

            if (ChangeTrackingEnabled)
            {
                var context = this as IChangeTrackableContext;

                ITrackedDocumentNode documentWrapper = document as ITrackedDocumentNode;
                if (documentWrapper == null)
                {
                    // This is a new document, so wrap in NewDocumentWrapper

                    documentWrapper = new NewDocumentWrapper
                    {
                        Value    = document,
                        Metadata = new DocumentMetadata {
                            Id = id
                        },
                        IsDirty = true
                    };

                    context.Track(documentWrapper);
                }
                else
                {
                    documentWrapper.IsDeleted = false;
                    documentWrapper.IsDirty   = true;
                }

                context.Modified(documentWrapper);
            }
            else
            {
                var result = Bucket.Upsert(id, document);
                if (!result.Success)
                {
                    throw new CouchbaseWriteException(result);
                }

                AddToMutationState(result.Token);
            }
        }