├── .gitignore
├── AssemblyInfoGlobal.cs
├── Deploy.proj
├── LICENSE
├── MongoDB-CSharp-2010.sln
├── MongoDB-CSharp.sln
├── README.rst
├── StrongName.snk
├── examples
├── Simple
│ ├── AssemblyInfo.cs
│ ├── Main.cs
│ ├── Simple.csproj
│ └── Simple.exe.config
└── SimpleVB
│ ├── Application.vb
│ ├── AssemblyInfo.vb
│ ├── SimpleVB.exe.config
│ └── SimpleVB.vbproj
├── redist
├── Moq.dll
├── Moq.xml
├── nunit.framework.dll
└── nunit.framework.xml
├── source
├── MongoDB.GridFS.Tests
│ ├── GridFileInfoTest.cs
│ ├── GridFileStreamTest.cs
│ ├── GridFileTest.cs
│ ├── GridTestBase.cs
│ └── MongoDB.GridFS.Tests.csproj
├── MongoDB.GridFS
│ ├── AssemblyInfo.cs
│ ├── GridException.cs
│ ├── GridFile.cs
│ ├── GridFileInfo.cs
│ ├── GridFileStream.cs
│ └── MongoDB.GridFS.csproj
├── MongoDB.Tests
│ ├── App.config
│ ├── IntegrationTests
│ │ ├── Connections
│ │ │ ├── TestConnection.cs
│ │ │ ├── TestConnectionFactory.cs
│ │ │ ├── TestPooledConnectionFactory.cs
│ │ │ └── TestSimpleConnectionFactory.cs
│ │ ├── Inheritance
│ │ │ ├── TestInheritanceWithAbstractBaseClass.cs
│ │ │ └── TestInheritanceWithConcreteBaseClass.cs
│ │ ├── Linq
│ │ │ ├── LinqDomain.cs
│ │ │ ├── LinqExtensionsTests.cs
│ │ │ ├── LinqTestsBase.cs
│ │ │ ├── MapReduceTests.cs
│ │ │ ├── MongoQueryProviderTests.cs
│ │ │ └── MongoQueryTests.cs
│ │ ├── SecondServer
│ │ │ ├── TestAuthentication.cs
│ │ │ └── TestPooledConnectionFactory.cs
│ │ ├── TestCollection.cs
│ │ ├── TestCollectionMetaData.cs
│ │ ├── TestCollectionSafeMode.cs
│ │ ├── TestCollection_1.cs
│ │ ├── TestConcurrency.cs
│ │ ├── TestCursor.cs
│ │ ├── TestDatabase.cs
│ │ ├── TestDatabaseJavascript.cs
│ │ ├── TestDatabaseMetaData.cs
│ │ ├── TestMapReduce.cs
│ │ └── TestMongo.cs
│ ├── MongoDB.Tests.csproj
│ ├── MongoTestBase.cs
│ ├── Test-Data
│ │ ├── tests.binary.txt
│ │ ├── tests.charreads.txt
│ │ ├── tests.reads.txt
│ │ └── tests.smallreads.txt
│ └── UnitTests
│ │ ├── Bson
│ │ ├── BsonTestBase.cs
│ │ ├── TestBsonBinary.cs
│ │ ├── TestBsonReader.cs
│ │ ├── TestBsonWriter.cs
│ │ └── TestRoundTrips.cs
│ │ ├── Configuration
│ │ ├── MongoConfigurationBuilderTests.cs
│ │ ├── MongoConfigurationSectionTests.1.config
│ │ ├── MongoConfigurationSectionTests.2.config
│ │ ├── MongoConfigurationSectionTests.cs
│ │ └── MongoConfigurationTests.cs
│ │ ├── IO
│ │ ├── TestMsgMessage.cs
│ │ └── TestQueryMessage.cs
│ │ ├── Serialization
│ │ ├── Builders
│ │ │ ├── DocumentPropertyTests.cs
│ │ │ ├── EmbeddedClassPropertyTests.cs
│ │ │ ├── EnumerablePropertyTests.cs
│ │ │ ├── ExtendedPropertiesTests.cs
│ │ │ ├── GenericListPropertyTests.cs
│ │ │ ├── PolymorphicObjectTests.cs
│ │ │ └── ValueConversionTests.cs
│ │ ├── Descriptors
│ │ │ ├── DocumentPropertyTests.cs
│ │ │ ├── DotPropertyTests.cs
│ │ │ ├── EmbeddedClassPropertyTests.cs
│ │ │ ├── EnumerablePropertyTests.cs
│ │ │ ├── ExtendedPropertiesTests.cs
│ │ │ ├── GenericListPropertyTests.cs
│ │ │ ├── ModifierTests.cs
│ │ │ ├── OperatorTests.cs
│ │ │ ├── PolymorphicObjectTests.cs
│ │ │ ├── WhereTests.cs
│ │ │ └── ___A1A.tmp
│ │ ├── SerializationFactoryTests.cs
│ │ └── SerializationTestBase.cs
│ │ ├── TestBinary.cs
│ │ ├── TestCode.cs
│ │ ├── TestCodeWScope.cs
│ │ ├── TestConnectionStringBuilder.cs
│ │ ├── TestDBRef.cs
│ │ ├── TestDocument.cs
│ │ ├── TestMo.cs
│ │ ├── TestMongoRegex.cs
│ │ ├── TestMongoServerEndPoint.cs
│ │ ├── TestMongoSymbol.cs
│ │ ├── TestOid.cs
│ │ ├── TestOidGenerator.cs
│ │ ├── TestOp.cs
│ │ └── Util
│ │ └── TestJsonUtils.cs
└── MongoDB
│ ├── AssemblyInfo.cs
│ ├── Attributes
│ ├── MongoAliasAttribute.cs
│ ├── MongoDefaultAttribute.cs
│ ├── MongoIdAttribute.cs
│ └── MongoIgnoreAttribute.cs
│ ├── Binary.cs
│ ├── BinarySubtype.cs
│ ├── Bson
│ ├── BsonDocumentBuilder.cs
│ ├── BsonDocumentDescriptor.cs
│ ├── BsonInfo.cs
│ ├── BsonProperty.cs
│ ├── BsonReader.cs
│ ├── BsonReaderSettings.cs
│ ├── BsonType.cs
│ ├── BsonWriter.cs
│ ├── BsonWriterSettings.cs
│ ├── IBsonObjectBuilder.cs
│ └── IBsonObjectDescriptor.cs
│ ├── Code.cs
│ ├── CodeWScope.cs
│ ├── CollectionMetadata.cs
│ ├── Commands
│ └── MapReduceCommand.cs
│ ├── Configuration
│ ├── Builders
│ │ ├── AutoMappingProfileBuilder.cs
│ │ ├── ClassOverridesBuilder.cs
│ │ ├── IdOverridesBuilder.cs
│ │ ├── MappingStoreBuilder.cs
│ │ └── MemberOverridesBuilder.cs
│ ├── CollectionAdapters
│ │ ├── ArrayCollectionAdapter.cs
│ │ ├── ArrayListCollectionAdapter.cs
│ │ ├── GenericListCollectionAdapter.cs
│ │ ├── GenericSetCollectionAdapter.cs
│ │ └── ICollectionAdapter.cs
│ ├── DictionaryAdapters
│ │ ├── GenericDictionaryDictionaryAdapter.cs
│ │ ├── GenericSortedListDictionaryAdapter.cs
│ │ ├── HashtableDictionaryAdapter.cs
│ │ └── IDictionaryAdapter.cs
│ ├── IdGenerators
│ │ ├── AssignedIdGenerator.cs
│ │ ├── GuidCombGenerator.cs
│ │ ├── IIdGenerator.cs
│ │ └── OidGenerator.cs
│ ├── Mapping
│ │ ├── Auto
│ │ │ ├── AggregateAutoMapper.cs
│ │ │ ├── AutoMapper.cs
│ │ │ ├── AutoMappingProfile.cs
│ │ │ ├── ClassOverrides.cs
│ │ │ ├── ClassOverridesMap.cs
│ │ │ ├── DefaultMemberFinder.cs
│ │ │ ├── IAutoMapper.cs
│ │ │ ├── IAutoMappingProfile.cs
│ │ │ ├── IMemberFinder.cs
│ │ │ ├── IdOverrides.cs
│ │ │ ├── MemberOverrides.cs
│ │ │ └── OverridableAutoMappingProfile.cs
│ │ ├── AutoMappingStore.cs
│ │ ├── Conventions
│ │ │ ├── ConventionProfile.cs
│ │ │ ├── DefaultCollectionAdapterConvention.cs
│ │ │ ├── DefaultDefaultValueConvention.cs
│ │ │ ├── DefaultDictionaryAdapterConvention.cs
│ │ │ ├── DefaultIdGeneratorConvention.cs
│ │ │ ├── DefaultIdUnsavedValueConvention.cs
│ │ │ ├── DelegateAliasConvention.cs
│ │ │ ├── DelegateCollectionNameConvention.cs
│ │ │ ├── DelegateDiscriminatorAliasConvention.cs
│ │ │ ├── DelegateDiscriminatorConvention.cs
│ │ │ ├── DelegateExtendedPropertiesConvention.cs
│ │ │ ├── DelegateIdConvention.cs
│ │ │ ├── IAliasConvention.cs
│ │ │ ├── ICollectionAdapterConvention.cs
│ │ │ ├── ICollectionNameConvention.cs
│ │ │ ├── IDefaultValueConvention.cs
│ │ │ ├── IDictionaryAdapterConvention.cs
│ │ │ ├── IDiscriminatorAliasConvention.cs
│ │ │ ├── IDiscriminatorConvention.cs
│ │ │ ├── IExtendedPropertiesConvention.cs
│ │ │ ├── IIdConvention.cs
│ │ │ ├── IIdGeneratorConvention.cs
│ │ │ ├── IIdUnsavedValueConvention.cs
│ │ │ └── MemberFinderBase.cs
│ │ ├── IMappingStore.cs
│ │ ├── Model
│ │ │ ├── ClassMap.cs
│ │ │ ├── ClassMapBase.cs
│ │ │ ├── CollectionMemberMap.cs
│ │ │ ├── DictionaryMemberMap.cs
│ │ │ ├── ExtendedPropertiesMap.cs
│ │ │ ├── IClassMap.cs
│ │ │ ├── IdMap.cs
│ │ │ ├── MemberMapBase.cs
│ │ │ ├── PersistentMemberMap.cs
│ │ │ └── SubClassMap.cs
│ │ └── Util
│ │ │ ├── MemberReflectionOptimizer.cs
│ │ │ └── ValueConverter.cs
│ ├── MongoConfiguration.cs
│ ├── MongoConfigurationBuilder.cs
│ ├── MongoConfigurationSection.cs
│ └── Section
│ │ ├── ConnectionCollection.cs
│ │ └── ConnectionElement.cs
│ ├── Connections
│ ├── Connection.cs
│ ├── ConnectionFactoryBase.cs
│ ├── ConnectionFactoryFactory.cs
│ ├── IConnectionFactory.cs
│ ├── PooledConnectionFactory.cs
│ ├── RawConnection.cs
│ └── SimpleConnectionFactory.cs
│ ├── Cursor_1.cs
│ ├── DBRef.cs
│ ├── DatabaseJavascript.cs
│ ├── DatabaseMetadata.cs
│ ├── Document.cs
│ ├── Exceptions
│ ├── IdGenerationException.cs
│ ├── InvalidQueryException.cs
│ ├── MongoCommandException.cs
│ ├── MongoConnectionException.cs
│ ├── MongoDuplicateKeyException.cs
│ ├── MongoDuplicateKeyUpdateException.cs
│ ├── MongoException.cs
│ ├── MongoMapReduceException.cs
│ ├── MongoOperationException.cs
│ └── UnmappedMemberException.cs
│ ├── ICursor_1.cs
│ ├── IMongo.cs
│ ├── IMongoCollection_1.cs
│ ├── IMongoDatabase.cs
│ ├── IndexOrder.cs
│ ├── Linq
│ ├── DocumentQuery.cs
│ ├── ExecutionBuilder.cs
│ ├── Expressions
│ │ ├── AggregateExpression.cs
│ │ ├── AggregateSubqueryExpression.cs
│ │ ├── AggregateType.cs
│ │ ├── Alias.cs
│ │ ├── AliasedExpression.cs
│ │ ├── CollectionExpression.cs
│ │ ├── ExpressionComparer.cs
│ │ ├── ExpressionVisitor.cs
│ │ ├── FieldDeclaration.cs
│ │ ├── FieldExpression.cs
│ │ ├── MongoExpression.cs
│ │ ├── MongoExpressionComparer.cs
│ │ ├── MongoExpressionExtensions.cs
│ │ ├── MongoExpressionType.cs
│ │ ├── MongoExpressionVisitor.cs
│ │ ├── OrderExpression.cs
│ │ ├── OrderType.cs
│ │ ├── ProjectionExpression.cs
│ │ ├── ScalarExpression.cs
│ │ ├── SelectExpression.cs
│ │ └── SubqueryExpression.cs
│ ├── Grouping.cs
│ ├── IMongoQueryable.cs
│ ├── MongoQuery.cs
│ ├── MongoQueryObject.cs
│ ├── MongoQueryProvider.cs
│ ├── MongoQueryable.cs
│ └── Translators
│ │ ├── AggregateChecker.cs
│ │ ├── AggregateRewriter.cs
│ │ ├── DeclaredAliasGatherer.cs
│ │ ├── DocumentFormatter.cs
│ │ ├── ExpressionReplacer.cs
│ │ ├── FieldBinder.cs
│ │ ├── FieldGatherer.cs
│ │ ├── FieldMapper.cs
│ │ ├── FieldProjector.cs
│ │ ├── JavascriptFormatter.cs
│ │ ├── MapReduceFinalizerFunctionBuilder.cs
│ │ ├── MapReduceMapFunctionBuilder.cs
│ │ ├── MapReduceReduceFunctionBuilder.cs
│ │ ├── MongoQueryObjectBuilder.cs
│ │ ├── Nominator.cs
│ │ ├── OrderByRewriter.cs
│ │ ├── PartialEvaluator.cs
│ │ ├── ProjectionBuilder.cs
│ │ ├── QueryBinder.cs
│ │ ├── QueryDuplicator.cs
│ │ ├── RedundantFieldRemover.cs
│ │ ├── RedundantSubqueryRemover.cs
│ │ └── SubqueryRemover.cs
│ ├── LinqExtensions.cs
│ ├── MapReduce.cs
│ ├── Mo.cs
│ ├── Mongo.cs
│ ├── MongoCollection_1.cs
│ ├── MongoConnectionStringBuilder.cs
│ ├── MongoDB.csproj
│ ├── MongoDatabase.cs
│ ├── MongoMaxKey.cs
│ ├── MongoMinKey.cs
│ ├── MongoRegex.cs
│ ├── MongoRegexOption.cs
│ ├── MongoServerEndPoint.cs
│ ├── MongoSymbol.cs
│ ├── Obsolete
│ ├── Cursor.cs
│ ├── ICursor.cs
│ ├── IMongoCollection.cs
│ └── MongoCollection.cs
│ ├── Oid.cs
│ ├── Op.cs
│ ├── Protocol
│ ├── DeleteMessage.cs
│ ├── GetMoreMessage.cs
│ ├── IRequestMessage.cs
│ ├── InsertMessage.cs
│ ├── KillCursorsMessage.cs
│ ├── MessageBase.cs
│ ├── MessageHeader.cs
│ ├── MsgMessage.cs
│ ├── OpCode.cs
│ ├── QueryMessage.cs
│ ├── ReplyMessage.cs
│ ├── RequestMessageBase.cs
│ └── UpdateMessage.cs
│ ├── QueryOptions.cs
│ ├── Results
│ ├── CommandResultBase.cs
│ ├── FindAndModifyResult.cs
│ └── MapReduceResult.cs
│ ├── Serialization
│ ├── BsonClassMapBuilder.cs
│ ├── BsonClassMapDescriptor.cs
│ ├── Builders
│ │ ├── ArrayBuilder.cs
│ │ ├── ConcreteClassMapBuilder.cs
│ │ ├── DictionaryBuilder.cs
│ │ ├── DocumentBuilder.cs
│ │ ├── IObjectBuilder.cs
│ │ ├── PolymorphicClassMapBuilder.cs
│ │ └── PropertyDescriptor.cs
│ ├── ClassMapObjectDescriptorAdapter.cs
│ ├── Descriptors
│ │ ├── ArrayDescriptor.cs
│ │ ├── BsonPropertyValue.cs
│ │ ├── ClassMapPropertyDescriptor.cs
│ │ ├── ClassMapPropertyDescriptorBase.cs
│ │ ├── DictionaryPropertyDescriptor.cs
│ │ ├── DocumentClassMapPropertyDescriptor.cs
│ │ ├── DocumentPropertyDescriptor.cs
│ │ ├── ExampleClassMapPropertyDescriptor.cs
│ │ └── IPropertyDescriptor.cs
│ ├── DocumentObjectDescriptorAdapter.cs
│ ├── IObjectDescriptor.cs
│ ├── ISerializationFactory.cs
│ └── SerializationFactory.cs
│ ├── UpdateFlags.cs
│ └── Util
│ ├── ErrorTranslator.cs
│ ├── Inflector.cs
│ ├── JsonFormatter.cs
│ ├── MongoHash.cs
│ ├── OidGenerator.cs
│ ├── ReflectionExtensions.cs
│ ├── ScopedDictionary.cs
│ └── TypeHelper.cs
└── tools
├── Benchmark
├── AssemblyInfo.cs
├── Benchmark.csproj
└── Main.cs
└── MSBuild.Community.Tasks
├── ICSharpCode.SharpZipLib.dll
├── MSBuild.Community.Tasks.Targets
└── MSBuild.Community.Tasks.dll
/.gitignore:
--------------------------------------------------------------------------------
1 | *~
2 | *.*~
3 | *.mdb
4 | *.pdb
5 | *.pidb
6 | *.user
7 | *.ncb
8 | *.aps
9 | *.suo
10 | *.userprefs
11 | *.usertasks
12 | *.orig
13 | *.cache
14 | *.ReSharper
15 |
16 | bin/
17 | obj/
18 | deploy/
19 | test-results/
20 | test-results/
21 |
22 | /Release/*
23 | /_UpgradeReport_Files/*
24 |
25 | /redist/*.zip
26 |
27 | *_ReSharper.*/**
28 |
--------------------------------------------------------------------------------
/AssemblyInfoGlobal.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 |
3 | // Information about this assembly is defined by the following attributes.
4 | // Change them to the values specific to your project.
5 |
6 | [assembly: AssemblyConfiguration("")]
7 | [assembly: AssemblyCompany("MongoDB-CSharp")]
8 | [assembly: AssemblyCopyright("MongoDB-CSharp")]
9 | [assembly: AssemblyTrademark("")]
10 | [assembly: AssemblyCulture("")]
11 |
12 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
13 | // The form "{Major}.{Minor}.*" will automatically update the build and revision,
14 | // and "{Major}.{Minor}.{Build}.*" will update just the revision.
15 |
16 | [assembly: AssemblyVersion("0.90.0.1")]
17 |
--------------------------------------------------------------------------------
/StrongName.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samus/mongodb-csharp/6397a0f032f24227894afa6ec37e92dd18dd0b3f/StrongName.snk
--------------------------------------------------------------------------------
/examples/Simple/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 |
4 | // Information about this assembly is defined by the following attributes.
5 | // Change them to the values specific to your project.
6 |
7 | [assembly: AssemblyTitle("Simple")]
8 | [assembly: AssemblyDescription("")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("")]
12 | [assembly: AssemblyCopyright("")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision,
18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision.
19 |
20 | [assembly: AssemblyVersion("1.0.*")]
21 |
22 | // The following attributes are used to specify the signing key for the assembly,
23 | // if desired. See the Mono documentation for more information about signing.
24 |
25 | //[assembly: AssemblyDelaySign(false)]
26 | //[assembly: AssemblyKeyFile("")]
27 |
--------------------------------------------------------------------------------
/examples/Simple/Simple.exe.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/examples/SimpleVB/AssemblyInfo.vb:
--------------------------------------------------------------------------------
1 | Imports System.Reflection
2 | Imports System.Runtime.CompilerServices
3 |
4 | ' Information about this assembly is defined by the following attributes.
5 | ' Change them to the values specific to your project.
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | ' The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17 | ' If the build and revision are set to '*' they will be updated automatically.
18 |
19 |
20 |
21 | ' The following attributes are used to specify the signing key for the assembly,
22 | ' if desired. See the Mono documentation for more information about signing.
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/examples/SimpleVB/SimpleVB.exe.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/redist/Moq.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samus/mongodb-csharp/6397a0f032f24227894afa6ec37e92dd18dd0b3f/redist/Moq.dll
--------------------------------------------------------------------------------
/redist/nunit.framework.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samus/mongodb-csharp/6397a0f032f24227894afa6ec37e92dd18dd0b3f/redist/nunit.framework.dll
--------------------------------------------------------------------------------
/source/MongoDB.GridFS.Tests/GridTestBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Configuration;
3 | using System.Text;
4 |
5 | using NUnit.Framework;
6 |
7 | using MongoDB;
8 |
9 | namespace MongoDB.GridFS
10 | {
11 | public abstract class GridTestBase : MongoTestBase
12 | {
13 | ///
14 | /// Comma separated list of collections to clean at startup.
15 | ///
16 | public abstract string TestFileSystems{get;}
17 |
18 | ///
19 | /// Turns the TestFileSystems string into a comma delimited set of collections
20 | ///
21 | public override string TestCollections {
22 | get {
23 | StringBuilder sb = new StringBuilder();
24 | foreach(string fs in this.TestFileSystems.Split(',')){
25 | sb.Append(fs + ".files,");
26 | sb.Append(fs + ".chunks,");
27 | }
28 | sb.Remove(sb.Length - 1,1); //remove last ,
29 | return sb.ToString();
30 | }
31 | }
32 |
33 | public long CountChunks(string filesystem, Object fileid){
34 | return DB[filesystem + ".chunks"].Count(new Document().Add("files_id", fileid));
35 | }
36 |
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/source/MongoDB.GridFS/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 |
4 | // Information about this assembly is defined by the following attributes.
5 | // Change them to the values specific to your project.
6 |
7 | [assembly: AssemblyTitle("MongoDB.GridFS")]
8 | [assembly: AssemblyDescription("GridFS implementation for MongoDB-CSharp driver")]
9 | [assembly: AssemblyProduct("MongoDB-CSharp")]
10 |
11 | [assembly: InternalsVisibleTo("MongoDB.GridFS.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100ed9e936c4563336be2e14ca802ea727ff49cad3bb1c0b287beed2a9b5eb823c4c44becc80be4bb11dcd7e49d5d6171f68b488853dcbdeb3152ea3db95ba13a70855a715ee21ac76b67f50bcbc93f2e29e409530a00b98fa79b06ac008dd1f4f3582ba6746af3d218b43b70a63254b094be1a2d493590837273f357fc56b2a7a0")]
12 |
--------------------------------------------------------------------------------
/source/MongoDB.GridFS/GridException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.GridFS
4 | {
5 | ///
6 | ///
7 | ///
8 | public class MongoGridFSException : Exception
9 | {
10 | private string filename;
11 | ///
12 | /// Gets the filename.
13 | ///
14 | /// The filename.
15 | public string Filename
16 | {
17 | get { return filename; }
18 | }
19 |
20 | ///
21 | /// Initializes a new instance of the class.
22 | ///
23 | /// The message.
24 | /// The filename.
25 | /// The inner.
26 | public MongoGridFSException(string message, string filename, Exception inner)
27 | : base(message, inner)
28 | {
29 | this.filename = filename;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/IntegrationTests/Connections/TestConnectionFactory.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using MongoDB.Connections;
3 | using NUnit.Framework;
4 |
5 | namespace MongoDB.IntegrationTests.Connections
6 | {
7 | [TestFixture]
8 | public class TestConnectionFactory
9 | {
10 | [TearDown]
11 | public void TearDown (){
12 | ConnectionFactoryFactory.Shutdown ();
13 | }
14 |
15 | [Test]
16 | public void TestGetConnection (){
17 | var connection1 = ConnectionFactoryFactory.GetConnection (string.Empty);
18 | var connection2 = ConnectionFactoryFactory.GetConnection (string.Empty);
19 | Assert.IsNotNull (connection1);
20 | Assert.IsNotNull (connection2);
21 | Assert.AreEqual (1, ConnectionFactoryFactory.PoolCount);
22 | }
23 |
24 | [Test]
25 | public void TestCreatePoolForEachUniqeConnectionString (){
26 | ConnectionFactoryFactory.GetConnection (string.Empty);
27 | ConnectionFactoryFactory.GetConnection (string.Empty);
28 | ConnectionFactoryFactory.GetConnection ("Username=test");
29 | ConnectionFactoryFactory.GetConnection ("Username=test");
30 | ConnectionFactoryFactory.GetConnection ("Server=localhost");
31 | Assert.AreEqual (3, ConnectionFactoryFactory.PoolCount);
32 | }
33 |
34 | [Test]
35 | public void TestExceptionWhenMinimumPoolSizeIsGreaterThenMaximumPoolSize (){
36 | try{
37 | ConnectionFactoryFactory.GetConnection("MinimumPoolSize=50; MaximumPoolSize=10");
38 | }catch(ArgumentException){
39 | }catch(Exception){
40 | Assert.Fail("Wrong exception thrown");
41 | }
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/IntegrationTests/Connections/TestSimpleConnectionFactory.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Connections;
2 | using NUnit.Framework;
3 |
4 | namespace MongoDB.IntegrationTests.Connections
5 | {
6 | [TestFixture]
7 | public class TestSimpleConnectionFactory
8 | {
9 | [Test]
10 | public void TestCanOpenConnection(){
11 | using(var simpleConnectionFactory = new SimpleConnectionFactory(string.Empty))
12 | using(var connection = simpleConnectionFactory.Open()){
13 | Assert.IsNotNull(connection);
14 | }
15 | }
16 |
17 | public void TestCanCloseConnection(){
18 | using(var simpleConnectionFactory = new SimpleConnectionFactory(string.Empty))
19 | using(var connection = simpleConnectionFactory.Open())
20 | {
21 | simpleConnectionFactory.Close(connection);
22 | Assert.IsFalse(connection.IsConnected);
23 | }
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/IntegrationTests/Linq/LinqDomain.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | using MongoDB.Attributes;
5 |
6 | namespace MongoDB.IntegrationTests.Linq
7 | {
8 | public class Person
9 | {
10 | [MongoAlias("fn")]
11 | public string FirstName { get; set; }
12 |
13 | [MongoAlias("ln")]
14 | public string LastName { get; set; }
15 |
16 | [MongoAlias("age")]
17 | public int Age { get; set; }
18 |
19 | [MongoAlias("add")]
20 | public Address PrimaryAddress { get; set; }
21 |
22 | [MongoAlias("otherAdds")]
23 | public List Addresses { get; set; }
24 |
25 | [MongoAlias("emps")]
26 | public int[] EmployerIds { get; set; }
27 |
28 | public string MidName { get; set; }
29 |
30 | public Oid LinkedId { get; set; }
31 | }
32 |
33 | public class Address
34 | {
35 | [MongoAlias("city")]
36 | public string City { get; set; }
37 |
38 | public bool IsInternational { get; set; }
39 |
40 | public AddressType AddressType { get; set; }
41 | }
42 |
43 | public enum AddressType
44 | {
45 | Company,
46 | Private
47 | }
48 |
49 | public class PersonWrapper
50 | {
51 | public Person Person { get; set; }
52 | public string Name { get; set; }
53 |
54 | public PersonWrapper(Person person, string name)
55 | {
56 | Person = person;
57 | Name = name;
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/IntegrationTests/Linq/LinqTestsBase.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace MongoDB.IntegrationTests.Linq
4 | {
5 | public class LinqTestsBase : MongoTestBase
6 | {
7 | public override string TestCollections
8 | {
9 | get { return "people"; }
10 | }
11 |
12 | protected IMongoCollection Collection;
13 | protected IMongoCollection DocumentCollection;
14 |
15 | [SetUp]
16 | public virtual void TestSetup()
17 | {
18 | Collection = DB.GetCollection("people");
19 | DocumentCollection = DB.GetCollection("people");
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/IntegrationTests/SecondServer/TestPooledConnectionFactory.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Connections;
2 | using NUnit.Framework;
3 |
4 | namespace MongoDB.IntegrationTests.SecondServer
5 | {
6 | [Ignore("Run manually since it needs a second server")]
7 | [TestFixture]
8 | public class TestPooledConnectionFactory
9 | {
10 | [TestFixtureSetUp]
11 | public void SetUp()
12 | {
13 | ConnectionFactoryFactory.Shutdown();
14 | }
15 |
16 | [Test]
17 | public void TestServerCirculationWorks()
18 | {
19 | var builder = new MongoConnectionStringBuilder();
20 | builder.AddServer("localhost", 27017);
21 | builder.AddServer("localhost", 27018);
22 | using(var pool = new PooledConnectionFactory(builder.ToString()))
23 | {
24 | var connection1 = pool.Open();
25 | var connection2 = pool.Open();
26 | var connection3 = pool.Open();
27 | var connection4 = pool.Open();
28 | var connection5 = pool.Open();
29 | Assert.AreEqual(27017, connection1.EndPoint.Port);
30 | Assert.AreEqual(27018, connection2.EndPoint.Port);
31 | Assert.AreEqual(27017, connection3.EndPoint.Port);
32 | Assert.AreEqual(27018, connection4.EndPoint.Port);
33 | Assert.AreEqual(27017, connection5.EndPoint.Port);
34 | }
35 | }
36 |
37 | }
38 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/IntegrationTests/TestMongo.cs:
--------------------------------------------------------------------------------
1 | using System.Configuration;
2 | using NUnit.Framework;
3 | using System.Linq;
4 |
5 | namespace MongoDB.IntegrationTests
6 | {
7 | [TestFixture]
8 | public class TestMongo
9 | {
10 | private readonly string _connectionString = ConfigurationManager.AppSettings["tests"];
11 |
12 | [Test]
13 | public void TestDefaults(){
14 | using(var m = new Mongo())
15 | {
16 | //Connection string not needed since connect not called and it would screw up the test.
17 | Assert.AreEqual(string.Empty, m.ConnectionString);
18 | }
19 | }
20 |
21 | [Test]
22 | public void TestExplicitConnection(){
23 | using(var m = new Mongo(_connectionString))
24 | Assert.IsTrue(m.TryConnect());
25 | }
26 |
27 | [Test]
28 | [ExpectedException(typeof(MongoConnectionException))]
29 | public void TestConnectThrowIfConnectionFailed(){
30 | using(var m = new Mongo("Server=notexists"))
31 | m.Connect();
32 | }
33 |
34 | [Test]
35 | public void TestTryConnectDoesNotThrowIfConnectionFailed(){
36 | using(var m = new Mongo("Server=notexists"))
37 | Assert.IsFalse(m.TryConnect());
38 | }
39 |
40 | [Test]
41 | [ExpectedException(typeof(MongoConnectionException))]
42 | public void TestThatConnectMustBeCalled(){
43 | using(var m = new Mongo(_connectionString))
44 | {
45 | var db = m["admin"];
46 | db["$cmd"].FindOne(new Document().Add("listDatabases", 1.0));
47 | }
48 | }
49 |
50 | [Test]
51 | public void TestGetDatabasesReturnsSomething()
52 | {
53 | using(var m = new Mongo(_connectionString))
54 | {
55 | m.Connect();
56 | var databaseCount = m.GetDatabases().Count();
57 |
58 | Assert.Greater(databaseCount, 1);
59 | }
60 | }
61 |
62 | public void TestDoesNotThrowIfDisposeIsCalledTwice(){
63 | using(var m = new Mongo(_connectionString))
64 | {
65 | m.Dispose();
66 | }
67 | }
68 | }
69 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/MongoTestBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Configuration;
3 | using NUnit.Framework;
4 | using MongoDB.Configuration;
5 |
6 | namespace MongoDB
7 | {
8 | public abstract class MongoTestBase
9 | {
10 | public Mongo Mongo{get;set;}
11 |
12 | public IMongoDatabase DB{
13 | get{
14 | return this.Mongo["tests"];
15 | }
16 | }
17 |
18 | ///
19 | /// Comma separated list of collections to clean at startup.
20 | ///
21 | public abstract string TestCollections{get;}
22 |
23 | ///
24 | /// Override to add custom initialization code.
25 | ///
26 | public virtual void OnInit(){}
27 |
28 | ///
29 | /// Override to add custom code to invoke during the test end.
30 | ///
31 | public virtual void OnDispose(){}
32 |
33 | ///
34 | /// Sets up the test environment. You can either override this OnInit to add custom initialization.
35 | ///
36 | [TestFixtureSetUp]
37 | public virtual void Init(){
38 | this.Mongo = new Mongo(GetConfiguration().BuildConfiguration());
39 | this.Mongo.Connect();
40 | CleanDB();
41 | OnInit();
42 | }
43 |
44 |
45 | [TestFixtureTearDown]
46 | public virtual void Dispose(){
47 | OnDispose();
48 | this.Mongo.Disconnect();
49 | }
50 |
51 | protected void CleanDB(){
52 | foreach(string col in this.TestCollections.Split(',')){
53 | DB["$cmd"].FindOne(new Document(){{"drop", col.Trim()}});
54 | //Console.WriteLine("Dropping " + col);
55 | }
56 | }
57 |
58 | protected virtual MongoConfigurationBuilder GetConfiguration()
59 | {
60 | var builder = new MongoConfigurationBuilder();
61 | builder.ReadConnectionStringFromAppSettings("tests");
62 | return builder;
63 | }
64 | }
65 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/Test-Data/tests.binary.txt:
--------------------------------------------------------------------------------
1 | this is some data to test with.
2 | this is some data to test with.
3 | this is some data to test with.
4 | this is some data to test with.
5 | this is some data to test with.
6 | this is some data to test with.
7 | this is some data to test with.
8 | this is some data to test with.
9 | this is some data to test with.
10 | this is some data to test with.
11 | this is some data to test with.
12 | this is some data to test with.
13 | this is some data to test with.
14 | this is some data to test with.
15 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/Test-Data/tests.charreads.txt:
--------------------------------------------------------------------------------
1 | { "_id" : "4abbed9d1d8b0f0218000001", "test" : "1234£56" }
2 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/Test-Data/tests.smallreads.txt:
--------------------------------------------------------------------------------
1 | { "_id" : "4a6a1f7f58760000000028ed", "x" : 4, "j" : 1 }
2 | { "_id" : "4a6a1f7f58760000000028ee", "x" : 4, "j" : 2 }
3 | { "_id" : "4a6a1f7f58760000000028ef", "x" : 4, "j" : 3 }
4 | { "_id" : "4a6a1f7f58760000000028f0", "x" : 4, "j" : 4 }
5 | { "_id" : "4ae6157d982c000000000c39", "x" : 4, "j" : 5, "n" : 1}
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Bson/BsonTestBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using MongoDB.Bson;
4 |
5 | namespace MongoDB.UnitTests.Bson
6 | {
7 | public abstract class BsonTestBase
8 | {
9 | protected string Serialize(Document document)
10 | {
11 | return Serialize(document, new BsonWriterSettings());
12 | }
13 |
14 | protected string Serialize(Document document, BsonWriterSettings settings)
15 | {
16 | using(var mem = new MemoryStream())
17 | {
18 | var writer = new BsonWriter(mem, settings);
19 | writer.WriteObject(document);
20 | writer.Flush();
21 | return Convert.ToBase64String(mem.ToArray());
22 | }
23 | }
24 |
25 | protected Document Deserialize(string base64){
26 | return Deserialize(base64, new BsonReaderSettings());
27 | }
28 |
29 | protected Document Deserialize(string base64, BsonReaderSettings settings)
30 | {
31 | using(var mem = new MemoryStream(Convert.FromBase64String(base64)))
32 | {
33 | var reader = new BsonReader(mem, settings);
34 | return (Document)reader.ReadObject();
35 | }
36 | }
37 |
38 |
39 | protected byte[] HexToBytes(string hex)
40 | {
41 | //TODO externalize somewhere.
42 | if (hex.Length % 2 == 1)
43 | {
44 | Console.WriteLine("uneven number of hex pairs.");
45 | hex = "0" + hex;
46 | }
47 | var numberChars = hex.Length;
48 | var bytes = new byte[numberChars / 2];
49 | for (var i = 0; i < numberChars; i += 2)
50 | try
51 | {
52 | bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
53 | }
54 | catch
55 | {
56 | //failed to convert these 2 chars, they may contain illegal charracters
57 | bytes[i / 2] = 0;
58 | }
59 | return bytes;
60 | }
61 | }
62 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Bson/TestBsonBinary.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using MongoDB.Bson;
4 | using NUnit.Framework;
5 |
6 | namespace MongoDB.UnitTests.Bson
7 | {
8 | [TestFixture]
9 | public class TestBsonBinary
10 | {
11 | protected static byte[] DecodeHex(string val){
12 | var numberChars = val.Length;
13 |
14 | var bytes = new byte[numberChars/2];
15 | for(var i = 0; i < numberChars; i += 2)
16 | try{
17 | bytes[i/2] = Convert.ToByte(val.Substring(i, 2), 16);
18 | }
19 | catch{
20 | //failed to convert these 2 chars, they may contain illegal charracters
21 | bytes[i/2] = 0;
22 | }
23 | return bytes;
24 | }
25 |
26 | [Test]
27 | public void TestBinaryRead(){
28 | const string hex = "28000000075f6964004b1971811d8b0f00c0000000056461746100070000000203000000e188b400";
29 |
30 | var data = DecodeHex(hex);
31 | var inmem = new MemoryStream(data);
32 | var inreader = new BsonReader(inmem,new BsonDocumentBuilder());
33 | var indoc = inreader.Read();
34 |
35 | var outmem = new MemoryStream();
36 | var outwriter = new BsonWriter(outmem, new BsonDocumentDescriptor());
37 | outwriter.WriteObject(indoc);
38 | var outdata = outmem.ToArray();
39 | var outhex = BitConverter.ToString(outdata);
40 | outhex = outhex.Replace("-", "");
41 |
42 | Assert.AreEqual(hex, outhex.ToLower());
43 | }
44 |
45 | [Test]
46 | public void TestRoundTrip(){
47 | var idoc = new Document{{"b", new Binary(new[]{(byte)1, (byte)2})}};
48 |
49 | var stream = new MemoryStream();
50 | var writer = new BsonWriter(stream, new BsonDocumentDescriptor());
51 | writer.WriteObject(idoc);
52 |
53 | stream.Seek(0, SeekOrigin.Begin);
54 | var reader = new BsonReader(stream,new BsonDocumentBuilder());
55 | var odoc = reader.Read();
56 |
57 | Assert.AreEqual(idoc.ToString(), odoc.ToString());
58 | }
59 | }
60 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Configuration/MongoConfigurationBuilderTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using MongoDB.Configuration;
3 | using NUnit.Framework;
4 |
5 | namespace MongoDB.UnitTests.Configuration
6 | {
7 | [TestFixture]
8 | public class MongoConfigurationBuilderTests
9 | {
10 | private class Person
11 | {
12 | public Guid Id { get; set; }
13 |
14 | public int Age { get; set; }
15 |
16 | public string Name { get; set; }
17 | }
18 |
19 | [Test]
20 | public void Test()
21 | {
22 | var configure = new MongoConfigurationBuilder();
23 |
24 | configure.ConnectionString(cs =>
25 | {
26 | cs.Pooled = true;
27 | });
28 |
29 | configure.Mapping(mapping =>
30 | {
31 | mapping.DefaultProfile(p =>
32 | {
33 | p.AliasesAreCamelCased();
34 | p.CollectionNamesAreCamelCasedAndPlural();
35 | });
36 |
37 | mapping.Map(m =>
38 | {
39 | m.CollectionName("people");
40 | m.Member(x => x.Age).Alias("age");
41 | m.Member(x => x.Name).Alias("name").DefaultValue("something").Ignore();
42 | });
43 | });
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Configuration/MongoConfigurationSectionTests.1.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Configuration/MongoConfigurationSectionTests.2.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Configuration/MongoConfigurationTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using MongoDB.Configuration;
3 | using NUnit.Framework;
4 |
5 | namespace MongoDB.UnitTests.Configuration
6 | {
7 | [TestFixture]
8 | public class MongoConfigurationTests
9 | {
10 | [Test]
11 | public void IsModifiableByDefault()
12 | {
13 | var config = new MongoConfiguration();
14 | Assert.IsTrue(config.IsModifiable);
15 | }
16 |
17 | [Test]
18 | public void IsNotModifiableAfterValidate()
19 | {
20 | var config = new MongoConfiguration();
21 | config.ValidateAndSeal();
22 | Assert.IsFalse(config.IsModifiable);
23 | }
24 |
25 | [Test]
26 | [ExpectedException(typeof(InvalidOperationException))]
27 | public void CanNotChangeConnectionStringAfterValidate()
28 | {
29 | var config = new MongoConfiguration();
30 | config.ValidateAndSeal();
31 | config.ConnectionString = "";
32 | }
33 |
34 | [Test]
35 | [ExpectedException(typeof(InvalidOperationException))]
36 | public void CanNotChangeMappingStoreAfterValidate()
37 | {
38 | var config = new MongoConfiguration();
39 | config.ValidateAndSeal();
40 | config.MappingStore = null;
41 | }
42 |
43 | [Test]
44 | [ExpectedException(typeof(InvalidOperationException))]
45 | public void CanNotChangeReadLocalTimeAfterValidate()
46 | {
47 | var config = new MongoConfiguration();
48 | config.ValidateAndSeal();
49 | config.ReadLocalTime = true;
50 | }
51 |
52 | [Test]
53 | [ExpectedException(typeof(InvalidOperationException))]
54 | public void CanNotChangeSerializationFactoryAfterValidate()
55 | {
56 | var config = new MongoConfiguration();
57 | config.ValidateAndSeal();
58 | config.SerializationFactory = null;
59 | }
60 | }
61 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/IO/TestMsgMessage.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using MongoDB.Protocol;
4 | using NUnit.Framework;
5 |
6 | namespace MongoDB.UnitTests.IO
7 | {
8 | [TestFixture()]
9 | public class TestMsgMessage
10 | {
11 |
12 | [Test()]
13 | public void TestAllBytesWritten(){
14 | MsgMessage msg = new MsgMessage();
15 | msg.Header.RequestId = 1;
16 | msg.Header.ResponseTo = 11;
17 | Assert.AreEqual(16,msg.Header.MessageLength);
18 |
19 | msg.Message = "A";
20 | MemoryStream buffer = new MemoryStream(18);
21 | msg.Write(buffer);
22 |
23 | Byte[] output = buffer.ToArray();
24 | String hexdump = BitConverter.ToString(output);
25 | //Console.WriteLine("Dump: " + hexdump);
26 |
27 | Assert.IsTrue(output.Length > 0);
28 | Assert.AreEqual(output.Length, msg.Header.MessageLength);
29 | Assert.AreEqual("12-00-00-00-01-00-00-00-0B-00-00-00-E8-03-00-00-41-00", hexdump);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/IO/TestQueryMessage.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using MongoDB.Bson;
4 | using MongoDB.Protocol;
5 | using NUnit.Framework;
6 |
7 | namespace MongoDB.UnitTests.IO
8 | {
9 | [TestFixture]
10 | public class TestQueryMessage
11 | {
12 | [Test]
13 | public void TestAllBytesWritten()
14 | {
15 | var query = new Document {{"col1", 1}};
16 |
17 | var msg = new QueryMessage(new BsonWriterSettings(), query, "TestDB.TestCol");
18 | var buffer = new MemoryStream();
19 | msg.Write(buffer);
20 |
21 | var output = buffer.ToArray();
22 | var hexdump = BitConverter.ToString(output);
23 | //Console.WriteLine("Dump: " + hexdump);
24 |
25 | Assert.IsTrue(output.Length > 0);
26 | Assert.AreEqual("3A-00-00-00-00-00-00-00-00-00-00-00-D4-07-00-00-00-00-00-00-54-65-73-74-44-42-2E-54-65-73-74-43-6F-6C-00-00-00-00-00-00-00-00-00-0F-00-00-00-10-63-6F-6C-31-00-01-00-00-00-00",
27 | hexdump);
28 | }
29 |
30 | [Test]
31 | public void TestWriteMessageTwice()
32 | {
33 | const string expectedHex = "3A-00-00-00-00-00-00-00-00-00-00-00-D4-07-00-00-00-00-00-00-54-65-73-74-44-42-2E-54-65-73-74-43-6F-6C-00-00-00-00-00-00-00-00-00-0F-00-00-00-10-63-6F-6C-31-00-01-00-00-00-00";
34 | var query = new Document();
35 | query.Add("col1", 1);
36 |
37 | var msg = new QueryMessage(new BsonWriterSettings(), query, "TestDB.TestCol");
38 | var buffer = new MemoryStream();
39 | msg.Write(buffer);
40 |
41 | var output = buffer.ToArray();
42 | var hexdump = BitConverter.ToString(output);
43 |
44 | var buffer2 = new MemoryStream();
45 | msg.Write(buffer2);
46 |
47 | var output2 = buffer.ToArray();
48 | var hexdump2 = BitConverter.ToString(output2);
49 |
50 | Assert.AreEqual(expectedHex, hexdump);
51 | Assert.AreEqual(hexdump, hexdump2);
52 | }
53 | }
54 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Builders/DocumentPropertyTests.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace MongoDB.UnitTests.Serialization.Builders
4 | {
5 | [TestFixture]
6 | public class DocumentPropertyTests : SerializationTestBase
7 | {
8 | public class DocumentProperty
9 | {
10 | public Document A { get; set; }
11 | }
12 |
13 | [Test]
14 | public void CanDeserializeADocumentProperty()
15 | {
16 | //{ A: { B: "b" } }
17 | const string bson = "FgAAAANBAA4AAAACQgACAAAAYgAAAA==";
18 | var documentProperty = Deserialize(bson);
19 | Assert.IsNotNull(documentProperty);
20 | Assert.IsNotNull(documentProperty.A);
21 | Assert.AreEqual("b", documentProperty.A["B"]);
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Builders/EmbeddedClassPropertyTests.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace MongoDB.UnitTests.Serialization.Builders
4 | {
5 | [TestFixture]
6 | public class EmbeddedClassPropertyTests : SerializationTestBase
7 | {
8 | public class SimpleObject
9 | {
10 | public string A { get; set; }
11 | public string B { get; set; }
12 | public SimpleObjectC C { get; set; }
13 | }
14 |
15 | public class SimpleObjectC
16 | {
17 | public string D { get; set; }
18 | }
19 |
20 | [Test]
21 | public void CanDeserializeASimpleObject()
22 | {
23 | //{ A: "a", B: "b", C: { D: "d" } }
24 | const string bson = "KAAAAAJBAAIAAABhAAJCAAIAAABiAANDAA4AAAACRAACAAAAZAAAAA==";
25 | var simpleObject = Deserialize(bson);
26 | Assert.IsNotNull(simpleObject);
27 | Assert.AreEqual("a", simpleObject.A);
28 | Assert.AreEqual("b", simpleObject.B);
29 | Assert.IsNotNull(simpleObject.C);
30 | Assert.AreEqual("d", simpleObject.C.D);
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Builders/ExtendedPropertiesTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using NUnit.Framework;
3 |
4 | namespace MongoDB.UnitTests.Serialization.Builders
5 | {
6 | [TestFixture]
7 | public class ExtendedPropertiesTests : SerializationTestBase
8 | {
9 | public class IDictionaryProperty
10 | {
11 | public IDictionary ExtendedProperties { get; private set; }
12 | }
13 |
14 | [Test]
15 | public void CanDeserializePropertiesWithoutMapsUsingAnIDictionary()
16 | {
17 | //{ A: { B: "b" } }
18 | const string bson = "FgAAAANBAA4AAAACQgACAAAAYgAAAA==";
19 | var prop = Deserialize(bson);
20 | Assert.IsNotNull(prop);
21 | Assert.IsNotNull(prop.ExtendedProperties["A"]);
22 | Assert.AreEqual("b", ((Document)prop.ExtendedProperties["A"])["B"]);
23 | }
24 |
25 | public class DictionaryProperty
26 | {
27 | public Dictionary ExtendedProperties { get; private set; }
28 | }
29 |
30 | [Test]
31 | public void CanDeserializePropertiesWithoutMapsUsingADictionary()
32 | {
33 | //{ A: { B: "b" } }
34 | const string bson = "FgAAAANBAA4AAAACQgACAAAAYgAAAA==";
35 | var prop = Deserialize(bson);
36 | Assert.IsNotNull(prop);
37 | Assert.IsNotNull(prop.ExtendedProperties["A"]);
38 | Assert.AreEqual("b", ((Document)prop.ExtendedProperties["A"])["B"]);
39 | }
40 |
41 | public class DocumentProperty
42 | {
43 | public Document ExtendedProperties { get; private set; }
44 | }
45 |
46 | [Test]
47 | public void CanDeserializePropertiesWithoutMapsUsingADocument()
48 | {
49 | //{ A: { B: "b" } }
50 | const string bson = "FgAAAANBAA4AAAACQgACAAAAYgAAAA==";
51 | var prop = Deserialize(bson);
52 | Assert.IsNotNull(prop);
53 | Assert.IsNotNull(prop.ExtendedProperties["A"]);
54 | Assert.AreEqual("b", ((Document)prop.ExtendedProperties["A"])["B"]);
55 | }
56 | }
57 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Builders/ValueConversionTests.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace MongoDB.UnitTests.Serialization.Builders
4 | {
5 | [TestFixture]
6 | public class ValueConversionTests : SerializationTestBase
7 | {
8 | public class SimpleObject
9 | {
10 | public bool A { get; set; }
11 | }
12 |
13 | [Test]
14 | public void CanConvertSimpleValues(){
15 | var bson = Serialize(new Document("A", 1.0));
16 | var result = Deserialize(bson);
17 |
18 | Assert.IsTrue(result.A);
19 | }
20 | }
21 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/DocumentPropertyTests.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace MongoDB.UnitTests.Serialization.Descriptors
4 | {
5 | [TestFixture]
6 | public class DocumentPropertyTests : SerializationTestBase
7 | {
8 | public class DocumentProperty
9 | {
10 | public Document A { get; set; }
11 | }
12 |
13 | [Test]
14 | public void CanSerialize()
15 | {
16 | var bson = Serialize(new DocumentProperty() { A = new Document("B", "b") });
17 | Assert.AreEqual("FgAAAANBAA4AAAACQgACAAAAYgAAAA==", bson);
18 | }
19 |
20 | [Test]
21 | public void CanSerializeUsingAnonymousType()
22 | {
23 | var bson = Serialize(new { A = new { B = "b" } });
24 | Assert.AreEqual("FgAAAANBAA4AAAACQgACAAAAYgAAAA==", bson);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/DotPropertyTests.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Attributes;
2 | using NUnit.Framework;
3 | using System.Collections.Generic;
4 |
5 | namespace MongoDB.UnitTests.Serialization.Descriptors
6 | {
7 | [TestFixture]
8 | public class DotPropertyTests : SerializationTestBase
9 | {
10 | public class DotClass
11 | {
12 | [MongoAlias("a")]
13 | public List A { get; set; }
14 |
15 | [MongoAlias("c")]
16 | public DotChildC C { get; set; }
17 | }
18 |
19 | public class DotChildA
20 | {
21 | [MongoAlias("b")]
22 | public int B { get; set; }
23 | }
24 |
25 | public class DotChildC
26 | {
27 | [MongoAlias("d")]
28 | public int D { get; set; }
29 | }
30 |
31 | [Test]
32 | public void CanSerializeWithChild()
33 | {
34 | var expected = Serialize(new Document("c.d", 10));
35 | var bson = Serialize(new Document("C.D", 10));
36 | Assert.AreEqual(expected, bson);
37 | }
38 |
39 | [Test]
40 | public void CanSerializeWithChildIndexer()
41 | {
42 | var expected = Serialize(new Document("a.5.b", 10));
43 | var bson = Serialize(new Document("A.5.B", 10));
44 | Assert.AreEqual(expected, bson);
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/EmbeddedClassPropertyTests.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Attributes;
2 | using NUnit.Framework;
3 |
4 | namespace MongoDB.UnitTests.Serialization.Descriptors
5 | {
6 | [TestFixture]
7 | public class EmbeddedClassPropertyTests : SerializationTestBase
8 | {
9 | public class SimpleObject
10 | {
11 | public string A { get; set; }
12 | public string B { get; set; }
13 | public SimpleObjectC C { get; set; }
14 | }
15 | public class SimpleObjectC
16 | {
17 | public string D { get; set; }
18 | }
19 |
20 | [Test]
21 | public void CanSerializeASimpleObject()
22 | {
23 | var bson = Serialize(new SimpleObject() { A = "a", B = "b", C = new SimpleObjectC() { D = "d" } });
24 | Assert.AreEqual("KAAAAAJBAAIAAABhAAJCAAIAAABiAANDAA4AAAACRAACAAAAZAAAAA==", bson);
25 | }
26 |
27 | [Test]
28 | public void CanSerializeASimpleObjectUsingAnAnonymousType()
29 | {
30 | var bson = Serialize(new { A = "a", B = "b", C = new { D = "d" } });
31 | Assert.AreEqual("KAAAAAJBAAIAAABhAAJCAAIAAABiAANDAA4AAAACRAACAAAAZAAAAA==", bson);
32 | }
33 |
34 | [Test]
35 | public void CanSerializeASimpleObjectWithANullProperty()
36 | {
37 | var bson = Serialize(new SimpleObject());
38 | Assert.AreEqual("DgAAAApBAApCAApDAAA=", bson);
39 | }
40 |
41 | public class SuperClass
42 | {
43 | [MongoAlias("a")]
44 | public SuperClassA A { get; set; }
45 | }
46 |
47 | public class SuperClassA
48 | {
49 | [MongoAlias("b")]
50 | public string B { get; set; }
51 | }
52 |
53 | [Test]
54 | public void CanSerializeAnEmbeddedClassPropertyUsingDotSyntaxWhenAliasesExist()
55 | {
56 | var expected = Serialize(new Document("a.b", "b"));
57 | var bson = Serialize(new Document("A.B", "b"));
58 | Assert.AreEqual(expected, bson);
59 | }
60 | }
61 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/EnumerablePropertyTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 | using NUnit.Framework;
4 |
5 | namespace MongoDB.UnitTests.Serialization.Descriptors
6 | {
7 | [TestFixture]
8 | public class EnumerablePropertyTests : SerializationTestBase
9 | {
10 | public class Enumerable
11 | {
12 | public IEnumerable A { get; set; }
13 | }
14 |
15 | [Test]
16 | public void CanDeserializeAnSimpleArrayAsIEnumerable()
17 | {
18 | var e = new Enumerable();
19 | e.A = new ArrayList { 1, 2 };
20 | string bson = Serialize(e);
21 | Assert.AreEqual("GwAAAARBABMAAAAQMAABAAAAEDEAAgAAAAAA", bson);
22 | }
23 |
24 | [Test]
25 | public void CanDeserializeAnSimpleArrayAsIEnumerableUsingAnonymousType()
26 | {
27 | string bson = Serialize(new { A = new[] { 1, 2 } });
28 | Assert.AreEqual("GwAAAARBABMAAAAQMAABAAAAEDEAAgAAAAAA", bson);
29 | }
30 |
31 | public class EnumerableOfIntegers
32 | {
33 | public IEnumerable A { get; set; }
34 | }
35 |
36 | [Test]
37 | public void CanDeserializeAnSimpleArrayAsIEnumerableOfInt()
38 | {
39 | var e = new EnumerableOfIntegers();
40 | e.A = new List { 1, 2 };
41 | string bson = Serialize(e);
42 | Assert.AreEqual("GwAAAARBABMAAAAQMAABAAAAEDEAAgAAAAAA", bson);
43 | }
44 |
45 | [Test]
46 | public void CanDeserializeAnSimpleArrayAsIEnumerableOfIntUsingAnonymousType()
47 | {
48 | string bson = Serialize(new { A = new[] { 1, 2 } });
49 | Assert.AreEqual("GwAAAARBABMAAAAQMAABAAAAEDEAAgAAAAAA", bson);
50 | }
51 | }
52 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/ExtendedPropertiesTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using NUnit.Framework;
3 |
4 | namespace MongoDB.UnitTests.Serialization.Descriptors
5 | {
6 | [TestFixture]
7 | public class ExtendedPropertiesTests : SerializationTestBase
8 | {
9 | public class IDictionaryProperty
10 | {
11 | public IDictionary ExtendedProperties { get; set; }
12 | }
13 |
14 | [Test]
15 | public void CanSerializeUsingIDictionary()
16 | {
17 | var bson = Serialize(new IDictionaryProperty() { ExtendedProperties = new Dictionary { { "A", new Document("B", "b") } } });
18 | Assert.AreEqual("FgAAAANBAA4AAAACQgACAAAAYgAAAA==", bson);
19 | }
20 |
21 | public class DictionaryProperty
22 | {
23 | public Dictionary ExtendedProperties { get; set; }
24 | }
25 |
26 | [Test]
27 | public void CanSerializeUsingDictionary()
28 | {
29 | var bson = Serialize(new DictionaryProperty() { ExtendedProperties = new Dictionary { { "A", new Document("B", "b") } } });
30 | Assert.AreEqual("FgAAAANBAA4AAAACQgACAAAAYgAAAA==", bson);
31 | }
32 |
33 | public class DocumentProperty
34 | {
35 | public Document ExtendedProperties { get; set; }
36 | }
37 |
38 | [Test]
39 | public void CanSerializeUsingDocument()
40 | {
41 | var bson = Serialize(new DocumentProperty() { ExtendedProperties = new Document("A", new Document("B", "b")) });
42 | Assert.AreEqual("FgAAAANBAA4AAAACQgACAAAAYgAAAA==", bson);
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/ModifierTests.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace MongoDB.UnitTests.Serialization.Descriptors
4 | {
5 | [TestFixture]
6 | [Ignore("Currently we do not plan to support this. As an alternative you can use Mo or Linq.")]
7 | public class ModifierTests : SerializationTestBase
8 | {
9 | public class ModifierEntity
10 | {
11 | public int A { get; set; }
12 | }
13 |
14 | [Test]
15 | public void CanSerializeIncrementUsingAnonymousType()
16 | {
17 | var expectedBson = Serialize(new Document("$inc", new Document("A", 1)));
18 | var bson = Serialize(new { A = new Document("$inc", 1) });
19 |
20 | Assert.AreEqual(expectedBson, bson);
21 | }
22 |
23 | [Test]
24 | public void CanSerializeSetUsingAnonymousType()
25 | {
26 | var expectedBson = Serialize(new Document("$set", new Document("A", 4)));
27 | var bson = Serialize(new { A = new Document("$inc", 1) });
28 |
29 | Assert.AreEqual(expectedBson, bson);
30 | }
31 | }
32 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/OperatorTests.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace MongoDB.UnitTests.Serialization.Descriptors
4 | {
5 | [TestFixture]
6 | public class OperatorTests : SerializationTestBase
7 | {
8 | public class OperatorProperty
9 | {
10 | public int A { get; set; }
11 | }
12 |
13 | [Test]
14 | public void CanSerializeWithStandardOperatorUsingAnonymousType()
15 | {
16 | var bson = Serialize(new { A = Op.GreaterThan(12) });
17 | Assert.AreEqual("FgAAAANBAA4AAAAQJGd0AAwAAAAAAA==", bson);
18 | }
19 |
20 | [Test]
21 | public void CanSerializeWithMetaOperatorUsingAnonymousType()
22 | {
23 | var bson = Serialize(new { A = !Op.GreaterThan(12) });
24 |
25 | Assert.AreEqual("IQAAAANBABkAAAADJG5vdAAOAAAAECRndAAMAAAAAAAA", bson);
26 | }
27 |
28 | [Test]
29 | public void CanSerializeWithComplexOperatorsUsingAnonymousType()
30 | {
31 | var bson = Serialize(new { A = Op.GreaterThan(12) & !Op.GreaterThan(24) });
32 |
33 | Assert.AreEqual("KgAAAANBACIAAAAQJGd0AAwAAAADJG5vdAAOAAAAECRndAAYAAAAAAAA", bson);
34 | }
35 | }
36 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/PolymorphicObjectTests.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 | using MongoDB.Configuration.Mapping.Auto;
3 | using MongoDB.Configuration.Mapping;
4 |
5 | namespace MongoDB.UnitTests.Serialization.Descriptors
6 | {
7 | [TestFixture]
8 | public class PolymorphicObjectTests : SerializationTestBase
9 | {
10 | protected override IMappingStore MappingStore
11 | {
12 | get
13 | {
14 | var profile = new AutoMappingProfile();
15 | profile.IsSubClassDelegate = t => t == typeof(ClassA) || t == typeof(ClassB);
16 | var store = new AutoMappingStore(profile);
17 | //eagerly automap so they are known at deserialization time...
18 | store.GetClassMap(typeof(ClassA));
19 | store.GetClassMap(typeof(ClassB));
20 | return store;
21 | }
22 | }
23 |
24 | public abstract class BaseClass
25 | {
26 | public string A { get; set; }
27 | }
28 |
29 | public class ClassA : BaseClass
30 | {
31 | public string B { get; set; }
32 | }
33 |
34 | public class ClassB : BaseClass
35 | {
36 | public string C { get; set; }
37 | }
38 |
39 | [Test]
40 | public void CanDeserializeDirectly()
41 | {
42 | var classB = new ClassB() { A = "a", C = "c" };
43 | string bson = Serialize(classB);
44 | Assert.AreEqual("JgAAAAJfdAAHAAAAQ2xhc3NCAAJBAAIAAABhAAJDAAIAAABjAAA=", bson);
45 | }
46 |
47 | [Test]
48 | public void CanDeserializeDirectlyWithAnonymousType()
49 | {
50 | string bson = Serialize(new { A = "a", C = "c" });
51 | Assert.AreEqual("JgAAAAJfdAAHAAAAQ2xhc3NCAAJBAAIAAABhAAJDAAIAAABjAAA=", bson);
52 | }
53 |
54 | [Test]
55 | public void CanDeserializeIndirectly()
56 | {
57 | var baseClass = new ClassB() { A = "a", C = "c" };
58 | string bson = Serialize(baseClass);
59 | Assert.AreEqual("JgAAAAJfdAAHAAAAQ2xhc3NCAAJBAAIAAABhAAJDAAIAAABjAAA=", bson);
60 | }
61 | }
62 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/WhereTests.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Attributes;
2 | using NUnit.Framework;
3 | using System.Collections.Generic;
4 |
5 | namespace MongoDB.UnitTests.Serialization.Descriptors
6 | {
7 | [TestFixture]
8 | public class WhereTests : SerializationTestBase
9 | {
10 | public class WhereClass
11 | {
12 | [MongoAlias("a")]
13 | public List A { get; set; }
14 |
15 | [MongoAlias("c")]
16 | public WhereChildC C { get; set; }
17 | }
18 |
19 | public class WhereChildA
20 | {
21 | [MongoAlias("b")]
22 | public int B { get; set; }
23 | }
24 |
25 | public class WhereChildC
26 | {
27 | [MongoAlias("d")]
28 | public int D { get; set; }
29 | }
30 |
31 | [Test]
32 | public void CanSerializeWithChild()
33 | {
34 | var expected = Serialize(Op.Where("this.c.d > 10"));
35 | var bson = Serialize(Op.Where("this.C.D > 10"));
36 | Assert.AreEqual(expected, bson);
37 | }
38 |
39 | [Test]
40 | public void CanSerializeWithChildIndexer()
41 | {
42 | var expected = Serialize(Op.Where("this.a[0].b > 10"));
43 | var bson = Serialize(Op.Where("this.A[0].B > 10"));
44 | Assert.AreEqual(expected, bson);
45 | }
46 |
47 | [Test]
48 | public void CanSerializeWithNonMember()
49 | {
50 | var expected = Serialize(Op.Where("this.a.length > 10"));
51 | var bson = Serialize(Op.Where("this.A.length > 10"));
52 | Assert.AreEqual(expected, bson);
53 | }
54 |
55 | [Test]
56 | public void CanSerializeWithMethod()
57 | {
58 | var expected = Serialize(Op.Where("this.a.awesome().someProperty > 10"));
59 | var bson = Serialize(Op.Where("this.A.awesome().someProperty > 10"));
60 | Assert.AreEqual(expected, bson);
61 | }
62 |
63 | [Test]
64 | public void CanSerializeComplex()
65 | {
66 | var expected = Serialize(Op.Where("this.a[4].b > this.c.d && this.c.d == 2"));
67 | var bson = Serialize(Op.Where("this.A[4].B > this.C.D && this.C.D == 2"));
68 | Assert.AreEqual(expected, bson);
69 | }
70 | }
71 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/___A1A.tmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samus/mongodb-csharp/6397a0f032f24227894afa6ec37e92dd18dd0b3f/source/MongoDB.Tests/UnitTests/Serialization/Descriptors/___A1A.tmp
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/Serialization/SerializationTestBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using MongoDB.Bson;
4 | using MongoDB.Configuration.Mapping;
5 | using MongoDB.Serialization;
6 |
7 | namespace MongoDB.UnitTests.Serialization
8 | {
9 | public abstract class SerializationTestBase
10 | {
11 | public const string EmptyDocumentBson = "BQAAAAA=";
12 |
13 | protected virtual IMappingStore MappingStore
14 | {
15 | get { return new AutoMappingStore(); }
16 | }
17 |
18 | protected T Deserialize(string base64)
19 | {
20 | using (var mem = new MemoryStream(Convert.FromBase64String(base64)))
21 | {
22 | var reader = new BsonReader(mem, new BsonClassMapBuilder(MappingStore, typeof(T)));
23 | return (T)reader.ReadObject();
24 | }
25 | }
26 |
27 | protected string Serialize(object instance)
28 | {
29 | using (var mem = new MemoryStream())
30 | {
31 | var writer = new BsonWriter(mem, new BsonClassMapDescriptor(MappingStore, typeof(T)));
32 | writer.WriteObject(instance);
33 | writer.Flush();
34 | return Convert.ToBase64String(mem.ToArray());
35 | }
36 | }
37 |
38 | protected string Serialize(Document document)
39 | {
40 | using (var mem = new MemoryStream())
41 | {
42 | var writer = new BsonWriter(mem, new BsonDocumentDescriptor());
43 | writer.WriteObject(document);
44 | writer.Flush();
45 | return Convert.ToBase64String(mem.ToArray());
46 | }
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/TestCode.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using System.Runtime.Serialization.Formatters.Binary;
3 | using System.Xml.Serialization;
4 | using NUnit.Framework;
5 |
6 | namespace MongoDB.UnitTests
7 | {
8 | [TestFixture]
9 | public class TestCode
10 | {
11 | [Test]
12 | public void CanBeConstructedWithValue()
13 | {
14 | var code = new Code("code");
15 |
16 | Assert.AreEqual("code",code.Value);
17 | }
18 |
19 | [Test]
20 | public void CanBeConstructoredWithNull()
21 | {
22 | var code = new Code(null);
23 |
24 | Assert.IsNull(code.Value);
25 | }
26 |
27 | [Test]
28 | public void CanBeEqual()
29 | {
30 | var code1 = new Code("code");
31 | var code2 = new Code("code");
32 |
33 | Assert.AreEqual(code1,code2);
34 | }
35 |
36 | [Test]
37 | public void CanBeBinarySerialized()
38 | {
39 | var source = new Code("code");
40 | var formatter = new BinaryFormatter();
41 |
42 | var mem = new MemoryStream();
43 | formatter.Serialize(mem, source);
44 | mem.Position = 0;
45 |
46 | var dest = (Code)formatter.Deserialize(mem);
47 |
48 | Assert.AreEqual(source, dest);
49 | }
50 |
51 | [Test]
52 | public void CanBeXmlSerialized()
53 | {
54 | var source = new Code("code");
55 | var serializer = new XmlSerializer(typeof(Code));
56 |
57 | var writer = new StringWriter();
58 | serializer.Serialize(writer, source);
59 | var dest = (Code)serializer.Deserialize(new StringReader(writer.ToString()));
60 |
61 | Assert.AreEqual(source, dest);
62 | }
63 |
64 | [Test]
65 | public void CanBeXmlSerializedWithNullValue()
66 | {
67 | var source = new Code(null);
68 | var serializer = new XmlSerializer(typeof(Code));
69 |
70 | var writer = new StringWriter();
71 | serializer.Serialize(writer, source);
72 | var dest = (Code)serializer.Deserialize(new StringReader(writer.ToString()));
73 |
74 | Assert.AreEqual(source, dest);
75 | }
76 | }
77 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/TestMongoServerEndPoint.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using System.Runtime.Serialization.Formatters.Binary;
3 | using System.Xml.Serialization;
4 | using NUnit.Framework;
5 |
6 | namespace MongoDB.UnitTests
7 | {
8 | [TestFixture]
9 | public class TestMongoServerEndPoint
10 | {
11 | [Test]
12 | public void CanBeBinarySerialized()
13 | {
14 | var source = new MongoServerEndPoint("myserver", 12345);
15 | var formatter = new BinaryFormatter();
16 |
17 | var mem = new MemoryStream();
18 | formatter.Serialize(mem, source);
19 | mem.Position = 0;
20 |
21 | var dest = (MongoServerEndPoint)formatter.Deserialize(mem);
22 |
23 | Assert.AreEqual(source, dest);
24 | }
25 |
26 | [Test]
27 | public void CanBeXmlSerialized()
28 | {
29 | var source = new MongoServerEndPoint("myserver", 12345);
30 | var serializer = new XmlSerializer(typeof(MongoServerEndPoint));
31 |
32 | var writer = new StringWriter();
33 | serializer.Serialize(writer, source);
34 | var dest = (MongoServerEndPoint)serializer.Deserialize(new StringReader(writer.ToString()));
35 |
36 | Assert.AreEqual(source, dest);
37 | }
38 | }
39 | }
--------------------------------------------------------------------------------
/source/MongoDB.Tests/UnitTests/TestOidGenerator.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using MongoDB.Util;
3 | using NUnit.Framework;
4 |
5 | namespace MongoDB.UnitTests
6 | {
7 | [TestFixture]
8 | public class TestOidGenerator
9 | {
10 | [Test]
11 | public void TestGenerate(){
12 | OidGenerator ogen = new OidGenerator();
13 | Oid oid = ogen.Generate();
14 |
15 | String hex = BitConverter.ToString(oid.ToByteArray()).Replace("-","");
16 | Assert.IsTrue(hex.EndsWith("000001"), "Increment didn't start with 1.");
17 |
18 | oid = ogen.Generate();
19 | hex = BitConverter.ToString(oid.ToByteArray()).Replace("-","");
20 | Assert.IsTrue(hex.EndsWith("000002"), "Next increment should have been 2");
21 |
22 |
23 | DateTime created = oid.Created;
24 | DateTime now = DateTime.UtcNow;
25 | Console.Out.WriteLine(oid.Created);
26 | Assert.AreEqual(now.Year, created.Year);
27 | Assert.AreEqual(now.Month, created.Month);
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/source/MongoDB/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 | using System.Runtime.CompilerServices;
4 | using System.Security.Permissions;
5 |
6 | // Information about this assembly is defined by the following attributes.
7 | // Change them to the values specific to your project.
8 |
9 | [assembly: AssemblyTitle("MongoDB")]
10 | [assembly: AssemblyDescription("MongoDB-CSharp driver for C#")]
11 | [assembly: AssemblyProduct("MongoDB-CSharp")]
12 |
13 | // The following attributes are used to specify the signing key for the assembly,
14 | // if desired. See the Mono documentation for more information about signing.
15 |
16 | [assembly: System.Runtime.InteropServices.ComVisible(false)]
17 | [assembly: CLSCompliantAttribute(true)]
18 |
19 | [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Bson")]
20 | [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Bson")]
21 | [assembly: SecurityPermission(SecurityAction.RequestMinimum, Execution = true)]
22 |
23 | [assembly: InternalsVisibleTo("MongoDB.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100ed9e936c4563336be2e14ca802ea727ff49cad3bb1c0b287beed2a9b5eb823c4c44becc80be4bb11dcd7e49d5d6171f68b488853dcbdeb3152ea3db95ba13a70855a715ee21ac76b67f50bcbc93f2e29e409530a00b98fa79b06ac008dd1f4f3582ba6746af3d218b43b70a63254b094be1a2d493590837273f357fc56b2a7a0")]
24 |
--------------------------------------------------------------------------------
/source/MongoDB/Attributes/MongoAliasAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Attributes
4 | {
5 | ///
6 | ///
7 | ///
8 | [AttributeUsage(AttributeTargets.Property)]
9 | public sealed class MongoAliasAttribute : Attribute
10 | {
11 | ///
12 | /// Gets or sets the name.
13 | ///
14 | /// The name.
15 | public string Name { get; private set; }
16 |
17 | ///
18 | /// Initializes a new instance of the class.
19 | ///
20 | /// The name.
21 | public MongoAliasAttribute(string name){
22 | if(name == null)
23 | throw new ArgumentNullException("name");
24 | if (name == "_id")
25 | throw new ArgumentException("_id is a reserved alias.");
26 |
27 | Name = name;
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/source/MongoDB/Attributes/MongoDefaultAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Attributes
4 | {
5 | ///
6 | ///
7 | ///
8 | [AttributeUsage(AttributeTargets.Property)]
9 | public sealed class MongoDefaultAttribute : Attribute
10 | {
11 |
12 | ///
13 | /// Initializes a new instance of the class.
14 | ///
15 | /// The value.
16 | public MongoDefaultAttribute(object value)
17 | : this(value, true)
18 | { }
19 |
20 | ///
21 | /// Initializes a new instance of the class.
22 | ///
23 | /// The value.
24 | /// if set to true [persist default value].
25 | public MongoDefaultAttribute(object value, bool persistDefaultValue)
26 | {
27 | Value = value;
28 | PersistDefaultValue = persistDefaultValue;
29 | }
30 |
31 | ///
32 | /// Gets or sets a value indicating whether the default value should be persisted.
33 | ///
34 | /// true if [persist default value]; otherwise, false.
35 | public bool PersistDefaultValue { get; private set; }
36 |
37 | ///
38 | /// Gets or sets the value.
39 | ///
40 | /// The value.
41 | public object Value { get; private set; }
42 | }
43 | }
--------------------------------------------------------------------------------
/source/MongoDB/Attributes/MongoIdAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Attributes
4 | {
5 | ///
6 | ///
7 | ///
8 | [AttributeUsage(AttributeTargets.Property)]
9 | public sealed class MongoIdAttribute : Attribute
10 | {
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | public MongoIdAttribute()
15 | { }
16 | }
17 | }
--------------------------------------------------------------------------------
/source/MongoDB/Attributes/MongoIgnoreAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Attributes
4 | {
5 | ///
6 | ///
7 | ///
8 | [AttributeUsage(AttributeTargets.Property)]
9 | public sealed class MongoIgnoreAttribute : Attribute
10 | {
11 | }
12 | }
--------------------------------------------------------------------------------
/source/MongoDB/BinarySubtype.cs:
--------------------------------------------------------------------------------
1 | namespace MongoDB
2 | {
3 | ///
4 | ///
5 | public enum BinarySubtype : byte
6 | {
7 | ///
8 | ///
9 | Unknown = 0,
10 | ///
11 | ///
12 | General = 2,
13 | // Uuid = 3 is now replaced by Guid
14 | ///
15 | ///
16 | Md5 = 5,
17 | ///
18 | ///
19 | UserDefined = 80
20 | }
21 | }
--------------------------------------------------------------------------------
/source/MongoDB/Bson/BsonInfo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Bson
4 | {
5 | ///
6 | ///
7 | ///
8 | public static class BsonInfo
9 | {
10 | ///
11 | /// Initializes the class.
12 | ///
13 | static BsonInfo(){
14 | Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
15 | MaxDocumentSize = 1024 * 1024 * 4; //4MB.
16 | }
17 |
18 | ///
19 | /// Gets or sets the epoch.
20 | ///
21 | /// The epoch.
22 | public static DateTime Epoch { get; private set; }
23 |
24 | ///
25 | /// The maximum size a document can be.
26 | ///
27 | public static int MaxDocumentSize { get; private set; }
28 | }
29 | }
--------------------------------------------------------------------------------
/source/MongoDB/Bson/BsonProperty.cs:
--------------------------------------------------------------------------------
1 | namespace MongoDB.Bson
2 | {
3 | ///
4 | ///
5 | ///
6 | public class BsonProperty
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The name.
12 | public BsonProperty(string name){
13 | Name = name;
14 | }
15 |
16 | ///
17 | /// Gets or sets the name.
18 | ///
19 | /// The name.
20 | public string Name { get; set; }
21 |
22 | ///
23 | /// Gets or sets the value.
24 | ///
25 | /// The value.
26 | public object Value { get; set; }
27 | }
28 | }
--------------------------------------------------------------------------------
/source/MongoDB/Bson/BsonReaderSettings.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Bson
4 | {
5 | ///
6 | ///
7 | ///
8 | public class BsonReaderSettings
9 | {
10 | ///
11 | /// Initializes a new instance of the class.
12 | ///
13 | public BsonReaderSettings()
14 | :this(new BsonDocumentBuilder()){
15 | }
16 |
17 | ///
18 | /// Initializes a new instance of the class.
19 | ///
20 | /// The builder.
21 | public BsonReaderSettings(IBsonObjectBuilder builder){
22 | if(builder == null)
23 | throw new ArgumentNullException("builder");
24 |
25 | Builder = builder;
26 | }
27 |
28 | ///
29 | /// Gets or sets the builder.
30 | ///
31 | /// The builder.
32 | public IBsonObjectBuilder Builder { get; private set; }
33 |
34 | ///
35 | /// Reads DataTime from server as local time.
36 | ///
37 | /// true if [read local time]; otherwise, false.
38 | ///
39 | /// MongoDB stores all time values in UTC timezone. If true the
40 | /// time is converted from UTC to local timezone after is was read.
41 | ///
42 | public bool ReadLocalTime { get; set; }
43 | }
44 | }
--------------------------------------------------------------------------------
/source/MongoDB/Bson/BsonWriterSettings.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Bson
4 | {
5 | ///
6 | ///
7 | ///
8 | public class BsonWriterSettings
9 | {
10 | ///
11 | /// Initializes a new instance of the class.
12 | ///
13 | public BsonWriterSettings()
14 | : this(new BsonDocumentDescriptor())
15 | {
16 | }
17 |
18 | ///
19 | /// Initializes a new instance of the class.
20 | ///
21 | /// The descriptor.
22 | public BsonWriterSettings(IBsonObjectDescriptor descriptor){
23 | if(descriptor == null)
24 | throw new ArgumentNullException("descriptor");
25 |
26 | Descriptor = descriptor;
27 | }
28 |
29 | ///
30 | /// Gets or sets the descriptor.
31 | ///
32 | /// The descriptor.
33 | public IBsonObjectDescriptor Descriptor { get; private set; }
34 | }
35 | }
--------------------------------------------------------------------------------
/source/MongoDB/Bson/IBsonObjectBuilder.cs:
--------------------------------------------------------------------------------
1 | namespace MongoDB.Bson
2 | {
3 | ///
4 | ///
5 | ///
6 | public interface IBsonObjectBuilder
7 | {
8 | ///
9 | /// Begins the object.
10 | ///
11 | ///
12 | object BeginObject();
13 |
14 | ///
15 | /// Ends the object.
16 | ///
17 | /// The instance.
18 | ///
19 | object EndObject(object instance);
20 |
21 | ///
22 | /// Begins the array.
23 | ///
24 | ///
25 | object BeginArray();
26 |
27 | ///
28 | /// Ends the array.
29 | ///
30 | /// The instance.
31 | ///
32 | object EndArray(object instance);
33 |
34 | ///
35 | /// Begins the property.
36 | ///
37 | /// The instance.
38 | /// The name.
39 | void BeginProperty(object instance, string name);
40 |
41 | ///
42 | /// Ends the property.
43 | ///
44 | /// The instance.
45 | /// The name.
46 | /// The value.
47 | void EndProperty(object instance, string name, object value);
48 | }
49 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Builders/IdOverridesBuilder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using MongoDB.Configuration.Mapping.Auto;
3 | using MongoDB.Configuration.IdGenerators;
4 |
5 | namespace MongoDB.Configuration.Builders
6 | {
7 | ///
8 | ///
9 | ///
10 | public class IdOverridesBuilder
11 | {
12 | private readonly IdOverrides _overrides;
13 |
14 | ///
15 | /// Initializes a new instance of the class.
16 | ///
17 | /// The overrides.
18 | internal IdOverridesBuilder(IdOverrides overrides)
19 | {
20 | if (overrides == null)
21 | throw new ArgumentNullException("overrides");
22 |
23 | _overrides = overrides;
24 | }
25 |
26 | ///
27 | /// Generateds the by.
28 | ///
29 | ///
30 | ///
31 | public IdOverridesBuilder GeneratedBy() where T : IIdGenerator, new()
32 | {
33 | return GeneratedBy(new T());
34 | }
35 |
36 | ///
37 | /// Generateds the by.
38 | ///
39 | /// The generator.
40 | ///
41 | public IdOverridesBuilder GeneratedBy(IIdGenerator generator)
42 | {
43 | _overrides.Generator = generator;
44 | return this;
45 | }
46 |
47 | ///
48 | /// Unsaveds the value.
49 | ///
50 | /// The unsaved value.
51 | ///
52 | public IdOverridesBuilder UnsavedValue(object unsavedValue)
53 | {
54 | _overrides.UnsavedValue = unsavedValue;
55 | return this;
56 | }
57 | }
58 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Builders/MemberOverridesBuilder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using MongoDB.Configuration.Mapping.Auto;
3 |
4 | namespace MongoDB.Configuration.Builders
5 | {
6 | ///
7 | ///
8 | ///
9 | public class MemberOverridesBuilder
10 | {
11 | private readonly MemberOverrides _overrides;
12 |
13 | ///
14 | /// Initializes a new instance of the class.
15 | ///
16 | /// The overrides.
17 | internal MemberOverridesBuilder(MemberOverrides overrides)
18 | {
19 | if (overrides == null)
20 | throw new ArgumentNullException("overrides");
21 |
22 | _overrides = overrides;
23 | }
24 |
25 | ///
26 | /// Aliases the specified name.
27 | ///
28 | /// The name.
29 | ///
30 | public MemberOverridesBuilder Alias(string name)
31 | {
32 | _overrides.Alias = name;
33 | return this;
34 | }
35 |
36 | ///
37 | /// Defaults the value.
38 | ///
39 | /// The default value.
40 | ///
41 | public MemberOverridesBuilder DefaultValue(object defaultValue)
42 | {
43 | _overrides.DefaultValue = defaultValue;
44 | return this;
45 | }
46 |
47 | ///
48 | /// Ignores this instance.
49 | ///
50 | ///
51 | public MemberOverridesBuilder Ignore()
52 | {
53 | _overrides.Ignore = true;
54 | return this;
55 | }
56 |
57 | ///
58 | /// Persists the default value.
59 | ///
60 | ///
61 | public MemberOverridesBuilder PersistDefaultValue()
62 | {
63 | _overrides.PersistDefaultValue = true;
64 | return this;
65 | }
66 | }
67 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/CollectionAdapters/ArrayCollectionAdapter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using MongoDB.Configuration.Mapping.Util;
4 |
5 | namespace MongoDB.Configuration.CollectionAdapters
6 | {
7 | ///
8 | ///
9 | ///
10 | public class ArrayCollectionAdapter : ICollectionAdapter
11 | {
12 | ///
13 | /// Adds the element to instance.
14 | ///
15 | /// Type of the element.
16 | /// The elements.
17 | ///
18 | public object CreateCollection(Type elementType, object[] elements)
19 | {
20 | return ValueConverter.ConvertArray(elements, elementType);
21 | }
22 |
23 | ///
24 | /// Gets the elements from collection.
25 | ///
26 | /// The collection.
27 | ///
28 | public IEnumerable GetElementsFromCollection(object collection)
29 | {
30 | return (IEnumerable)collection;
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/CollectionAdapters/ArrayListCollectionAdapter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 |
4 | namespace MongoDB.Configuration.CollectionAdapters
5 | {
6 | ///
7 | ///
8 | ///
9 | public class ArrayListCollectionAdapter : ICollectionAdapter
10 | {
11 | ///
12 | /// Adds the element to instance.
13 | ///
14 | /// Type of the element.
15 | /// The elements.
16 | ///
17 | public object CreateCollection(Type elementType, object[] elements)
18 | {
19 | return new ArrayList(elements);
20 | }
21 |
22 | ///
23 | /// Gets the elements from collection.
24 | ///
25 | /// The collection.
26 | ///
27 | public IEnumerable GetElementsFromCollection(object collection)
28 | {
29 | return (IEnumerable)collection;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/CollectionAdapters/GenericListCollectionAdapter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using MongoDB.Configuration.Mapping.Util;
5 |
6 | namespace MongoDB.Configuration.CollectionAdapters
7 | {
8 | ///
9 | ///
10 | ///
11 | public class GenericListCollectionAdapter : ICollectionAdapter
12 | {
13 | static readonly Type OpenListType = typeof(List<>);
14 |
15 | ///
16 | /// Adds the element to instance.
17 | ///
18 | /// Type of the element.
19 | /// The elements.
20 | ///
21 | public object CreateCollection(Type elementType, object[] elements)
22 | {
23 | var closedListType = OpenListType.MakeGenericType(elementType);
24 | var typedElements = ValueConverter.ConvertArray(elements, elementType);
25 | return Activator.CreateInstance(closedListType, typedElements);
26 | }
27 |
28 | ///
29 | /// Gets the elements from collection.
30 | ///
31 | /// The collection.
32 | ///
33 | public IEnumerable GetElementsFromCollection(object collection)
34 | {
35 | return (IEnumerable)collection;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/CollectionAdapters/GenericSetCollectionAdapter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using MongoDB.Configuration.Mapping.Util;
5 |
6 | namespace MongoDB.Configuration.CollectionAdapters
7 | {
8 | ///
9 | ///
10 | ///
11 | public class GenericSetCollectionAdapter : ICollectionAdapter
12 | {
13 | static readonly Type OpenSetType = typeof(HashSet<>);
14 |
15 | ///
16 | /// Adds the element to instance.
17 | ///
18 | /// Type of the element.
19 | /// The elements.
20 | ///
21 | public object CreateCollection(Type elementType, object[] elements)
22 | {
23 | var closedSetType = OpenSetType.MakeGenericType(elementType);
24 | var typedElements = ValueConverter.ConvertArray(elements,elementType);
25 | return Activator.CreateInstance(closedSetType, new[] { typedElements });
26 | }
27 |
28 | ///
29 | /// Gets the elements from collection.
30 | ///
31 | /// The collection.
32 | ///
33 | public IEnumerable GetElementsFromCollection(object collection)
34 | {
35 | return (IEnumerable)collection;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/CollectionAdapters/ICollectionAdapter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 |
4 | namespace MongoDB.Configuration.CollectionAdapters
5 | {
6 | ///
7 | ///
8 | ///
9 | public interface ICollectionAdapter
10 | {
11 | ///
12 | /// Adds the element to instance.
13 | ///
14 | /// Type of the element.
15 | /// The elements.
16 | ///
17 | object CreateCollection(Type elementType, object[] elements);
18 |
19 | ///
20 | /// Gets the elements from collection.
21 | ///
22 | /// The collection.
23 | ///
24 | IEnumerable GetElementsFromCollection(object collection);
25 | }
26 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/DictionaryAdapters/GenericDictionaryDictionaryAdapter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using MongoDB.Configuration.Mapping.Util;
5 |
6 | namespace MongoDB.Configuration.DictionaryAdapters
7 | {
8 | ///
9 | ///
10 | ///
11 | public class GenericDictionaryDictionaryAdapter : IDictionaryAdapter
12 | {
13 | ///
14 | /// Gets the type of the key.
15 | ///
16 | /// The type of the key.
17 | public Type KeyType
18 | {
19 | get { return typeof(TKey); }
20 | }
21 |
22 | ///
23 | /// Gets the type of the value.
24 | ///
25 | /// The type of the value.
26 | public Type ValueType
27 | {
28 | get { return typeof(TValue); }
29 | }
30 |
31 | ///
32 | /// Creates the dictionary.
33 | ///
34 | /// The document.
35 | ///
36 | public object CreateDictionary(Document document)
37 | {
38 | if(document==null)
39 | return null;
40 |
41 | return document.ToDictionary(pair => (TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), pair => (TValue)pair.Value);
42 | }
43 |
44 | ///
45 | /// Gets the pairs.
46 | ///
47 | /// The collection.
48 | ///
49 | public Document GetDocument(object dictionary)
50 | {
51 | var instance = dictionary as IDictionary;
52 |
53 | if (instance == null)
54 | return null;
55 |
56 | var doc = new Document();
57 |
58 | foreach (var e in instance)
59 | doc.Add(ValueConverter.ConvertKey(e.Key), e.Value);
60 |
61 | return doc;
62 | }
63 |
64 | }
65 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/DictionaryAdapters/GenericSortedListDictionaryAdapter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using MongoDB.Configuration.Mapping.Util;
4 |
5 | namespace MongoDB.Configuration.DictionaryAdapters
6 | {
7 | ///
8 | ///
9 | ///
10 | public class GenericSortedListDictionaryAdapter : IDictionaryAdapter
11 | {
12 | ///
13 | /// Gets the type of the key.
14 | ///
15 | /// The type of the key.
16 | public Type KeyType
17 | {
18 | get { return typeof(TKey); }
19 | }
20 |
21 | ///
22 | /// Gets the type of the value.
23 | ///
24 | /// The type of the value.
25 | public Type ValueType
26 | {
27 | get { return typeof(TValue); }
28 | }
29 |
30 | ///
31 | /// Creates the dictionary.
32 | ///
33 | /// The document.
34 | ///
35 | public object CreateDictionary(Document document)
36 | {
37 | if(document == null)
38 | return null;
39 |
40 | var list = new SortedList();
41 |
42 | foreach(var pair in document)
43 | list.Add((TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), (TValue)pair.Value);
44 |
45 | return list;
46 | }
47 |
48 | ///
49 | /// Gets the pairs.
50 | ///
51 | /// The collection.
52 | ///
53 | public Document GetDocument(object dictionary)
54 | {
55 | var instance = dictionary as IDictionary;
56 |
57 | if (instance == null)
58 | return null;
59 |
60 | var doc = new Document();
61 |
62 | foreach (var e in instance)
63 | doc.Add(ValueConverter.ConvertKey(e.Key), e.Value);
64 |
65 | return doc;
66 | }
67 | }
68 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/DictionaryAdapters/HashtableDictionaryAdapter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using MongoDB.Configuration.Mapping.Util;
4 |
5 | namespace MongoDB.Configuration.DictionaryAdapters
6 | {
7 | ///
8 | ///
9 | ///
10 | public class HashtableDictionaryAdapter : IDictionaryAdapter
11 | {
12 | ///
13 | /// Gets the type of the key.
14 | ///
15 | /// The type of the key.
16 | public Type KeyType
17 | {
18 | get { return typeof(object); }
19 | }
20 |
21 | ///
22 | /// Gets the type of the value.
23 | ///
24 | /// The type of the value.
25 | public Type ValueType
26 | {
27 | get { return typeof(object); }
28 | }
29 |
30 | ///
31 | /// Creates the dictionary.
32 | ///
33 | /// The document.
34 | ///
35 | public object CreateDictionary(Document document)
36 | {
37 | if(document == null)
38 | return null;
39 |
40 | var hashtable = new Hashtable();
41 |
42 | foreach (var pair in document)
43 | hashtable.Add(pair.Key, pair.Value);
44 |
45 | return hashtable;
46 | }
47 |
48 | ///
49 | /// Gets the document.
50 | ///
51 | /// The collection.
52 | ///
53 | public Document GetDocument(object collection)
54 | {
55 | var hashtable = collection as Hashtable;
56 | if (hashtable == null)
57 | return new Document();
58 |
59 | var doc = new Document();
60 | foreach (DictionaryEntry entry in hashtable)
61 | doc.Add(ValueConverter.ConvertKey(entry.Key), entry.Value);
62 |
63 | return doc;
64 | }
65 | }
66 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/DictionaryAdapters/IDictionaryAdapter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.DictionaryAdapters
4 | {
5 | ///
6 | ///
7 | ///
8 | public interface IDictionaryAdapter
9 | {
10 | ///
11 | /// Gets the type of the key.
12 | ///
13 | /// The type of the key.
14 | Type KeyType { get; }
15 |
16 | ///
17 | /// Gets the type of the value.
18 | ///
19 | /// The type of the value.
20 | Type ValueType { get; }
21 |
22 | ///
23 | /// Creates the dictionary.
24 | ///
25 | /// The document.
26 | ///
27 | object CreateDictionary(Document document);
28 |
29 | ///
30 | /// Gets the document.
31 | ///
32 | /// The dictionary.
33 | ///
34 | Document GetDocument(object dictionary);
35 | }
36 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/IdGenerators/AssignedIdGenerator.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Configuration.Mapping.Model;
2 |
3 | namespace MongoDB.Configuration.IdGenerators
4 | {
5 | ///
6 | ///
7 | ///
8 | public class AssignedIdGenerator : IIdGenerator
9 | {
10 | ///
11 | /// Generates an id for the specified entity.
12 | ///
13 | /// The entity.
14 | /// The id map.
15 | ///
16 | public object Generate(object entity, IdMap idMap)
17 | {
18 | var id = idMap.GetValue(entity);
19 |
20 | if (Equals(id, idMap.UnsavedValue))
21 | throw new IdGenerationException(string.Format("Ids for {0} must be manually assigned before saving.", entity.GetType()));
22 |
23 | return id;
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/IdGenerators/GuidCombGenerator.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using MongoDB.Configuration.Mapping.Model;
4 |
5 | namespace MongoDB.Configuration.IdGenerators
6 | {
7 | ///
8 | ///
9 | ///
10 | public class GuidCombGenerator : IIdGenerator
11 | {
12 | ///
13 | /// Generates an id for the specified entity.
14 | ///
15 | /// The entity.
16 | /// The id map.
17 | ///
18 | ///
19 | /// This code was taken from NHibernate.
20 | ///
21 | public object Generate(object entity, IdMap idMap)
22 | {
23 | var guidArray = Guid.NewGuid().ToByteArray();
24 |
25 | var baseDate = new DateTime(1900, 1, 1);
26 | var now = DateTime.Now;
27 |
28 | // Get the days and milliseconds which will be used to build the byte string
29 | var days = new TimeSpan(now.Ticks - baseDate.Ticks);
30 | var msecs = now.TimeOfDay;
31 |
32 | // Convert to a byte array
33 | // Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333
34 | var daysArray = BitConverter.GetBytes(days.Days);
35 | var msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
36 |
37 | // Reverse the bytes to match SQL Servers ordering
38 | Array.Reverse(daysArray);
39 | Array.Reverse(msecsArray);
40 |
41 | // Copy the bytes into the guid
42 | Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
43 | Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
44 |
45 | return new Guid(guidArray);
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/IdGenerators/IIdGenerator.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Configuration.Mapping.Model;
2 |
3 | namespace MongoDB.Configuration.IdGenerators
4 | {
5 | ///
6 | ///
7 | ///
8 | public interface IIdGenerator
9 | {
10 | ///
11 | /// Generates an id for the specified entity.
12 | ///
13 | /// The entity.
14 | /// The id map.
15 | ///
16 | object Generate(object entity, IdMap idMap);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/IdGenerators/OidGenerator.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Configuration.Mapping.Model;
2 |
3 | namespace MongoDB.Configuration.IdGenerators
4 | {
5 | ///
6 | ///
7 | public class OidGenerator : IIdGenerator
8 | {
9 | ///
10 | /// Generates an id for the specified entity.
11 | ///
12 | /// The entity.
13 | /// The id map.
14 | ///
15 | public object Generate(object entity, IdMap idMap)
16 | {
17 | return Oid.NewOid();
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Auto/AggregateAutoMapper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | using MongoDB.Configuration.Mapping.Model;
5 |
6 | namespace MongoDB.Configuration.Mapping.Auto
7 | {
8 | ///
9 | ///
10 | ///
11 | public class AggregateAutoMapper : IAutoMapper
12 | {
13 | private readonly List _autoMappers;
14 |
15 | ///
16 | /// Initializes a new instance of the class.
17 | ///
18 | public AggregateAutoMapper()
19 | {
20 | this._autoMappers = new List();
21 | }
22 |
23 | ///
24 | /// Adds the auto mapper.
25 | ///
26 | /// The auto mapper.
27 | public void AddAutoMapper(IAutoMapper autoMapper)
28 | {
29 | if (autoMapper == null)
30 | throw new ArgumentNullException("autoMapper");
31 |
32 | this._autoMappers.Add(autoMapper);
33 | }
34 |
35 | ///
36 | /// Creates the class map.
37 | ///
38 | /// Type of the entity.
39 | /// The class map finder.
40 | ///
41 | public IClassMap CreateClassMap(Type classType, Func classMapFinder)
42 | {
43 | foreach (var autoMapper in _autoMappers)
44 | {
45 | var classMap = autoMapper.CreateClassMap(classType, classMapFinder);
46 | if (classMap != null)
47 | return classMap;
48 | }
49 |
50 | throw new Exception(string.Format("Unable to create map for {0}.", classType));
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Auto/ClassOverrides.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Reflection;
3 |
4 | namespace MongoDB.Configuration.Mapping.Auto
5 | {
6 | ///
7 | ///
8 | ///
9 | public class ClassOverrides
10 | {
11 | private readonly Dictionary _memberOverrides;
12 |
13 | ///
14 | /// Gets or sets the name of the collection.
15 | ///
16 | /// The name of the collection.
17 | public string CollectionName { get; set; }
18 |
19 | ///
20 | /// Gets or sets the id overrides.
21 | ///
22 | /// The id overrides.
23 | public IdOverrides IdOverrides { get; set; }
24 |
25 | ///
26 | /// Initializes a new instance of the class.
27 | ///
28 | public ClassOverrides()
29 | {
30 | _memberOverrides = new Dictionary();
31 | }
32 |
33 | ///
34 | /// Gets the overrides for.
35 | ///
36 | /// The member info.
37 | ///
38 | public MemberOverrides GetOverridesFor(MemberInfo memberInfo)
39 | {
40 | MemberOverrides memberOverrides;
41 | if (!_memberOverrides.TryGetValue(memberInfo, out memberOverrides))
42 | memberOverrides = _memberOverrides[memberInfo] = new MemberOverrides();
43 |
44 | return memberOverrides;
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Auto/ClassOverridesMap.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace MongoDB.Configuration.Mapping.Auto
5 | {
6 | ///
7 | ///
8 | ///
9 | public class ClassOverridesMap
10 | {
11 | private readonly Dictionary _overrides;
12 |
13 | ///
14 | /// Initializes a new instance of the class.
15 | ///
16 | public ClassOverridesMap()
17 | {
18 | _overrides = new Dictionary();
19 | }
20 |
21 | ///
22 | /// Gets the class overrides for the type.
23 | ///
24 | /// The type.
25 | ///
26 | public ClassOverrides GetOverridesForType(Type type)
27 | {
28 | if (type == null)
29 | throw new ArgumentNullException("type");
30 |
31 | ClassOverrides classOverrides;
32 | if (!this._overrides.TryGetValue(type, out classOverrides))
33 | classOverrides = this._overrides[type] = new ClassOverrides();
34 |
35 | return classOverrides;
36 | }
37 |
38 | ///
39 | /// Determines whether [has overrides for type] [the specified type].
40 | ///
41 | /// The type.
42 | ///
43 | /// true if [has overrides for type] [the specified type]; otherwise, false.
44 | ///
45 | public bool HasOverridesForType(Type type)
46 | {
47 | return _overrides.ContainsKey(type);
48 | }
49 | }
50 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Auto/DefaultMemberFinder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Reflection;
4 |
5 | namespace MongoDB.Configuration.Mapping.Auto
6 | {
7 | ///
8 | ///
9 | ///
10 | public class DefaultMemberFinder : IMemberFinder
11 | {
12 | ///
13 | ///
14 | public static readonly DefaultMemberFinder Instance = new DefaultMemberFinder();
15 |
16 | ///
17 | /// Initializes a new instance of the class.
18 | ///
19 | private DefaultMemberFinder()
20 | {
21 | }
22 |
23 | ///
24 | /// Finds the members.
25 | ///
26 | /// The type.
27 | ///
28 | public IEnumerable FindMembers(Type type)
29 | {
30 | foreach (var prop in type.GetProperties(BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public))
31 | {
32 | var getMethod = prop.GetGetMethod(true);
33 | var setMethod = prop.GetSetMethod(true);
34 |
35 | if(getMethod==null || getMethod.IsPrivate || setMethod==null)
36 | continue;
37 |
38 | if (setMethod.GetParameters().Length != 1) //an indexer
39 | continue;
40 |
41 | yield return prop;
42 | }
43 |
44 | foreach (var field in type.GetFields()) //all public fields
45 | {
46 | if (!field.IsInitOnly && !field.IsLiteral) //readonly
47 | yield return field;
48 | }
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Auto/IAutoMapper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using MongoDB.Configuration.Mapping.Model;
4 |
5 | namespace MongoDB.Configuration.Mapping.Auto
6 | {
7 | ///
8 | ///
9 | ///
10 | public interface IAutoMapper
11 | {
12 | ///
13 | /// Creates the class map.
14 | ///
15 | /// Type of the entity.
16 | /// The class map finder.
17 | ///
18 | IClassMap CreateClassMap(Type classType, Func classMapFinder);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Auto/IMemberFinder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Reflection;
4 |
5 | namespace MongoDB.Configuration.Mapping.Auto
6 | {
7 | ///
8 | ///
9 | ///
10 | public interface IMemberFinder
11 | {
12 | ///
13 | /// Finds the members.
14 | ///
15 | /// The type.
16 | ///
17 | IEnumerable FindMembers(Type type);
18 | }
19 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Auto/IdOverrides.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Reflection;
6 | using MongoDB.Configuration.IdGenerators;
7 |
8 | namespace MongoDB.Configuration.Mapping.Auto
9 | {
10 | ///
11 | /// Overrides the Id member.
12 | ///
13 | public class IdOverrides
14 | {
15 | ///
16 | /// Gets or sets the member.
17 | ///
18 | /// The member.
19 | public MemberInfo Member { get; set; }
20 |
21 | ///
22 | /// Gets or sets the generator.
23 | ///
24 | /// The generator.
25 | public IIdGenerator Generator { get; set; }
26 |
27 | ///
28 | /// Gets or sets the unsaved value.
29 | ///
30 | /// The unsaved value.
31 | public object UnsavedValue { get; set; }
32 | }
33 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Auto/MemberOverrides.cs:
--------------------------------------------------------------------------------
1 | namespace MongoDB.Configuration.Mapping.Auto
2 | {
3 | ///
4 | ///
5 | public class MemberOverrides
6 | {
7 | ///
8 | /// Gets or sets the alias to use for the member.
9 | ///
10 | /// The alias.
11 | public string Alias { get; set; }
12 |
13 | ///
14 | /// Gets or sets the default value.
15 | ///
16 | /// The default value.
17 | public object DefaultValue { get; set; }
18 |
19 | ///
20 | /// Gets or sets a value whether the member should be ignored from the map.
21 | ///
22 | /// true if exclude; otherwise, false.
23 | public bool? Ignore { get; set; }
24 |
25 | ///
26 | /// Gets or sets a value indicating whether a member with the default value gets persisted.
27 | ///
28 | /// The persist default value.
29 | public bool? PersistDefaultValue { get; set; }
30 | }
31 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/DefaultDefaultValueConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public class DefaultDefaultValueConvention : IDefaultValueConvention
9 | {
10 | ///
11 | ///
12 | ///
13 | public static readonly DefaultDefaultValueConvention Instance = new DefaultDefaultValueConvention();
14 |
15 | ///
16 | /// Initializes a new instance of the class.
17 | ///
18 | private DefaultDefaultValueConvention()
19 | {
20 | }
21 |
22 | ///
23 | /// Gets the default value.
24 | ///
25 | /// The type.
26 | ///
27 | public object GetDefaultValue(Type type){
28 | return type.IsValueType ? Activator.CreateInstance(type) : null;
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/DefaultIdGeneratorConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using MongoDB.Configuration.IdGenerators;
3 |
4 | namespace MongoDB.Configuration.Mapping.Conventions
5 | {
6 | ///
7 | ///
8 | ///
9 | public class DefaultIdGeneratorConvention : IIdGeneratorConvention
10 | {
11 | ///
12 | ///
13 | public static readonly DefaultIdGeneratorConvention Instance = new DefaultIdGeneratorConvention();
14 |
15 | ///
16 | /// Initializes a new instance of the class.
17 | ///
18 | private DefaultIdGeneratorConvention()
19 | { }
20 |
21 | ///
22 | /// Gets the generator.
23 | ///
24 | /// The type.
25 | ///
26 | public IIdGenerator GetGenerator(Type type)
27 | {
28 | if (type == typeof(Oid))
29 | return new OidGenerator();
30 |
31 | if (type == typeof(Guid))
32 | return new GuidCombGenerator();
33 |
34 | return new AssignedIdGenerator();
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/DefaultIdUnsavedValueConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public class DefaultIdUnsavedValueConvention : IIdUnsavedValueConvention
9 | {
10 | ///
11 | ///
12 | ///
13 | public static readonly DefaultIdUnsavedValueConvention Instance = new DefaultIdUnsavedValueConvention();
14 |
15 | ///
16 | /// Initializes a new instance of the class.
17 | ///
18 | private DefaultIdUnsavedValueConvention()
19 | { }
20 |
21 | ///
22 | /// Gets the unsaved value.
23 | ///
24 | /// The type.
25 | ///
26 | public object GetUnsavedValue(Type type)
27 | {
28 | return type.IsValueType ? Activator.CreateInstance(type) : null;
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/DelegateAliasConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 |
4 | namespace MongoDB.Configuration.Mapping.Conventions
5 | {
6 | ///
7 | ///
8 | ///
9 | public class DelegateAliasConvention : IAliasConvention
10 | {
11 | readonly Func _alias;
12 |
13 | ///
14 | /// Initializes a new instance of the class.
15 | ///
16 | /// The alias.
17 | public DelegateAliasConvention(Func alias)
18 | {
19 | _alias = alias;
20 | }
21 |
22 | ///
23 | /// Gets the alias.
24 | ///
25 | /// The member info.
26 | ///
27 | public string GetAlias(MemberInfo memberInfo)
28 | {
29 | return _alias(memberInfo);
30 | }
31 | }
32 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/DelegateCollectionNameConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public class DelegateCollectionNameConvention : ICollectionNameConvention
9 | {
10 | private readonly Func _collectionName;
11 |
12 | ///
13 | /// Initializes a new instance of the class.
14 | ///
15 | /// Name of the collection.
16 | public DelegateCollectionNameConvention(Func collectionName)
17 | {
18 | _collectionName = collectionName;
19 | }
20 |
21 | ///
22 | /// Gets the name of the collection.
23 | ///
24 | /// Type of the entity.
25 | ///
26 | public string GetCollectionName(Type classType)
27 | {
28 | return _collectionName(classType);
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/DelegateDiscriminatorAliasConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public class DelegateDiscriminatorAliasConvention : IDiscriminatorAliasConvention
9 | {
10 | private readonly Func _discriminatorAlias;
11 |
12 | ///
13 | /// Initializes a new instance of the class.
14 | ///
15 | /// The discriminator alias.
16 | public DelegateDiscriminatorAliasConvention(Func discriminatorAlias)
17 | {
18 | _discriminatorAlias = discriminatorAlias;
19 | }
20 |
21 | ///
22 | /// Gets the discriminator alias.
23 | ///
24 | /// Type of the class.
25 | ///
26 | public string GetDiscriminatorAlias(Type classType)
27 | {
28 | return _discriminatorAlias(classType);
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/DelegateDiscriminatorConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public class DelegateDiscriminatorConvention : IDiscriminatorConvention
9 | {
10 | private readonly Func _discriminator;
11 |
12 | ///
13 | /// Initializes a new instance of the class.
14 | ///
15 | /// The discriminator.
16 | public DelegateDiscriminatorConvention(Func discriminator)
17 | {
18 | _discriminator = discriminator;
19 | }
20 |
21 | ///
22 | /// Gets the discriminator.
23 | ///
24 | /// Type of the entity.
25 | ///
26 | public object GetDiscriminator(Type classType)
27 | {
28 | return _discriminator(classType);
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/DelegateExtendedPropertiesConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 |
4 | namespace MongoDB.Configuration.Mapping.Conventions
5 | {
6 | ///
7 | ///
8 | ///
9 | public class DelegateExtendedPropertiesConvention : MemberFinderBase, IExtendedPropertiesConvention
10 | {
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | /// The predicate.
15 | public DelegateExtendedPropertiesConvention(Func predicate)
16 | : base(predicate)
17 | { }
18 |
19 | ///
20 | /// Initializes a new instance of the class.
21 | ///
22 | /// The predicate.
23 | /// The member types.
24 | /// The binding flags.
25 | public DelegateExtendedPropertiesConvention(Func predicate, MemberTypes memberTypes, BindingFlags bindingFlags)
26 | : base(predicate, memberTypes, bindingFlags)
27 | { }
28 |
29 | ///
30 | /// Gets the member representing extended properties if one exists.
31 | ///
32 | /// Type of the entity.
33 | ///
34 | public MemberInfo GetExtendedPropertiesMember(Type classType)
35 | {
36 | return GetMember(classType);
37 | }
38 | }
39 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/DelegateIdConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 |
4 | namespace MongoDB.Configuration.Mapping.Conventions
5 | {
6 | ///
7 | ///
8 | ///
9 | public class DelegateIdConvention : MemberFinderBase, IIdConvention
10 | {
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | /// The predicate.
15 | public DelegateIdConvention(Func predicate)
16 | : base(predicate)
17 | {
18 | }
19 |
20 | ///
21 | /// Initializes a new instance of the class.
22 | ///
23 | /// The predicate.
24 | /// The member types.
25 | /// The binding flags.
26 | public DelegateIdConvention(Func predicate, MemberTypes memberTypes, BindingFlags bindingFlags)
27 | : base(predicate, memberTypes, bindingFlags)
28 | {
29 | }
30 |
31 | ///
32 | /// Gets the member representing the id if one exists.
33 | ///
34 | /// Type of the entity.
35 | ///
36 | public MemberInfo GetIdMember(Type classType)
37 | {
38 | return GetMember(classType);
39 | }
40 | }
41 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/IAliasConvention.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public interface IAliasConvention
9 | {
10 | ///
11 | /// Gets the alias.
12 | ///
13 | /// The member.
14 | ///
15 | string GetAlias(MemberInfo member);
16 | }
17 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/ICollectionAdapterConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using MongoDB.Configuration.CollectionAdapters;
4 |
5 | namespace MongoDB.Configuration.Mapping.Conventions
6 | {
7 | ///
8 | ///
9 | ///
10 | public interface ICollectionAdapterConvention
11 | {
12 | ///
13 | /// Gets the collection adapter.
14 | ///
15 | /// The type.
16 | ///
17 | ICollectionAdapter GetCollectionAdapter(Type type);
18 |
19 | ///
20 | /// Gets the type of the element.
21 | ///
22 | /// The type.
23 | ///
24 | Type GetElementType(Type type);
25 | }
26 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/ICollectionNameConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public interface ICollectionNameConvention
9 | {
10 | ///
11 | /// Gets the name of the collection.
12 | ///
13 | /// Type of the entity.
14 | ///
15 | string GetCollectionName(Type classType);
16 | }
17 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/IDefaultValueConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public interface IDefaultValueConvention
9 | {
10 | ///
11 | /// Gets the default value.
12 | ///
13 | /// The type.
14 | ///
15 | object GetDefaultValue(Type type);
16 | }
17 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/IDictionaryAdapterConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using MongoDB.Configuration.DictionaryAdapters;
4 |
5 | namespace MongoDB.Configuration.Mapping.Conventions
6 | {
7 | ///
8 | ///
9 | ///
10 | public interface IDictionarynAdapterConvention
11 | {
12 | ///
13 | /// Gets the dictionary adapter.
14 | ///
15 | /// The type.
16 | ///
17 | IDictionaryAdapter GetDictionaryAdapter(Type type);
18 | }
19 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/IDiscriminatorAliasConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public interface IDiscriminatorAliasConvention
9 | {
10 | ///
11 | /// Gets the name of the discriminator property if one exists.
12 | ///
13 | /// The type.
14 | ///
15 | string GetDiscriminatorAlias(Type type);
16 | }
17 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/IDiscriminatorConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public interface IDiscriminatorConvention
9 | {
10 | ///
11 | /// Gets the discriminator if one exists.
12 | ///
13 | /// The type.
14 | ///
15 | object GetDiscriminator(Type type);
16 | }
17 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/IExtendedPropertiesConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 |
4 | namespace MongoDB.Configuration.Mapping.Conventions
5 | {
6 | ///
7 | ///
8 | ///
9 | public interface IExtendedPropertiesConvention
10 | {
11 | ///
12 | /// Gets the member representing extended properties if one exists.
13 | ///
14 | /// Type of the entity.
15 | ///
16 | MemberInfo GetExtendedPropertiesMember(Type classType);
17 | }
18 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/IIdConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 |
4 | namespace MongoDB.Configuration.Mapping.Conventions
5 | {
6 | ///
7 | ///
8 | ///
9 | public interface IIdConvention
10 | {
11 | ///
12 | /// Gets the member representing the id if one exists.
13 | ///
14 | /// Type of the entity.
15 | ///
16 | MemberInfo GetIdMember(Type classType);
17 | }
18 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/IIdGeneratorConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using MongoDB.Configuration.IdGenerators;
4 |
5 | namespace MongoDB.Configuration.Mapping.Conventions
6 | {
7 | ///
8 | ///
9 | ///
10 | public interface IIdGeneratorConvention
11 | {
12 | ///
13 | /// Gets the generator.
14 | ///
15 | /// The type.
16 | ///
17 | IIdGenerator GetGenerator(Type type);
18 | }
19 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Conventions/IIdUnsavedValueConvention.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Conventions
4 | {
5 | ///
6 | ///
7 | ///
8 | public interface IIdUnsavedValueConvention
9 | {
10 | ///
11 | /// Gets the unsaved value.
12 | ///
13 | /// The type.
14 | ///
15 | object GetUnsavedValue(Type type);
16 | }
17 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/IMappingStore.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using MongoDB.Configuration.Mapping.Model;
4 |
5 | namespace MongoDB.Configuration.Mapping
6 | {
7 | ///
8 | ///
9 | ///
10 | public interface IMappingStore
11 | {
12 | ///
13 | /// Gets the class map.
14 | ///
15 | /// Type of the class.
16 | ///
17 | IClassMap GetClassMap(Type classType);
18 | }
19 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Model/ClassMap.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Model
4 | {
5 | ///
6 | ///
7 | public class ClassMap : ClassMapBase
8 | {
9 | ///
10 | /// Initializes a new instance of the class.
11 | ///
12 | /// Type of the entity.
13 | public ClassMap(Type classType)
14 | : base(classType){
15 | }
16 |
17 | ///
18 | /// Gets the name of the collection.
19 | ///
20 | /// The name of the collection.
21 | public override string CollectionName { get; internal set; }
22 |
23 | ///
24 | /// Gets the alias used to store the discriminator.
25 | ///
26 | /// The discriminator alias.
27 | public override string DiscriminatorAlias { get; internal set; }
28 |
29 | ///
30 | /// Gets the extended properties map.
31 | ///
32 | /// The extended properties map.
33 | public override ExtendedPropertiesMap ExtendedPropertiesMap { get; internal set; }
34 |
35 | ///
36 | /// Gets the id map.
37 | ///
38 | /// The id map.
39 | public override IdMap IdMap { get; internal set; }
40 |
41 | ///
42 | /// Gets a value indicating whether this class map is a subclass.
43 | ///
44 | ///
45 | /// true if this class map is a subclass; otherwise, false.
46 | ///
47 | public override bool IsSubClass{
48 | get { return false; }
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/source/MongoDB/Configuration/Mapping/Model/ExtendedPropertiesMap.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MongoDB.Configuration.Mapping.Model
4 | {
5 | ///
6 | ///
7 | ///
8 | public class ExtendedPropertiesMap : MemberMapBase
9 | {
10 | ///
11 | /// Initializes a new instance of the class.
12 | ///
13 | /// Name of the member.
14 | /// Type of the member return.
15 | /// The getter.
16 | /// The setter.
17 | public ExtendedPropertiesMap(string memberName, Type memberReturnType, Func