Skip to content

Commit

Permalink
Add GetBsonReaderSettings to SerializationFactory. This allows to pas…
Browse files Browse the repository at this point in the history
…s Read configuration options down to the protocol.
  • Loading branch information
lanwin committed May 5, 2010
1 parent fd675ff commit 3f5f34c
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 20 deletions.
3 changes: 2 additions & 1 deletion source/MongoDB.Tests/MongoDB.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<Compile Include="UnitTests\Serialization\Descriptors\GenericListPropertyTests.cs" />
<Compile Include="UnitTests\Serialization\Descriptors\OperatorTests.cs" />
<Compile Include="UnitTests\Serialization\Descriptors\PolymorphicObjectTests.cs" />
<Compile Include="UnitTests\Serialization\SerializationFactoryTests.cs" />
<Compile Include="UnitTests\Serialization\SerializationTestBase.cs" />
<Compile Include="IntegrationTests\SecondServer\TestAuthentication.cs" />
<Compile Include="UnitTests\Bson\TestBsonReader.cs" />
Expand Down Expand Up @@ -209,4 +210,4 @@
</None>
</ItemGroup>
<ItemGroup />
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ public void Test()
});
});
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using MongoDB.Configuration;
using MongoDB.Serialization;
using NUnit.Framework;

namespace MongoDB.UnitTests.Serialization
{
[TestFixture]
public class SerializationFactoryTests
{
[Test]
public void GetBsonReaderSettingsDefaults()
{
var factory = new SerializationFactory();
var readerSettings = factory.GetBsonReaderSettings(typeof(int));
Assert.AreEqual(readerSettings.ReadLocalTime,true);
Assert.IsNotNull(readerSettings.Builder);
}

[Test]
public void ReadLocalTimeCanBeChangedByConfig()
{
var factory = new SerializationFactory(new MongoConfiguration {ReadLocalTime = false});
var readerSettings = factory.GetBsonReaderSettings(typeof(int));
Assert.AreEqual(readerSettings.ReadLocalTime, false);
}
}
}
5 changes: 2 additions & 3 deletions source/MongoDB/Connections/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,11 @@ private T SendCommandCore<T>(ISerializationFactory factory, string database, Typ
Query = command
};

var builder = factory.GetBsonBuilder(typeof(T));
var readerSettings = factory.GetBsonReaderSettings(typeof(T));

try
{
var settings = new BsonReaderSettings(builder);
var reply = SendTwoWayMessage<T>(query, settings);
var reply = SendTwoWayMessage<T>(query, readerSettings);

if(reply.CursorId > 0)
SendMessage(new KillCursorsMessage(reply.CursorId));
Expand Down
10 changes: 4 additions & 6 deletions source/MongoDB/Cursor_1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,10 @@ private void RetrieveData(){
if (_fields != null)
query.ReturnFieldSelector = _fields;

var builder = _serializationFactory.GetBsonBuilder(typeof(T));
var readerSettings = _serializationFactory.GetBsonReaderSettings(typeof(T));

try {
var settings = new BsonReaderSettings(builder);
_reply = _connection.SendTwoWayMessage<T>(query, settings);
_reply = _connection.SendTwoWayMessage<T>(query, readerSettings);
Id = _reply.CursorId;
if (_limit < 0)
_limit = _limit * -1;
Expand All @@ -291,11 +290,10 @@ private void RetrieveData(){
private void RetrieveMoreData(){
var getMoreMessage = new GetMoreMessage(FullCollectionName, Id, _limit);

var builder = _serializationFactory.GetBsonBuilder(typeof(T));
var readerSettings = _serializationFactory.GetBsonReaderSettings(typeof(T));

try {
var settings = new BsonReaderSettings(builder);
_reply = _connection.SendTwoWayMessage<T>(getMoreMessage, settings);
_reply = _connection.SendTwoWayMessage<T>(getMoreMessage, readerSettings);
Id = _reply.CursorId;
} catch (IOException exception) {
Id = 0;
Expand Down
25 changes: 16 additions & 9 deletions source/MongoDB/Serialization/ISerializationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@ namespace MongoDB.Serialization
public interface ISerializationFactory
{
/// <summary>
/// Gets the bson builder.
/// Gets the bson writer settings.
/// </summary>
/// <param name="rootType">Type of the root.</param>
/// <returns></returns>
IBsonObjectBuilder GetBsonBuilder(Type rootType);

/// <summary>
/// Gets the bson descriptor.
/// </summary>
/// <param name="rootType">Type of the root.</param>
/// <returns></returns>
IBsonObjectDescriptor GetBsonDescriptor(Type rootType);
BsonWriterSettings GetBsonWriterSettings(Type rootType);

/// <summary>
/// Gets the name of the collection given the rootType.
Expand All @@ -35,5 +28,19 @@ public interface ISerializationFactory
/// <param name="type">The type.</param>
/// <returns></returns>
IObjectDescriptor GetObjectDescriptor(Type type);

/// <summary>
/// Gets the descriptor.
/// </summary>
/// <param name="rootType">Type of the root.</param>
/// <returns></returns>
IBsonObjectDescriptor GetBsonDescriptor(Type rootType);

/// <summary>
/// Gets the bson reader settings.
/// </summary>
/// <param name="rootType">Type of the root.</param>
/// <returns></returns>
BsonReaderSettings GetBsonReaderSettings(Type rootType);
}
}
23 changes: 23 additions & 0 deletions source/MongoDB/Serialization/SerializationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ public IBsonObjectDescriptor GetBsonDescriptor(Type rootType)
return new BsonClassMapDescriptor(_configuration.MappingStore, rootType);
}

/// <summary>
/// Gets the bson reader settings.
/// </summary>
/// <param name="rootType">Type of the root.</param>
/// <returns></returns>
public BsonReaderSettings GetBsonReaderSettings(Type rootType)
{
return new BsonReaderSettings(GetBsonBuilder(rootType))
{
ReadLocalTime = _configuration.ReadLocalTime
};
}

/// <summary>
/// Gets the bson writer settings.
/// </summary>
/// <param name="rootType">Type of the root.</param>
/// <returns></returns>
public BsonWriterSettings GetBsonWriterSettings(Type rootType)
{
return new BsonWriterSettings(GetBsonDescriptor(rootType));
}

/// <summary>
/// Gets the name of the collection given the rootType.
/// </summary>
Expand Down

0 comments on commit 3f5f34c

Please sign in to comment.