Skip to content

Commit ed8de1e

Browse files
committed
added the rest of the typed builders.
1 parent b818efa commit ed8de1e

12 files changed

+1874
-34
lines changed

Driver/Builders/FieldsBuilder.cs

Lines changed: 187 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Linq;
19-
using System.Text;
20-
19+
using System.Linq.Expressions;
2120
using MongoDB.Bson;
2221
using MongoDB.Bson.IO;
2322
using MongoDB.Bson.Serialization;
24-
using MongoDB.Driver;
23+
using MongoDB.Driver.Linq.Utils;
2524

2625
namespace MongoDB.Driver.Builders
2726
{
@@ -177,4 +176,189 @@ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBson
177176
((IBsonSerializable)_document).Serialize(bsonWriter, nominalType, options);
178177
}
179178
}
179+
180+
/// <summary>
181+
/// A builder for specifying which fields of a document the server should return.
182+
/// </summary>
183+
/// <typeparam name="TDocument">The type of the document.</typeparam>
184+
public static class Fields<TDocument>
185+
{
186+
// public static properties
187+
/// <summary>
188+
/// Gets a null value with a type of IMongoFields.
189+
/// </summary>
190+
public static IMongoFields Null
191+
{
192+
get { return null; }
193+
}
194+
195+
// public static methods
196+
/// <summary>
197+
/// Adds one or more field names to be excluded from the results.
198+
/// </summary>
199+
/// <param name="memberExpressions">One or more field names.</param>
200+
/// <returns>
201+
/// The builder (so method calls can be chained).
202+
/// </returns>
203+
public static FieldsBuilder<TDocument> Exclude(params Expression<Func<TDocument, object>>[] memberExpressions)
204+
{
205+
return new FieldsBuilder<TDocument>().Exclude(memberExpressions);
206+
}
207+
208+
/// <summary>
209+
/// Adds one or more field names to be included in the results.
210+
/// </summary>
211+
/// <param name="memberExpressions">The member expressions.</param>
212+
/// <returns>
213+
/// The builder (so method calls can be chained).
214+
/// </returns>
215+
public static FieldsBuilder<TDocument> Include(params Expression<Func<TDocument, object>>[] memberExpressions)
216+
{
217+
return new FieldsBuilder<TDocument>().Include(memberExpressions);
218+
}
219+
220+
/// <summary>
221+
/// Adds a slice to be included in the results.
222+
/// </summary>
223+
/// <typeparam name="TMember">The type of the member.</typeparam>
224+
/// <param name="memberExpression">The member expression.</param>
225+
/// <param name="size">The size of the slice (negative sizes are taken from the end).</param>
226+
/// <returns>
227+
/// The builder (so method calls can be chained).
228+
/// </returns>
229+
public static FieldsBuilder<TDocument> Slice<TMember>(Expression<Func<TDocument, IEnumerable<TMember>>> memberExpression, int size)
230+
{
231+
return new FieldsBuilder<TDocument>().Slice(memberExpression, size);
232+
}
233+
234+
/// <summary>
235+
/// Adds a slice to be included in the results.
236+
/// </summary>
237+
/// <typeparam name="TMember">The type of the member.</typeparam>
238+
/// <param name="memberExpression">The member expression.</param>
239+
/// <param name="skip">The number of values to skip.</param>
240+
/// <param name="limit">The number of values to extract.</param>
241+
/// <returns>
242+
/// The builder (so method calls can be chained).
243+
/// </returns>
244+
public static FieldsBuilder<TDocument> Slice<TMember>(Expression<Func<TDocument, IEnumerable<TMember>>> memberExpression, int skip, int limit)
245+
{
246+
return new FieldsBuilder<TDocument>().Slice(memberExpression, skip, limit);
247+
}
248+
}
249+
250+
/// <summary>
251+
/// A builder for specifying which fields of a document the server should return.
252+
/// </summary>
253+
/// <typeparam name="TDocument">The type of the document.</typeparam>
254+
[Serializable]
255+
public class FieldsBuilder<TDocument> : BuilderBase, IMongoFields
256+
{
257+
private readonly BsonSerializationInfoHelper _serializationHelper;
258+
private FieldsBuilder _fieldsBuilder;
259+
260+
// constructors
261+
/// <summary>
262+
/// Initializes a new instance of the FieldsBuilder class.
263+
/// </summary>
264+
public FieldsBuilder()
265+
{
266+
_serializationHelper = new BsonSerializationInfoHelper();
267+
_fieldsBuilder = new FieldsBuilder();
268+
}
269+
270+
// public methods
271+
/// <summary>
272+
/// Adds one or more field names to be excluded from the results.
273+
/// </summary>
274+
/// <param name="memberExpressions">The member expressions.</param>
275+
/// <returns>
276+
/// The builder (so method calls can be chained).
277+
/// </returns>
278+
public FieldsBuilder<TDocument> Exclude(params Expression<Func<TDocument, object>>[] memberExpressions)
279+
{
280+
var elementNames = GetElementNames(memberExpressions);
281+
282+
_fieldsBuilder = _fieldsBuilder.Exclude(elementNames.ToArray());
283+
return this;
284+
}
285+
286+
/// <summary>
287+
/// Adds one or more field names to be included in the results.
288+
/// </summary>
289+
/// <param name="memberExpressions">The member expressions.</param>
290+
/// <returns>
291+
/// The builder (so method calls can be chained).
292+
/// </returns>
293+
public FieldsBuilder<TDocument> Include(params Expression<Func<TDocument, object>>[] memberExpressions)
294+
{
295+
var elementNames = GetElementNames(memberExpressions);
296+
297+
_fieldsBuilder = _fieldsBuilder.Include(elementNames.ToArray());
298+
return this;
299+
}
300+
301+
/// <summary>
302+
/// Adds a slice to be included in the results.
303+
/// </summary>
304+
/// <typeparam name="TMember">The type of the member.</typeparam>
305+
/// <param name="memberExpression">The member expression.</param>
306+
/// <param name="size">The size of the slice (negative sizes are taken from the end).</param>
307+
/// <returns>
308+
/// The builder (so method calls can be chained).
309+
/// </returns>
310+
public FieldsBuilder<TDocument> Slice<TMember>(Expression<Func<TDocument, IEnumerable<TMember>>> memberExpression, int size)
311+
{
312+
var info = _serializationHelper.GetSerializationInfo(memberExpression);
313+
_fieldsBuilder = _fieldsBuilder.Slice(info.ElementName, size);
314+
return this;
315+
}
316+
317+
/// <summary>
318+
/// Adds a slice to be included in the results.
319+
/// </summary>
320+
/// <typeparam name="TMember">The type of the member.</typeparam>
321+
/// <param name="memberExpression">The member expression.</param>
322+
/// <param name="skip">The number of values to skip.</param>
323+
/// <param name="limit">The number of values to extract.</param>
324+
/// <returns>
325+
/// The builder (so method calls can be chained).
326+
/// </returns>
327+
public FieldsBuilder<TDocument> Slice<TMember>(Expression<Func<TDocument, IEnumerable<TMember>>> memberExpression, int skip, int limit)
328+
{
329+
var info = _serializationHelper.GetSerializationInfo(memberExpression);
330+
_fieldsBuilder = _fieldsBuilder.Slice(info.ElementName, skip, limit);
331+
return this;
332+
}
333+
334+
/// <summary>
335+
/// Converts this object to a BsonDocument.
336+
/// </summary>
337+
/// <returns>
338+
/// A BsonDocument.
339+
/// </returns>
340+
public override BsonDocument ToBsonDocument()
341+
{
342+
return _fieldsBuilder.ToBsonDocument();
343+
}
344+
345+
/// <summary>
346+
/// Serializes the result of the builder to a BsonWriter.
347+
/// </summary>
348+
/// <param name="bsonWriter">The writer.</param>
349+
/// <param name="nominalType">The nominal type.</param>
350+
/// <param name="options">The serialization options.</param>
351+
protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
352+
{
353+
((IBsonSerializable)_fieldsBuilder).Serialize(bsonWriter, nominalType, options);
354+
}
355+
356+
private IEnumerable<string> GetElementNames(IEnumerable<Expression<Func<TDocument, object>>> memberExpressions)
357+
{
358+
var elementNames = memberExpressions
359+
.Select(x => _serializationHelper.GetSerializationInfo(x))
360+
.Select(x => x.ElementName);
361+
return elementNames;
362+
}
363+
}
180364
}

Driver/Builders/GeoHaystackSearchOptionsBuilder.cs

Lines changed: 137 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
*/
1515

1616
using System;
17-
using System.Collections.Generic;
18-
using System.Linq;
19-
using System.Text;
20-
17+
using System.Linq.Expressions;
2118
using MongoDB.Bson;
2219
using MongoDB.Bson.IO;
2320
using MongoDB.Bson.Serialization;
24-
using MongoDB.Driver;
21+
using MongoDB.Driver.Linq.Utils;
2522

2623
namespace MongoDB.Driver.Builders
2724
{
@@ -146,4 +143,138 @@ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBson
146143
((IBsonSerializable)_document).Serialize(bsonWriter, nominalType, options);
147144
}
148145
}
149-
}
146+
147+
/// <summary>
148+
/// A builder for the options of the GeoHaystackSearch command.
149+
/// </summary>
150+
/// <typeparam name="TDocument">The type of the document.</typeparam>
151+
public static class GeoHaystackSearchOptions<TDocument>
152+
{
153+
// public static properties
154+
/// <summary>
155+
/// Gets a null value with a type of IMongoGeoHaystackSearchOptions.
156+
/// </summary>
157+
public static IMongoGeoHaystackSearchOptions Null
158+
{
159+
get { return null; }
160+
}
161+
162+
// public static methods
163+
/// <summary>
164+
/// Sets the maximum number of results to return.
165+
/// </summary>
166+
/// <param name="value">The maximum number of results to return.</param>
167+
/// <returns>The builder (so method calls can be chained).</returns>
168+
public static GeoHaystackSearchOptionsBuilder<TDocument> SetLimit(int value)
169+
{
170+
return new GeoHaystackSearchOptionsBuilder<TDocument>().SetLimit(value);
171+
}
172+
173+
/// <summary>
174+
/// Sets the max distance.
175+
/// </summary>
176+
/// <param name="value">The max distance.</param>
177+
/// <returns>The builder (so method calls can be chained).</returns>
178+
public static GeoHaystackSearchOptionsBuilder<TDocument> SetMaxDistance(double value)
179+
{
180+
return new GeoHaystackSearchOptionsBuilder<TDocument>().SetMaxDistance(value);
181+
}
182+
183+
/// <summary>
184+
/// Sets the query on the optional additional field.
185+
/// </summary>
186+
/// <typeparam name="TMember">The member type.</typeparam>
187+
/// <param name="memberExpression">The member expression.</param>
188+
/// <param name="value">The value fo the additional field.</param>
189+
/// <returns>
190+
/// The builder (so method calls can be chained).
191+
/// </returns>
192+
public static GeoHaystackSearchOptionsBuilder<TDocument> SetQuery<TMember>(Expression<Func<TDocument, TMember>> memberExpression, TMember value)
193+
{
194+
return new GeoHaystackSearchOptionsBuilder<TDocument>().SetQuery(memberExpression, value);
195+
}
196+
}
197+
198+
/// <summary>
199+
/// A builder for the options of the GeoHaystackSearch command.
200+
/// </summary>
201+
/// <typeparam name="TDocument">The type of the document.</typeparam>
202+
[Serializable]
203+
public class GeoHaystackSearchOptionsBuilder<TDocument> : BuilderBase, IMongoGeoHaystackSearchOptions
204+
{
205+
private readonly BsonSerializationInfoHelper _serializationHelper;
206+
private GeoHaystackSearchOptionsBuilder _geoHaystackBuilder;
207+
208+
// constructors
209+
/// <summary>
210+
/// Initializes a new instance of the GeoHaystackSearchOptionsBuilder class.
211+
/// </summary>
212+
public GeoHaystackSearchOptionsBuilder()
213+
{
214+
_serializationHelper = new BsonSerializationInfoHelper();
215+
_geoHaystackBuilder = new GeoHaystackSearchOptionsBuilder();
216+
}
217+
218+
// public methods
219+
/// <summary>
220+
/// Sets the maximum number of results to return.
221+
/// </summary>
222+
/// <param name="value">The maximum number of results to return.</param>
223+
/// <returns>The builder (so method calls can be chained).</returns>
224+
public GeoHaystackSearchOptionsBuilder<TDocument> SetLimit(int value)
225+
{
226+
_geoHaystackBuilder = _geoHaystackBuilder.SetLimit(value);
227+
return this;
228+
}
229+
230+
/// <summary>
231+
/// Sets the max distance.
232+
/// </summary>
233+
/// <param name="value">The max distance.</param>
234+
/// <returns>The builder (so method calls can be chained).</returns>
235+
public GeoHaystackSearchOptionsBuilder<TDocument> SetMaxDistance(double value)
236+
{
237+
_geoHaystackBuilder = _geoHaystackBuilder.SetMaxDistance(value);
238+
return this;
239+
}
240+
241+
/// <summary>
242+
/// Sets the query on the optional additional field.
243+
/// </summary>
244+
/// <typeparam name="TMember">The type of the member.</typeparam>
245+
/// <param name="memberExpression">The member expression.</param>
246+
/// <param name="value">The value fo the additional field.</param>
247+
/// <returns>
248+
/// The builder (so method calls can be chained).
249+
/// </returns>
250+
public GeoHaystackSearchOptionsBuilder<TDocument> SetQuery<TMember>(Expression<Func<TDocument, TMember>> memberExpression, TMember value)
251+
{
252+
var info = _serializationHelper.GetSerializationInfo(memberExpression);
253+
var serializedValue = _serializationHelper.SerializeValue(info, value);
254+
_geoHaystackBuilder = _geoHaystackBuilder.SetQuery(info.ElementName, serializedValue);
255+
return this;
256+
}
257+
258+
/// <summary>
259+
/// Converts this object to a BsonDocument.
260+
/// </summary>
261+
/// <returns>
262+
/// A BsonDocument.
263+
/// </returns>
264+
public override BsonDocument ToBsonDocument()
265+
{
266+
return _geoHaystackBuilder.ToBsonDocument();
267+
}
268+
269+
/// <summary>
270+
/// Serializes the result of the builder to a BsonWriter.
271+
/// </summary>
272+
/// <param name="bsonWriter">The writer.</param>
273+
/// <param name="nominalType">The nominal type.</param>
274+
/// <param name="options">The serialization options.</param>
275+
protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
276+
{
277+
((IBsonSerializable)_geoHaystackBuilder).Serialize(bsonWriter, nominalType, options);
278+
}
279+
}
280+
}

0 commit comments

Comments
 (0)