|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Threading; |
| 18 | +using System.Threading.Tasks; |
| 19 | +using MongoDB.Driver.Core.Operations; |
| 20 | + |
| 21 | +namespace MongoDB.Driver |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Wraps provided IOperationExecutor to preserve it from disposal. |
| 25 | + /// Used by MongoClient when IOperationExecutor is injected from outside. |
| 26 | + /// </summary> |
| 27 | + internal sealed class OperationExecutorWrapper : IOperationExecutor |
| 28 | + { |
| 29 | + private readonly IOperationExecutor _wrappedExecutor; |
| 30 | + private bool _isDisposed; |
| 31 | + |
| 32 | + public OperationExecutorWrapper(IOperationExecutor executor) |
| 33 | + { |
| 34 | + _wrappedExecutor = executor; |
| 35 | + } |
| 36 | + |
| 37 | + public void Dispose() |
| 38 | + { |
| 39 | + _isDisposed = true; |
| 40 | + } |
| 41 | + |
| 42 | + public TResult ExecuteReadOperation<TResult>( |
| 43 | + IReadOperation<TResult> operation, |
| 44 | + ReadOperationOptions options, |
| 45 | + IClientSessionHandle session = null, |
| 46 | + CancellationToken cancellationToken = default) |
| 47 | + { |
| 48 | + ThrowIfDisposed(); |
| 49 | + return _wrappedExecutor.ExecuteReadOperation(operation, options, session, cancellationToken); |
| 50 | + } |
| 51 | + |
| 52 | + public Task<TResult> ExecuteReadOperationAsync<TResult>( |
| 53 | + IReadOperation<TResult> operation, |
| 54 | + ReadOperationOptions options, |
| 55 | + IClientSessionHandle session = null, |
| 56 | + CancellationToken cancellationToken = default) |
| 57 | + { |
| 58 | + ThrowIfDisposed(); |
| 59 | + return _wrappedExecutor.ExecuteReadOperationAsync(operation, options, session, cancellationToken); |
| 60 | + } |
| 61 | + |
| 62 | + public TResult ExecuteWriteOperation<TResult>( |
| 63 | + IWriteOperation<TResult> operation, |
| 64 | + WriteOperationOptions options, |
| 65 | + IClientSessionHandle session = null, |
| 66 | + CancellationToken cancellationToken = default) |
| 67 | + { |
| 68 | + ThrowIfDisposed(); |
| 69 | + return _wrappedExecutor.ExecuteWriteOperation(operation, options, session, cancellationToken); |
| 70 | + } |
| 71 | + |
| 72 | + public Task<TResult> ExecuteWriteOperationAsync<TResult>( |
| 73 | + IWriteOperation<TResult> operation, |
| 74 | + WriteOperationOptions options, |
| 75 | + IClientSessionHandle session = null, |
| 76 | + CancellationToken cancellationToken = default) |
| 77 | + { |
| 78 | + ThrowIfDisposed(); |
| 79 | + return _wrappedExecutor.ExecuteWriteOperationAsync(operation, options, session, cancellationToken); |
| 80 | + } |
| 81 | + |
| 82 | + public IClientSessionHandle StartImplicitSession(CancellationToken cancellationToken) |
| 83 | + { |
| 84 | + ThrowIfDisposed(); |
| 85 | + return _wrappedExecutor.StartImplicitSession(cancellationToken); |
| 86 | + } |
| 87 | + |
| 88 | + public Task<IClientSessionHandle> StartImplicitSessionAsync(CancellationToken cancellationToken) |
| 89 | + { |
| 90 | + ThrowIfDisposed(); |
| 91 | + return _wrappedExecutor.StartImplicitSessionAsync(cancellationToken); |
| 92 | + } |
| 93 | + |
| 94 | + private void ThrowIfDisposed() |
| 95 | + { |
| 96 | + if (_isDisposed) |
| 97 | + { |
| 98 | + throw new ObjectDisposedException(nameof(OperationExecutorWrapper)); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments