Skip to content

Commit e3af660

Browse files
committed
CSHARP-1191: Code review changes to GridFSBucketOptions.
1 parent 922bfbc commit e3af660

File tree

4 files changed

+32
-100
lines changed

4 files changed

+32
-100
lines changed

src/MongoDB.Driver.GridFS.Tests/GridFSBucketOptionsTests.cs

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void ChunkSizeBytes_set_should_throw_when_value_is_invalid(
102102
public void constructor_with_immutable_other_should_initialize_instance()
103103
{
104104
var mutable = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority };
105-
var other = mutable.ToImmutable();
105+
var other = new ImmutableGridFSBucketOptions(mutable);
106106

107107
var result = new GridFSBucketOptions(other);
108108

@@ -156,19 +156,6 @@ public void ReadPreference_set_should_have_expected_result()
156156
subject.ReadPreference.Should().Be(ReadPreference.Secondary);
157157
}
158158

159-
[Test]
160-
public void ToImmutable_should_return_expected_result()
161-
{
162-
var subject = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority };
163-
164-
var result = subject.ToImmutable();
165-
166-
result.BucketName.Should().Be(subject.BucketName);
167-
result.ChunkSizeBytes.Should().Be(subject.ChunkSizeBytes);
168-
result.ReadPreference.Should().Be(subject.ReadPreference);
169-
result.WriteConcern.Should().Be(subject.WriteConcern);
170-
}
171-
172159
[Test]
173160
public void WriteConcern_get_should_return_expected_result()
174161
{
@@ -196,7 +183,7 @@ public class ImmutableGridFSBucketOptionsTests
196183
[Test]
197184
public void BucketName_get_should_return_expected_result()
198185
{
199-
var subject = new GridFSBucketOptions { BucketName = "bucket" }.ToImmutable();
186+
var subject = new ImmutableGridFSBucketOptions(new GridFSBucketOptions { BucketName = "bucket" });
200187

201188
var result = subject.BucketName;
202189

@@ -206,7 +193,7 @@ public void BucketName_get_should_return_expected_result()
206193
[Test]
207194
public void ChunkSizeBytes_get_should_return_expected_result()
208195
{
209-
var subject = new GridFSBucketOptions { ChunkSizeBytes = 123 }.ToImmutable();
196+
var subject = new ImmutableGridFSBucketOptions(new GridFSBucketOptions { ChunkSizeBytes = 123 });
210197

211198
var result = subject.ChunkSizeBytes;
212199

@@ -216,7 +203,9 @@ public void ChunkSizeBytes_get_should_return_expected_result()
216203
[Test]
217204
public void constructor_with_arguments_should_initialize_instance()
218205
{
219-
var result = new ImmutableGridFSBucketOptions("bucket", 123, ReadPreference.Secondary, WriteConcern.WMajority);
206+
var mutable = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority };
207+
208+
var result = new ImmutableGridFSBucketOptions(mutable);
220209

221210
result.BucketName.Should().Be("bucket");
222211
result.ChunkSizeBytes.Should().Be(123);
@@ -255,46 +244,20 @@ public void Defaults_get_should_return_expected_result()
255244
result.WriteConcern.Should().BeNull();
256245
}
257246

258-
[Test]
259-
public void implicit_conversion_from_mutable_should_return_expected_result()
260-
{
261-
var mutable = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority };
262-
263-
var result = (ImmutableGridFSBucketOptions)mutable;
264-
265-
result.BucketName.Should().Be(mutable.BucketName);
266-
result.ChunkSizeBytes.Should().Be(mutable.ChunkSizeBytes);
267-
result.ReadPreference.Should().Be(mutable.ReadPreference);
268-
result.WriteConcern.Should().Be(mutable.WriteConcern);
269-
}
270-
271247
[Test]
272248
public void ReadPreference_get_should_return_expected_result()
273249
{
274-
var subject = new GridFSBucketOptions { ReadPreference = ReadPreference.Secondary }.ToImmutable();
250+
var subject = new ImmutableGridFSBucketOptions(new GridFSBucketOptions { ReadPreference = ReadPreference.Secondary });
275251

276252
var result = subject.ReadPreference;
277253

278254
result.Should().Be(ReadPreference.Secondary);
279255
}
280256

281-
[Test]
282-
public void ToMutable_should_return_expected_result()
283-
{
284-
var subject = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority }.ToImmutable();
285-
286-
var result = subject.ToMutable();
287-
288-
result.BucketName.Should().Be(subject.BucketName);
289-
result.ChunkSizeBytes.Should().Be(subject.ChunkSizeBytes);
290-
result.ReadPreference.Should().Be(subject.ReadPreference);
291-
result.WriteConcern.Should().Be(subject.WriteConcern);
292-
}
293-
294257
[Test]
295258
public void WriteConcern_get_should_return_expected_result()
296259
{
297-
var subject = new GridFSBucketOptions { WriteConcern = WriteConcern.WMajority }.ToImmutable();
260+
var subject = new ImmutableGridFSBucketOptions(new GridFSBucketOptions { WriteConcern = WriteConcern.WMajority });
298261

299262
var result = subject.WriteConcern;
300263

src/MongoDB.Driver.GridFS.Tests/GridFSBucketTests.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,21 @@ public class GridFSBucketTests
3434
public void constructor_should_initialize_instance()
3535
{
3636
var database = Substitute.For<IMongoDatabase>();
37-
var options = new ImmutableGridFSBucketOptions();
37+
var options = new GridFSBucketOptions();
3838

3939
var result = new GridFSBucket(database, options);
4040

4141
result.Database.Should().BeSameAs(database);
42-
result.Options.Should().BeSameAs(options);
42+
result.Options.BucketName.Should().Be(options.BucketName);
43+
result.Options.ChunkSizeBytes.Should().Be(options.ChunkSizeBytes);
44+
result.Options.ReadPreference.Should().Be(options.ReadPreference);
45+
result.Options.WriteConcern.Should().Be(options.WriteConcern);
4346
}
4447

4548
[Test]
4649
public void constructor_should_throw_when_database_is_null()
4750
{
48-
var options = new ImmutableGridFSBucketOptions();
51+
var options = new GridFSBucketOptions();
4952

5053
Action action = () => new GridFSBucket(null, options);
5154

@@ -214,12 +217,15 @@ public void OpenUploadStreamAsync_should_throw_when_filename_is_null()
214217
public void Options_get_should_return_the_expected_result()
215218
{
216219
var database = Substitute.For<IMongoDatabase>();
217-
var options = new ImmutableGridFSBucketOptions();
220+
var options = new GridFSBucketOptions();
218221
var subject = new GridFSBucket(database, options);
219222

220223
var result = subject.Options;
221224

222-
result.Should().BeSameAs(options);
225+
result.BucketName.Should().Be(options.BucketName);
226+
result.ChunkSizeBytes.Should().Be(options.ChunkSizeBytes);
227+
result.ReadPreference.Should().Be(options.ReadPreference);
228+
result.WriteConcern.Should().Be(options.WriteConcern);
223229
}
224230

225231
[Test]
@@ -306,7 +312,7 @@ public void UploadFromStreamAsync_id_should_throw_when_source_is_null()
306312
}
307313

308314
// private methods
309-
private GridFSBucket CreateSubject(ImmutableGridFSBucketOptions options = null)
315+
private GridFSBucket CreateSubject(GridFSBucketOptions options = null)
310316
{
311317
var cluster = Substitute.For<ICluster>();
312318

src/MongoDB.Driver.GridFS/GridFSBucket.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public class GridFSBucket : IGridFSBucket
4343

4444
// constructors
4545
/// <inheritdoc />
46-
public GridFSBucket(IMongoDatabase database, ImmutableGridFSBucketOptions options = null)
46+
public GridFSBucket(IMongoDatabase database, GridFSBucketOptions options = null)
4747
{
4848
_database = Ensure.IsNotNull(database, nameof(database));
49-
_options = options ?? ImmutableGridFSBucketOptions.Defaults;
49+
_options = options == null ? ImmutableGridFSBucketOptions.Defaults : new ImmutableGridFSBucketOptions(options);
5050

5151
_cluster = database.Client.Cluster;
5252
_ensuredIndexes = 0;

src/MongoDB.Driver.GridFS/GridFSBucketOptions.cs

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public GridFSBucketOptions()
4343
/// <param name="other">The other <see cref="GridFSBucketOptions"/> from which to copy the values.</param>
4444
public GridFSBucketOptions(GridFSBucketOptions other)
4545
{
46+
Ensure.IsNotNull(other, nameof(other));
4647
_bucketName = other.BucketName;
4748
_chunkSizeBytes = other.ChunkSizeBytes;
4849
_readPreference = other.ReadPreference;
@@ -55,6 +56,7 @@ public GridFSBucketOptions(GridFSBucketOptions other)
5556
/// <param name="other">The other <see cref="ImmutableGridFSBucketOptions"/> from which to copy the values.</param>
5657
public GridFSBucketOptions(ImmutableGridFSBucketOptions other)
5758
{
59+
Ensure.IsNotNull(other, nameof(other));
5860
_bucketName = other.BucketName;
5961
_chunkSizeBytes = other.ChunkSizeBytes;
6062
_readPreference = other.ReadPreference;
@@ -117,16 +119,6 @@ public WriteConcern WriteConcern
117119
get { return _writeConcern; }
118120
set { _writeConcern = value; }
119121
}
120-
121-
// public methods
122-
/// <summary>
123-
/// Returns an immutable GridFSBucketOptions.
124-
/// </summary>
125-
/// <returns>An immutable GridFSBucketOptions.</returns>
126-
public ImmutableGridFSBucketOptions ToImmutable()
127-
{
128-
return new ImmutableGridFSBucketOptions(_bucketName, _chunkSizeBytes, _readPreference, _writeConcern);
129-
}
130122
}
131123

132124
/// <summary>
@@ -149,19 +141,6 @@ public static ImmutableGridFSBucketOptions Defaults
149141
{
150142
get { return __defaults; }
151143
}
152-
153-
// static methods
154-
/// <summary>
155-
/// Performs an implicit conversion from <see cref="GridFSBucketOptions"/> to <see cref="ImmutableGridFSBucketOptions"/>.
156-
/// </summary>
157-
/// <param name="mutable">The mutable options.</param>
158-
/// <returns>
159-
/// The result of the conversion.
160-
/// </returns>
161-
public static implicit operator ImmutableGridFSBucketOptions(GridFSBucketOptions mutable)
162-
{
163-
return mutable.ToImmutable();
164-
}
165144
#endregion
166145

167146
// fields
@@ -181,22 +160,16 @@ public ImmutableGridFSBucketOptions()
181160
}
182161

183162
/// <summary>
184-
/// Initializes a new instance of the <see cref="ImmutableGridFSBucketOptions"/> class.
163+
/// Initializes a new instance of the <see cref="ImmutableGridFSBucketOptions" /> class.
185164
/// </summary>
186-
/// <param name="bucketName">The bucket name.</param>
187-
/// <param name="chunkSizeBytes">The chunk size bytes.</param>
188-
/// <param name="readPreference">The read preference.</param>
189-
/// <param name="writeConcern">The write concern.</param>
190-
public ImmutableGridFSBucketOptions(
191-
string bucketName,
192-
int chunkSizeBytes,
193-
ReadPreference readPreference,
194-
WriteConcern writeConcern)
165+
/// <param name="other">The other <see cref="GridFSBucketOptions"/> from which to copy the values.</param>
166+
public ImmutableGridFSBucketOptions(GridFSBucketOptions other)
195167
{
196-
_bucketName = Ensure.IsNotNullOrEmpty(bucketName, nameof(bucketName));
197-
_chunkSizeBytes = Ensure.IsGreaterThanOrEqualToZero(chunkSizeBytes, nameof(chunkSizeBytes));
198-
_readPreference = readPreference;
199-
_writeConcern = writeConcern;
168+
Ensure.IsNotNull(other, nameof(other));
169+
_bucketName = other.BucketName;
170+
_chunkSizeBytes = other.ChunkSizeBytes;
171+
_readPreference = other.ReadPreference;
172+
_writeConcern = other.WriteConcern;
200173
}
201174

202175
// properties
@@ -243,15 +216,5 @@ public WriteConcern WriteConcern
243216
{
244217
get { return _writeConcern; }
245218
}
246-
247-
// public methods
248-
/// <summary>
249-
/// Converts this immutable instance of GridFSBucketOptions to a mutable instance.
250-
/// </summary>
251-
/// <returns>A mutable instance of the GridFSBucketOptions.</returns>
252-
public GridFSBucketOptions ToMutable()
253-
{
254-
return new GridFSBucketOptions(this);
255-
}
256219
}
257220
}

0 commit comments

Comments
 (0)