Skip to content

Commit e5d696e

Browse files
committed
Refactored ReadPreference to not using Optional<T> and move Optional<T> to MongoDB.Driver namespace.
1 parent 3a97459 commit e5d696e

File tree

7 files changed

+27
-35
lines changed

7 files changed

+27
-35
lines changed

src/MongoDB.Driver.Core.Tests/ReadPreferenceTests.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ namespace MongoDB.Driver
2626
[TestFixture]
2727
public class ReadPreferenceTests
2828
{
29-
private static readonly ReadPreference __defaults = new ReadPreference();
30-
3129
[Test]
3230
public void constructor_should_throw_when_mode_is_primary_and_tagSets_is_not_empty()
3331
{
@@ -39,11 +37,12 @@ public void constructor_should_throw_when_mode_is_primary_and_tagSets_is_not_emp
3937
}
4038

4139
[Test]
42-
public void constructor_should_throw_when_tagSets_is_null()
40+
public void constructor_should_initialize_instance_when_tagSets_is_null()
4341
{
44-
Action action = () => new ReadPreference(ReadPreferenceMode.Primary, null);
42+
var result = new ReadPreference(ReadPreferenceMode.Secondary, null);
4543

46-
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("tagSets");
44+
result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.Secondary);
45+
result.TagSets.Should().BeEmpty();
4746
}
4847

4948
[Test]
@@ -54,22 +53,13 @@ public void constructor_with_mode_should_initialize_instance()
5453
var result = new ReadPreference(mode: mode);
5554

5655
result.ReadPreferenceMode.Should().Be(mode);
57-
result.TagSets.Should().Equal(__defaults.TagSets);
58-
}
59-
60-
[Test]
61-
public void constructor_with_no_arguments_should_initialize_instance()
62-
{
63-
var result = new ReadPreference();
64-
65-
result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.Primary);
66-
result.TagSets.Count.Should().Be(0);
56+
result.TagSets.Should().BeEmpty();
6757
}
6858

6959
[Test]
7060
public void constructor_with_tagSets_should_initialize_instance()
7161
{
72-
var mode = ReadPreferenceMode.Secondary; // can't use tagSets with default mode Primary
62+
var mode = ReadPreferenceMode.Secondary; // can't use tagSets with mode Primary
7363
var tagSets = new[] { new TagSet(new[] { new Tag("name", "value") }) };
7464

7565
var result = new ReadPreference(mode: mode, tagSets: tagSets);

src/MongoDB.Driver.Core.Tests/Specifications/server-selection/ServerSelectionTestRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private ReadPreference BuildReadPreference(BsonDocument readPreferenceDescriptio
9999
throw new NotSupportedException("Unknown read preference mode: " + readPreferenceDescription["mode"]);
100100
}
101101

102-
return readPreference.With(tagSets: Optional.Enumerable(tagSets));
102+
return readPreference.With(tagSets: tagSets);
103103
}
104104

105105
private ClusterDescription BuildClusterDescription(BsonDocument topologyDescription)

src/MongoDB.Driver.Core/MongoDB.Driver.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
<Compile Include="Core\Events\ConnectionBeforeClosingEvent.cs" />
124124
<Compile Include="Core\Events\ConnectionFailedEvent.cs" />
125125
<Compile Include="Core\Misc\ExceptionMapper.cs" />
126-
<Compile Include="Core\Misc\Optional.cs" />
126+
<Compile Include="Optional.cs" />
127127
<Compile Include="Core\Misc\ReferenceCounted.cs" />
128128
<Compile Include="Core\Bindings\WritableServerBinding.cs" />
129129
<Compile Include="Core\Clusters\ServerSelectors\WritableServerSelector.cs" />

src/MongoDB.Driver.Core/Core/Misc/Optional.cs renamed to src/MongoDB.Driver.Core/Optional.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using System.Text;
2020
using System.Threading.Tasks;
2121

22-
namespace MongoDB.Driver.Core.Misc
22+
namespace MongoDB.Driver
2323
{
2424
/// <summary>
2525
/// Represents helper methods for use with the <see cref="Optional{T}"/> struct.

src/MongoDB.Driver.Core/ReadPreference.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ public static ReadPreference SecondaryPreferred
102102
/// <param name="mode">The read preference mode.</param>
103103
/// <param name="tagSets">The tag sets.</param>
104104
public ReadPreference(
105-
Optional<ReadPreferenceMode> mode = default(Optional<ReadPreferenceMode>),
106-
Optional<IEnumerable<TagSet>> tagSets = default(Optional<IEnumerable<TagSet>>))
105+
ReadPreferenceMode mode,
106+
IEnumerable<TagSet> tagSets = null)
107107
{
108-
_mode = mode.WithDefault(ReadPreferenceMode.Primary);
109-
_tagSets = Ensure.IsNotNull(tagSets.WithDefault(Enumerable.Empty<TagSet>()), "tagSets").ToList();
108+
_mode = mode;
109+
_tagSets = (tagSets ?? Enumerable.Empty<TagSet>()).ToList();
110110

111111
if (_mode == ReadPreferenceMode.Primary && _tagSets.Count() > 0)
112112
{
@@ -176,15 +176,20 @@ public override string ToString()
176176
/// Returns a new instance of ReadPreference with some values changed.
177177
/// </summary>
178178
/// <param name="mode">The read preference mode.</param>
179+
/// <returns>A new instance of ReadPreference.</returns>
180+
public ReadPreference With(ReadPreferenceMode mode)
181+
{
182+
return new ReadPreference(mode, _tagSets);
183+
}
184+
185+
/// <summary>
186+
/// Returns a new instance of ReadPreference with some values changed.
187+
/// </summary>
179188
/// <param name="tagSets">The tag sets.</param>
180189
/// <returns>A new instance of ReadPreference.</returns>
181-
public ReadPreference With(
182-
Optional<ReadPreferenceMode> mode = default(Optional<ReadPreferenceMode>),
183-
Optional<IEnumerable<TagSet>> tagSets = default(Optional<IEnumerable<TagSet>>))
190+
public ReadPreference With(IEnumerable<TagSet> tagSets)
184191
{
185-
return new ReadPreference(
186-
mode.WithDefault(_mode),
187-
Optional.Enumerable(tagSets.WithDefault(_tagSets)));
192+
return new ReadPreference(_mode, tagSets);
188193
}
189194
}
190195
}

src/MongoDB.Driver.Core/WriteConcern.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18-
using System.Linq;
19-
using System.Text;
20-
using System.Threading.Tasks;
2118
using MongoDB.Bson;
2219
using MongoDB.Driver.Core.Misc;
2320
using MongoDB.Shared;
@@ -125,7 +122,7 @@ public WriteConcern(
125122
Optional<TimeSpan?> wTimeout = default(Optional<TimeSpan?>),
126123
Optional<bool?> fsync = default(Optional<bool?>),
127124
Optional<bool?> journal = default(Optional<bool?>))
128-
: this((WValue)Ensure.IsGreaterThanOrEqualToZero(w, "w"), wTimeout, fsync, journal)
125+
: this(new WCount(Ensure.IsGreaterThanOrEqualToZero(w, "w")), wTimeout, fsync, journal)
129126
{
130127
}
131128

@@ -141,7 +138,7 @@ public WriteConcern(
141138
Optional<TimeSpan?> wTimeout = default(Optional<TimeSpan?>),
142139
Optional<bool?> fsync = default(Optional<bool?>),
143140
Optional<bool?> journal = default(Optional<bool?>))
144-
: this((WValue)Ensure.IsNotNullOrEmpty(mode, "mode"), wTimeout, fsync, journal)
141+
: this(new WMode(Ensure.IsNotNullOrEmpty(mode, "mode")), wTimeout, fsync, journal)
145142
{
146143
}
147144

src/MongoDB.Driver/MongoUrlBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ public void Parse(string url)
561561
{
562562
throw new MongoConfigurationException("ReadPreferenceMode is required when using tag sets.");
563563
}
564-
_readPreference = _readPreference.With(tagSets: Optional.Enumerable(connectionString.ReadPreferenceTags));
564+
_readPreference = _readPreference.With(tagSets: connectionString.ReadPreferenceTags);
565565
}
566566

567567
_replicaSetName = connectionString.ReplicaSet;

0 commit comments

Comments
 (0)