コード例 #1
0
        /// <summary>
        /// Disposes ambient database context.
        /// </summary>
        public void Dispose()
        {
            var immutableStack = _storageHelper.GetStack();

            // This can only happen if the consumer of the library is messing with the storage.
            if (immutableStack.IsEmpty)
            {
                throw new AmbientDbContextException("Could not dispose ambient database context because it does not exist in storage.");
            }

            var topItem = immutableStack.Peek();

            if (this != topItem)
            {
                throw new InvalidOperationException("Could not dispose ambient database context because it is not the active ambient database context. This could occur because ambient database context is being disposed out of order.");
            }

            immutableStack = immutableStack.Pop();

            _storageHelper.SaveStack(immutableStack);

            if (Parent == null)
            {
                if (Connection?.State == ConnectionState.Open)
                {
                    Connection.Close();
                    Connection.Dispose();

                    Connection = null;
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Initializes the underlying contextual storage.
        /// </summary>
        /// <returns>
        /// The immutable stack containing active ambient database contexts.
        /// </returns>
        private IImmutableStack <IAmbientDbContext> PrepareStorageAndStack()
        {
            var storage = AmbientDbContextStorageProvider.Storage;

            _storageHelper = new ContextualStorageHelper(storage);

            return(_storageHelper.GetStack());
        }
コード例 #3
0
        /// <summary>
        /// Retrieves active ambient database context from the storage.
        /// </summary>
        /// <returns>
        /// The active <see cref="IAmbientDbContextQueryProxy"/> instance.
        /// </returns>
        /// <exception cref="AmbientDbContextException">
        /// when the contextual storage does not contain an active ambient database context.
        /// </exception>
        public IAmbientDbContextQueryProxy Get()
        {
            var immutableStack = _storageHelper.GetStack();

            if (immutableStack.IsEmpty)
            {
                throw new InvalidOperationException("Could not find active ambient database context instance. Use AmbientDbContextFactory to create ambient database context before attempting to execute queries.");
            }

            return((IAmbientDbContextQueryProxy)immutableStack.Peek());
        }