Ejemplo n.º 1
0
        public void New_interception_context_has_no_state()
        {
            var interceptionContext = new DbCommandBaseInterceptionContext();

            Assert.Empty(interceptionContext.ObjectContexts);
            Assert.Empty(interceptionContext.DbContexts);
            Assert.False(interceptionContext.IsAsync);
            Assert.Equal(CommandBehavior.Default, interceptionContext.CommandBehavior);
        }
Ejemplo n.º 2
0
        public void New_interception_context_has_no_state()
        {
            var interceptionContext = new DbCommandBaseInterceptionContext();

            Assert.Empty(interceptionContext.ObjectContexts);
            Assert.Empty(interceptionContext.DbContexts);
            Assert.False(interceptionContext.IsAsync);
            Assert.Equal(CommandBehavior.Default, interceptionContext.CommandBehavior);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Sends <see cref="IDbCommandInterceptor.ScalarExecuting" /> and
        ///     <see cref="IDbCommandInterceptor.ScalarExecuted" /> to any  <see cref="IDbCommandInterceptor" />
        ///     interceptors that are registered on <see cref="Interception" /> before/after making a
        ///     call to <see cref="DbCommand.ExecuteScalar" />.
        /// </summary>
        /// <remarks>
        ///     Note that the result of executing the command is returned by this method. The result is not available
        ///     in the interception context passed into this method since the interception context is cloned before
        ///     being passed to interceptors.
        /// </remarks>
        /// <param name="command">The command on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        /// <returns>The result of the operation, which may have been modified by interceptors.</returns>
        public virtual object Scalar(DbCommand command, DbCommandBaseInterceptionContext interceptionContext)
        {
            Check.NotNull(command, "command");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbCommandInterceptionContext <object>(interceptionContext);

            return(InternalDispatcher.Dispatch(
                       command.ExecuteScalar,
                       clonedInterceptionContext,
                       i => i.ScalarExecuting(command, clonedInterceptionContext),
                       i => i.ScalarExecuted(command, clonedInterceptionContext)));
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Sends <see cref="IDbCommandInterceptor.NonQueryExecuting" /> and
        ///     <see cref="IDbCommandInterceptor.NonQueryExecuted" /> to any  <see cref="IDbCommandInterceptor" />
        ///     interceptors that are registered on <see cref="Interception" /> before/after making a
        ///     call to <see cref="DbCommand.ExecuteNonQuery" />.
        /// </summary>
        /// <remarks>
        ///     Note that the result of executing the command is returned by this method. The result is not available
        ///     in the interception context passed into this method since the interception context is cloned before
        ///     being passed to interceptors.
        /// </remarks>
        /// <param name="command">The command on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        /// <returns>The result of the operation, which may have been modified by interceptors.</returns>
        public virtual int NonQuery(DbCommand command, DbCommandBaseInterceptionContext interceptionContext)
        {
            Check.NotNull(command, "command");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbCommandInterceptionContext <int>(interceptionContext);

            return(InternalDispatcher.Dispatch(
                       command.ExecuteNonQuery,
                       clonedInterceptionContext,
                       i => i.NonQueryExecuting(command, clonedInterceptionContext),
                       i => i.NonQueryExecuted(command, clonedInterceptionContext)));
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Sends <see cref="IDbCommandInterceptor.ReaderExecuting" /> and
        ///     <see cref="IDbCommandInterceptor.ReaderExecuted" /> to any  <see cref="IDbCommandInterceptor" />
        ///     interceptors that are registered on <see cref="Interception" /> before/after making a
        ///     call to <see cref="DbCommand.ExecuteReader(CommandBehavior)" />.
        /// </summary>
        /// <remarks>
        ///     Note that the result of executing the command is returned by this method. The result is not available
        ///     in the interception context passed into this method since the interception context is cloned before
        ///     being passed to interceptors.
        /// </remarks>
        /// <param name="command">The command on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        /// <returns>The result of the operation, which may have been modified by interceptors.</returns>
        public virtual DbDataReader Reader(
            DbCommand command, DbCommandBaseInterceptionContext interceptionContext)
        {
            Check.NotNull(command, "command");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbCommandInterceptionContext <DbDataReader>(interceptionContext);

            return(InternalDispatcher.Dispatch(
                       () => command.ExecuteReader(clonedInterceptionContext.CommandBehavior),
                       clonedInterceptionContext,
                       i => i.ReaderExecuting(command, clonedInterceptionContext),
                       i => i.ReaderExecuted(command, clonedInterceptionContext)));
        }
Ejemplo n.º 6
0
        public void Cloning_the_interception_context_preserves_contextual_information()
        {
            var objectContext = new ObjectContext();
            var dbContext = CreateDbContext(objectContext);

            var interceptionContext = new DbCommandBaseInterceptionContext()
                .WithDbContext(dbContext)
                .WithObjectContext(objectContext)
                .WithCommandBehavior(CommandBehavior.SchemaOnly)
                .AsAsync();

            Assert.Equal(new[] { objectContext }, interceptionContext.ObjectContexts);
            Assert.Equal(new[] { dbContext }, interceptionContext.DbContexts);
            Assert.True(interceptionContext.IsAsync);
            Assert.Equal(CommandBehavior.SchemaOnly, interceptionContext.CommandBehavior);
        }
Ejemplo n.º 7
0
        public void Cloning_the_interception_context_preserves_contextual_information()
        {
            var objectContext = new ObjectContext();
            var dbContext     = CreateDbContext(objectContext);

            var interceptionContext = new DbCommandBaseInterceptionContext()
                                      .WithDbContext(dbContext)
                                      .WithObjectContext(objectContext)
                                      .WithCommandBehavior(CommandBehavior.SchemaOnly)
                                      .AsAsync();

            Assert.Equal(new[] { objectContext }, interceptionContext.ObjectContexts);
            Assert.Equal(new[] { dbContext }, interceptionContext.DbContexts);
            Assert.True(interceptionContext.IsAsync);
            Assert.Equal(CommandBehavior.SchemaOnly, interceptionContext.CommandBehavior);
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Sends <see cref="IDbCommandInterceptor.ReaderExecuting" /> and
        ///     <see cref="IDbCommandInterceptor.ReaderExecuted" /> to any  <see cref="IDbCommandInterceptor" />
        ///     interceptors that are registered on <see cref="Interception" /> before/after making a
        ///     call to <see cref="DbCommand.ExecuteReaderAsync(CommandBehavior, CancellationToken)" />.
        /// </summary>
        /// <remarks>
        ///     Note that the result of executing the command is returned by this method. The result is not available
        ///     in the interception context passed into this method since the interception context is cloned before
        ///     being passed to interceptors.
        /// </remarks>
        /// <param name="command">The command on which the operation will be executed.</param>
        /// <param name="cancellationToken">The cancellation token for the asynchronous operation.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        /// <returns>The result of the operation, which may have been modified by interceptors.</returns>
        public virtual Task <DbDataReader> AsyncReader(
            DbCommand command, CancellationToken cancellationToken, DbCommandBaseInterceptionContext interceptionContext)
        {
            Check.NotNull(command, "command");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbCommandInterceptionContext <DbDataReader>(interceptionContext);

            if (!clonedInterceptionContext.IsAsync)
            {
                clonedInterceptionContext = clonedInterceptionContext.AsAsync();
            }

            return(InternalDispatcher.DispatchAsync(
                       () => command.ExecuteReaderAsync(clonedInterceptionContext.CommandBehavior, cancellationToken),
                       clonedInterceptionContext,
                       i => i.ReaderExecuting(command, clonedInterceptionContext),
                       i => i.ReaderExecuted(command, clonedInterceptionContext)));
        }
Ejemplo n.º 9
0
        /// <summary>
        ///     Sends <see cref="IDbCommandInterceptor.ScalarExecuting" /> and
        ///     <see cref="IDbCommandInterceptor.ScalarExecuted" /> to any  <see cref="IDbCommandInterceptor" />
        ///     interceptors that are registered on <see cref="Interception" /> before/after making a
        ///     call to <see cref="DbCommand.ExecuteScalarAsync(CancellationToken)" />.
        /// </summary>
        /// <remarks>
        ///     Note that the result of executing the command is returned by this method. The result is not available
        ///     in the interception context passed into this method since the interception context is cloned before
        ///     being passed to interceptors.
        /// </remarks>
        /// <param name="command">The command on which the operation will be executed.</param>
        /// <param name="cancellationToken">The cancellation token for the asynchronous operation.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        /// <returns>The result of the operation, which may have been modified by interceptors.</returns>
        public virtual Task <object> AsyncScalar(
            DbCommand command, CancellationToken cancellationToken, DbCommandBaseInterceptionContext interceptionContext)
        {
            Check.NotNull(command, "command");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbCommandInterceptionContext <object>(interceptionContext);

            if (!clonedInterceptionContext.IsAsync)
            {
                clonedInterceptionContext = clonedInterceptionContext.AsAsync();
            }

            return(InternalDispatcher.DispatchAsync(
                       () => command.ExecuteScalarAsync(cancellationToken),
                       clonedInterceptionContext,
                       i => i.ScalarExecuting(command, clonedInterceptionContext),
                       i => i.ScalarExecuted(command, clonedInterceptionContext)));
        }
        protected override Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
        {
            if (!_dispatchers.CancelableCommand.Executing(_command, _interceptionContext))
            {
                return new Task<DbDataReader>(() => new NullDataReader());
            }

            var interceptionContext = new DbCommandBaseInterceptionContext(_interceptionContext);
            if (behavior != CommandBehavior.Default)
            {
                interceptionContext = interceptionContext.WithCommandBehavior(behavior);
            }

            return _dispatchers.Command.AsyncReader(_command, cancellationToken, interceptionContext);
        }
        protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
        {
            if (!_dispatchers.CancelableCommand.Executing(_command, _interceptionContext))
            {
                return new NullDataReader();
            }

            var interceptionContext = new DbCommandBaseInterceptionContext(_interceptionContext);
            if (behavior != CommandBehavior.Default)
            {
                interceptionContext = interceptionContext.WithCommandBehavior(behavior);
            }

            return _dispatchers.Command.Reader(_command, interceptionContext);
        }