|
16 | 16 | using System;
|
17 | 17 | using System.Collections.Generic;
|
18 | 18 | using System.Linq;
|
| 19 | +using System.Reflection; |
19 | 20 | using System.Threading;
|
20 | 21 | using System.Threading.Tasks;
|
21 | 22 | using MongoDB.Bson;
|
@@ -67,37 +68,21 @@ public override MongoDatabaseSettings Settings
|
67 | 68 | // methods
|
68 | 69 | public override Task CreateCollectionAsync(string name, CreateCollectionOptions options, CancellationToken cancellationToken)
|
69 | 70 | {
|
70 |
| - return CreateCollectionAsync<BsonDocument>(name, options, cancellationToken); |
71 |
| - } |
72 |
| - |
73 |
| - public override Task CreateCollectionAsync<TDocument>(string name, CreateCollectionOptions<TDocument> options, CancellationToken cancellationToken) |
74 |
| - { |
75 |
| - Ensure.IsNotNullOrEmpty(name, nameof(name)); |
76 |
| - options = options ?? new CreateCollectionOptions<TDocument>(); |
77 |
| - |
78 |
| - var messageEncoderSettings = GetMessageEncoderSettings(); |
79 |
| - BsonDocument validator = null; |
80 |
| - if (options.Validator != null) |
| 71 | + if (options == null) |
81 | 72 | {
|
82 |
| - var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry; |
83 |
| - var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer<TDocument>(); |
84 |
| - validator = options.Validator.Render(documentSerializer, serializerRegistry); |
| 73 | + return CreateCollectionHelperAsync<BsonDocument>(name, null, cancellationToken); |
85 | 74 | }
|
86 | 75 |
|
87 |
| - var operation = new CreateCollectionOperation(new CollectionNamespace(_databaseNamespace, name), messageEncoderSettings) |
| 76 | + if (options.GetType() == typeof(CreateCollectionOptions)) |
88 | 77 | {
|
89 |
| - AutoIndexId = options.AutoIndexId, |
90 |
| - Capped = options.Capped, |
91 |
| - MaxDocuments = options.MaxDocuments, |
92 |
| - MaxSize = options.MaxSize, |
93 |
| - StorageEngine = options.StorageEngine, |
94 |
| - UsePowerOf2Sizes = options.UsePowerOf2Sizes, |
95 |
| - ValidationAction = options.ValidationAction, |
96 |
| - ValidationLevel = options.ValidationLevel, |
97 |
| - Validator = validator |
98 |
| - }; |
| 78 | + var genericOptions = CreateCollectionOptions<BsonDocument>.CoercedFrom(options); |
| 79 | + return CreateCollectionHelperAsync<BsonDocument>(name, genericOptions, cancellationToken); |
| 80 | + } |
99 | 81 |
|
100 |
| - return ExecuteWriteOperationAsync(operation, cancellationToken); |
| 82 | + var genericMethodDefinition = typeof(MongoDatabaseImpl).GetMethod("CreateCollectionHelperAsync", BindingFlags.NonPublic | BindingFlags.Instance); |
| 83 | + var documentType = options.GetType().GetGenericArguments()[0]; |
| 84 | + var methodInfo = genericMethodDefinition.MakeGenericMethod(documentType); |
| 85 | + return (Task)methodInfo.Invoke(this, new object[] { name, options, cancellationToken }); |
101 | 86 | }
|
102 | 87 |
|
103 | 88 | public override Task DropCollectionAsync(string name, CancellationToken cancellationToken)
|
@@ -159,6 +144,36 @@ public override Task RenameCollectionAsync(string oldName, string newName, Renam
|
159 | 144 | return ExecuteReadOperationAsync(operation, readPreference, cancellationToken);
|
160 | 145 | }
|
161 | 146 |
|
| 147 | + private Task CreateCollectionHelperAsync<TDocument>(string name, CreateCollectionOptions<TDocument> options, CancellationToken cancellationToken) |
| 148 | + { |
| 149 | + Ensure.IsNotNullOrEmpty(name, nameof(name)); |
| 150 | + options = options ?? new CreateCollectionOptions<TDocument>(); |
| 151 | + |
| 152 | + var messageEncoderSettings = GetMessageEncoderSettings(); |
| 153 | + BsonDocument validator = null; |
| 154 | + if (options.Validator != null) |
| 155 | + { |
| 156 | + var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry; |
| 157 | + var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer<TDocument>(); |
| 158 | + validator = options.Validator.Render(documentSerializer, serializerRegistry); |
| 159 | + } |
| 160 | + |
| 161 | + var operation = new CreateCollectionOperation(new CollectionNamespace(_databaseNamespace, name), messageEncoderSettings) |
| 162 | + { |
| 163 | + AutoIndexId = options.AutoIndexId, |
| 164 | + Capped = options.Capped, |
| 165 | + MaxDocuments = options.MaxDocuments, |
| 166 | + MaxSize = options.MaxSize, |
| 167 | + StorageEngine = options.StorageEngine, |
| 168 | + UsePowerOf2Sizes = options.UsePowerOf2Sizes, |
| 169 | + ValidationAction = options.ValidationAction, |
| 170 | + ValidationLevel = options.ValidationLevel, |
| 171 | + Validator = validator |
| 172 | + }; |
| 173 | + |
| 174 | + return ExecuteWriteOperationAsync(operation, cancellationToken); |
| 175 | + } |
| 176 | + |
162 | 177 | private Task<T> ExecuteReadOperationAsync<T>(IReadOperation<T> operation, CancellationToken cancellationToken)
|
163 | 178 | {
|
164 | 179 | return ExecuteReadOperationAsync(operation, _settings.ReadPreference, cancellationToken);
|
|
0 commit comments