Skip to content

Commit 4deba8d

Browse files
committed
CSHARP-1439: Improve backward compatibility regarding CreateCollectionOptions.
1 parent d0d81ba commit 4deba8d

File tree

5 files changed

+97
-61
lines changed

5 files changed

+97
-61
lines changed

src/MongoDB.Driver.Tests/MongoDatabaseImplTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void Settings_should_be_set()
7171
public async Task CreateCollectionAsync_should_execute_the_CreateCollectionOperation()
7272
{
7373
var storageEngine = new BsonDocument("awesome", true);
74-
var options = new CreateCollectionOptions
74+
var options = new CreateCollectionOptions<BsonDocument>
7575
{
7676
AutoIndexId = false,
7777
Capped = true,

src/MongoDB.Driver/CreateCollectionOptions.cs

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,18 @@ namespace MongoDB.Driver
2222
/// <summary>
2323
/// Options for creating a collection.
2424
/// </summary>
25-
public class CreateCollectionOptions<TDocument>
25+
public class CreateCollectionOptions
2626
{
2727
// fields
2828
private bool? _autoIndexId;
2929
private bool? _capped;
30-
private IBsonSerializer<TDocument> _documentSerializer;
3130
private long? _maxDocuments;
3231
private long? _maxSize;
3332
private BsonDocument _storageEngine;
3433
private bool? _usePowerOf2Sizes;
3534
private IBsonSerializerRegistry _serializerRegistry;
3635
private DocumentValidationAction? _validationAction;
3736
private DocumentValidationLevel? _validationLevel;
38-
private FilterDefinition<TDocument> _validator;
3937

4038
// properties
4139
/// <summary>
@@ -56,15 +54,6 @@ public bool? Capped
5654
set { _capped = value; }
5755
}
5856

59-
/// <summary>
60-
/// Gets or sets the document serializer.
61-
/// </summary>
62-
public IBsonSerializer<TDocument> DocumentSerializer
63-
{
64-
get { return _documentSerializer; }
65-
set { _documentSerializer = value; }
66-
}
67-
6857
/// <summary>
6958
/// Gets or sets the maximum number of documents (used with capped collections).
7059
/// </summary>
@@ -133,6 +122,60 @@ public DocumentValidationLevel? ValidationLevel
133122
get { return _validationLevel; }
134123
set { _validationLevel = value; }
135124
}
125+
}
126+
127+
/// <summary>
128+
/// Options for creating a collection.
129+
/// </summary>
130+
public class CreateCollectionOptions<TDocument> : CreateCollectionOptions
131+
{
132+
#region static
133+
// public static methods
134+
/// <summary>
135+
/// Coerces a generic CreateCollectionOptions{TDocument} from a non-generic CreateCollectionOptions.
136+
/// </summary>
137+
/// <param name="options">The options.</param>
138+
/// <returns>The generic options.</returns>
139+
public static CreateCollectionOptions<TDocument> CoercedFrom(CreateCollectionOptions options)
140+
{
141+
if (options == null)
142+
{
143+
return null;
144+
}
145+
146+
if (options.GetType() == typeof(CreateCollectionOptions))
147+
{
148+
return new CreateCollectionOptions<TDocument>
149+
{
150+
AutoIndexId = options.AutoIndexId,
151+
Capped = options.Capped,
152+
MaxDocuments = options.MaxDocuments,
153+
MaxSize = options.MaxSize,
154+
SerializerRegistry = options.SerializerRegistry,
155+
StorageEngine = options.StorageEngine,
156+
UsePowerOf2Sizes = options.UsePowerOf2Sizes,
157+
ValidationAction = options.ValidationAction,
158+
ValidationLevel = options.ValidationLevel
159+
};
160+
}
161+
162+
return (CreateCollectionOptions<TDocument>)options;
163+
}
164+
#endregion
165+
166+
// private fields
167+
private IBsonSerializer<TDocument> _documentSerializer;
168+
private FilterDefinition<TDocument> _validator;
169+
170+
// public properties
171+
/// <summary>
172+
/// Gets or sets the document serializer.
173+
/// </summary>
174+
public IBsonSerializer<TDocument> DocumentSerializer
175+
{
176+
get { return _documentSerializer; }
177+
set { _documentSerializer = value; }
178+
}
136179

137180
/// <summary>
138181
/// Gets or sets the validator.
@@ -146,11 +189,4 @@ public FilterDefinition<TDocument> Validator
146189
set { _validator = value; }
147190
}
148191
}
149-
150-
/// <summary>
151-
/// Options for creating a collection.
152-
/// </summary>
153-
public class CreateCollectionOptions : CreateCollectionOptions<BsonDocument>
154-
{
155-
}
156192
}

src/MongoDB.Driver/IMongoDatabase.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ public interface IMongoDatabase
5454
/// <returns>A task.</returns>
5555
Task CreateCollectionAsync(string name, CreateCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
5656

57-
/// <summary>
58-
/// Creates the collection with the specified name.
59-
/// </summary>
60-
/// <param name="name">The name.</param>
61-
/// <param name="options">The options.</param>
62-
/// <param name="cancellationToken">The cancellation token.</param>
63-
/// <returns>A task.</returns>
64-
Task CreateCollectionAsync<TDocument>(string name, CreateCollectionOptions<TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken));
65-
6657
/// <summary>
6758
/// Drops the collection with the specified name.
6859
/// </summary>

src/MongoDB.Driver/MongoDatabaseBase.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ public abstract class MongoDatabaseBase : IMongoDatabase
3737
/// <inheritdoc />
3838
public abstract Task CreateCollectionAsync(string name, CreateCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
3939

40-
/// <inheritdoc />
41-
public virtual Task CreateCollectionAsync<TDocument>(string name, CreateCollectionOptions<TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
42-
{
43-
throw new NotImplementedException();
44-
}
45-
4640
/// <inheritdoc />
4741
public abstract Task DropCollectionAsync(string name, CancellationToken cancellationToken = default(CancellationToken));
4842

src/MongoDB.Driver/MongoDatabaseImpl.cs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Linq;
19+
using System.Reflection;
1920
using System.Threading;
2021
using System.Threading.Tasks;
2122
using MongoDB.Bson;
@@ -67,37 +68,21 @@ public override MongoDatabaseSettings Settings
6768
// methods
6869
public override Task CreateCollectionAsync(string name, CreateCollectionOptions options, CancellationToken cancellationToken)
6970
{
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)
8172
{
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);
8574
}
8675

87-
var operation = new CreateCollectionOperation(new CollectionNamespace(_databaseNamespace, name), messageEncoderSettings)
76+
if (options.GetType() == typeof(CreateCollectionOptions))
8877
{
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+
}
9981

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 });
10186
}
10287

10388
public override Task DropCollectionAsync(string name, CancellationToken cancellationToken)
@@ -159,6 +144,36 @@ public override Task RenameCollectionAsync(string oldName, string newName, Renam
159144
return ExecuteReadOperationAsync(operation, readPreference, cancellationToken);
160145
}
161146

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+
162177
private Task<T> ExecuteReadOperationAsync<T>(IReadOperation<T> operation, CancellationToken cancellationToken)
163178
{
164179
return ExecuteReadOperationAsync(operation, _settings.ReadPreference, cancellationToken);

0 commit comments

Comments
 (0)