コード例 #1
0
        /// <summary>
        /// Adds one or more document keys to an entity's cascade delete metadata.
        /// Requires the cascade delete bundle on the server.
        /// </summary>
        /// <param name="session">The Raven advanced session.</param>
        /// <param name="entity">The entity to update.</param>
        /// <param name="documentKeys">One or more keys to foreign documents.</param>
        public static void AddCascadeDeleteReference(this IAdvancedDocumentSessionOperations session, object entity, params string[] documentKeys)
        {
            var metadata = session.GetMetadataFor(entity);

            if (metadata == null)
            {
                throw new InvalidOperationException("The entity must be tracked in the session before calling this method.");
            }

            if (documentKeys.Length == 0)
            {
                throw new ArgumentException("At least one document key must be specified.");
            }

            const string metadataKey = "Raven-Cascade-Delete-Documents";

            RavenJToken token;

            if (!metadata.TryGetValue(metadataKey, out token))
            {
                token = new RavenJArray();
            }

            var list = (RavenJArray)token;

            foreach (var documentKey in documentKeys.Where(key => !list.Contains(key)))
            {
                list.Add(documentKey);
            }

            metadata[metadataKey] = list;
        }
コード例 #2
0
        /// <summary>
        /// Gets a document containing all of the temporal metadata for every revision of a document.
        /// </summary>
        /// <param name="session">The advanced session.</param>
        /// <param name="id">The non-temporal document id.</param>
        /// <returns>A TemporalHistory document.</returns>
        public static TemporalHistory GetTemporalHistoryFor(this IAdvancedDocumentSessionOperations session, string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }

            if (id.IndexOf(TemporalConstants.TemporalKeySeparator, StringComparison.OrdinalIgnoreCase) != -1)
            {
                throw new ArgumentException("Pass the non-temporal id, not a temporal revisions key.");
            }

            if (id.StartsWith("Raven/", StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException("Raven system docs can not be versioned.");
            }

            var key     = TemporalHistory.GetKeyFor(id);
            var history = ((IDocumentSession)session).Load <TemporalHistory>(key);

            if (history != null)
            {
                // don't track this in the session
                session.Evict(history);
            }

            return(history);
        }
コード例 #3
0
        /// <summary>
        /// Provides access to DatabaseCommands for the same database that the session was opened for.
        /// </summary>
        /// <param name="session">The Raven advanced session.</param>
        /// <returns>A DatabaseCommands instance.</returns>
        public static IDatabaseCommands GetDatabaseCommands(this IAdvancedDocumentSessionOperations session)
        {
            // Note - We must cast to a DocumentSession to get the correct DatabaseCommands instance.
            //        session.DocumentStore.DatabaseCommands will not necessarily point at the same database
            //        that the session was opened for.  Hence the usefulness of this extension method.

            return(((DocumentSession)session).DatabaseCommands);
        }
コード例 #4
0
        private static void ConfigureTemporalVersioning(this IAdvancedDocumentSessionOperations session, bool enabled, string entityName)
        {
            var inMemoryDocumentSessionOperations = ((InMemoryDocumentSessionOperations)session);
            var configuration = new TemporalVersioningConfiguration
            {
                Id      = String.Format("Raven/{0}/{1}", TemporalConstants.BundleName, entityName),
                Enabled = enabled
            };

            inMemoryDocumentSessionOperations.Store(configuration);
        }
コード例 #5
0
        /// <summary>
        /// Generates a document id using the registered conventions, such as the Raven HiLo generator.
        /// Returns the id without applying it to any particular entity, thus allowing you to pre-generate the id.
        /// Useful for detached operations, such as in CQRS. For example, the id could be generated ahead of time,
        /// returned to the caller, then used in a command message.  The entity would then be created in a command
        /// handler, using the id from the message.  There are other scenarios that this can also be useful.
        /// </summary>
        /// <typeparam name="T">The entity type.</typeparam>
        /// <param name="session">The current session.</param>
        /// <returns>The generated string id.</returns>
        public static string GenerateIdFor <T>(this IAdvancedDocumentSessionOperations session)
        {
            // An entity instance is required to generate a key, but we only have a type.
            // We might not have a public constructor, so we must use reflection.
            var entity = Activator.CreateInstance(typeof(T), true);

            // Generate an ID using the commands and conventions from the current session
            var conventions      = session.DocumentStore.Conventions;
            var databaseName     = session.GetDatabaseName();
            var databaseCommands = session.GetDatabaseCommands();

            return(conventions.GenerateDocumentKey(databaseName, databaseCommands, entity));
        }
コード例 #6
0
        private static T[] ProcessResults <T>(IAdvancedDocumentSessionOperations session, GetRevisionCommand command)
        {
            var results = command.Result.Results;
            var res     = new T[results.Length];

            for (int i = 0; i < results.Length; i++)
            {
                var    obj = (BlittableJsonReaderObject)results[i];
                object key;
                obj.TryGetMember("Id", out key);
                res[i] = (T)session.ConvertToEntity(typeof(T), key.ToString(), obj);
            }
            return(res);
        }
コード例 #7
0
 /// <summary>
 /// Checks the database to see if a document with the id specified exists.
 /// </summary>
 /// <param name="session">The advanced session instance for a particular database.</param>
 /// <param name="key">The document key.</param>
 /// <returns>True if the document exists, false otherwise.</returns>
 public static bool DocumentExists(this IAdvancedDocumentSessionOperations session, string key)
 {
     return(session.GetDatabaseCommands().DocumentExists(key));
 }
コード例 #8
0
 /// <summary>
 /// Gets the database name that the session was opened for.
 /// </summary>
 /// <param name="session">The Raven advanced session.</param>
 /// <returns>The database name.</returns>
 public static string GetDatabaseName(this IAdvancedDocumentSessionOperations session)
 {
     return(((DocumentSession)session).DatabaseName);
 }
コード例 #9
0
        /// <summary>
        /// Configures temporal versioning for an individual document type.
        /// </summary>
        public static void ConfigureTemporalVersioning(this IAdvancedDocumentSessionOperations session, bool enabled, Type documentType)
        {
            var entityName = session.DocumentStore.Conventions.GetTypeTagName(documentType);

            session.ConfigureTemporalVersioning(enabled, entityName);
        }
コード例 #10
0
 /// <summary>
 /// Configures temporal versioning for an individual document type.
 /// </summary>
 public static void ConfigureTemporalVersioning <T>(this IAdvancedDocumentSessionOperations session, bool enabled)
 {
     session.ConfigureTemporalVersioning(enabled, typeof(T));
 }
コード例 #11
0
 /// <summary>
 /// Configures temporal versioning for all documents that aren't configured separately.
 /// </summary>
 public static void ConfigureTemporalVersioningDefaults(this IAdvancedDocumentSessionOperations session, bool enabled)
 {
     session.ConfigureTemporalVersioning(enabled, "DefaultConfiguration");
 }