public virtual bool Executing(DbCommand command, DbInterceptionContext interceptionContext) { DebugCheck.NotNull(command); DebugCheck.NotNull(interceptionContext); return(InternalDispatcher.Dispatch(true, (b, i) => i.CommandExecuting(command, interceptionContext) && b)); }
public virtual bool Opening(EntityConnection entityConnection, DbInterceptionContext interceptionContext) { DebugCheck.NotNull(entityConnection); DebugCheck.NotNull(interceptionContext); return(InternalDispatcher.Dispatch(true, (b, i) => i.ConnectionOpening(entityConnection, interceptionContext) && b)); }
public virtual DbCommandTree Created(DbCommandTree commandTree, DbInterceptionContext interceptionContext) { DebugCheck.NotNull(commandTree); DebugCheck.NotNull(interceptionContext); return(InternalDispatcher.Dispatch(commandTree, (r, i) => i.TreeCreated(r, interceptionContext))); }
public void Interceptors_can_be_added_removed_and_dispatched_to_concurrently() { var interceptors = new ConcurrentStack <InterceptorForThreads>(); var dispatcher = new InternalDispatcher <InterceptorForThreads>(); const int interceptorCount = 20; const int dispatchCount = 10; // Add in parallel ExecuteInParallel( () => { var interceptor = new InterceptorForThreads(); interceptors.Push(interceptor); dispatcher.Add(interceptor); }, interceptorCount); Assert.Equal(interceptorCount, interceptors.Count); // Dispatch in parallel var calledInterceptors = new ConcurrentStack <InterceptorForThreads>(); ExecuteInParallel(() => dispatcher.Dispatch(calledInterceptors.Push), dispatchCount); Assert.Equal(dispatchCount * interceptorCount, calledInterceptors.Count); interceptors.Each(i => Assert.Equal(dispatchCount, calledInterceptors.Count(c => c == i))); var toRemove = new ConcurrentStack <InterceptorForThreads>(interceptors); // Add, remove, and dispatch in parallel ExecuteInParallel( () => { dispatcher.Dispatch(i => { }); InterceptorForThreads interceptor; toRemove.TryPop(out interceptor); dispatcher.Remove(interceptor); dispatcher.Add(interceptor); }, interceptorCount); // Dispatch in parallel calledInterceptors = new ConcurrentStack <InterceptorForThreads>(); ExecuteInParallel(() => dispatcher.Dispatch(calledInterceptors.Push), dispatchCount); Assert.Equal(dispatchCount * interceptorCount, calledInterceptors.Count); interceptors.Each(i => Assert.Equal(dispatchCount, calledInterceptors.Count(c => c == i))); }
public virtual DbCommandTree Created(DbCommandTree commandTree, DbInterceptionContext interceptionContext) { DebugCheck.NotNull(commandTree); DebugCheck.NotNull(interceptionContext); var clonedInterceptionContext = new DbCommandTreeInterceptionContext(interceptionContext); return(InternalDispatcher.Dispatch( commandTree, clonedInterceptionContext, i => i.TreeCreated(commandTree, clonedInterceptionContext))); }
/// <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> /// <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, DbInterceptionContext interceptionContext) { Check.NotNull(command, "command"); Check.NotNull(interceptionContext, "interceptionContext"); return(InternalDispatcher.Dispatch( command.ExecuteNonQuery, i => i.NonQueryExecuting(command, interceptionContext), (r, i) => i.NonQueryExecuted(command, r, interceptionContext))); }
/// <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> /// <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, DbInterceptionContext interceptionContext) { Check.NotNull(command, "command"); Check.NotNull(interceptionContext, "interceptionContext"); return(InternalDispatcher.Dispatch( command.ExecuteScalar, i => i.ScalarExecuting(command, interceptionContext), (r, i) => i.ScalarExecuted(command, r, interceptionContext))); }
/// <summary> /// Sends <see cref="IDbCommandInterceptor.AsyncReaderExecuting" /> and /// <see cref="IDbCommandInterceptor.AsyncReaderExecuted" /> 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> /// <param name="command">The command on which the operation will be executed.</param> /// <param name="behavior">The command behavior to use for the operation.</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, CommandBehavior behavior, CancellationToken cancellationToken, DbInterceptionContext interceptionContext) { Check.NotNull(command, "command"); Check.NotNull(interceptionContext, "interceptionContext"); return(InternalDispatcher.Dispatch( () => command.ExecuteReaderAsync(behavior, cancellationToken), i => i.AsyncReaderExecuting(command, behavior, interceptionContext), (r, i) => i.AsyncReaderExecuted(command, behavior, r, interceptionContext))); }
public void Result_Dispatch_dispatches_to_all_registered_interceptors_and_aggregates_result() { var mockInterceptors = CreateMockInterceptors(); var dispatcher = new InternalDispatcher <FakeInterceptor1>(); mockInterceptors.Each(i => dispatcher.Add(i.Object)); Assert.Equal("0123", dispatcher.Dispatch("0", (r, i) => r + i.CallMe())); mockInterceptors.Each(i => i.Verify(m => m.CallMe(), Times.Once())); }
public void Simple_Dispatch_dispatches_to_all_registered_interceptors() { var mockInterceptors = CreateMockInterceptors(); var dispatcher = new InternalDispatcher <FakeInterceptor1>(); mockInterceptors.Each(i => dispatcher.Add(i.Object)); dispatcher.Dispatch(i => i.CallMe()); mockInterceptors.Each(i => i.Verify(m => m.CallMe(), Times.Once())); }
/// <summary> /// Sends <see cref="IDbCommandInterceptor.AsyncNonQueryExecuting" /> and /// <see cref="IDbCommandInterceptor.AsyncNonQueryExecuted" /> to any <see cref="IDbCommandInterceptor" /> /// interceptors that are registered on <see cref="Interception" /> before/after making a /// call to <see cref="DbCommand.ExecuteNonQueryAsync(CancellationToken)" />. /// </summary> /// <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 <int> AsyncNonQuery( DbCommand command, CancellationToken cancellationToken, DbInterceptionContext interceptionContext) { Check.NotNull(command, "command"); Check.NotNull(interceptionContext, "interceptionContext"); return(InternalDispatcher.Dispatch( () => command.ExecuteNonQueryAsync(cancellationToken), i => i.AsyncNonQueryExecuting(command, interceptionContext), (r, i) => i.AsyncNonQueryExecuted(command, r, interceptionContext))); }
/// <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> /// <param name="command">The command on which the operation will be executed.</param> /// <param name="behavior">The command behavior to use for the 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 DbDataReader Reader( DbCommand command, CommandBehavior behavior, DbInterceptionContext interceptionContext) { Check.NotNull(command, "command"); Check.NotNull(interceptionContext, "interceptionContext"); return(InternalDispatcher.Dispatch( () => command.ExecuteReader(behavior), i => i.ReaderExecuting(command, behavior, interceptionContext), (r, i) => i.ReaderExecuted(command, behavior, r, interceptionContext))); }
public void Result_Dispatch_dispatches_to_all_registered_interceptors_and_aggregates_result() { var mockInterceptors = CreateMockInterceptors(c => { }, c => { }); var dispatcher = new InternalDispatcher <IFakeInterceptor1>(); mockInterceptors.Each(i => dispatcher.Add(i.Object)); Assert.Equal("0123", dispatcher.Dispatch("0", (r, i) => r + i.CallMe(new DbCommandInterceptionContext <string>()))); mockInterceptors.Each(i => i.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Once())); }
public void Simple_Dispatch_dispatches_to_all_registered_interceptors() { var mockInterceptors = CreateMockInterceptors(c => { }, c => { }); var dispatcher = new InternalDispatcher <IFakeInterceptor1>(); mockInterceptors.Each(i => dispatcher.Add(i.Object)); dispatcher.Dispatch(i => i.CallMe(new DbCommandInterceptionContext <string>())); mockInterceptors.Each(i => i.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Once())); }
/// <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))); }
/// <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))); }
public void Interceptors_for_only_the_matching_interface_type_can_be_added_and_removed() { var mockInterceptor1 = new Mock <IFakeInterceptor1>(); var mockInterceptor2 = new Mock <IFakeInterceptor2>(); var dispatcher = new InternalDispatcher <IFakeInterceptor1>(); dispatcher.Add(mockInterceptor1.Object); dispatcher.Add(mockInterceptor2.Object); dispatcher.Dispatch(i => i.CallMe(new DbCommandInterceptionContext <string>())); mockInterceptor1.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Once()); mockInterceptor2.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Never()); dispatcher.Remove(mockInterceptor1.Object); dispatcher.Remove(mockInterceptor2.Object); dispatcher.Dispatch(i => i.CallMe(new DbCommandInterceptionContext <string>())); mockInterceptor1.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Once()); mockInterceptor2.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Never()); }
/// <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))); }
public void Execution_of_operation_can_be_suppressed_by_setting_exception_with_everything_else_still_happening() { var interceptionContext = new DbCommandInterceptionContext <string>(); var mockInterceptors = CreateMockInterceptors( c => { Assert.False(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.IsSuppressed); }, c => { Assert.False(c.IsAsync); Assert.Null(c.Result); Assert.Null(c.OriginalResult); Assert.Equal("Bing!", c.Exception.Message); Assert.Null(c.OriginalException); Assert.True(c.IsSuppressed); }); var dispatcher = new InternalDispatcher <IFakeInterceptor1>(); mockInterceptors.Each(i => dispatcher.Add(i.Object)); var exception = Assert.Throws <Exception>( () => dispatcher.Dispatch( (Func <string>)(() => { throw new Exception("Bang!"); }), interceptionContext, i => i.CallMeFirst(interceptionContext), i => i.CallMe(interceptionContext))); Assert.Equal("Bing!", exception.Message); mockInterceptors.Each(i => i.Verify(m => m.CallMeFirst(interceptionContext), Times.Once())); mockInterceptors.Each(i => i.Verify(m => m.CallMe(interceptionContext), Times.Once())); }
public void Operation_Dispatch_can_change_exception_thrown() { var interceptionContext = new DbCommandInterceptionContext <string>(); var mockInterceptors = CreateMockInterceptors( c => { Assert.False(c.IsAsync); Assert.Null(c.Result); Assert.Null(c.OriginalResult); Assert.Null(c.Exception); Assert.Null(c.OriginalException); Assert.False(c.IsSuppressed); }, c => { Assert.False(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.IsSuppressed); }); var dispatcher = new InternalDispatcher <IFakeInterceptor1>(); mockInterceptors.Each(i => dispatcher.Add(i.Object)); var exception = Assert.Throws <Exception>( () => dispatcher.Dispatch( (Func <string>)(() => { throw new Exception("Bang!"); }), interceptionContext, i => i.CallMeFirst(interceptionContext), i => i.CallMe(interceptionContext))); Assert.Equal("Bing!", exception.Message); mockInterceptors.Each(i => i.Verify(m => m.CallMeFirst(interceptionContext), Times.Once())); mockInterceptors.Each(i => i.Verify(m => m.CallMe(interceptionContext), Times.Once())); }
public void Operation_Dispatch_can_prevent_exception_from_being_thrown_and_return_result_instead() { var interceptionContext = new DbCommandInterceptionContext <string>(); var mockInterceptors = CreateMockInterceptors( c => { Assert.False(c.IsAsync); Assert.Null(c.Result); Assert.Null(c.OriginalResult); Assert.Null(c.Exception); Assert.Null(c.OriginalException); Assert.False(c.IsSuppressed); }, c => { Assert.False(c.IsAsync); Assert.Null(c.OriginalResult); Assert.Equal("Bang!", c.OriginalException.Message); c.Exception = null; c.Result = "N"; Assert.False(c.IsSuppressed); }); var dispatcher = new InternalDispatcher <IFakeInterceptor1>(); mockInterceptors.Each(i => dispatcher.Add(i.Object)); Assert.Equal( "N", dispatcher.Dispatch( (Func <string>)(() => { throw new Exception("Bang!"); }), 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(interceptionContext), Times.Once())); }
public void Operation_Dispatch_dispatches_to_all_registered_interceptors_and_aggregates_results_of_operations() { var interceptionContext = new DbCommandInterceptionContext <string>(); var mockInterceptors = CreateMockInterceptors( c => { Assert.False(c.IsAsync); Assert.Null(c.Result); Assert.Null(c.OriginalResult); Assert.Null(c.Exception); Assert.Null(c.OriginalException); Assert.False(c.IsSuppressed); }, c => { Assert.False(c.IsAsync); Assert.NotEmpty(c.Result); Assert.Equal("0", c.OriginalResult); Assert.Null(c.Exception); Assert.Null(c.OriginalException); Assert.False(c.IsSuppressed); }); var dispatcher = new InternalDispatcher <IFakeInterceptor1>(); mockInterceptors.Each(i => dispatcher.Add(i.Object)); Assert.Equal( "0123", dispatcher.Dispatch( () => "0", 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())); }
public void Operation_Dispatch_is_aborted_if_Executed_interceptor_throws_exception() { var interceptionContext = new DbCommandInterceptionContext <string>(); var mockInterceptors = CreateMockInterceptors( c => { }, c => { throw new Exception("Ba-da-bing!"); }); var dispatcher = new InternalDispatcher <IFakeInterceptor1>(); mockInterceptors.Each(i => dispatcher.Add(i.Object)); Assert.Equal( "Ba-da-bing!", Assert.Throws <Exception>( () => dispatcher.Dispatch( () => "N", interceptionContext, i => i.CallMeFirst(interceptionContext), i => i.CallMe(interceptionContext))).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())); }