Skip to content

Commit 87cd1dd

Browse files
committed
CSHARP-684: added builder support for setOnInsert and the push each variants.
1 parent d7e9fa8 commit 87cd1dd

File tree

5 files changed

+708
-1
lines changed

5 files changed

+708
-1
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Linq.Expressions;
20+
using System.Text;
21+
using MongoDB.Driver;
22+
using MongoDB.Driver.Linq.Utils;
23+
24+
namespace MongoDB.Driver.Builders
25+
{
26+
/// <summary>
27+
/// Arguments for $push with an $each clause.
28+
/// </summary>
29+
public class PushEachOptions
30+
{
31+
// private fields
32+
private int? _slice;
33+
private IMongoSortBy _sort;
34+
35+
// public properties
36+
/// <summary>
37+
/// Gets or sets the slice (see $slice).
38+
/// </summary>
39+
public int? Slice
40+
{
41+
get { return _slice; }
42+
set { _slice = value; }
43+
}
44+
45+
/// <summary>
46+
/// Gets or sets the sort (see $sort).
47+
/// </summary>
48+
public IMongoSortBy Sort
49+
{
50+
get { return _sort; }
51+
set { _sort = value; }
52+
}
53+
}
54+
55+
/// <summary>
56+
/// A fluent builder for PushEachOptions.
57+
/// </summary>
58+
/// <typeparam name="TValue">The type of the value.</typeparam>
59+
public class PushEachOptionsBuilder<TValue>
60+
{
61+
// private fields
62+
private readonly BsonSerializationInfoHelper _serializationInfoHelper;
63+
private SortByBuilder<TValue> _sortBy;
64+
private int? _slice;
65+
66+
// constructors
67+
/// <summary>
68+
/// Initializes a new instance of the <see cref="PushEachOptionsBuilder{TValue}" /> class.
69+
/// </summary>
70+
/// <param name="serializationInfoHelper">The serialization info helper.</param>
71+
internal PushEachOptionsBuilder(BsonSerializationInfoHelper serializationInfoHelper)
72+
{
73+
_serializationInfoHelper = serializationInfoHelper;
74+
}
75+
76+
// public methods
77+
/// <summary>
78+
/// Specifies a slice for the array.
79+
/// </summary>
80+
/// <param name="slice">The slice.</param>
81+
/// <returns>The builder.</returns>
82+
public PushEachOptionsBuilder<TValue> Slice(int slice)
83+
{
84+
_slice = slice;
85+
return this;
86+
}
87+
88+
/// <summary>
89+
/// Sorts the array in ascending order.
90+
/// </summary>
91+
/// <param name="memberExpressions">The member expressions.</param>
92+
/// <returns>The builder.</returns>
93+
public PushEachOptionsBuilder<TValue> SortAscending(params Expression<Func<TValue, object>>[] memberExpressions)
94+
{
95+
if (_sortBy == null)
96+
{
97+
_sortBy = new SortByBuilder<TValue>(_serializationInfoHelper);
98+
}
99+
100+
_sortBy.Ascending(memberExpressions);
101+
return this;
102+
}
103+
104+
/// <summary>
105+
/// Sorts the array in descending order.
106+
/// </summary>
107+
/// <param name="memberExpressions">The member expressions.</param>
108+
/// <returns>The builder.</returns>
109+
public PushEachOptionsBuilder<TValue> SortDescending(params Expression<Func<TValue, object>>[] memberExpressions)
110+
{
111+
if (_sortBy == null)
112+
{
113+
_sortBy = new SortByBuilder<TValue>(_serializationInfoHelper);
114+
}
115+
116+
_sortBy.Descending(memberExpressions);
117+
return this;
118+
}
119+
120+
/// <summary>
121+
/// Builds the PushEachArgs.
122+
/// </summary>
123+
/// <returns>A built PushEachOptions.</returns>
124+
public PushEachOptions Build()
125+
{
126+
return new PushEachOptions
127+
{
128+
Slice = _slice,
129+
Sort = _sortBy
130+
};
131+
}
132+
}
133+
}

MongoDB.Driver/Builders/SortByBuilder.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,16 @@ public class SortByBuilder<TDocument> : BuilderBase, IMongoSortBy
177177
/// Initializes a new instance of the <see cref="SortByBuilder&lt;TDocument&gt;"/> class.
178178
/// </summary>
179179
public SortByBuilder()
180+
: this(new BsonSerializationInfoHelper())
181+
{ }
182+
183+
/// <summary>
184+
/// Initializes a new instance of the <see cref="SortByBuilder{TDocument}" /> class.
185+
/// </summary>
186+
/// <param name="serializationInfoHelper">The serialization info helper.</param>
187+
internal SortByBuilder(BsonSerializationInfoHelper serializationInfoHelper)
180188
{
181-
_serializationInfoHelper = new BsonSerializationInfoHelper();
189+
_serializationInfoHelper = serializationInfoHelper;
182190
_sortByBuilder = new SortByBuilder();
183191
}
184192

0 commit comments

Comments
 (0)