/// <summary>
        /// Sends <see cref="IDbCommandInterceptor.NonQueryExecuting" /> and
        /// <see cref="IDbCommandInterceptor.NonQueryExecuted" /> to any <see cref="IDbCommandInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbCommand.ExecuteNonQueryAsync(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="interceptionContext">Optional information about the context of the call being made.</param>
        /// <param name="cancellationToken">The cancellation token for the asynchronous operation.</param>
        /// <returns>The result of the operation, which may have been modified by interceptors.</returns>
        public virtual Task <int> NonQueryAsync(
            DbCommand command, DbCommandInterceptionContext interceptionContext, CancellationToken cancellationToken)
        {
            Check.NotNull(command, "command");
            Check.NotNull(interceptionContext, "interceptionContext");

            return(_internalDispatcher.DispatchAsync(
                       command,
                       (t, c, ct) => t.ExecuteNonQueryAsync(ct),
                       new DbCommandInterceptionContext <int>(interceptionContext).AsAsync(),
                       (i, t, c) => i.NonQueryExecuting(t, c),
                       (i, t, c) => i.NonQueryExecuted(t, c),
                       cancellationToken));
        }
Exemplo n.º 2
0
            public void Async_Dispatch_creates_faulted_task_if_Executed_interceptor_throws_exception()
            {
                var interceptionContext = new DbCommandInterceptionContext <string>();
                var mockInterceptors    = CreateMockInterceptors(
                    c => { },
                    c => { throw new Exception("Ba-da-bing!"); });

                var operation = new Task <string>(() => "0");

                var dispatcher = new InternalDispatcher <IFakeInterceptor1>();

                mockInterceptors.Each(i => dispatcher.Add(i.Object));

                var interceptTask = dispatcher.DispatchAsync(
                    () => operation,
                    interceptionContext,
                    i => i.CallMeFirst(interceptionContext),
                    i => i.CallMe(interceptionContext));

                operation.Start();

                Assert.Equal(
                    "Ba-da-bing!",
                    Assert.Throws <AggregateException>(() => interceptTask.Wait()).InnerException.Message);

                mockInterceptors.Each(i => i.Verify(m => m.CallMeFirst(interceptionContext), Times.Once()));
                mockInterceptors.First().Verify(m => m.CallMe(interceptionContext), Times.Once());
                mockInterceptors.Skip(1).Each(i => i.Verify(m => m.CallMe(interceptionContext), Times.Never()));
            }
Exemplo n.º 3
0
            public async Task Async_Dispatch_is_aborted_if_Executing_interceptor_throws_exception()
            {
                var interceptionContext = new DbCommandInterceptionContext <string>().AsAsync();
                var mockInterceptors    = CreateMockInterceptors(
                    c => { throw new Exception("Ba-da-bing!"); },
                    c => { });

                var operation = new Task <string>(() => "0");

                var dispatcher = new InternalDispatcher <IFakeInterceptor1>();

                mockInterceptors.Each(i => dispatcher.Add(i.Object));

                Assert.Equal(
                    "Ba-da-bing!",
                    (await Assert.ThrowsAsync <Exception>(
                         () => dispatcher.DispatchAsync(
                             new object(),
                             (t, c, tc) => operation,
                             interceptionContext,
                             (i, t, c) => i.CallMeFirst(c),
                             (i, t, c) => i.CallMe(c),
                             CancellationToken.None))).Message);

                mockInterceptors.First().Verify(m => m.CallMeFirst(interceptionContext), Times.Once());
                mockInterceptors.Skip(1).Each(i => i.Verify(m => m.CallMeFirst(interceptionContext), Times.Never()));
                mockInterceptors.Each(i => i.Verify(m => m.CallMe(interceptionContext), Times.Never()));
            }
Exemplo n.º 4
0
        /// <summary>
        /// Sends <see cref="IDbCommandInterceptor.NonQueryExecuting" /> and
        /// <see cref="IDbCommandInterceptor.NonQueryExecuted" /> to any <see cref="IDbCommandInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbCommand.ExecuteNonQueryAsync(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="interceptionContext">Optional information about the context of the call being made.</param>
        /// <param name="cancellationToken">The cancellation token for the asynchronous operation.</param>
        /// <returns>The result of the operation, which may have been modified by interceptors.</returns>
        public virtual Task <int> NonQueryAsync(
            DbCommand command, DbCommandInterceptionContext interceptionContext, CancellationToken cancellationToken)
        {
            Check.NotNull(command, "command");
            Check.NotNull(interceptionContext, "interceptionContext");

            cancellationToken.ThrowIfCancellationRequested();

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

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

            return(_internalDispatcher.DispatchAsync(
                       () => command.ExecuteNonQueryAsync(cancellationToken),
                       clonedInterceptionContext,
                       i => i.NonQueryExecuting(command, clonedInterceptionContext),
                       i => i.NonQueryExecuted(command, clonedInterceptionContext)));
        }
Exemplo n.º 5
0
            public void Async_Dispatch_dispatches_to_all_registered_interceptors_even_if_task_is_canceled()
            {
                var mockInterceptors = CreateMockInterceptors(
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.Null(c.Result);
                    Assert.Null(c.OriginalResult);
                    Assert.Null(c.Exception);
                    Assert.Null(c.OriginalException);
                    Assert.False(c.IsExecutionSuppressed);
                },
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.Null(c.Result);
                    Assert.Null(c.OriginalResult);
                    Assert.Null(c.Exception);
                    Assert.Null(c.OriginalException);
                    Assert.False(c.IsExecutionSuppressed);
                });

                var dispatcher = new InternalDispatcher <IFakeInterceptor1>();

                mockInterceptors.Each(i => dispatcher.Add(i.Object));

                var cancellationTokenSource = new CancellationTokenSource();
                var cancellationToken       = cancellationTokenSource.Token;
                var operation = new Task <string>(() => "0", cancellationToken);

                var interceptionContext = new DbCommandInterceptionContext <string>().AsAsync();

                var interceptTask = dispatcher.DispatchAsync(
                    new object(),
                    (t, c, tc) => operation,
                    interceptionContext,
                    (i, t, c) => i.CallMeFirst(c),
                    (i, t, c) => i.CallMe(c),
                    CancellationToken.None);

                mockInterceptors.Each(i => i.Verify(m => m.CallMeFirst(interceptionContext), Times.Once()));
                mockInterceptors.Each(i => i.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Never()));

                cancellationTokenSource.Cancel();
                Assert.Throws <AggregateException>(() => interceptTask.Wait());

                mockInterceptors.Each(i => i.Verify(m => m.CallMe(interceptionContext), Times.Once()));
            }
Exemplo n.º 6
0
            public void Async_Dispatch_can_change_exception_thrown()
            {
                var interceptionContext = new DbCommandInterceptionContext <string>().AsAsync();
                var mockInterceptors    = CreateMockInterceptors(
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.Null(c.Result);
                    Assert.Null(c.OriginalResult);
                    Assert.Null(c.Exception);
                    Assert.Null(c.OriginalException);
                    Assert.False(c.IsExecutionSuppressed);
                },
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.Null(c.Result);
                    Assert.Null(c.OriginalResult);
                    Assert.NotEmpty(c.Exception.Message);
                    Assert.Equal("Bang!", c.OriginalException.Message);
                    c.Exception = new Exception("Bing!");
                    Assert.False(c.IsExecutionSuppressed);
                });

                var dispatcher = new InternalDispatcher <IFakeInterceptor1>();

                mockInterceptors.Each(i => dispatcher.Add(i.Object));

                var operation = new Task <string>(() => { throw new Exception("Bang!"); });

                var interceptTask = dispatcher.DispatchAsync(
                    new object(),
                    (t, c, tc) => operation,
                    interceptionContext,
                    (i, t, c) => i.CallMeFirst(c),
                    (i, t, c) => i.CallMe(c),
                    CancellationToken.None);

                mockInterceptors.Each(i => i.Verify(m => m.CallMeFirst(interceptionContext), Times.Once()));
                mockInterceptors.Each(i => i.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Never()));

                operation.Start();
                Assert.Equal(
                    "Bing!",
                    Assert.Throws <AggregateException>(() => interceptTask.Wait()).InnerException.Message);

                mockInterceptors.Each(i => i.Verify(m => m.CallMe(interceptionContext), Times.Once()));
            }
Exemplo n.º 7
0
            public void Execution_of_async_operation_can_be_suppressed_by_setting_exception_with_everything_else_still_happening()
            {
                var interceptionContext = new DbCommandInterceptionContext <string>().AsAsync();
                var mockInterceptors    = CreateMockInterceptors(
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.Null(c.Result);
                    Assert.Null(c.OriginalResult);
                    Assert.Null(c.OriginalException);
                    if (c.Exception == null)
                    {
                        c.Exception = new Exception("Bing!");
                    }
                    Assert.True(c.IsExecutionSuppressed);
                },
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.Null(c.Result);
                    Assert.Null(c.OriginalResult);
                    Assert.Equal("Bing!", c.Exception.Message);
                    Assert.Null(c.OriginalException);
                    Assert.True(c.IsExecutionSuppressed);
                });

                var dispatcher = new InternalDispatcher <IFakeInterceptor1>();

                mockInterceptors.Each(i => dispatcher.Add(i.Object));

                var operation = new Task <string>(() => "0");

                var interceptTask = dispatcher.DispatchAsync(
                    new object(),
                    (t, c, tc) => operation,
                    interceptionContext,
                    (i, t, c) => i.CallMeFirst(c),
                    (i, t, c) => i.CallMe(c),
                    CancellationToken.None);

                mockInterceptors.Each(i => i.Verify(m => m.CallMeFirst(interceptionContext), Times.Once()));
                mockInterceptors.Each(i => i.Verify(m => m.CallMe(interceptionContext), Times.Once()));

                Assert.Equal(
                    "Bing!",
                    Assert.Throws <AggregateException>(() => interceptTask.Wait()).InnerException.Message);
            }
Exemplo n.º 8
0
            public void Async_Dispatch_dispatches_to_all_registered_interceptors_and_aggregates_results_of_operations()
            {
                var interceptionContext = new DbCommandInterceptionContext <string>().AsAsync();
                var mockInterceptors    = CreateMockInterceptors(
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.Null(c.Result);
                    Assert.Null(c.OriginalResult);
                    Assert.Null(c.Exception);
                    Assert.Null(c.OriginalException);
                    Assert.False(c.IsExecutionSuppressed);
                },
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.NotEmpty(c.Result);
                    Assert.Equal("0", c.OriginalResult);
                    Assert.Null(c.Exception);
                    Assert.Null(c.OriginalException);
                    Assert.False(c.IsExecutionSuppressed);
                });

                var dispatcher = new InternalDispatcher <IFakeInterceptor1>();

                mockInterceptors.Each(i => dispatcher.Add(i.Object));

                var operation = new Task <string>(() => "0");

                var interceptTask = dispatcher.DispatchAsync(
                    new object(),
                    (t, c, ct) => operation,
                    interceptionContext,
                    (i, t, c) => i.CallMeFirst(c),
                    (i, t, c) => c.Result = c.Result + i.CallMe(c),
                    CancellationToken.None);

                mockInterceptors.Each(i => i.Verify(m => m.CallMeFirst(interceptionContext), Times.Once()));
                mockInterceptors.Each(i => i.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Never()));

                operation.Start();
                interceptTask.Wait();

                Assert.Equal("0123", interceptTask.Result);

                mockInterceptors.Each(i => i.Verify(m => m.CallMe(interceptionContext), Times.Once()));
            }
Exemplo n.º 9
0
            public void Async_Dispatch_can_prevent_exception_from_being_thrown_and_return_result_instead()
            {
                var interceptionContext = new DbCommandInterceptionContext <string>().AsAsync();
                var mockInterceptors    = CreateMockInterceptors(
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.Null(c.Result);
                    Assert.Null(c.OriginalResult);
                    Assert.Null(c.Exception);
                    Assert.Null(c.OriginalException);
                    Assert.False(c.IsExecutionSuppressed);
                },
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.Null(c.OriginalResult);
                    Assert.Equal("Bang!", c.OriginalException.Message);
                    c.Exception = null;
                    c.Result    = "N";
                    Assert.False(c.IsExecutionSuppressed);
                });

                var dispatcher = new InternalDispatcher <IFakeInterceptor1>();

                mockInterceptors.Each(i => dispatcher.Add(i.Object));

                var operation = new Task <string>(() => { throw new Exception("Bang!"); });

                var interceptTask = dispatcher.DispatchAsync(
                    () => operation,
                    interceptionContext,
                    i => i.CallMeFirst(interceptionContext),
                    i => i.CallMe(interceptionContext));

                mockInterceptors.Each(i => i.Verify(m => m.CallMeFirst(interceptionContext), Times.Once()));
                mockInterceptors.Each(i => i.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Never()));

                operation.Start();
                interceptTask.Wait();

                Assert.Equal("N", interceptTask.Result);

                mockInterceptors.Each(i => i.Verify(m => m.CallMe(interceptionContext), Times.Once()));
            }
Exemplo n.º 10
0
            public void Execution_of_async_operation_can_be_suppressed_by_setting_result_with_everything_else_still_happening()
            {
                var interceptionContext = new DbCommandInterceptionContext <string>().AsAsync();
                var mockInterceptors    = CreateMockInterceptors(
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.Null(c.OriginalResult);
                    Assert.Null(c.Exception);
                    Assert.Null(c.OriginalException);
                    if (c.Result == null)
                    {
                        c.Result = "N";
                    }
                    Assert.True(c.IsExecutionSuppressed);
                },
                    c =>
                {
                    Assert.True(c.IsAsync);
                    Assert.NotEmpty(c.Result);
                    Assert.Null(c.OriginalResult);
                    Assert.Null(c.Exception);
                    Assert.Null(c.OriginalException);
                    Assert.True(c.IsExecutionSuppressed);
                });

                var dispatcher = new InternalDispatcher <IFakeInterceptor1>();

                mockInterceptors.Each(i => dispatcher.Add(i.Object));

                var operation = new Task <string>(() => "0");

                var interceptTask = dispatcher.DispatchAsync(
                    () => operation,
                    interceptionContext,
                    i => i.CallMeFirst(interceptionContext),
                    i => interceptionContext.Result = interceptionContext.Result + i.CallMe(interceptionContext));

                mockInterceptors.Each(i => i.Verify(m => m.CallMeFirst(interceptionContext), Times.Once()));
                mockInterceptors.Each(i => i.Verify(m => m.CallMe(interceptionContext), Times.Once()));

                interceptTask.Wait();

                Assert.Equal("N123", interceptTask.Result);
            }
Exemplo n.º 11
0
            public void Async_Dispatch_executes_operation_and_returns_result_if_no_dispatchers_registered()
            {
                var interceptionContext = new DbCommandInterceptionContext <string>().AsAsync();
                var dispatcher          = new InternalDispatcher <IFakeInterceptor1>();
                var operation           = new Task <string>(() => "0");

                var interceptTask = dispatcher.DispatchAsync(
                    () => operation,
                    interceptionContext,
                    i => i.CallMeFirst(interceptionContext),
                    i => interceptionContext.Result = interceptionContext.Result + i.CallMe(interceptionContext));

                operation.Start();
                interceptTask.Wait();

                Assert.Equal("0", operation.Result);
                Assert.Equal("0", interceptTask.Result);
            }
Exemplo n.º 12
0
            public void Async_Dispatch_executes_operation_and_returns_result_if_no_dispatchers_registered()
            {
                var interceptionContext = new DbCommandInterceptionContext <string>().AsAsync();
                var dispatcher          = new InternalDispatcher <IFakeInterceptor1>();
                var operation           = new Task <string>(() => "0");

                var interceptTask = dispatcher.DispatchAsync(
                    new object(),
                    (t, c, tc) => operation,
                    interceptionContext,
                    (i, t, c) => i.CallMeFirst(c),
                    (i, t, c) => c.Result = c.Result + i.CallMe(c),
                    CancellationToken.None);

                operation.Start();
                interceptTask.Wait();

                Assert.Equal("0", operation.Result);
                Assert.Equal("0", interceptTask.Result);
            }