Permalink
Browse files

Integrate command results into the standard serialization protocols.

  • Loading branch information...
1 parent 3df21a6 commit b46c8dbf679e1cb26447d1f616ce7d449ba3feb2 rstam committed Mar 25, 2013
Showing with 349 additions and 131 deletions.
  1. +6 −2 MongoDB.Driver/CommandResults/AggregateResult.cs
  2. +6 −2 MongoDB.Driver/CommandResults/CollectionStatsResult.cs
  3. +12 −31 MongoDB.Driver/CommandResults/CommandResult.cs
  4. +46 −0 MongoDB.Driver/CommandResults/CommandResultSerializer.cs
  5. +7 −2 MongoDB.Driver/CommandResults/DatabaseStatsResult.cs
  6. +57 −0 MongoDB.Driver/CommandResults/DistinctCommandResult.cs
  7. +67 −0 MongoDB.Driver/CommandResults/DistinctCommandResultSerializer.cs
  8. +6 −2 MongoDB.Driver/CommandResults/FindAndModifyResult.cs
  9. +10 −4 MongoDB.Driver/CommandResults/GeoHaystackSearchResult.cs
  10. +10 −4 MongoDB.Driver/CommandResults/GeoNearResult.cs
  11. +7 −2 MongoDB.Driver/CommandResults/GetLastErrorResult.cs
  12. +7 −2 MongoDB.Driver/CommandResults/GetProfilingLevelResult.cs
  13. +13 −0 MongoDB.Driver/CommandResults/IsMasterResult.cs
  14. +6 −2 MongoDB.Driver/CommandResults/MapReduceResult.cs
  15. +10 −0 MongoDB.Driver/CommandResults/SafeModeResult.cs
  16. +6 −2 MongoDB.Driver/CommandResults/ValidateCollectionResult.cs
  17. +12 −0 MongoDB.Driver/CommandResults/WriteConcernResult.cs
  18. +25 −11 MongoDB.Driver/Communication/MongoConnection.cs
  19. +4 −5 MongoDB.Driver/Communication/MongoServerInstance.cs
  20. +2 −3 MongoDB.Driver/Communication/Security/MongoCRAuthenticationProtocol.cs
  21. +1 −1 MongoDB.Driver/Communication/Security/SaslAuthenticationProtocol.cs
  22. +9 −31 MongoDB.Driver/MongoCollection.cs
  23. +3 −0 MongoDB.Driver/MongoDB.Driver.csproj
  24. +2 −2 MongoDB.Driver/MongoDatabase.cs
  25. +12 −14 MongoDB.DriverUnitTests/CommandResults/CommandResultTests.cs
  26. +3 −9 MongoDB.DriverUnitTests/CommandResults/IsMasterResultTests.cs
@@ -17,20 +17,24 @@
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson;
+using MongoDB.Bson.Serialization.Attributes;
namespace MongoDB.Driver
{
/// <summary>
/// Represents the results of a Aggregate command.
/// </summary>
[Serializable]
+ [BsonSerializer(typeof(CommandResultSerializer))]
public class AggregateResult : CommandResult
{
// constructors
/// <summary>
- /// Initializes a new instance of the AggregateResult class.
+ /// Initializes a new instance of the <see cref="AggregateResult"/> class.
/// </summary>
- public AggregateResult()
+ /// <param name="response">The response.</param>
+ public AggregateResult(BsonDocument response)
+ : base(response)
{
}
@@ -17,6 +17,7 @@
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson;
+using MongoDB.Bson.Serialization.Attributes;
namespace MongoDB.Driver
{
@@ -56,16 +57,19 @@ public enum CollectionUserFlags
/// Represents the results of the collection stats command.
/// </summary>
[Serializable]
+ [BsonSerializer(typeof(CommandResultSerializer))]
public class CollectionStatsResult : CommandResult
{
// private fields
private IndexSizesResult _indexSizes;
// constructors
/// <summary>
- /// Initializes a new instance of the CollectionStatsResult class.
+ /// Initializes a new instance of the <see cref="CollectionStatsResult"/> class.
/// </summary>
- public CollectionStatsResult()
+ /// <param name="response">The response.</param>
+ public CollectionStatsResult(BsonDocument response)
+ : base(response)
{
}
@@ -15,13 +15,15 @@
using System;
using MongoDB.Bson;
+using MongoDB.Bson.Serialization.Attributes;
namespace MongoDB.Driver
{
/// <summary>
/// Represents the result of a command (there are also subclasses for various commands).
/// </summary>
[Serializable]
+ [BsonSerializer(typeof(CommandResultSerializer))]
public class CommandResult
{
// private fields
@@ -30,22 +32,11 @@ public class CommandResult
// constructors
/// <summary>
- /// Initializes a new instance of the CommandResult class.
+ /// Initializes a new instance of the <see cref="CommandResult"/> class.
/// </summary>
- // since we often create instances of CommandResult using a generic type parameter
- // we need a constructor with no arguments (see also the Initialize method below)
- public CommandResult()
- {
- }
-
- /// <summary>
- /// Initializes a new instance of the CommandResult class.
- /// </summary>
- /// <param name="command">The command.</param>
/// <param name="response">The response.</param>
- public CommandResult(IMongoCommand command, BsonDocument response)
+ public CommandResult(BsonDocument response)
{
- _command = command;
_response = response;
}
@@ -64,6 +55,14 @@ public CommandResult(IMongoCommand command, BsonDocument response)
public IMongoCommand Command
{
get { return _command; }
+ set
+ {
+ if (value == null)
+ {
+ throw new ArgumentNullException("value");
+ }
+ _command = value;
+ }
}
/// <summary>
@@ -128,23 +127,5 @@ public bool Ok
}
}
}
-
- // public methods
- /// <summary>
- /// Initializes an existing instance of the CommandResult class.
- /// </summary>
- /// <param name="command">The command.</param>
- /// <param name="response">The response.</param>
- // used after a constructor with no arguments (when creating a CommandResult from a generic type parameter)
- public void Initialize(IMongoCommand command, BsonDocument response)
- {
- if (_command != null || _response != null)
- {
- var message = string.Format("{0} has already been initialized.", this.GetType().Name);
- throw new InvalidOperationException(message);
- }
- _command = command;
- _response = response;
- }
}
}
@@ -0,0 +1,46 @@
+/* Copyright 2010-2013 10gen Inc.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using MongoDB.Bson;
+using MongoDB.Bson.IO;
+using MongoDB.Bson.Serialization;
+using MongoDB.Bson.Serialization.Attributes;
+using MongoDB.Bson.Serialization.Serializers;
+
+namespace MongoDB.Driver
+{
+ /// <summary>
+ /// Represents a serializer for a CommandResult.
+ /// </summary>
+ public class CommandResultSerializer : BsonBaseSerializer
+ {
+ /// <summary>
+ /// Deserializes an object from a BsonReader.
+ /// </summary>
+ /// <param name="bsonReader">The BsonReader.</param>
+ /// <param name="nominalType">The nominal type of the object.</param>
+ /// <param name="actualType">The actual type of the object.</param>
+ /// <param name="options">The serialization options.</param>
+ /// <returns>
+ /// An object.
+ /// </returns>
+ public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
+ {
+ var response = (BsonDocument)BsonDocumentSerializer.Instance.Deserialize(bsonReader, typeof(BsonDocument), null);
+ return (CommandResult)Activator.CreateInstance(actualType, new object[] { response });
+ }
+ }
+}
@@ -14,20 +14,25 @@
*/
using System;
+using MongoDB.Bson;
+using MongoDB.Bson.Serialization.Attributes;
namespace MongoDB.Driver
{
/// <summary>
/// Represents the result of the database stats command.
/// </summary>
[Serializable]
+ [BsonSerializer(typeof(CommandResultSerializer))]
public class DatabaseStatsResult : CommandResult
{
// constructors
/// <summary>
- /// Initializes a new instance of the DatabaseStatsResult class.
+ /// Initializes a new instance of the <see cref="DatabaseStatsResult"/> class.
/// </summary>
- public DatabaseStatsResult()
+ /// <param name="response">The response.</param>
+ public DatabaseStatsResult(BsonDocument response)
+ : base(response)
{
}
@@ -0,0 +1,57 @@
+/* Copyright 2010-2013 10gen Inc.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using MongoDB.Bson;
+using MongoDB.Bson.Serialization.Attributes;
+using System.Collections.Generic;
+
+namespace MongoDB.Driver
+{
+ /// <summary>
+ /// Represents the result of a command (there are also subclasses for various commands).
+ /// </summary>
+ [Serializable]
+ [BsonSerializer(typeof(DistinctCommandResultSerializer<>))]
+ public class DistinctCommandResult<TValue> : CommandResult
+ {
+ // private fields
+ private IEnumerable<TValue> _values;
+
+ // constructors
+ /// <summary>
+ /// Initializes a new instance of the <see cref="DistinctCommandResult{TValue}" /> class.
+ /// </summary>
+ /// <param name="response">The response.</param>
+ /// <param name="values">The values.</param>
+ internal DistinctCommandResult(BsonDocument response, IEnumerable<TValue> values)
+ : base(response)
+ {
+ _values = values;
+ }
+
+ // public properties
+ /// <summary>
+ /// Gets the values.
+ /// </summary>
+ /// <value>
+ /// The values.
+ /// </value>
+ public IEnumerable<TValue> Values
+ {
+ get { return _values; }
+ }
+ }
+}
@@ -0,0 +1,67 @@
+/* Copyright 2010-2013 10gen Inc.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using MongoDB.Bson;
+using MongoDB.Bson.IO;
+using MongoDB.Bson.Serialization;
+using MongoDB.Bson.Serialization.Attributes;
+using MongoDB.Bson.Serialization.Serializers;
+using System.Collections.Generic;
+
+namespace MongoDB.Driver
+{
+ /// <summary>
+ /// Represents a serializer for a DistinctCommandResult with values of type TValue.
+ /// </summary>
+ /// <typeparam name="TValue">The type of the value.</typeparam>
+ public class DistinctCommandResultSerializer<TValue> : BsonBaseSerializer
+ {
+ /// <summary>
+ /// Deserializes an object from a BsonReader.
+ /// </summary>
+ /// <param name="bsonReader">The BsonReader.</param>
+ /// <param name="nominalType">The nominal type of the object.</param>
+ /// <param name="actualType">The actual type of the object.</param>
+ /// <param name="options">The serialization options.</param>
+ /// <returns>
+ /// An object.
+ /// </returns>
+ public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
+ {
+ var response = new BsonDocument();
+ IEnumerable<TValue> values = null;
+
+ bsonReader.ReadStartDocument();
+ while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
+ {
+ var name = bsonReader.ReadName();
+ if (name == "values")
+ {
+ var enumerableSerializer = new EnumerableSerializer<TValue>();
+ values = (IEnumerable<TValue>)enumerableSerializer.Deserialize(bsonReader, typeof(List<TValue>), null);
+ }
+ else
+ {
+ var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
+ response.Add(name, value);
+ }
+ }
+ bsonReader.ReadEndDocument();
+
+ return new DistinctCommandResult<TValue>(response, values);
+ }
+ }
+}
@@ -16,20 +16,24 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
+using MongoDB.Bson.Serialization.Attributes;
namespace MongoDB.Driver
{
/// <summary>
/// Represents the result of a FindAndModify command.
/// </summary>
[Serializable]
+ [BsonSerializer(typeof(CommandResultSerializer))]
public class FindAndModifyResult : CommandResult
{
// constructors
/// <summary>
- /// Initializes a new instance of the FindAndModifyResult class.
+ /// Initializes a new instance of the <see cref="FindAndModifyResult"/> class.
/// </summary>
- public FindAndModifyResult()
+ /// <param name="response">The response.</param>
+ public FindAndModifyResult(BsonDocument response)
+ : base(response)
{
}
Oops, something went wrong.

0 comments on commit b46c8db

Please sign in to comment.