public void New_interception_context_has_no_state() { var interceptionContext = new DbInterceptionContext(); Assert.Empty(interceptionContext.ObjectContexts); Assert.Empty(interceptionContext.DbContexts); }
/// <summary> /// Creates a new <see cref="DbCommandBaseInterceptionContext" /> by copying state from the given /// interception context. Also see <see cref="DbInterceptionContext.Clone" /> /// </summary> /// <param name="copyFrom">The context from which to copy state.</param> public DbCommandBaseInterceptionContext(DbInterceptionContext copyFrom) : base(copyFrom) { var asThisType = copyFrom as DbCommandBaseInterceptionContext; if (asThisType != null) { _commandBehavior = asThisType._commandBehavior; } }
public DbCommandTree TreeCreated(DbCommandTree commandTree, DbInterceptionContext interceptionContext) { if (interceptionContext.DbContexts.Contains(_context, ReferenceEquals)) { _commandTrees.Add(commandTree); } return commandTree; }
/// <summary> /// Creates a new <see cref="DbInterceptionContext" /> that contains all the contextual information in this /// interception context with the addition of the given <see cref="ObjectContext" />. /// </summary> /// <param name="context">The context to associate.</param> /// <returns>A new interception context associated with the given context.</returns> public DbInterceptionContext WithObjectContext(ObjectContext context) { Check.NotNull(context, "context"); var copy = new DbInterceptionContext(this); if (!copy._objectContexts.Contains(context, new ObjectReferenceEqualityComparer())) { copy._objectContexts.Add(context); } return copy; }
public bool CommandExecuting(DbCommand command, DbInterceptionContext interceptionContext) { if (interceptionContext.DbContexts.Contains(_context, ReferenceEquals)) { _commands.Add(command); return false; // cancel execution } return true; }
public InterceptableDbCommand(DbCommand command, DbInterceptionContext context, Dispatchers dispatchers = null) { DebugCheck.NotNull(command); DebugCheck.NotNull(context); _command = command; _interceptionContext = context; _dispatchers = dispatchers ?? Interception.Dispatch; }
internal static EntityCommandDefinition CreateCommandDefinition( DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver = null) { DebugCheck.NotNull(storeProviderFactory); DebugCheck.NotNull(interceptionContext); DebugCheck.NotNull(commandTree); return new EntityCommandDefinition(storeProviderFactory, commandTree, interceptionContext, resolver); }
internal override DbCommandDefinition CreateDbCommandDefinition( DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext) { DebugCheck.NotNull(providerManifest); DebugCheck.NotNull(commandTree); DebugCheck.NotNull(interceptionContext); var storeMetadata = (StoreItemCollection)commandTree.MetadataWorkspace.GetItemCollection(DataSpace.SSpace); return CreateCommandDefinition(storeMetadata.StoreProviderFactory, commandTree, interceptionContext); }
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 Dispatchers(); 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 Opening_dispatches_to_interceptors() { var interceptionContext = new DbInterceptionContext(); var connection = new Mock<EntityConnection>().Object; var mockInterceptor = new Mock<IEntityConnectionInterceptor>(); mockInterceptor.Setup(m => m.ConnectionOpening(connection, interceptionContext)).Returns(true); var dispatcher = new EntityConnectionDispatcher(); 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 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 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(tree, interceptionContext)).Returns(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(tree, interceptionContext)); }
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 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 Opening_dispatches_to_interceptors() { var interceptionContext = new DbInterceptionContext(); var connection = new Mock <EntityConnection>().Object; var mockInterceptor = new Mock <IEntityConnectionInterceptor>(); mockInterceptor.Setup(m => m.ConnectionOpening(connection, interceptionContext)).Returns(true); var dispatcher = new EntityConnectionDispatcher(); var internalDispatcher = dispatcher.InternalDispatcher; internalDispatcher.Add(mockInterceptor.Object); Assert.True(dispatcher.Opening(connection, interceptionContext)); mockInterceptor.Verify(m => m.ConnectionOpening(connection, interceptionContext)); }
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<DbInterceptor> { CallBase = true }; var dispatchers = new Dispatchers(); 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, interceptionContext)).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<DbInterceptionContext>()), Times.Never()); mockPublicInterceptor.Verify( m => m.ScalarExecuted(It.IsAny<DbCommand>(), It.IsAny<int>(), It.IsAny<DbInterceptionContext>()), Times.Never()); mockCancelable.Setup(m => m.CommandExecuting(mockCommand.Object, interceptionContext)).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, interceptionContext), Times.Once()); mockPublicInterceptor.Verify( m => m.ScalarExecuted(mockCommand.Object, 11, interceptionContext), 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(tree, interceptionContext)).Returns(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(tree, interceptionContext)); }
public void Dispatch_method_executes_command_and_dispatches_to_interceptors_which_can_change_result() { var interceptionContext = new DbInterceptionContext(); var mockCommand = new Mock<DbCommand>(); mockCommand.Setup(m => m.ExecuteScalar()).Returns(11); var mockInterceptor = new Mock<IDbCommandInterceptor>(); mockInterceptor.Setup(m => m.ScalarExecuted(mockCommand.Object, 11, interceptionContext)).Returns(13); var dispatcher = new DbCommandDispatcher(); var internalDispatcher = dispatcher.InternalDispatcher; internalDispatcher.Add(mockInterceptor.Object); Assert.Equal(13, dispatcher.Scalar(mockCommand.Object, interceptionContext)); mockCommand.Verify(m => m.ExecuteScalar()); mockInterceptor.Verify(m => m.ScalarExecuting(mockCommand.Object, interceptionContext)); mockInterceptor.Verify(m => m.ScalarExecuted(mockCommand.Object, 11, interceptionContext)); }
public void Dispatch_method_executes_command_and_dispatches_to_interceptors_which_can_change_result() { var interceptionContext = new DbInterceptionContext(); var mockCommand = new Mock <DbCommand>(); mockCommand.Setup(m => m.ExecuteScalar()).Returns(11); var mockInterceptor = new Mock <IDbCommandInterceptor>(); mockInterceptor.Setup(m => m.ScalarExecuted(mockCommand.Object, 11, interceptionContext)).Returns(13); var dispatcher = new DbCommandDispatcher(); var internalDispatcher = dispatcher.InternalDispatcher; internalDispatcher.Add(mockInterceptor.Object); Assert.Equal(13, dispatcher.Scalar(mockCommand.Object, interceptionContext)); mockCommand.Verify(m => m.ExecuteScalar()); mockInterceptor.Verify(m => m.ScalarExecuting(mockCommand.Object, interceptionContext)); mockInterceptor.Verify(m => m.ScalarExecuted(mockCommand.Object, 11, interceptionContext)); }
/// <summary> /// Checks whether the supplied interception context contains the target context /// or the supplied connection is the same as the one used by the target context. /// </summary> /// <param name="connection">A connection.</param> /// <param name="interceptionContext">An interception context.</param> /// <returns> /// <c>true</c> if the supplied interception context contains the target context or /// the supplied connection is the same as the one used by the target context if /// the supplied interception context doesn't contain any contexts; <c>false</c> otherwise. /// </returns> /// <remarks> /// Note that calling this method will trigger initialization of any DbContext referenced from the <paramref name="interceptionContext"/> /// </remarks> protected virtual bool MatchesParentContext(DbConnection connection, DbInterceptionContext interceptionContext) { if (DbContext != null && interceptionContext.DbContexts.Contains(DbContext, ReferenceEquals)) { return(true); } if (ObjectContext != null && interceptionContext.ObjectContexts.Contains(ObjectContext, ReferenceEquals)) { return(true); } if (Connection != null && !interceptionContext.ObjectContexts.Any() && !interceptionContext.DbContexts.Any()) { return(connection == Connection); } return(false); }
public void Dispatch_method_executes_command_and_dispatches_to_interceptors_which_can_change_result() { var interceptionContext = new DbInterceptionContext(); var cancellationToken = new CancellationToken(); var result = new Task <DbDataReader>(() => new Mock <DbDataReader>().Object); var mockCommand = new Mock <DbCommand>(); mockCommand.Protected() .Setup <Task <DbDataReader> >("ExecuteDbDataReaderAsync", CommandBehavior.SequentialAccess, cancellationToken) .Returns(result); var mockInterceptor = new Mock <IDbCommandInterceptor>(); var interceptResult = new Task <DbDataReader>(() => new Mock <DbDataReader>().Object); mockInterceptor.Setup( m => m.AsyncReaderExecuted(mockCommand.Object, CommandBehavior.SequentialAccess, result, interceptionContext)) .Returns(interceptResult); var dispatcher = new DbCommandDispatcher(); var internalDispatcher = dispatcher.InternalDispatcher; internalDispatcher.Add(mockInterceptor.Object); Assert.Same( interceptResult, dispatcher.AsyncReader(mockCommand.Object, CommandBehavior.SequentialAccess, cancellationToken, interceptionContext)); mockCommand.Protected() .Verify("ExecuteDbDataReaderAsync", Times.Once(), CommandBehavior.SequentialAccess, cancellationToken); mockInterceptor.Verify( m => m.AsyncReaderExecuting(mockCommand.Object, CommandBehavior.SequentialAccess, interceptionContext)); mockInterceptor.Verify( m => m.AsyncReaderExecuted(mockCommand.Object, CommandBehavior.SequentialAccess, result, interceptionContext)); }
private InterceptableDbCommand ConfigureCommand(DbCommand command, string commandText) { command.CommandText = commandText; if (_configuration.CommandTimeout.HasValue) { command.CommandTimeout = _configuration.CommandTimeout.Value; } var interceptionContext = new DbInterceptionContext(); if (_contextForInterception != null) { interceptionContext = interceptionContext.WithDbContext(_contextForInterception); } return new InterceptableDbCommand(command, interceptionContext); }
/// <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) { }
internal EntityCommandDefinition( DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver = null, BridgeDataReaderFactory bridgeDataReaderFactory = null, ColumnMapFactory columnMapFactory = null) { DebugCheck.NotNull(storeProviderFactory); DebugCheck.NotNull(commandTree); DebugCheck.NotNull(interceptionContext); _bridgeDataReaderFactory = bridgeDataReaderFactory ?? new BridgeDataReaderFactory(); _columnMapFactory = columnMapFactory ?? new ColumnMapFactory(); _storeProviderServices = (resolver != null ? resolver.GetService<DbProviderServices>(storeProviderFactory.GetProviderInvariantName()) : null) ?? storeProviderFactory.GetProviderServices(); try { if (DbCommandTreeKind.Query == commandTree.CommandTreeKind) { // Next compile the plan for the command tree var mappedCommandList = new List<ProviderCommandInfo>(); ColumnMap columnMap; int columnCount; PlanCompiler.Compile(commandTree, out mappedCommandList, out columnMap, out columnCount, out _entitySets); _columnMapGenerators = new IColumnMapGenerator[] { new ConstantColumnMapGenerator(columnMap, columnCount) }; // Note: we presume that the first item in the ProviderCommandInfo is the root node; Debug.Assert(mappedCommandList.Count > 0, "empty providerCommandInfo collection and no exception?"); // this shouldn't ever happen. // Then, generate the store commands from the resulting command tree(s) _mappedCommandDefinitions = new List<DbCommandDefinition>(mappedCommandList.Count); foreach (var providerCommandInfo in mappedCommandList) { var providerCommandDefinition = _storeProviderServices.CreateCommandDefinition( providerCommandInfo.CommandTree, interceptionContext); if (null == providerCommandDefinition) { throw new ProviderIncompatibleException(Strings.ProviderReturnedNullForCreateCommandDefinition); } _mappedCommandDefinitions.Add(providerCommandDefinition); } } else { Debug.Assert( DbCommandTreeKind.Function == commandTree.CommandTreeKind, "only query and function command trees are supported"); var entityCommandTree = (DbFunctionCommandTree)commandTree; // Retrieve mapping and metadata information for the function import. var mapping = GetTargetFunctionMapping(entityCommandTree); IList<FunctionParameter> returnParameters = entityCommandTree.EdmFunction.ReturnParameters; var resultSetCount = returnParameters.Count > 1 ? returnParameters.Count : 1; _columnMapGenerators = new IColumnMapGenerator[resultSetCount]; var storeResultType = DetermineStoreResultType(mapping, 0, out _columnMapGenerators[0]); for (var i = 1; i < resultSetCount; i++) { DetermineStoreResultType(mapping, i, out _columnMapGenerators[i]); } // Copy over parameters (this happens through a more indirect route in the plan compiler, but // it happens nonetheless) var providerParameters = new List<KeyValuePair<string, TypeUsage>>(); foreach (var parameter in entityCommandTree.Parameters) { providerParameters.Add(parameter); } // Construct store command tree usage. var providerCommandTree = new DbFunctionCommandTree( entityCommandTree.MetadataWorkspace, DataSpace.SSpace, mapping.TargetFunction, storeResultType, providerParameters); var storeCommandDefinition = _storeProviderServices.CreateCommandDefinition(providerCommandTree); _mappedCommandDefinitions = new List<DbCommandDefinition>(1) { storeCommandDefinition }; var firstResultEntitySet = mapping.FunctionImport.EntitySets.FirstOrDefault(); if (firstResultEntitySet != null) { _entitySets = new Set<EntitySet>(); _entitySets.Add(mapping.FunctionImport.EntitySets.FirstOrDefault()); _entitySets.MakeReadOnly(); } } // Finally, build a list of the parameters that the resulting command should have; var parameterList = new List<EntityParameter>(); foreach (var queryParameter in commandTree.Parameters) { var parameter = CreateEntityParameterFromQueryParameter(queryParameter); parameterList.Add(parameter); } _parameters = new ReadOnlyCollection<EntityParameter>(parameterList); } catch (EntityCommandCompilationException) { // No need to re-wrap EntityCommandCompilationException throw; } catch (Exception e) { // we should not be wrapping all exceptions if (e.IsCatchableExceptionType()) { // we don't wan't folks to have to know all the various types of exceptions that can // occur, so we just rethrow a CommandDefinitionException and make whatever we caught // the inner exception of it. throw new EntityCommandCompilationException(Strings.EntityClient_CommandDefinitionPreparationFailed, e); } throw; } }
public override void ScalarExecuting(DbCommand command, DbInterceptionContext interceptionContext) { Commands.Add(command); Assert.Empty(interceptionContext.DbContexts); Assert.Empty(interceptionContext.ObjectContexts); }
/// <inheritdoc /> public virtual Task <DbDataReader> AsyncReaderExecuted( DbCommand command, CommandBehavior behavior, Task <DbDataReader> result, DbInterceptionContext interceptionContext) { return(result); }
public abstract bool ConnectionOpening(EntityConnection connection, DbInterceptionContext interceptionContext);
/// <summary> /// Creates a new <see cref="DbCommandInterceptionContext" /> by copying state from the given /// interception context. Also see <see cref="DbCommandInterceptionContext{TResult}.Clone" /> /// </summary> /// <param name="copyFrom">The context from which to copy state.</param> protected DbCommandInterceptionContext(DbInterceptionContext copyFrom) : base(copyFrom) { }
/// <inheritdoc /> public virtual Task <object> AsyncScalarExecuted(DbCommand command, Task <object> result, DbInterceptionContext interceptionContext) { return(result); }
private DbInterceptionContext(DbInterceptionContext copyFrom) { _dbContexts = copyFrom.DbContexts.Where(c => !c.InternalContext.IsDisposed).ToList(); _objectContexts = copyFrom.ObjectContexts.Where(c => !c.IsDisposed).ToList(); }
/// <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) { _dbContexts = copyFrom.DbContexts.Where(c => !c.InternalContext.IsDisposed).ToList(); _objectContexts = copyFrom.ObjectContexts.Where(c => !c.IsDisposed).ToList(); _isAsync = copyFrom._isAsync; }
public void Multiple_contexts_can_be_combined_together() { var objectContext1 = new ObjectContext(); var context1 = CreateDbContext(objectContext1); var objectContext2 = new ObjectContext(); var context2 = CreateDbContext(objectContext2); var interceptionContext1 = new DbInterceptionContext() .WithDbContext(context1) .WithDbContext(context2) .WithObjectContext(objectContext1); var interceptionContext2 = interceptionContext1 .WithDbContext(context2) .WithObjectContext(objectContext1) .WithObjectContext(objectContext2); var combined = DbInterceptionContext.Combine(new[] { interceptionContext1, interceptionContext2 }); Assert.Equal(2, combined.DbContexts.Count()); Assert.Equal(2, combined.ObjectContexts.Count()); Assert.Contains(context1, combined.DbContexts); Assert.Contains(context2, combined.DbContexts); Assert.Contains(objectContext1, combined.ObjectContexts); Assert.Contains(objectContext2, combined.ObjectContexts); }
public abstract bool CommandExecuting(DbCommand command, DbInterceptionContext interceptionContext);
public abstract int NonQueryExecuted(DbCommand command, int result, DbInterceptionContext interceptionContext);
public abstract DbDataReader ReaderExecuted( DbCommand command, CommandBehavior behavior, DbDataReader result, DbInterceptionContext interceptionContext);
public abstract object ScalarExecuted(DbCommand command, object result, DbInterceptionContext interceptionContext);
public void Combine_throws_for_null_arg() { Assert.Equal( "interceptionContexts", Assert.Throws <ArgumentNullException>(() => DbInterceptionContext.Combine(null)).ParamName); }
/// <inheritdoc /> public virtual void AsyncScalarExecuting(DbCommand command, DbInterceptionContext interceptionContext) { }
public bool ConnectionOpening(EntityConnection connection, DbInterceptionContext interceptionContext) { return !interceptionContext.DbContexts.Contains(_context, ReferenceEquals); }
internal virtual DbCommand CreateCommand(DbCommandTree commandTree, DbInterceptionContext interceptionContext) { DebugCheck.NotNull(commandTree); DebugCheck.NotNull(interceptionContext); var commandDefinition = CreateCommandDefinition(commandTree, interceptionContext); var command = commandDefinition.CreateCommand(); return command; }
/// <inheritdoc /> public virtual void AsyncReaderExecuting(DbCommand command, CommandBehavior behavior, DbInterceptionContext 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 override void ReaderExecuting(DbCommand command, CommandBehavior behavior, DbInterceptionContext interceptionContext) { Commands.Add(command); Assert.Empty(interceptionContext.DbContexts); Assert.Empty(interceptionContext.ObjectContexts); }
/// <inheritdoc /> public virtual Task <int> AsyncNonQueryExecuted(DbCommand command, Task <int> result, DbInterceptionContext interceptionContext) { return(result); }
public abstract Task <int> AsyncNonQueryExecuted(DbCommand command, Task <int> result, DbInterceptionContext interceptionContext);
internal DbCommandDefinition CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext) { DebugCheck.NotNull(commandTree); DebugCheck.NotNull(interceptionContext); ValidateDataSpace(commandTree); var storeMetadata = (StoreItemCollection)commandTree.MetadataWorkspace.GetItemCollection(DataSpace.SSpace); Debug.Assert( storeMetadata.StoreProviderManifest != null, "StoreItemCollection has null StoreProviderManifest?"); commandTree = _treeDispatcher.Created(commandTree, interceptionContext); return CreateDbCommandDefinition(storeMetadata.StoreProviderManifest, commandTree); }
public abstract void AsyncReaderExecuting( DbCommand command, CommandBehavior behavior, DbInterceptionContext interceptionContext);
/// <summary> /// Checks whether the supplied interception context contains the target context /// or the supplied connection is the same as the one used by the target context. /// </summary> /// <param name="connection">A connection.</param> /// <param name="interceptionContext">An interception context.</param> /// <returns> /// <c>true</c> if the supplied interception context contains the target context or /// the supplied connection is the same as the one used by the target context if /// the supplied interception context doesn't contain any contexts; <c>false</c> otherwise. /// </returns> /// <remarks> /// Note that calling this method will trigger initialization of any DbContext referenced from the <paramref name="interceptionContext"/> /// </remarks> protected internal virtual bool MatchesParentContext(DbConnection connection, DbInterceptionContext interceptionContext) { Check.NotNull(connection, "connection"); Check.NotNull(interceptionContext, "interceptionContext"); if (DbContext != null && interceptionContext.DbContexts.Contains(DbContext, ReferenceEquals)) { return(true); } if (ObjectContext != null && interceptionContext.ObjectContexts.Contains(ObjectContext, ReferenceEquals)) { return(true); } if (Connection != null && !interceptionContext.ObjectContexts.Any() && !interceptionContext.DbContexts.Any()) { return(ReferenceEquals(connection, Connection)); } return(false); }
public abstract Task <DbDataReader> AsyncReaderExecuted( DbCommand command, CommandBehavior behavior, Task <DbDataReader> result, DbInterceptionContext interceptionContext);
/// <inheritdoc /> public virtual void AsyncNonQueryExecuting(DbCommand command, DbInterceptionContext interceptionContext) { }
public abstract void AsyncScalarExecuting(DbCommand command, DbInterceptionContext interceptionContext);
public abstract Task <object> AsyncScalarExecuted( DbCommand command, Task <object> result, DbInterceptionContext interceptionContext);
public abstract DbCommandTree TreeCreated(DbCommandTree commandTree, DbInterceptionContext interceptionContext);
public abstract void AsyncNonQueryExecuting(DbCommand command, DbInterceptionContext interceptionContext);