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);
        }
        internal override void ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement, DbInterceptionContext interceptionContext)
        {
            DebugCheck.NotNull(transaction);
            DebugCheck.NotNull(migrationStatement);

            _logger.Verbose(migrationStatement.Sql);

            var connection = DbInterception.Dispatch.Transaction.GetConnection(transaction, interceptionContext);
            var providerServices = DbProviderServices.GetProviderServices(connection);

            if (providerServices != null)
            {
                providerServices.RegisterInfoMessageHandler(
                    connection,
                    message =>
                        {
                            if (!string.Equals(message, _lastInfoMessage, StringComparison.OrdinalIgnoreCase))
                            {
                                _logger.Warning(message);

                                // simple duplicate filtering
                                _lastInfoMessage = message;
                            }
                        });
            }

            base.ExecuteSql(transaction, migrationStatement, interceptionContext);
        }
        /// <summary>
        /// Creates a new <see cref="DbInterceptionContext" /> by copying state from the given
        /// interception context. See <see cref="DbInterceptionContext.Clone" />
        /// </summary>
        /// <param name="copyFrom">The context from which to copy state.</param>
        protected DbInterceptionContext(DbInterceptionContext copyFrom)
        {
            Check.NotNull(copyFrom, "copyFrom");

            _dbContexts = copyFrom.DbContexts.Where(c => c.InternalContext == null || !c.InternalContext.IsDisposed).ToList();
            _objectContexts = copyFrom.ObjectContexts.Where(c => !c.IsDisposed).ToList();
            _isAsync = copyFrom._isAsync;
        }
        public void New_interception_context_has_no_state()
        {
            var interceptionContext = new DbInterceptionContext();

            Assert.Empty(interceptionContext.ObjectContexts);
            Assert.Empty(interceptionContext.DbContexts);
            Assert.False(interceptionContext.IsAsync);
        }
        public virtual void Loaded(DbConfigurationLoadedEventArgs loadedEventArgs, DbInterceptionContext interceptionContext)
        {
            DebugCheck.NotNull(loadedEventArgs);
            DebugCheck.NotNull(interceptionContext);

            var clonedInterceptionContext = new DbConfigurationInterceptionContext(interceptionContext);

            _internalDispatcher.Dispatch(i => i.Loaded(loadedEventArgs, clonedInterceptionContext));
        }
        public virtual DbCommandTree Created(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
        {
            DebugCheck.NotNull(commandTree);
            DebugCheck.NotNull(interceptionContext);

            return _internalDispatcher.Dispatch(
                commandTree,
                new DbCommandTreeInterceptionContext(interceptionContext),
                (i, c) => i.TreeCreated(c));
        }
        /// <summary>
        /// Creates a new <see cref="EnlistTransactionInterceptionContext" /> by copying immutable state from the given
        /// interception context. Also see <see cref="Clone" />
        /// </summary>
        /// <param name="copyFrom">The context from which to copy state.</param>
        public EnlistTransactionInterceptionContext(DbInterceptionContext copyFrom)
            : base(copyFrom)
        {
            Check.NotNull(copyFrom, "copyFrom");

            var asThisType = copyFrom as EnlistTransactionInterceptionContext;
            if (asThisType != null)
            {
                _transaction = asThisType._transaction;
            }
        }
        /// <summary>
        /// Creates a new <see cref="DbTransactionInterceptionContext" /> by copying immutable state from the given
        /// interception context. Also see <see cref="Clone" />
        /// </summary>
        /// <param name="copyFrom">The context from which to copy state.</param>
        public DbTransactionInterceptionContext(DbInterceptionContext copyFrom)
            : base(copyFrom)
        {
            var transactionInterceptionContext = copyFrom as DbTransactionInterceptionContext;
            if (transactionInterceptionContext != null)
            {
                _connection = transactionInterceptionContext.Connection;
            }

            Check.NotNull(copyFrom, "copyFrom");
        }
        /// <summary>
        /// Creates a new <see cref="DbCommandInterceptionContext" /> by copying state from the given
        /// interception context. Also see <see cref="Clone" />
        /// </summary>
        /// <param name="copyFrom">The context from which to copy state.</param>
        public DbCommandInterceptionContext(DbInterceptionContext copyFrom)
            : base(copyFrom)
        {
            Check.NotNull(copyFrom, "copyFrom");

            var asThisType = copyFrom as DbCommandInterceptionContext;
            if (asThisType != null)
            {
                _commandBehavior = asThisType._commandBehavior;
            }
        }
        /// <summary>
        /// Sends <see cref="IDbTransactionInterceptor.IsolationLevelGetting" /> and
        /// <see cref="IDbTransactionInterceptor.IsolationLevelGot" /> to any <see cref="IDbTransactionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after
        /// getting <see cref="DbTransaction.IsolationLevel" />.
        /// </summary>
        /// <remarks>
        /// Note that the value of the property 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="transaction">The transaction 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 IsolationLevel GetIsolationLevel(DbTransaction transaction, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            return InternalDispatcher.Dispatch(
                transaction,
                (t, c) => t.IsolationLevel,
                new DbTransactionInterceptionContext<IsolationLevel>(interceptionContext),
                (i, t, c) => i.IsolationLevelGetting(t, c),
                (i, t, c) => i.IsolationLevelGot(t, c));
        }
        /// <summary>
        /// Sends <see cref="IDbTransactionInterceptor.ConnectionGetting" /> and
        /// <see cref="IDbTransactionInterceptor.ConnectionGot" /> to any <see cref="IDbTransactionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after
        /// getting <see cref="DbTransaction.Connection" />.
        /// </summary>
        /// <remarks>
        /// Note that the value of the property 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="transaction">The transaction 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 DbConnection GetConnection(DbTransaction transaction, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            return InternalDispatcher.Dispatch(
                transaction,
                (t, c) => t.Connection,
                new DbTransactionInterceptionContext<DbConnection>(interceptionContext),
                (i, t, c) => i.ConnectionGetting(t, c),
                (i, t, c) => i.ConnectionGot(t, c));
        }
        public InterceptableDbCommand(DbCommand command, DbInterceptionContext context, DbDispatchers dispatchers = null)
        {
            DebugCheck.NotNull(command);
            DebugCheck.NotNull(context);

            GC.SuppressFinalize(this);

            _command = command;

            _interceptionContext = context;

            _dispatchers = dispatchers ?? DbInterception.Dispatch;
        }
        /// <summary>
        /// Sends <see cref="IDbConnectionInterceptor.Closing" /> and
        /// <see cref="IDbConnectionInterceptor.Closed" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbConnection.Close" />.
        /// </summary>
        /// <param name="connection">The connection on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        public virtual void Close(
            DbConnection connection, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(interceptionContext, "interceptionContext");

            InternalDispatcher.Dispatch(
                connection,
                (t, c) => t.Close(),
                new DbConnectionInterceptionContext(interceptionContext),
                (i, t, c) => i.Closing(t, c),
                (i, t, c) => i.Closed(t, c));
        }
        public void Interception_context_can_be_made_async_and_other_state_is_preserved()
        {
            var objectContext = new ObjectContext();
            var dbContext = CreateDbContext(objectContext);

            var interceptionContext = new DbInterceptionContext()
                .WithDbContext(dbContext)
                .WithObjectContext(objectContext)
                .AsAsync();

            Assert.Equal(new[] { objectContext }, interceptionContext.ObjectContexts);
            Assert.Equal(new[] { dbContext }, interceptionContext.DbContexts);
            Assert.True(interceptionContext.IsAsync);
        }
        public void ExecuteScalar_should_dispatch_to_interceptor_and_optionally_execute()
        {
            var mockCommand = new Mock<DbCommand>();
            mockCommand.Setup(m => m.ExecuteScalar()).Returns(11);

            var mockCancelable = new Mock<ICancelableDbCommandInterceptor>();
            var mockPublicInterceptor = new Mock<DbCommandInterceptor> { CallBase = true };

            var dispatchers = new DbDispatchers();
            dispatchers.AddInterceptor(mockCancelable.Object);
            dispatchers.AddInterceptor(mockPublicInterceptor.Object);

            var context = new Mock<InternalContextForMock>().Object.Owner;
            var interceptionContext = new DbInterceptionContext().WithDbContext(context);
            var interceptableDbCommand = new InterceptableDbCommand(mockCommand.Object, interceptionContext, dispatchers);

            mockCancelable.Setup(m => m.CommandExecuting(mockCommand.Object, It.IsAny<DbInterceptionContext>())).Returns(false);

            interceptableDbCommand.ExecuteScalar();

            mockCommand.Verify(m => m.ExecuteScalar(), Times.Never());

            mockCancelable.Verify(m => m.CommandExecuting(mockCommand.Object, interceptionContext), Times.Once());

            mockPublicInterceptor.Verify(
                m => m.ScalarExecuting(It.IsAny<DbCommand>(), It.IsAny<DbCommandInterceptionContext<object>>()), Times.Never());

            mockPublicInterceptor.Verify(
                m => m.ScalarExecuted(It.IsAny<DbCommand>(), It.IsAny<DbCommandInterceptionContext<object>>()), Times.Never());

            mockCancelable.Setup(m => m.CommandExecuting(mockCommand.Object, It.IsAny<DbInterceptionContext>())).Returns(true);

            Assert.Equal(11, interceptableDbCommand.ExecuteScalar());

            mockCommand.Verify(m => m.ExecuteScalar(), Times.Once());

            mockCancelable.Verify(m => m.CommandExecuting(mockCommand.Object, interceptionContext), Times.Exactly(2));

            mockPublicInterceptor.Verify(
                m => m.ScalarExecuting(
                    mockCommand.Object,
                    It.Is<DbCommandInterceptionContext<object>>(c => c.DbContexts.Contains(context, ReferenceEquals))), Times.Once());

            mockPublicInterceptor.Verify(
                m => m.ScalarExecuted(
                    mockCommand.Object,
                    It.Is<DbCommandInterceptionContext<object>>(
                        c => c.DbContexts.Contains(context, ReferenceEquals) && (int)c.Result == 11)), Times.Once());
        }
        public void Dispose_executes_operation_and_dispatches_to_interceptors()
        {
            var transactionMock = new Mock<DbTransaction>();
            var interceptorMock = new Mock<IDbTransactionInterceptor>();
            var dispatcher = new DbTransactionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(interceptorMock.Object);

            var interceptionContext = new DbInterceptionContext();
            dispatcher.Dispose(transactionMock.Object, interceptionContext);

            transactionMock.Protected().Verify("Dispose", Times.Once(), ItExpr.IsAny<bool>());
            interceptorMock.Verify(m => m.Disposing(transactionMock.Object, It.IsAny<DbTransactionInterceptionContext>()), Times.Once());
            interceptorMock.Verify(m => m.Disposed(transactionMock.Object, It.IsAny<DbTransactionInterceptionContext>()), Times.Once());
        }
        public void Opening_dispatches_to_interceptors()
        {
            var interceptionContext = new DbInterceptionContext();
            var connection = new Mock<EntityConnection>().Object;

            var mockInterceptor = new Mock<ICancelableEntityConnectionInterceptor>();
            mockInterceptor.Setup(m => m.ConnectionOpening(connection, interceptionContext)).Returns(true);

            var dispatcher = new CancelableEntityConnectionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(mockInterceptor.Object);

            Assert.True(dispatcher.Opening(connection, interceptionContext));

            mockInterceptor.Verify(m => m.ConnectionOpening(connection, interceptionContext));
        }
        public void Interception_context_can_be_associated_with_one_or_more_DbContexts()
        {
            var context1 = CreateDbContext(new ObjectContext());
            var interceptionContext1 = new DbInterceptionContext().WithDbContext(context1);

            Assert.Equal(new[] { context1 }, interceptionContext1.DbContexts);

            var context2 = CreateDbContext(new ObjectContext());
            var interceptionContext2 = interceptionContext1.WithDbContext(context2);

            Assert.Contains(context1, interceptionContext2.DbContexts);
            Assert.Contains(context2, interceptionContext2.DbContexts);
            Assert.Empty(interceptionContext2.ObjectContexts);

            Assert.Equal(new[] { context1 }, interceptionContext1.DbContexts);
        }
        public void Commit_executes_operation_and_dispatches_to_interceptors()
        {
            var transactionMock = new Mock<DbTransaction>();
            transactionMock.Protected().Setup<DbConnection>("DbConnection").Returns(new Mock<DbConnection>().Object);
            var interceptorMock = new Mock<IDbTransactionInterceptor>();
            var dispatcher = new DbTransactionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(interceptorMock.Object);

            var interceptionContext = new DbInterceptionContext();
            dispatcher.Commit(transactionMock.Object, interceptionContext);

            transactionMock.Verify(m => m.Commit(), Times.Once());
            interceptorMock.Verify(m => m.Committing(transactionMock.Object, It.IsAny<DbTransactionInterceptionContext>()), Times.Once());
            interceptorMock.Verify(m => m.Committed(transactionMock.Object, It.IsAny<DbTransactionInterceptionContext>()), Times.Once());
        }
        public void Executing_dispatches_to_interceptors()
        {
            var interceptionContext = new DbInterceptionContext();
            var command = new Mock<DbCommand>().Object;

            var mockInterceptor = new Mock<ICancelableDbCommandInterceptor>();
            mockInterceptor.Setup(m => m.CommandExecuting(command, interceptionContext)).Returns(true);

            var dispatcher = new CancelableDbCommandDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(mockInterceptor.Object);

            Assert.True(dispatcher.Executing(command, interceptionContext));

            mockInterceptor.Verify(m => m.CommandExecuting(command, interceptionContext));
        }
        public void Interception_context_can_be_associated_with_one_or_more_DbContexts()
        {
            var context1             = CreateDbContext(new ObjectContext());
            var interceptionContext1 = new DbInterceptionContext().WithDbContext(context1);

            Assert.Equal(new[] { context1 }, interceptionContext1.DbContexts);

            var context2             = CreateDbContext(new ObjectContext());
            var interceptionContext2 = interceptionContext1.WithDbContext(context2);

            Assert.Contains(context1, interceptionContext2.DbContexts);
            Assert.Contains(context2, interceptionContext2.DbContexts);
            Assert.Empty(interceptionContext2.ObjectContexts);

            Assert.Equal(new[] { context1 }, interceptionContext1.DbContexts);
        }
Beispiel #23
0
        public void Dispose_executes_operation_and_dispatches_to_interceptors()
        {
            var transactionMock    = new Mock <DbTransaction>();
            var interceptorMock    = new Mock <IDbTransactionInterceptor>();
            var dispatcher         = new DbTransactionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;

            internalDispatcher.Add(interceptorMock.Object);

            var interceptionContext = new DbInterceptionContext();

            dispatcher.Dispose(transactionMock.Object, interceptionContext);

            transactionMock.Protected().Verify("Dispose", Times.Once(), ItExpr.IsAny <bool>());
            interceptorMock.Verify(m => m.Disposing(transactionMock.Object, It.IsAny <DbTransactionInterceptionContext>()), Times.Once());
            interceptorMock.Verify(m => m.Disposed(transactionMock.Object, It.IsAny <DbTransactionInterceptionContext>()), Times.Once());
        }
        public void Loaded_dispatches_to_interceptors()
        {
            var interceptionContext = new DbInterceptionContext();
            var eventArgs = new DbConfigurationLoadedEventArgs(new Mock<InternalConfiguration>(null, null, null, null, null).Object);
            var mockInterceptor1 = new Mock<IDbConfigurationInterceptor>();
            var mockInterceptor2 = new Mock<IDbConfigurationInterceptor>();

            var dispatcher = new DbConfigurationDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(mockInterceptor1.Object);
            internalDispatcher.Add(mockInterceptor2.Object);

            dispatcher.Loaded(eventArgs, interceptionContext);

            mockInterceptor1.Verify(m => m.Loaded(eventArgs, It.IsAny<DbConfigurationInterceptionContext>()), Times.Once());
            mockInterceptor2.Verify(m => m.Loaded(eventArgs, It.IsAny<DbConfigurationInterceptionContext>()), Times.Once());
        }
        public void GetState_executes_operation_and_dispatches_to_interceptors()
        {
            var mockConnection     = new Mock <DbConnection>();
            var mockInterceptor    = new Mock <IDbConnectionInterceptor>();
            var dispatcher         = new DbConnectionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;

            internalDispatcher.Add(mockInterceptor.Object);

            var interceptionContext = new DbInterceptionContext();

            dispatcher.GetState(mockConnection.Object, interceptionContext);

            mockConnection.Verify(m => m.State, Times.Once());
            mockInterceptor.Verify(m => m.StateGetting(mockConnection.Object, It.IsAny <DbConnectionInterceptionContext <ConnectionState> >()), Times.Once());
            mockInterceptor.Verify(m => m.StateGot(mockConnection.Object, It.IsAny <DbConnectionInterceptionContext <ConnectionState> >()), Times.Once());
        }
        public void GetConnection_executes_operation_and_dispatches_to_interceptors()
        {
            var transactionMock = new Mock<DbTransaction>();
            var interceptorMock = new Mock<IDbTransactionInterceptor>();
            var dispatcher = new DbTransactionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(interceptorMock.Object);

            var interceptionContext = new DbInterceptionContext();
            dispatcher.GetConnection(transactionMock.Object, interceptionContext);

            transactionMock.Protected().Verify<DbConnection>("DbConnection", Times.Once());
            interceptorMock.Verify(
                m => m.ConnectionGetting(transactionMock.Object, It.IsAny<DbTransactionInterceptionContext<DbConnection>>()), Times.Once());
            interceptorMock.Verify(
                m => m.ConnectionGot(transactionMock.Object, It.IsAny<DbTransactionInterceptionContext<DbConnection>>()), Times.Once());
        }
        internal EntityCommand(DbInterceptionContext interceptionContext, EntityDataReaderFactory factory)
        {
            DebugCheck.NotNull(interceptionContext);

            // Initalize the member field with proper default values
            _designTimeVisible = true;
            _commandType = CommandType.Text;
            _updatedRowSource = UpdateRowSource.Both;
            _parameters = new EntityParameterCollection();
            _interceptionContext = interceptionContext;

            // Future Enhancement: (See SQLPT #300004256) At some point it would be  
            // really nice to read defaults from a global configuration, but we're not 
            // doing that today.  
            _enableQueryPlanCaching = true;

            _entityDataReaderFactory = factory ?? new EntityDataReaderFactory();
        }
        public void GetIsolationLevel_executes_operation_and_dispatches_to_interceptors()
        {
            var transactionMock = new Mock<DbTransaction>();
            var interceptorMock = new Mock<IDbTransactionInterceptor>();
            var dispatcher = new DbTransactionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(interceptorMock.Object);

            var interceptionContext = new DbInterceptionContext();
            dispatcher.GetIsolationLevel(transactionMock.Object, interceptionContext);

            transactionMock.Verify(m => m.IsolationLevel, Times.Once());
            interceptorMock.Verify(
                m => m.IsolationLevelGetting(transactionMock.Object, It.IsAny<DbTransactionInterceptionContext<IsolationLevel>>()),
                Times.Once());
            interceptorMock.Verify(
                m => m.IsolationLevelGot(transactionMock.Object, It.IsAny<DbTransactionInterceptionContext<IsolationLevel>>()), Times.Once());
        }
        public void Executing_dispatches_to_interceptors()
        {
            var interceptionContext = new DbInterceptionContext();
            var command             = new Mock <DbCommand>().Object;

            var mockInterceptor = new Mock <ICancelableDbCommandInterceptor>();

            mockInterceptor.Setup(m => m.CommandExecuting(command, interceptionContext)).Returns(true);

            var dispatcher         = new CancelableDbCommandDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;

            internalDispatcher.Add(mockInterceptor.Object);

            Assert.True(dispatcher.Executing(command, interceptionContext));

            mockInterceptor.Verify(m => m.CommandExecuting(command, interceptionContext));
        }
        public void Loaded_dispatches_to_interceptors()
        {
            var interceptionContext = new DbInterceptionContext();
            var eventArgs           = new DbConfigurationLoadedEventArgs(new Mock <InternalConfiguration>(null, null, null, null, null).Object);
            var mockInterceptor1    = new Mock <IDbConfigurationInterceptor>();
            var mockInterceptor2    = new Mock <IDbConfigurationInterceptor>();

            var dispatcher         = new DbConfigurationDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;

            internalDispatcher.Add(mockInterceptor1.Object);
            internalDispatcher.Add(mockInterceptor2.Object);

            dispatcher.Loaded(eventArgs, interceptionContext);

            mockInterceptor1.Verify(m => m.Loaded(eventArgs, It.IsAny <DbConfigurationInterceptionContext>()), Times.Once());
            mockInterceptor2.Verify(m => m.Loaded(eventArgs, It.IsAny <DbConfigurationInterceptionContext>()), Times.Once());
        }
Beispiel #31
0
        public void Opening_dispatches_to_interceptors()
        {
            var interceptionContext = new DbInterceptionContext();
            var connection          = new Mock <EntityConnection>().Object;

            var mockInterceptor = new Mock <ICancelableEntityConnectionInterceptor>();

            mockInterceptor.Setup(m => m.ConnectionOpening(connection, interceptionContext)).Returns(true);

            var dispatcher         = new CancelableEntityConnectionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;

            internalDispatcher.Add(mockInterceptor.Object);

            Assert.True(dispatcher.Opening(connection, interceptionContext));

            mockInterceptor.Verify(m => m.ConnectionOpening(connection, interceptionContext));
        }
Beispiel #32
0
        /// <summary>
        /// Sends <see cref="IDbConnectionInterceptor.Disposing" /> and
        /// <see cref="IDbConnectionInterceptor.Disposed" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="Component.Dispose()" />.
        /// </summary>
        /// <param name="connection">The connection on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        public virtual void Dispose(
            DbConnection connection, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(interceptionContext, "interceptionContext");

            InternalDispatcher.Dispatch(
                connection,
                (t, c) =>
            {
                // Will invoke the explicit IDisposable implementation if one exists
                using (t)
                {
                }
            },
                new DbConnectionInterceptionContext(interceptionContext),
                (i, t, c) => i.Disposing(t, c),
                (i, t, c) => i.Disposed(t, c));
        }
Beispiel #33
0
        /// <summary>
        /// Sends <see cref="IDbConnectionInterceptor.Opening" /> and
        /// <see cref="IDbConnectionInterceptor.Opened" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbConnection.Open" />.
        /// </summary>
        /// <param name="dbConnection">The connection 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.</param>
        /// <returns>A task that represents the asynchronous operation.</returns>
        public virtual Task OpenAsync(
            DbConnection dbConnection, DbInterceptionContext interceptionContext, CancellationToken cancellationToken)
        {
            Check.NotNull(dbConnection, "dbConnection");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbConnectionInterceptionContext(interceptionContext);

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

            return(InternalDispatcher.DispatchAsync(
                       () => dbConnection.OpenAsync(cancellationToken),
                       clonedInterceptionContext,
                       i => i.Opening(dbConnection, clonedInterceptionContext),
                       i => i.Opened(dbConnection, clonedInterceptionContext)));
        }
Beispiel #34
0
        /// <summary>
        /// Sends <see cref="IDbTransactionInterceptor.Disposing" /> and
        /// <see cref="IDbTransactionInterceptor.Disposed" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbTransaction.Dispose()" />.
        /// </summary>
        /// <param name="transaction">The transaction on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        public virtual void Dispose(DbTransaction transaction, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbTransactionInterceptionContext(interceptionContext);

            if (transaction.Connection != null)
            {
                clonedInterceptionContext = clonedInterceptionContext.WithConnection(transaction.Connection);
            }

            InternalDispatcher.Dispatch(
                transaction,
                (t, c) => t.Dispose(),
                clonedInterceptionContext,
                (i, t, c) => i.Disposing(t, c),
                (i, t, c) => i.Disposed(t, c));
        }
Beispiel #35
0
        public void GetConnection_executes_operation_and_dispatches_to_interceptors()
        {
            var transactionMock    = new Mock <DbTransaction>();
            var interceptorMock    = new Mock <IDbTransactionInterceptor>();
            var dispatcher         = new DbTransactionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;

            internalDispatcher.Add(interceptorMock.Object);

            var interceptionContext = new DbInterceptionContext();

            dispatcher.GetConnection(transactionMock.Object, interceptionContext);

            transactionMock.Protected().Verify <DbConnection>("DbConnection", Times.Once());
            interceptorMock.Verify(
                m => m.ConnectionGetting(transactionMock.Object, It.IsAny <DbTransactionInterceptionContext <DbConnection> >()), Times.Once());
            interceptorMock.Verify(
                m => m.ConnectionGot(transactionMock.Object, It.IsAny <DbTransactionInterceptionContext <DbConnection> >()), Times.Once());
        }
Beispiel #36
0
        public void Rollback_executes_operation_and_dispatches_to_interceptors()
        {
            var transactionMock = new Mock <DbTransaction>();

            transactionMock.Protected().Setup <DbConnection>("DbConnection").Returns(new Mock <DbConnection>().Object);
            var interceptorMock    = new Mock <IDbTransactionInterceptor>();
            var dispatcher         = new DbTransactionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;

            internalDispatcher.Add(interceptorMock.Object);

            var interceptionContext = new DbInterceptionContext();

            dispatcher.Rollback(transactionMock.Object, interceptionContext);

            transactionMock.Verify(m => m.Rollback(), Times.Once());
            interceptorMock.Verify(m => m.RollingBack(transactionMock.Object, It.IsAny <DbTransactionInterceptionContext>()), Times.Once());
            interceptorMock.Verify(m => m.RolledBack(transactionMock.Object, It.IsAny <DbTransactionInterceptionContext>()), Times.Once());
        }
Beispiel #37
0
        public void GetIsolationLevel_executes_operation_and_dispatches_to_interceptors()
        {
            var transactionMock    = new Mock <DbTransaction>();
            var interceptorMock    = new Mock <IDbTransactionInterceptor>();
            var dispatcher         = new DbTransactionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;

            internalDispatcher.Add(interceptorMock.Object);

            var interceptionContext = new DbInterceptionContext();

            dispatcher.GetIsolationLevel(transactionMock.Object, interceptionContext);

            transactionMock.Verify(m => m.IsolationLevel, Times.Once());
            interceptorMock.Verify(
                m => m.IsolationLevelGetting(transactionMock.Object, It.IsAny <DbTransactionInterceptionContext <IsolationLevel> >()),
                Times.Once());
            interceptorMock.Verify(
                m => m.IsolationLevelGot(transactionMock.Object, It.IsAny <DbTransactionInterceptionContext <IsolationLevel> >()), Times.Once());
        }
        public void Created_dispatches_to_interceptors_which_can_modify_result()
        {
            var interceptionContext = new DbInterceptionContext();
            var tree = new Mock<DbCommandTree>().Object;

            var mockInterceptor = new Mock<IDbCommandTreeInterceptor>();
            var interceptedTree = new Mock<DbCommandTree>().Object;
            mockInterceptor.Setup(m => m.TreeCreated(It.IsAny<DbCommandTreeInterceptionContext>()))
                .Callback<DbCommandTreeInterceptionContext>(
                    i =>
                        {
                            Assert.Same(tree, i.Result);
                            Assert.Same(tree, i.OriginalResult);
                            i.Result = interceptedTree;
                        });

            var dispatcher = new DbCommandTreeDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(mockInterceptor.Object);

            Assert.Same(interceptedTree, dispatcher.Created(tree, interceptionContext));

            mockInterceptor.Verify(m => m.TreeCreated(It.IsAny<DbCommandTreeInterceptionContext>()));
        }
Beispiel #39
0
 /// <summary>
 /// Creates a new <see cref="DbConnectionPropertyInterceptionContext{TValue}" /> by copying immutable state from the given
 /// interception context. Also see <see cref="Clone" />
 /// </summary>
 /// <param name="copyFrom">The context from which to copy state.</param>
 public DbConnectionPropertyInterceptionContext(DbInterceptionContext copyFrom)
     : base(copyFrom)
 {
     Check.NotNull(copyFrom, "copyFrom");
 }
 /// <summary>
 /// Creates a new <see cref="DbConfigurationInterceptionContext" /> by copying state from the given
 /// interception context. Also see <see cref="DbConfigurationInterceptionContext.Clone" />
 /// </summary>
 /// <param name="copyFrom">The context from which to copy state.</param>
 public DbConfigurationInterceptionContext(DbInterceptionContext copyFrom)
     : base(copyFrom)
 {
     Check.NotNull(copyFrom, "copyFrom");
 }
        public void ExecuteReaderAsync_should_dispatch_to_interceptor_and_optionally_execute()
        {
            var result = Task.FromResult(new Mock<DbDataReader>().Object);

            var mockCommand = new Mock<DbCommand>();
            mockCommand.Protected()
                .Setup<Task<DbDataReader>>(
                    "ExecuteDbDataReaderAsync", CommandBehavior.SingleRow, ItExpr.IsAny<CancellationToken>())
                .Returns(result);

            var mockCancelable = new Mock<ICancelableDbCommandInterceptor>();
            var mockPublicInterceptor = new Mock<DbCommandInterceptor> { CallBase = true };

            var dispatchers = new DbDispatchers();
            dispatchers.AddInterceptor(mockCancelable.Object);
            dispatchers.AddInterceptor(mockPublicInterceptor.Object);

            var interceptionContext = new DbInterceptionContext();
            var interceptableDbCommand = new InterceptableDbCommand(mockCommand.Object, interceptionContext, dispatchers);

            mockCancelable.Setup(m => m.CommandExecuting(mockCommand.Object, It.IsAny<DbInterceptionContext>())).Returns(false);

            Assert.NotSame(result, interceptableDbCommand.ExecuteReaderAsync(CommandBehavior.SingleRow));

            mockCommand.Protected().Verify(
                "ExecuteDbDataReaderAsync", Times.Never(), CommandBehavior.SingleRow, ItExpr.IsAny<CancellationToken>());

            mockCancelable.Verify(m => m.CommandExecuting(mockCommand.Object, interceptionContext), Times.Once());

            mockPublicInterceptor.Verify(
                m => m.ReaderExecuting(
                    It.IsAny<DbCommand>(),
                    It.IsAny<DbCommandInterceptionContext<DbDataReader>>()), Times.Never());

            mockPublicInterceptor.Verify(
                m => m.ReaderExecuted(
                    It.IsAny<DbCommand>(),
                    It.IsAny<DbCommandInterceptionContext<DbDataReader>>()), Times.Never());

            mockCancelable.Setup(m => m.CommandExecuting(mockCommand.Object, It.IsAny<DbInterceptionContext>())).Returns(true);

            var interceptResult = interceptableDbCommand.ExecuteReaderAsync(CommandBehavior.SingleRow);
            interceptResult.Wait();
            Assert.Same(result.Result, interceptResult.Result);

            mockCommand.Protected().Verify(
                "ExecuteDbDataReaderAsync", Times.Once(), CommandBehavior.SingleRow, ItExpr.IsAny<CancellationToken>());

            mockCancelable.Verify(m => m.CommandExecuting(mockCommand.Object, interceptionContext), Times.Exactly(2));

            mockPublicInterceptor.Verify(
                m => m.ReaderExecuting(
                    mockCommand.Object,
                    It.Is<DbCommandInterceptionContext<DbDataReader>>(c => c.IsAsync && c.CommandBehavior == CommandBehavior.SingleRow)),
                Times.Once());

            mockPublicInterceptor.Verify(
                m => m.ReaderExecuted(
                    mockCommand.Object,
                    It.Is<DbCommandInterceptionContext<DbDataReader>>(
                        c => c.IsAsync && c.CommandBehavior == CommandBehavior.SingleRow && c.Result == result.Result)), Times.Once());
        }
 public abstract bool ConnectionOpening(EntityConnection connection, DbInterceptionContext interceptionContext);
Beispiel #43
0
 /// <summary>
 /// Creates a new <see cref="MutableInterceptionContext{TResult}" /> by copying immutable state from the given
 /// interception context. Also see <see cref="DbInterceptionContext.Clone" />
 /// </summary>
 /// <param name="copyFrom">The context from which to copy state.</param>
 protected MutableInterceptionContext(DbInterceptionContext copyFrom)
     : base(copyFrom)
 {
     Check.NotNull(copyFrom, "copyFrom");
 }
        public void ExecuteScalarAsync_should_dispatch_to_interceptor_and_optionally_execute()
        {
            var mockCommand = new Mock<DbCommand>();
            var result = Task.FromResult<object>(11);
            mockCommand.Setup(m => m.ExecuteScalarAsync(It.IsAny<CancellationToken>())).Returns(result);

            var mockCancelable = new Mock<ICancelableDbCommandInterceptor>();
            var mockPublicInterceptor = new Mock<DbCommandInterceptor> { CallBase = true };

            var dispatchers = new DbDispatchers();
            dispatchers.AddInterceptor(mockCancelable.Object);
            dispatchers.AddInterceptor(mockPublicInterceptor.Object);

            var interceptionContext = new DbInterceptionContext();
            var interceptableDbCommand = new InterceptableDbCommand(mockCommand.Object, interceptionContext, dispatchers);

            mockCancelable.Setup(m => m.CommandExecuting(mockCommand.Object, It.IsAny<DbInterceptionContext>())).Returns(false);

            interceptableDbCommand.ExecuteScalarAsync();

            mockCommand.Verify(m => m.ExecuteScalarAsync(It.IsAny<CancellationToken>()), Times.Never());

            mockCancelable.Verify(m => m.CommandExecuting(mockCommand.Object, interceptionContext), Times.Once());

            mockPublicInterceptor.Verify(
                m => m.ScalarExecuting(It.IsAny<DbCommand>(), It.IsAny<DbCommandInterceptionContext<object>>()), Times.Never());

            mockPublicInterceptor.Verify(
                m => m.ScalarExecuted(It.IsAny<DbCommand>(), It.IsAny<DbCommandInterceptionContext<object>>()),
                Times.Never());

            mockCancelable.Setup(m => m.CommandExecuting(mockCommand.Object, It.IsAny<DbInterceptionContext>())).Returns(true);

            var interceptResult = interceptableDbCommand.ExecuteScalarAsync();
            interceptResult.Wait();
            Assert.Equal(11, interceptResult.Result);

            mockCommand.Verify(m => m.ExecuteScalarAsync(It.IsAny<CancellationToken>()), Times.Once());

            mockCancelable.Verify(m => m.CommandExecuting(mockCommand.Object, interceptionContext), Times.Exactly(2));

            mockPublicInterceptor.Verify(
                m => m.ScalarExecuting(mockCommand.Object, It.Is<DbCommandInterceptionContext<object>>(c => c.IsAsync)), Times.Once());

            mockPublicInterceptor.Verify(
                m => m.ScalarExecuted(
                    mockCommand.Object, It.Is<DbCommandInterceptionContext<object>>(c => c.IsAsync && (int)c.Result == 11)), Times.Once());
        }
 public abstract bool CommandExecuting(DbCommand command, DbInterceptionContext interceptionContext);
        public void GetState_executes_operation_and_dispatches_to_interceptors()
        {
            var mockConnection = new Mock<DbConnection>();
            var mockInterceptor = new Mock<IDbConnectionInterceptor>();
            var dispatcher = new DbConnectionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(mockInterceptor.Object);

            var interceptionContext = new DbInterceptionContext();
            dispatcher.GetState(mockConnection.Object, interceptionContext);

            mockConnection.Verify(m => m.State, Times.Once());
            mockInterceptor.Verify(m => m.StateGetting(mockConnection.Object, It.IsAny<DbConnectionInterceptionContext<ConnectionState>>()), Times.Once());
            mockInterceptor.Verify(m => m.StateGot(mockConnection.Object, It.IsAny<DbConnectionInterceptionContext<ConnectionState>>()), Times.Once());
        }
 /// <summary>
 /// Creates a new <see cref="DbCommandInterceptionContext{TResult}" /> by copying immutable state from the given
 /// interception context. Also see <see cref="Clone" />
 /// </summary>
 /// <param name="copyFrom">The context from which to copy state.</param>
 public DbCommandInterceptionContext(DbInterceptionContext copyFrom)
     : base(copyFrom)
 {
 }
 /// <summary>
 /// Creates a new <see cref="DbTransactionInterceptionContext{TResult}" /> by copying immutable state from the given
 /// interception context. Also see <see cref="Clone" />
 /// </summary>
 /// <param name="copyFrom">The context from which to copy state.</param>
 public DbTransactionInterceptionContext(DbInterceptionContext copyFrom)
     : base(copyFrom)
 {
     Check.NotNull(copyFrom, "copyFrom");
 }
        /// <summary>
        /// Sends <see cref="IDbConnectionInterceptor.Disposing" /> and
        /// <see cref="IDbConnectionInterceptor.Disposed" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="Component.Dispose()" />.
        /// </summary>
        /// <param name="connection">The connection on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        public virtual void Dispose(
            DbConnection connection, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(interceptionContext, "interceptionContext");

            InternalDispatcher.Dispatch(
                connection,
                (t, c) =>
                {
                    // Will invoke the explicit IDisposable implementation if one exists
                    using (t)
                    {
                    }
                },
                new DbConnectionInterceptionContext(interceptionContext),
                (i, t, c) => i.Disposing(t, c),
                (i, t, c) => i.Disposed(t, c));
        }
Beispiel #50
0
 /// <summary>
 /// Creates a new <see cref="T:System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext`1" /> by copying immutable state from the given
 /// interception context. Also see <see cref="M:System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext`1.Clone" />
 /// </summary>
 /// <param name="copyFrom">The context from which to copy state.</param>
 public DbConnectionInterceptionContext(DbInterceptionContext copyFrom)
     : base(copyFrom)
 {
     Check.NotNull <DbInterceptionContext>(copyFrom, nameof(copyFrom));
 }
Beispiel #51
0
 /// <summary>
 /// Creates a new <see cref="T:System.Data.Entity.Infrastructure.Interception.MutableInterceptionContext" /> by copying immutable state from the given
 /// interception context. Also see <see cref="M:System.Data.Entity.Infrastructure.Interception.DbInterceptionContext.Clone" />
 /// </summary>
 /// <param name="copyFrom">The context from which to copy state.</param>
 protected MutableInterceptionContext(DbInterceptionContext copyFrom)
     : base(copyFrom)
 {
     Check.NotNull <DbInterceptionContext>(copyFrom, nameof(copyFrom));
 }
Beispiel #52
0
 /// <summary>
 /// Creates a new <see cref="DbCommandTreeInterceptionContext" /> by copying state from the given
 /// interception context. Also see <see cref="DbCommandTreeInterceptionContext.Clone" />
 /// </summary>
 /// <param name="copyFrom">The context from which to copy state.</param>
 public DbCommandTreeInterceptionContext(DbInterceptionContext copyFrom)
     : base(copyFrom)
 {
     Check.NotNull(copyFrom, "copyFrom");
 }
Beispiel #53
0
 /// <summary>
 /// Sends <see cref="M:System.Data.Entity.Infrastructure.Interception.IDbTransactionInterceptor.Committing(System.Data.Common.DbTransaction,System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext)" /> and
 /// <see cref="M:System.Data.Entity.Infrastructure.Interception.IDbTransactionInterceptor.Committed(System.Data.Common.DbTransaction,System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext)" /> to any <see cref="T:System.Data.Entity.Infrastructure.Interception.IDbConnectionInterceptor" />
 /// registered on <see cref="T:System.Data.Entity.Infrastructure.Interception.DbInterception" /> before/after making a
 /// call to <see cref="M:System.Data.Common.DbTransaction.Commit" />.
 /// </summary>
 /// <param name="transaction">The transaction on which the operation will be executed.</param>
 /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
 public virtual void Commit(DbTransaction transaction, DbInterceptionContext interceptionContext)
 {
     Check.NotNull <DbTransaction>(transaction, nameof(transaction));
     Check.NotNull <DbInterceptionContext>(interceptionContext, nameof(interceptionContext));
     this.InternalDispatcher.Dispatch <DbTransaction, DbTransactionInterceptionContext>(transaction, (Action <DbTransaction, DbTransactionInterceptionContext>)((t, c) => t.Commit()), new DbTransactionInterceptionContext(interceptionContext).WithConnection(transaction.Connection), (Action <IDbTransactionInterceptor, DbTransaction, DbTransactionInterceptionContext>)((i, t, c) => i.Committing(t, c)), (Action <IDbTransactionInterceptor, DbTransaction, DbTransactionInterceptionContext>)((i, t, c) => i.Committed(t, c)));
 }
Beispiel #54
0
 /// <summary>
 /// Sends <see cref="M:System.Data.Entity.Infrastructure.Interception.IDbConnectionInterceptor.Closing(System.Data.Common.DbConnection,System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext)" /> and
 /// <see cref="M:System.Data.Entity.Infrastructure.Interception.IDbConnectionInterceptor.Closed(System.Data.Common.DbConnection,System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext)" /> to any <see cref="T:System.Data.Entity.Infrastructure.Interception.IDbConnectionInterceptor" />
 /// registered on <see cref="T:System.Data.Entity.Infrastructure.Interception.DbInterception" /> before/after making a
 /// call to <see cref="M:System.Data.Common.DbConnection.Close" />.
 /// </summary>
 /// <param name="connection">The connection on which the operation will be executed.</param>
 /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
 public virtual void Close(DbConnection connection, DbInterceptionContext interceptionContext)
 {
     Check.NotNull <DbConnection>(connection, nameof(connection));
     Check.NotNull <DbInterceptionContext>(interceptionContext, nameof(interceptionContext));
     this.InternalDispatcher.Dispatch <DbConnection, DbConnectionInterceptionContext>(connection, (Action <DbConnection, DbConnectionInterceptionContext>)((t, c) => t.Close()), new DbConnectionInterceptionContext(interceptionContext), (Action <IDbConnectionInterceptor, DbConnection, DbConnectionInterceptionContext>)((i, t, c) => i.Closing(t, c)), (Action <IDbConnectionInterceptor, DbConnection, DbConnectionInterceptionContext>)((i, t, c) => i.Closed(t, c)));
 }
Beispiel #55
0
 public virtual DbCommandTree Created(
     DbCommandTree commandTree,
     DbInterceptionContext interceptionContext)
 {
     return(this._internalDispatcher.Dispatch <DbCommandTreeInterceptionContext, DbCommandTree>(commandTree, new DbCommandTreeInterceptionContext(interceptionContext), (Action <IDbCommandTreeInterceptor, DbCommandTreeInterceptionContext>)((i, c) => i.TreeCreated(c))));
 }