13
13
// limitations under the License.
14
14
15
15
using System . Collections . Generic ;
16
+ using MongoDB . Driver . Core . Misc ;
16
17
17
18
namespace MongoDB . Driver . Search
18
19
{
19
20
/// <summary>
20
21
/// Fluent interface for compound search definitions.
21
22
/// </summary>
22
23
/// <typeparam name="TDocument">The type of the document.</typeparam>
23
- public abstract class CompoundFluent < TDocument >
24
+ public sealed class CompoundSearchDefinitionFluent < TDocument >
24
25
{
26
+ private List < SearchDefinition < TDocument > > _must ;
27
+ private List < SearchDefinition < TDocument > > _mustNot ;
28
+ private List < SearchDefinition < TDocument > > _should ;
29
+ private List < SearchDefinition < TDocument > > _filter ;
30
+ private int _minimumShouldMatch = 0 ;
31
+
25
32
/// <summary>
26
33
/// Adds clauses which must match to produce results.
27
34
/// </summary>
28
35
/// <param name="clauses">The clauses.</param>
29
36
/// <returns>The compound fluent interface.</returns>
30
- public abstract CompoundFluent < TDocument > Must ( IEnumerable < SearchDefinition < TDocument > > clauses ) ;
37
+ public CompoundSearchDefinitionFluent < TDocument > Must ( IEnumerable < SearchDefinition < TDocument > > clauses ) =>
38
+ AddClauses ( ref _must , clauses ) ;
31
39
32
40
/// <summary>
33
41
/// Adds clauses which must match to produce results.
34
42
/// </summary>
35
43
/// <param name="clauses">The clauses.</param>
36
44
/// <returns>The compound fluent interface.</returns>
37
- public CompoundFluent < TDocument > Must ( params SearchDefinition < TDocument > [ ] clauses ) =>
45
+ public CompoundSearchDefinitionFluent < TDocument > Must ( params SearchDefinition < TDocument > [ ] clauses ) =>
38
46
Must ( ( IEnumerable < SearchDefinition < TDocument > > ) clauses ) ;
39
47
40
48
/// <summary>
@@ -43,15 +51,16 @@ public CompoundFluent<TDocument> Must(params SearchDefinition<TDocument>[] claus
43
51
/// </summary>
44
52
/// <param name="clauses">The clauses.</param>
45
53
/// <returns>The compound fluent interface.</returns>
46
- public abstract CompoundFluent < TDocument > MustNot ( IEnumerable < SearchDefinition < TDocument > > clauses ) ;
54
+ public CompoundSearchDefinitionFluent < TDocument > MustNot ( IEnumerable < SearchDefinition < TDocument > > clauses ) =>
55
+ AddClauses ( ref _mustNot , clauses ) ;
47
56
48
57
/// <summary>
49
58
/// Adds clauses which must not match for a document to be included in the
50
59
/// results.
51
60
/// </summary>
52
61
/// <param name="clauses">The clauses.</param>
53
62
/// <returns>The compound fluent interface.</returns>
54
- public CompoundFluent < TDocument > MustNot ( params SearchDefinition < TDocument > [ ] clauses ) =>
63
+ public CompoundSearchDefinitionFluent < TDocument > MustNot ( params SearchDefinition < TDocument > [ ] clauses ) =>
55
64
MustNot ( ( IEnumerable < SearchDefinition < TDocument > > ) clauses ) ;
56
65
57
66
/// <summary>
@@ -60,15 +69,16 @@ public CompoundFluent<TDocument> MustNot(params SearchDefinition<TDocument>[] cl
60
69
/// </summary>
61
70
/// <param name="clauses">The clauses.</param>
62
71
/// <returns>The compound fluent interface.</returns>
63
- public abstract CompoundFluent < TDocument > Should ( IEnumerable < SearchDefinition < TDocument > > clauses ) ;
72
+ public CompoundSearchDefinitionFluent < TDocument > Should ( IEnumerable < SearchDefinition < TDocument > > clauses ) =>
73
+ AddClauses ( ref _should , clauses ) ;
64
74
65
75
/// <summary>
66
76
/// Adds clauses which cause documents in the result set to be scored higher if
67
77
/// they match.
68
78
/// </summary>
69
79
/// <param name="clauses">The clauses.</param>
70
80
/// <returns>The compound fluent interface.</returns>
71
- public CompoundFluent < TDocument > Should ( params SearchDefinition < TDocument > [ ] clauses ) =>
81
+ public CompoundSearchDefinitionFluent < TDocument > Should ( params SearchDefinition < TDocument > [ ] clauses ) =>
72
82
Should ( ( IEnumerable < SearchDefinition < TDocument > > ) clauses ) ;
73
83
74
84
/// <summary>
@@ -77,15 +87,16 @@ public CompoundFluent<TDocument> Should(params SearchDefinition<TDocument>[] cla
77
87
/// </summary>
78
88
/// <param name="clauses">The clauses.</param>
79
89
/// <returns>The compound fluent interface.</returns>
80
- public abstract CompoundFluent < TDocument > Filter ( IEnumerable < SearchDefinition < TDocument > > clauses ) ;
90
+ public CompoundSearchDefinitionFluent < TDocument > Filter ( IEnumerable < SearchDefinition < TDocument > > clauses ) =>
91
+ AddClauses ( ref _filter , clauses ) ;
81
92
82
93
/// <summary>
83
94
/// Adds clauses which must all match for a document to be included in the
84
95
/// results.
85
96
/// </summary>
86
97
/// <param name="clauses">The clauses.</param>
87
98
/// <returns>The compound fluent interface.</returns>
88
- public CompoundFluent < TDocument > Filter ( params SearchDefinition < TDocument > [ ] clauses ) =>
99
+ public CompoundSearchDefinitionFluent < TDocument > Filter ( params SearchDefinition < TDocument > [ ] clauses ) =>
89
100
Filter ( ( IEnumerable < SearchDefinition < TDocument > > ) clauses ) ;
90
101
91
102
/// <summary>
@@ -94,21 +105,34 @@ public CompoundFluent<TDocument> Filter(params SearchDefinition<TDocument>[] cla
94
105
/// </summary>
95
106
/// <param name="minimumShouldMatch">The value to set.</param>
96
107
/// <returns>The compound fluent interface.</returns>
97
- public abstract CompoundFluent < TDocument > MinimumShouldMatch ( int minimumShouldMatch ) ;
108
+ public CompoundSearchDefinitionFluent < TDocument > MinimumShouldMatch ( int minimumShouldMatch )
109
+ {
110
+ _minimumShouldMatch = minimumShouldMatch ;
111
+ return this ;
112
+ }
98
113
99
114
/// <summary>
100
115
/// Constructs a search definition from the fluent interface.
101
116
/// </summary>
102
117
/// <returns>A compound search definition.</returns>
103
- public abstract SearchDefinition < TDocument > ToSearchDefinition ( ) ;
118
+ public SearchDefinition < TDocument > ToSearchDefinition ( ) =>
119
+ new CompoundSearchDefinition < TDocument > ( _must , _mustNot , _should , _filter , _minimumShouldMatch ) ;
104
120
105
121
/// <summary>
106
- /// Performs an implicit conversion from a <see cref="CompoundFluent {TDocument}"/>
122
+ /// Performs an implicit conversion from a <see cref="CompoundSearchDefinitionFluent {TDocument}"/>
107
123
/// to a <see cref="SearchDefinition{TDocument}"/>.
108
124
/// </summary>
109
125
/// <param name="compound">The compound fluent interface.</param>
110
126
/// <returns>The result of the conversion.</returns>
111
- public static implicit operator SearchDefinition < TDocument > ( CompoundFluent < TDocument > compound ) =>
127
+ public static implicit operator SearchDefinition < TDocument > ( CompoundSearchDefinitionFluent < TDocument > compound ) =>
112
128
compound . ToSearchDefinition ( ) ;
129
+
130
+ private CompoundSearchDefinitionFluent < TDocument > AddClauses ( ref List < SearchDefinition < TDocument > > clauses , IEnumerable < SearchDefinition < TDocument > > newClauses )
131
+ {
132
+ clauses ??= new ( ) ;
133
+ clauses . AddRange ( Ensure . IsNotNull ( newClauses , nameof ( newClauses ) ) ) ;
134
+
135
+ return this ;
136
+ }
113
137
}
114
138
}
0 commit comments