Skip to content

Commit b46c8db

Browse files
author
rstam
committed
Integrate command results into the standard serialization protocols.
1 parent 3df21a6 commit b46c8db

26 files changed

+349
-131
lines changed

MongoDB.Driver/CommandResults/AggregateResult.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@
1717
using System.Collections.Generic;
1818
using System.Linq;
1919
using MongoDB.Bson;
20+
using MongoDB.Bson.Serialization.Attributes;
2021

2122
namespace MongoDB.Driver
2223
{
2324
/// <summary>
2425
/// Represents the results of a Aggregate command.
2526
/// </summary>
2627
[Serializable]
28+
[BsonSerializer(typeof(CommandResultSerializer))]
2729
public class AggregateResult : CommandResult
2830
{
2931
// constructors
3032
/// <summary>
31-
/// Initializes a new instance of the AggregateResult class.
33+
/// Initializes a new instance of the <see cref="AggregateResult"/> class.
3234
/// </summary>
33-
public AggregateResult()
35+
/// <param name="response">The response.</param>
36+
public AggregateResult(BsonDocument response)
37+
: base(response)
3438
{
3539
}
3640

MongoDB.Driver/CommandResults/CollectionStatsResult.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Collections.Generic;
1818
using System.Linq;
1919
using MongoDB.Bson;
20+
using MongoDB.Bson.Serialization.Attributes;
2021

2122
namespace MongoDB.Driver
2223
{
@@ -56,16 +57,19 @@ public enum CollectionUserFlags
5657
/// Represents the results of the collection stats command.
5758
/// </summary>
5859
[Serializable]
60+
[BsonSerializer(typeof(CommandResultSerializer))]
5961
public class CollectionStatsResult : CommandResult
6062
{
6163
// private fields
6264
private IndexSizesResult _indexSizes;
6365

6466
// constructors
6567
/// <summary>
66-
/// Initializes a new instance of the CollectionStatsResult class.
68+
/// Initializes a new instance of the <see cref="CollectionStatsResult"/> class.
6769
/// </summary>
68-
public CollectionStatsResult()
70+
/// <param name="response">The response.</param>
71+
public CollectionStatsResult(BsonDocument response)
72+
: base(response)
6973
{
7074
}
7175

MongoDB.Driver/CommandResults/CommandResult.cs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515

1616
using System;
1717
using MongoDB.Bson;
18+
using MongoDB.Bson.Serialization.Attributes;
1819

1920
namespace MongoDB.Driver
2021
{
2122
/// <summary>
2223
/// Represents the result of a command (there are also subclasses for various commands).
2324
/// </summary>
2425
[Serializable]
26+
[BsonSerializer(typeof(CommandResultSerializer))]
2527
public class CommandResult
2628
{
2729
// private fields
@@ -30,22 +32,11 @@ public class CommandResult
3032

3133
// constructors
3234
/// <summary>
33-
/// Initializes a new instance of the CommandResult class.
35+
/// Initializes a new instance of the <see cref="CommandResult"/> class.
3436
/// </summary>
35-
// since we often create instances of CommandResult using a generic type parameter
36-
// we need a constructor with no arguments (see also the Initialize method below)
37-
public CommandResult()
38-
{
39-
}
40-
41-
/// <summary>
42-
/// Initializes a new instance of the CommandResult class.
43-
/// </summary>
44-
/// <param name="command">The command.</param>
4537
/// <param name="response">The response.</param>
46-
public CommandResult(IMongoCommand command, BsonDocument response)
38+
public CommandResult(BsonDocument response)
4739
{
48-
_command = command;
4940
_response = response;
5041
}
5142

@@ -64,6 +55,14 @@ public int? Code
6455
public IMongoCommand Command
6556
{
6657
get { return _command; }
58+
set
59+
{
60+
if (value == null)
61+
{
62+
throw new ArgumentNullException("value");
63+
}
64+
_command = value;
65+
}
6766
}
6867

6968
/// <summary>
@@ -128,23 +127,5 @@ public bool Ok
128127
}
129128
}
130129
}
131-
132-
// public methods
133-
/// <summary>
134-
/// Initializes an existing instance of the CommandResult class.
135-
/// </summary>
136-
/// <param name="command">The command.</param>
137-
/// <param name="response">The response.</param>
138-
// used after a constructor with no arguments (when creating a CommandResult from a generic type parameter)
139-
public void Initialize(IMongoCommand command, BsonDocument response)
140-
{
141-
if (_command != null || _response != null)
142-
{
143-
var message = string.Format("{0} has already been initialized.", this.GetType().Name);
144-
throw new InvalidOperationException(message);
145-
}
146-
_command = command;
147-
_response = response;
148-
}
149130
}
150131
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 MongoDB.Bson;
18+
using MongoDB.Bson.IO;
19+
using MongoDB.Bson.Serialization;
20+
using MongoDB.Bson.Serialization.Attributes;
21+
using MongoDB.Bson.Serialization.Serializers;
22+
23+
namespace MongoDB.Driver
24+
{
25+
/// <summary>
26+
/// Represents a serializer for a CommandResult.
27+
/// </summary>
28+
public class CommandResultSerializer : BsonBaseSerializer
29+
{
30+
/// <summary>
31+
/// Deserializes an object from a BsonReader.
32+
/// </summary>
33+
/// <param name="bsonReader">The BsonReader.</param>
34+
/// <param name="nominalType">The nominal type of the object.</param>
35+
/// <param name="actualType">The actual type of the object.</param>
36+
/// <param name="options">The serialization options.</param>
37+
/// <returns>
38+
/// An object.
39+
/// </returns>
40+
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
41+
{
42+
var response = (BsonDocument)BsonDocumentSerializer.Instance.Deserialize(bsonReader, typeof(BsonDocument), null);
43+
return (CommandResult)Activator.CreateInstance(actualType, new object[] { response });
44+
}
45+
}
46+
}

MongoDB.Driver/CommandResults/DatabaseStatsResult.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,25 @@
1414
*/
1515

1616
using System;
17+
using MongoDB.Bson;
18+
using MongoDB.Bson.Serialization.Attributes;
1719

1820
namespace MongoDB.Driver
1921
{
2022
/// <summary>
2123
/// Represents the result of the database stats command.
2224
/// </summary>
2325
[Serializable]
26+
[BsonSerializer(typeof(CommandResultSerializer))]
2427
public class DatabaseStatsResult : CommandResult
2528
{
2629
// constructors
2730
/// <summary>
28-
/// Initializes a new instance of the DatabaseStatsResult class.
31+
/// Initializes a new instance of the <see cref="DatabaseStatsResult"/> class.
2932
/// </summary>
30-
public DatabaseStatsResult()
33+
/// <param name="response">The response.</param>
34+
public DatabaseStatsResult(BsonDocument response)
35+
: base(response)
3136
{
3237
}
3338

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 MongoDB.Bson;
18+
using MongoDB.Bson.Serialization.Attributes;
19+
using System.Collections.Generic;
20+
21+
namespace MongoDB.Driver
22+
{
23+
/// <summary>
24+
/// Represents the result of a command (there are also subclasses for various commands).
25+
/// </summary>
26+
[Serializable]
27+
[BsonSerializer(typeof(DistinctCommandResultSerializer<>))]
28+
public class DistinctCommandResult<TValue> : CommandResult
29+
{
30+
// private fields
31+
private IEnumerable<TValue> _values;
32+
33+
// constructors
34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="DistinctCommandResult{TValue}" /> class.
36+
/// </summary>
37+
/// <param name="response">The response.</param>
38+
/// <param name="values">The values.</param>
39+
internal DistinctCommandResult(BsonDocument response, IEnumerable<TValue> values)
40+
: base(response)
41+
{
42+
_values = values;
43+
}
44+
45+
// public properties
46+
/// <summary>
47+
/// Gets the values.
48+
/// </summary>
49+
/// <value>
50+
/// The values.
51+
/// </value>
52+
public IEnumerable<TValue> Values
53+
{
54+
get { return _values; }
55+
}
56+
}
57+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 MongoDB.Bson;
18+
using MongoDB.Bson.IO;
19+
using MongoDB.Bson.Serialization;
20+
using MongoDB.Bson.Serialization.Attributes;
21+
using MongoDB.Bson.Serialization.Serializers;
22+
using System.Collections.Generic;
23+
24+
namespace MongoDB.Driver
25+
{
26+
/// <summary>
27+
/// Represents a serializer for a DistinctCommandResult with values of type TValue.
28+
/// </summary>
29+
/// <typeparam name="TValue">The type of the value.</typeparam>
30+
public class DistinctCommandResultSerializer<TValue> : BsonBaseSerializer
31+
{
32+
/// <summary>
33+
/// Deserializes an object from a BsonReader.
34+
/// </summary>
35+
/// <param name="bsonReader">The BsonReader.</param>
36+
/// <param name="nominalType">The nominal type of the object.</param>
37+
/// <param name="actualType">The actual type of the object.</param>
38+
/// <param name="options">The serialization options.</param>
39+
/// <returns>
40+
/// An object.
41+
/// </returns>
42+
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
43+
{
44+
var response = new BsonDocument();
45+
IEnumerable<TValue> values = null;
46+
47+
bsonReader.ReadStartDocument();
48+
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
49+
{
50+
var name = bsonReader.ReadName();
51+
if (name == "values")
52+
{
53+
var enumerableSerializer = new EnumerableSerializer<TValue>();
54+
values = (IEnumerable<TValue>)enumerableSerializer.Deserialize(bsonReader, typeof(List<TValue>), null);
55+
}
56+
else
57+
{
58+
var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
59+
response.Add(name, value);
60+
}
61+
}
62+
bsonReader.ReadEndDocument();
63+
64+
return new DistinctCommandResult<TValue>(response, values);
65+
}
66+
}
67+
}

MongoDB.Driver/CommandResults/FindAndModifyResult.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@
1616
using System;
1717
using MongoDB.Bson;
1818
using MongoDB.Bson.Serialization;
19+
using MongoDB.Bson.Serialization.Attributes;
1920

2021
namespace MongoDB.Driver
2122
{
2223
/// <summary>
2324
/// Represents the result of a FindAndModify command.
2425
/// </summary>
2526
[Serializable]
27+
[BsonSerializer(typeof(CommandResultSerializer))]
2628
public class FindAndModifyResult : CommandResult
2729
{
2830
// constructors
2931
/// <summary>
30-
/// Initializes a new instance of the FindAndModifyResult class.
32+
/// Initializes a new instance of the <see cref="FindAndModifyResult"/> class.
3133
/// </summary>
32-
public FindAndModifyResult()
34+
/// <param name="response">The response.</param>
35+
public FindAndModifyResult(BsonDocument response)
36+
: base(response)
3337
{
3438
}
3539

0 commit comments

Comments
 (0)