├── .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 getter, Action setter) 18 | : base(memberName, memberReturnType, getter, setter) 19 | { 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /source/MongoDB/Configuration/Mapping/Model/IdMap.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | using MongoDB.Configuration.IdGenerators; 4 | 5 | namespace MongoDB.Configuration.Mapping.Model 6 | { 7 | /// 8 | /// 9 | /// 10 | public sealed class IdMap : PersistentMemberMap 11 | { 12 | private readonly IIdGenerator _generator; 13 | 14 | /// 15 | /// Gets the id's unsaved value. 16 | /// 17 | /// The unsaved value. 18 | public object UnsavedValue { get; private set; } 19 | 20 | /// 21 | /// Initializes a new instance of the class. 22 | /// 23 | /// Name of the member. 24 | /// Type of the member. 25 | /// The getter. 26 | /// The setter. 27 | /// The generator. 28 | /// The unsaved value. 29 | public IdMap(string memberName, Type memberType, Func getter, Action setter, IIdGenerator generator, object unsavedValue) 30 | : base(memberName, memberType, getter, setter, null, "_id", true) 31 | { 32 | _generator = generator; 33 | UnsavedValue = unsavedValue; 34 | } 35 | 36 | /// 37 | /// Generates the specified entity. 38 | /// 39 | /// The entity. 40 | /// 41 | public object Generate(object entity) 42 | { 43 | return _generator.Generate(entity, this); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /source/MongoDB/Configuration/Mapping/Model/PersistentMemberMap.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB.Configuration.Mapping.Model 4 | { 5 | /// 6 | /// 7 | /// 8 | public class PersistentMemberMap : MemberMapBase 9 | { 10 | /// 11 | /// Gets the alias in which to store the value. 12 | /// 13 | /// The name. 14 | public string Alias { get; private set; } 15 | 16 | /// 17 | /// Gets a value indicating whether [default value]. 18 | /// 19 | /// true if [default value]; otherwise, false. 20 | public object DefaultValue { get; private set; } 21 | 22 | /// 23 | /// Gets or sets a value indicating whether the default value should be persisted. 24 | /// 25 | /// true if the default value should be persisted; otherwise, false. 26 | public bool PersistDefaultValue { get; private set; } 27 | 28 | /// 29 | /// Initializes a new instance of the class. 30 | /// 31 | /// Name of the member. 32 | /// Type of the member return. 33 | /// The getter. 34 | /// The setter. 35 | /// The default value. 36 | /// The alias. 37 | /// if set to true [persist default value]. 38 | public PersistentMemberMap(string memberName, Type memberReturnType, Func getter, Action setter, object defaultValue, string alias, bool persistDefaultValue) 39 | : base(memberName, memberReturnType, getter, setter) 40 | { 41 | Alias = alias; 42 | DefaultValue = defaultValue; 43 | PersistDefaultValue = persistDefaultValue; 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /source/MongoDB/Configuration/Section/ConnectionElement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Configuration; 3 | 4 | namespace MongoDB.Configuration.Section 5 | { 6 | 7 | /// 8 | /// 9 | /// 10 | public class ConnectionElement : ConfigurationElement 11 | { 12 | /// 13 | /// Gets or sets the name. 14 | /// 15 | /// The name. 16 | [ConfigurationProperty("key", IsRequired = true)] 17 | public string Name{ 18 | get{return (String)this["key"];} 19 | set{this["key"] = value;} 20 | } 21 | 22 | /// 23 | /// Gets or sets the connection string. 24 | /// 25 | /// The connection string. 26 | [ConfigurationProperty("connectionString", DefaultValue = "Server=localhost:27017")] 27 | public string ConnectionString{ 28 | get { return (String)this["connectionString"]; } 29 | set { this["connectionString"] = value; } 30 | } 31 | 32 | /// 33 | /// Gets a value indicating whether this instance is default. 34 | /// 35 | /// 36 | /// true if this instance is default; otherwise, false. 37 | /// 38 | public bool IsDefault 39 | { 40 | get { return string.IsNullOrEmpty(Name) || Name.EndsWith("default", StringComparison.InvariantCultureIgnoreCase); } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /source/MongoDB/Connections/IConnectionFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB.Connections 4 | { 5 | /// 6 | /// 7 | /// 8 | internal interface IConnectionFactory : IDisposable 9 | { 10 | /// 11 | /// Opens a connection. 12 | /// 13 | /// 14 | RawConnection Open(); 15 | 16 | /// 17 | /// Closes the specified connection. 18 | /// 19 | /// The connection. 20 | void Close(RawConnection connection); 21 | 22 | /// 23 | /// Gets the connection string. 24 | /// 25 | /// The connection string. 26 | string ConnectionString { get; } 27 | 28 | /// 29 | /// Cleanups this instance. 30 | /// 31 | void Cleanup(); 32 | } 33 | } -------------------------------------------------------------------------------- /source/MongoDB/Connections/SimpleConnectionFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB.Connections 4 | { 5 | /// 6 | /// Simple connection factory which only creates and closes connections. 7 | /// 8 | internal class SimpleConnectionFactory : ConnectionFactoryBase 9 | { 10 | /// 11 | /// Initializes a new instance of the class. 12 | /// 13 | /// The connection string. 14 | public SimpleConnectionFactory(string connectionString) 15 | : base(connectionString){ 16 | } 17 | 18 | /// 19 | /// Opens a connection. 20 | /// 21 | /// 22 | public override RawConnection Open(){ 23 | return CreateRawConnection(); 24 | } 25 | 26 | /// 27 | /// Closes the specified connection. 28 | /// 29 | /// The connection. 30 | public override void Close(RawConnection connection){ 31 | if(connection == null) 32 | throw new ArgumentNullException("connection"); 33 | 34 | connection.Dispose(); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /source/MongoDB/Exceptions/IdGenerationException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Serialization; 3 | 4 | namespace MongoDB 5 | { 6 | /// 7 | /// 8 | /// 9 | [Serializable] 10 | public class IdGenerationException : MongoException 11 | { 12 | /// 13 | /// Initializes a new instance of the class. 14 | /// 15 | /// The message. 16 | public IdGenerationException(string message) : base(message) { } 17 | 18 | /// 19 | /// Initializes a new instance of the class. 20 | /// 21 | /// The that holds the serialized object data about the exception being thrown. 22 | /// The that contains contextual information about the source or destination. 23 | /// 24 | /// The parameter is null. 25 | /// 26 | /// 27 | /// The class name is null or is zero (0). 28 | /// 29 | public IdGenerationException(SerializationInfo info, StreamingContext context) : base(info,context) 30 | { 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /source/MongoDB/Exceptions/MongoDuplicateKeyException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Serialization; 3 | 4 | namespace MongoDB 5 | { 6 | /// 7 | /// Raised when an action causes a unique constraint violation in an index. 8 | /// 9 | [Serializable] 10 | public class MongoDuplicateKeyException : MongoOperationException 11 | { 12 | /// 13 | /// Initializes a new instance of the class. 14 | /// 15 | /// The message. 16 | /// The error. 17 | public MongoDuplicateKeyException(string message, Document error):base(message, error,null){} 18 | 19 | /// 20 | /// Initializes a new instance of the class. 21 | /// 22 | /// The message. 23 | /// The error. 24 | /// The e. 25 | public MongoDuplicateKeyException(string message, Document error, Exception e):base(message, error,e){} 26 | 27 | /// 28 | /// Initializes a new instance of the class. 29 | /// 30 | /// The that holds the serialized object data about the exception being thrown. 31 | /// The that contains contextual information about the source or destination. 32 | /// 33 | /// The parameter is null. 34 | /// 35 | /// 36 | /// The class name is null or is zero (0). 37 | /// 38 | public MongoDuplicateKeyException(SerializationInfo info, StreamingContext context) 39 | : base(info, context) 40 | { 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /source/MongoDB/Exceptions/MongoDuplicateKeyUpdateException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Serialization; 3 | 4 | namespace MongoDB 5 | { 6 | /// 7 | /// Raised when an update action causes a unique constraint violation in an index. 8 | /// 9 | /// 10 | /// It is only another class because Mongo makes a distinction and it may be helpful. 11 | /// 12 | [Serializable] 13 | public class MongoDuplicateKeyUpdateException : MongoDuplicateKeyException 14 | { 15 | /// 16 | /// Initializes a new instance of the class. 17 | /// 18 | /// The message. 19 | /// The error. 20 | public MongoDuplicateKeyUpdateException(string message, Document error) 21 | :base(message,error){} 22 | 23 | /// 24 | /// Initializes a new instance of the class. 25 | /// 26 | /// The message. 27 | /// The error. 28 | /// The e. 29 | public MongoDuplicateKeyUpdateException(string message, Document error, Exception e):base(message, error,e){} 30 | 31 | /// 32 | /// Initializes a new instance of the class. 33 | /// 34 | /// The that holds the serialized object data about the exception being thrown. 35 | /// The that contains contextual information about the source or destination. 36 | /// 37 | /// The parameter is null. 38 | /// 39 | /// 40 | /// The class name is null or is zero (0). 41 | /// 42 | public MongoDuplicateKeyUpdateException(SerializationInfo info, StreamingContext context) 43 | : base(info, context) 44 | { 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /source/MongoDB/Exceptions/MongoException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Serialization; 3 | 4 | namespace MongoDB 5 | { 6 | /// 7 | /// Base class for all Mongo Exceptions 8 | /// 9 | [Serializable] 10 | public class MongoException : Exception 11 | { 12 | /// 13 | /// Initializes a new instance of the class. 14 | /// 15 | /// The message. 16 | /// The inner. 17 | public MongoException(string message, Exception innerException):base(message,innerException){} 18 | 19 | /// 20 | /// Initializes a new instance of the class. 21 | /// 22 | /// The message. 23 | public MongoException(string message):base(message){} 24 | 25 | /// 26 | /// Initializes a new instance of the class. 27 | /// 28 | /// The that holds the serialized object data about the exception being thrown. 29 | /// The that contains contextual information about the source or destination. 30 | /// 31 | /// The parameter is null. 32 | /// 33 | /// 34 | /// The class name is null or is zero (0). 35 | /// 36 | public MongoException(SerializationInfo info, StreamingContext context) : base(info,context) 37 | { 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /source/MongoDB/Exceptions/MongoMapReduceException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Serialization; 3 | using MongoDB.Results; 4 | 5 | namespace MongoDB 6 | { 7 | /// 8 | /// Raised when a map reduce call fails. 9 | /// 10 | [Serializable] 11 | public class MongoMapReduceException : MongoCommandException 12 | { 13 | /// 14 | /// Gets or sets the map reduce result. 15 | /// 16 | /// The map reduce result. 17 | public MapReduceResult MapReduceResult { get; private set; } 18 | 19 | /// 20 | /// Initializes a new instance of the class. 21 | /// 22 | /// The exception. 23 | public MongoMapReduceException(MongoCommandException exception) 24 | :base(exception.Message,exception.Error, exception.Command) { 25 | MapReduceResult = new MapReduceResult(exception.Error); 26 | } 27 | 28 | /// 29 | /// Initializes a new instance of the class. 30 | /// 31 | /// The that holds the serialized object data about the exception being thrown. 32 | /// The that contains contextual information about the source or destination. 33 | /// 34 | /// The parameter is null. 35 | /// 36 | /// 37 | /// The class name is null or is zero (0). 38 | /// 39 | public MongoMapReduceException(SerializationInfo info, StreamingContext context) 40 | : base(info, context) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /source/MongoDB/Exceptions/MongoOperationException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Serialization; 3 | 4 | namespace MongoDB 5 | { 6 | /// 7 | /// 8 | /// 9 | [Serializable] 10 | public class MongoOperationException : MongoException 11 | { 12 | /// 13 | /// Gets or sets the error. 14 | /// 15 | /// The error. 16 | public Document Error { get; private set; } 17 | 18 | /// 19 | /// Initializes a new instance of the class. 20 | /// 21 | /// The message. 22 | /// The error. 23 | public MongoOperationException(string message, Document error):this(message, error,null){} 24 | 25 | /// 26 | /// Initializes a new instance of the class. 27 | /// 28 | /// The message. 29 | /// The error. 30 | /// The e. 31 | public MongoOperationException(string message, Document error, Exception e):base(message,e){ 32 | this.Error = error; 33 | } 34 | 35 | /// 36 | /// Initializes a new instance of the class. 37 | /// 38 | /// The that holds the serialized object data about the exception being thrown. 39 | /// The that contains contextual information about the source or destination. 40 | /// 41 | /// The parameter is null. 42 | /// 43 | /// 44 | /// The class name is null or is zero (0). 45 | /// 46 | public MongoOperationException(SerializationInfo info, StreamingContext context) 47 | : base(info, context) 48 | { 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /source/MongoDB/Exceptions/UnmappedMemberException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Serialization; 3 | 4 | namespace MongoDB 5 | { 6 | /// 7 | /// 8 | /// 9 | [Serializable] 10 | public class UnmappedMemberException : MongoException 11 | { 12 | /// 13 | /// Initializes a new instance of the class. 14 | /// 15 | /// The message. 16 | public UnmappedMemberException(string message) : base(message) { } 17 | 18 | /// 19 | /// Initializes a new instance of the class. 20 | /// 21 | /// The that holds the serialized object data about the exception being thrown. 22 | /// The that contains contextual information about the source or destination. 23 | /// 24 | /// The parameter is null. 25 | /// 26 | /// 27 | /// The class name is null or is zero (0). 28 | /// 29 | public UnmappedMemberException(SerializationInfo info, StreamingContext context) 30 | : base(info, context) 31 | { 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /source/MongoDB/IMongo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace MongoDB 3 | { 4 | /// 5 | /// 6 | /// 7 | public interface IMongo : IDisposable 8 | { 9 | /// 10 | /// Gets the connection string. 11 | /// 12 | /// The connection string. 13 | string ConnectionString { get; } 14 | 15 | /// 16 | /// Gets the named database. 17 | /// 18 | /// The name. 19 | /// 20 | IMongoDatabase GetDatabase(string name); 21 | 22 | /// 23 | /// Gets the with the specified name. 24 | /// 25 | /// 26 | IMongoDatabase this[string name] { get; } 27 | 28 | /// 29 | /// Connects this instance. 30 | /// 31 | /// 32 | void Connect(); 33 | 34 | /// 35 | /// Tries to connect to server. 36 | /// 37 | /// 38 | bool TryConnect(); 39 | 40 | /// 41 | /// Disconnects this instance. 42 | /// 43 | /// 44 | bool Disconnect(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /source/MongoDB/IndexOrder.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB 2 | { 3 | /// 4 | /// 5 | /// 6 | public enum IndexOrder { 7 | /// 8 | /// 9 | /// 10 | Descending = -1, 11 | /// 12 | /// 13 | /// 14 | Ascending = 1 15 | } 16 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/ExecutionBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq.Expressions; 4 | using MongoDB.Linq.Expressions; 5 | using MongoDB.Linq.Translators; 6 | 7 | namespace MongoDB.Linq 8 | { 9 | internal class ExecutionBuilder : MongoExpressionVisitor 10 | { 11 | private Expression _provider; 12 | 13 | public Expression Build(Expression expression, Expression provider) 14 | { 15 | _provider = provider; 16 | return Visit(expression); 17 | } 18 | 19 | protected override Expression VisitProjection(ProjectionExpression projection) 20 | { 21 | var queryObject = new MongoQueryObjectBuilder().Build(projection); 22 | queryObject.Projector = new ProjectionBuilder().Build(projection.Projector, queryObject.DocumentType, "document", queryObject.IsMapReduce); 23 | queryObject.Aggregator = (LambdaExpression)Visit(projection.Aggregator); 24 | 25 | Expression result = Expression.Call( 26 | _provider, 27 | "ExecuteQueryObject", 28 | Type.EmptyTypes, 29 | Expression.Constant(queryObject, typeof(MongoQueryObject))); 30 | 31 | if (queryObject.Aggregator != null) 32 | result = Expression.Convert(result, queryObject.Aggregator.Body.Type); 33 | else 34 | result = Expression.Convert(result, typeof(IEnumerable<>).MakeGenericType(queryObject.Projector.Body.Type)); 35 | 36 | return result; 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/AggregateExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace MongoDB.Linq.Expressions 5 | { 6 | internal class AggregateExpression : MongoExpression 7 | { 8 | public AggregateType AggregateType { get; private set; } 9 | 10 | public Expression Argument { get; private set; } 11 | 12 | public bool Distinct { get; private set; } 13 | 14 | public AggregateExpression(Type type, AggregateType aggregateType, Expression argument, bool distinct) 15 | : base(MongoExpressionType.Aggregate, type) 16 | { 17 | AggregateType = aggregateType; 18 | Argument = argument; 19 | Distinct = distinct; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/AggregateSubqueryExpression.cs: -------------------------------------------------------------------------------- 1 | using System.Linq.Expressions; 2 | 3 | namespace MongoDB.Linq.Expressions 4 | { 5 | internal class AggregateSubqueryExpression : MongoExpression 6 | { 7 | public Expression AggregateInGroupSelect { get; private set; } 8 | 9 | public ScalarExpression AggregateAsSubquery { get; private set; } 10 | 11 | public Alias GroupByAlias { get; private set; } 12 | 13 | public AggregateSubqueryExpression(Alias groupByAlias, Expression aggregateInGroupSelect, ScalarExpression aggregateAsSubquery) 14 | : base(MongoExpressionType.AggregateSubquery, aggregateAsSubquery.Type) 15 | { 16 | GroupByAlias = groupByAlias; 17 | AggregateInGroupSelect = aggregateInGroupSelect; 18 | AggregateAsSubquery = aggregateAsSubquery; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/AggregateType.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB.Linq.Expressions 2 | { 3 | internal enum AggregateType 4 | { 5 | Count, 6 | Min, 7 | Max, 8 | Average, 9 | Sum 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/Alias.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB.Linq.Expressions 2 | { 3 | internal sealed class Alias 4 | { } 5 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/AliasedExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB.Linq.Expressions 4 | { 5 | internal abstract class AliasedExpression : MongoExpression 6 | { 7 | public Alias Alias { get; private set; } 8 | 9 | protected AliasedExpression(MongoExpressionType nodeType, Type type, Alias alias) 10 | : base(nodeType, type) 11 | { 12 | Alias = alias; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/CollectionExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB.Linq.Expressions 4 | { 5 | internal class CollectionExpression : AliasedExpression 6 | { 7 | public string CollectionName { get; private set; } 8 | 9 | public IMongoDatabase Database { get; private set; } 10 | 11 | public Type DocumentType { get; private set; } 12 | 13 | public CollectionExpression(Alias alias, IMongoDatabase database, string collectionName, Type documentType) 14 | : base(MongoExpressionType.Collection, typeof(void), alias) 15 | { 16 | CollectionName = collectionName; 17 | Database = database; 18 | DocumentType = documentType; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/FieldDeclaration.cs: -------------------------------------------------------------------------------- 1 | using System.Linq.Expressions; 2 | 3 | namespace MongoDB.Linq.Expressions 4 | { 5 | internal class FieldDeclaration 6 | { 7 | private readonly string _name; 8 | private readonly Expression _expression; 9 | 10 | public string Name 11 | { 12 | get { return _name; } 13 | } 14 | 15 | public Expression Expression 16 | { 17 | get { return _expression; } 18 | } 19 | 20 | public FieldDeclaration(string name, Expression expression) 21 | { 22 | _name = name; 23 | _expression = expression; 24 | } 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/FieldExpression.cs: -------------------------------------------------------------------------------- 1 | using System.Linq.Expressions; 2 | 3 | namespace MongoDB.Linq.Expressions 4 | { 5 | internal class FieldExpression : MongoExpression 6 | { 7 | public Alias Alias { get; private set; } 8 | 9 | public Expression Expression { get; private set; } 10 | 11 | public string Name { get; private set; } 12 | 13 | public FieldExpression(Expression expression, Alias alias, string name) 14 | : base(MongoExpressionType.Field, expression.Type) 15 | { 16 | Alias = alias; 17 | Expression = expression; 18 | Name = name; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/MongoExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace MongoDB.Linq.Expressions 5 | { 6 | internal abstract class MongoExpression : Expression 7 | { 8 | protected MongoExpression(MongoExpressionType nodeType, Type type) 9 | : base((ExpressionType)nodeType, type) 10 | { } 11 | } 12 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/MongoExpressionType.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB.Linq.Expressions 2 | { 3 | internal enum MongoExpressionType 4 | { 5 | Collection = 1000, 6 | ClientJoin, 7 | Field, 8 | Select, 9 | Projection, 10 | Join, 11 | Order, 12 | Aggregate, 13 | AggregateSubquery, 14 | Scalar, 15 | OuterJoined, 16 | NamedValue 17 | } 18 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/OrderExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace MongoDB.Linq.Expressions 5 | { 6 | internal class OrderExpression : MongoExpression 7 | { 8 | public Expression Expression { get; private set; } 9 | 10 | public OrderType OrderType { get; private set; } 11 | 12 | public OrderExpression(OrderType orderType, Expression expression) 13 | : base(MongoExpressionType.Order, expression.Type) 14 | { 15 | if (expression == null) 16 | throw new ArgumentNullException("expression"); 17 | 18 | Expression = expression; 19 | OrderType = orderType; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/OrderType.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB.Linq.Expressions 2 | { 3 | internal enum OrderType 4 | { 5 | Ascending, 6 | Descending 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/ProjectionExpression.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq.Expressions; 3 | 4 | namespace MongoDB.Linq.Expressions 5 | { 6 | internal class ProjectionExpression : MongoExpression 7 | { 8 | public LambdaExpression Aggregator { get; private set; } 9 | 10 | public bool IsSingleton 11 | { 12 | get { return Aggregator != null && Aggregator.Body.Type == Projector.Type; } 13 | } 14 | 15 | public Expression Projector { get; private set; } 16 | 17 | public SelectExpression Source { get; private set; } 18 | 19 | public ProjectionExpression(SelectExpression source, Expression projector) 20 | : this(source, projector, null) 21 | { } 22 | 23 | public ProjectionExpression(SelectExpression source, Expression projector, LambdaExpression aggregator) 24 | : base(MongoExpressionType.Projection, aggregator != null ? aggregator.Body.Type : typeof(IEnumerable<>).MakeGenericType(projector.Type)) 25 | { 26 | Source = source; 27 | Projector = projector; 28 | Aggregator = aggregator; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/ScalarExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB.Linq.Expressions 4 | { 5 | internal class ScalarExpression : SubqueryExpression 6 | { 7 | public ScalarExpression(Type type, SelectExpression select) 8 | : base(MongoExpressionType.Scalar, type, select) 9 | { } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Expressions/SubqueryExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB.Linq.Expressions 4 | { 5 | internal abstract class SubqueryExpression : MongoExpression 6 | { 7 | public SelectExpression Select { get; private set; } 8 | 9 | protected SubqueryExpression(MongoExpressionType nodeType, Type type, SelectExpression select) 10 | : base(nodeType, type) 11 | { 12 | Select = select; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Grouping.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace MongoDB.Linq 6 | { 7 | /// 8 | /// 9 | /// 10 | /// The type of the key. 11 | /// The type of the element. 12 | internal class Grouping : IGrouping 13 | { 14 | private readonly TKey _key; 15 | private readonly IEnumerable _group; 16 | 17 | /// 18 | /// Initializes a new instance of the class. 19 | /// 20 | /// The key. 21 | /// The group. 22 | public Grouping(TKey key, IEnumerable group) 23 | { 24 | _key = key; 25 | _group = group; 26 | } 27 | 28 | /// 29 | /// Gets the key of the . 30 | /// 31 | /// 32 | /// 33 | /// The key of the . 34 | /// 35 | public TKey Key 36 | { 37 | get { return _key; } 38 | } 39 | 40 | /// 41 | /// Returns an enumerator that iterates through the collection. 42 | /// 43 | /// 44 | /// A that can be used to iterate through the collection. 45 | /// 46 | public IEnumerator GetEnumerator() 47 | { 48 | return _group.GetEnumerator(); 49 | } 50 | 51 | /// 52 | /// Returns an enumerator that iterates through a collection. 53 | /// 54 | /// 55 | /// An object that can be used to iterate through the collection. 56 | /// 57 | IEnumerator IEnumerable.GetEnumerator() 58 | { 59 | return _group.GetEnumerator(); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/IMongoQueryable.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace MongoDB.Linq 4 | { 5 | internal interface IMongoQueryable : IQueryable 6 | { 7 | string CollectionName { get; } 8 | 9 | IMongoDatabase Database { get; } 10 | 11 | MongoQueryObject GetQueryObject(); 12 | } 13 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/MongoQueryable.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB.Linq 2 | { 3 | /// 4 | /// 5 | /// 6 | internal static class MongoQueryable 7 | { 8 | /// 9 | /// Keys the specified document. 10 | /// 11 | /// 12 | /// The document. 13 | /// The key. 14 | /// 15 | public static DocumentQuery Key(this T document, string key) where T : Document 16 | { 17 | return new DocumentQuery(document, key); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Translators/AggregateChecker.cs: -------------------------------------------------------------------------------- 1 | using System.Linq.Expressions; 2 | using MongoDB.Linq.Expressions; 3 | 4 | namespace MongoDB.Linq.Translators 5 | { 6 | internal class AggregateChecker : MongoExpressionVisitor 7 | { 8 | private bool _hasAggregate; 9 | 10 | public bool HasAggregates(Expression expression) 11 | { 12 | _hasAggregate = false; 13 | Visit(expression); 14 | return _hasAggregate; 15 | } 16 | 17 | protected override Expression VisitAggregate(AggregateExpression aggregate) 18 | { 19 | _hasAggregate = true; 20 | return aggregate; 21 | } 22 | 23 | protected override Expression VisitSelect(SelectExpression select) 24 | { 25 | Visit(select.Where); 26 | VisitOrderBy(select.OrderBy); 27 | VisitFieldDeclarationList(select.Fields); 28 | return select; 29 | } 30 | 31 | protected override Expression VisitSubquery(SubqueryExpression subquery) 32 | { 33 | return subquery; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Translators/DeclaredAliasGatherer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq.Expressions; 3 | using MongoDB.Linq.Expressions; 4 | 5 | namespace MongoDB.Linq.Translators 6 | { 7 | internal class DeclaredAliasGatherer : MongoExpressionVisitor 8 | { 9 | private HashSet _aliases; 10 | 11 | public HashSet Gather(Expression source) 12 | { 13 | _aliases = new HashSet(); 14 | Visit(source); 15 | return _aliases; 16 | } 17 | 18 | protected override Expression VisitSelect(SelectExpression select) 19 | { 20 | _aliases.Add(select.Alias); 21 | return select; 22 | } 23 | 24 | protected override Expression VisitCollection(CollectionExpression collection) 25 | { 26 | _aliases.Add(collection.Alias); 27 | return collection; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Translators/ExpressionReplacer.cs: -------------------------------------------------------------------------------- 1 | using System.Linq.Expressions; 2 | using MongoDB.Linq.Expressions; 3 | 4 | namespace MongoDB.Linq.Translators 5 | { 6 | internal class ExpressionReplacer : MongoExpressionVisitor 7 | { 8 | private Expression _replaceWith; 9 | private Expression _searchFor; 10 | 11 | public Expression Replace(Expression expression, Expression searchFor, Expression replaceWith) 12 | { 13 | _searchFor = searchFor; 14 | _replaceWith = replaceWith; 15 | return Visit(expression); 16 | } 17 | 18 | public Expression ReplaceAll(Expression expression, Expression[] searchFor, Expression[] replaceWith) 19 | { 20 | for(var i = 0; i < searchFor.Length; i++) 21 | expression = Replace(expression, searchFor[i], replaceWith[i]); 22 | return expression; 23 | } 24 | 25 | protected override Expression Visit(Expression exp) 26 | { 27 | return exp == _searchFor ? _replaceWith : base.Visit(exp); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Translators/FieldGatherer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Collections.ObjectModel; 3 | using System.Linq.Expressions; 4 | using MongoDB.Linq.Expressions; 5 | 6 | namespace MongoDB.Linq.Translators 7 | { 8 | internal class FieldGatherer : MongoExpressionVisitor 9 | { 10 | private List _fields; 11 | 12 | public ReadOnlyCollection Gather(Expression exp) 13 | { 14 | _fields = new List(); 15 | Visit(exp); 16 | return _fields.AsReadOnly(); 17 | } 18 | 19 | protected override Expression VisitSelect(SelectExpression select) 20 | { 21 | VisitFieldDeclarationList(select.Fields); 22 | return select; 23 | } 24 | 25 | protected override Expression VisitField(FieldExpression field) 26 | { 27 | var fields = new FieldGatherer().Gather(field.Expression); 28 | if (fields.Count == 0) 29 | _fields.Add(field); 30 | else 31 | _fields.AddRange(fields); 32 | 33 | return base.VisitField(field); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Translators/FieldMapper.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq.Expressions; 3 | using MongoDB.Linq.Expressions; 4 | 5 | namespace MongoDB.Linq.Translators 6 | { 7 | internal class FieldMapper : MongoExpressionVisitor 8 | { 9 | private HashSet _oldAliases; 10 | private Alias _newAlias; 11 | 12 | public Expression Map(Expression expression, Alias newAlias, IEnumerable oldAliases) 13 | { 14 | _oldAliases = new HashSet(oldAliases); 15 | _newAlias = newAlias; 16 | return Visit(expression); 17 | } 18 | 19 | public Expression Map(Expression expression, Alias newAlias, params Alias[] oldAliases) 20 | { 21 | return Map(expression, newAlias, (IEnumerable)oldAliases); 22 | } 23 | 24 | protected override Expression VisitField(FieldExpression field) 25 | { 26 | if (_oldAliases.Contains(field.Alias)) 27 | return new FieldExpression(field.Expression, _newAlias, field.Name); 28 | return field; 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Translators/Nominator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq.Expressions; 4 | using MongoDB.Linq.Expressions; 5 | 6 | namespace MongoDB.Linq.Translators 7 | { 8 | internal class Nominator : MongoExpressionVisitor 9 | { 10 | private readonly Func _predicate; 11 | private HashSet _candidates; 12 | private bool _isBlocked; 13 | 14 | public Nominator(Func predicate) 15 | { 16 | if (predicate == null) 17 | throw new ArgumentNullException("predicate"); 18 | 19 | _predicate = predicate; 20 | } 21 | 22 | public HashSet Nominate(Expression expression) 23 | { 24 | _candidates = new HashSet(); 25 | _isBlocked = false; 26 | Visit(expression); 27 | return _candidates; 28 | } 29 | 30 | protected override Expression Visit(Expression expression) 31 | { 32 | if (expression != null) 33 | { 34 | var saveIsBlocked = _isBlocked; 35 | _isBlocked = false; 36 | base.Visit(expression); 37 | if (!_isBlocked) 38 | { 39 | if (_predicate(expression)) 40 | _candidates.Add(expression); 41 | else 42 | _isBlocked = true; 43 | } 44 | _isBlocked |= saveIsBlocked; 45 | } 46 | return expression; 47 | } 48 | 49 | protected override Expression VisitField(FieldExpression f) 50 | { 51 | return f; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Translators/PartialEvaluator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq.Expressions; 4 | 5 | using MongoDB.Linq.Expressions; 6 | 7 | namespace MongoDB.Linq.Translators 8 | { 9 | internal static class PartialEvaluator 10 | { 11 | /// 12 | /// Performs evaluation and replacement of independent sub-trees 13 | /// 14 | /// The root of the expression tree. 15 | /// A function that decides whether a given expression node can be part of the local function. 16 | /// 17 | /// A new tree with sub-trees evaluated and replaced. 18 | /// 19 | public static Expression Evaluate(Expression expression, Func canBeEvaluated) 20 | { 21 | return new SubtreeEvaluator(new Nominator(canBeEvaluated).Nominate(expression)).Eval(expression); 22 | } 23 | 24 | /// 25 | /// 26 | /// 27 | private class SubtreeEvaluator : ExpressionVisitor 28 | { 29 | private readonly HashSet _candidates; 30 | 31 | internal SubtreeEvaluator(HashSet candidates) 32 | { 33 | _candidates = candidates; 34 | } 35 | 36 | internal Expression Eval(Expression exp) 37 | { 38 | return Visit(exp); 39 | } 40 | 41 | protected override Expression Visit(Expression exp) 42 | { 43 | if (exp == null) 44 | return null; 45 | if (_candidates.Contains(exp)) 46 | { 47 | return Evaluate(exp); 48 | } 49 | 50 | return base.Visit(exp); 51 | } 52 | 53 | private Expression Evaluate(Expression e) 54 | { 55 | if (e.NodeType == ExpressionType.Constant) 56 | return e; 57 | 58 | var lambda = Expression.Lambda(e); 59 | var fn = lambda.Compile(); 60 | return Expression.Constant(fn.DynamicInvoke(null), e.Type); 61 | } 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /source/MongoDB/Linq/Translators/QueryDuplicator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq.Expressions; 3 | using MongoDB.Linq.Expressions; 4 | 5 | namespace MongoDB.Linq.Translators 6 | { 7 | internal class QueryDuplicator : MongoExpressionVisitor 8 | { 9 | Dictionary _map; 10 | 11 | public Expression Duplicate(Expression expression) 12 | { 13 | _map = new Dictionary(); 14 | return Visit(expression); 15 | } 16 | 17 | protected override Expression VisitCollection(CollectionExpression collection) 18 | { 19 | var newAlias = new Alias(); 20 | _map[collection.Alias] = newAlias; 21 | return new CollectionExpression(newAlias, collection.Database, collection.CollectionName, collection.DocumentType); 22 | } 23 | 24 | protected override Expression VisitSelect(SelectExpression select) 25 | { 26 | var newAlias = new Alias(); 27 | _map[select.Alias] = newAlias; 28 | select = (SelectExpression)base.VisitSelect(select); 29 | return new SelectExpression(newAlias, select.Fields, select.From, select.Where, select.OrderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take); 30 | } 31 | 32 | protected override Expression VisitField(FieldExpression field) 33 | { 34 | Alias newAlias; 35 | if (_map.TryGetValue(field.Alias, out newAlias)) 36 | return new FieldExpression(field.Expression, newAlias, field.Name); 37 | 38 | return field; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /source/MongoDB/Linq/Translators/SubqueryRemover.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq.Expressions; 3 | using MongoDB.Linq.Expressions; 4 | 5 | namespace MongoDB.Linq.Translators 6 | { 7 | internal class SubqueryRemover : MongoExpressionVisitor 8 | { 9 | private HashSet _selectsToRemove; 10 | 11 | public Expression Remove(SelectExpression outerSelect, IEnumerable selectsToRemove) 12 | { 13 | _selectsToRemove = new HashSet(selectsToRemove); 14 | return Visit(outerSelect); 15 | } 16 | 17 | protected override Expression VisitSelect(SelectExpression s) 18 | { 19 | return _selectsToRemove.Contains(s) ? Visit(s.From) : base.VisitSelect(s); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /source/MongoDB/MongoMaxKey.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB 2 | { 3 | /// 4 | /// Class representing the MaxKey Bson type. It will always compare higher than any other type. 5 | /// 6 | public class MongoMaxKey 7 | { 8 | /// 9 | /// Gets the value. 10 | /// 11 | /// The value. 12 | public static MongoMaxKey Value { get; private set; } 13 | 14 | /// 15 | /// Initializes the class. 16 | /// 17 | static MongoMaxKey(){ 18 | Value = new MongoMaxKey(); 19 | } 20 | 21 | /// 22 | /// Initializes a new instance of the class. 23 | /// 24 | protected MongoMaxKey (){} 25 | 26 | /// 27 | /// Returns a that represents this instance. 28 | /// 29 | /// 30 | /// A that represents this instance. 31 | /// 32 | public override string ToString (){ 33 | return "{ \"$maxkey\": 1 }"; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /source/MongoDB/MongoMinKey.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB 2 | { 3 | /// 4 | /// Class representing the MinKey Bson type. It will always compare lower than any other type. 5 | /// 6 | public class MongoMinKey 7 | { 8 | /// 9 | /// Gets or sets the value. 10 | /// 11 | /// The value. 12 | public static MongoMinKey Value { get; private set; } 13 | 14 | /// 15 | /// Initializes the class. 16 | /// 17 | static MongoMinKey(){ 18 | Value = new MongoMinKey(); 19 | } 20 | 21 | /// 22 | /// Initializes a new instance of the class. 23 | /// 24 | protected MongoMinKey (){} 25 | 26 | /// 27 | /// Returns a that represents this instance. 28 | /// 29 | /// 30 | /// A that represents this instance. 31 | /// 32 | public override string ToString (){ 33 | return "{ \"$minkey\": 1 }"; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /source/MongoDB/MongoRegexOption.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB 4 | { 5 | /// 6 | /// Mongo Regex options 7 | /// 8 | [Flags] 9 | public enum MongoRegexOption 10 | { 11 | /// 12 | /// Specifies that no options are set. 13 | /// 14 | None = 0, 15 | /// 16 | /// i - Specifies case-insensitive matching. 17 | /// 18 | IgnoreCase = 1, 19 | /// 20 | /// m - Multiline mode. Changes the meaning of ^ and $ so they match at the beginning 21 | /// and end, respectively, of any line, and not just the beginning and end of the 22 | /// entire string. 23 | /// 24 | Multiline = 2, 25 | /// 26 | /// g - Eliminates unescaped white space from the pattern. 27 | /// 28 | IgnorePatternWhitespace = 4 29 | } 30 | } -------------------------------------------------------------------------------- /source/MongoDB/Protocol/IRequestMessage.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | 3 | namespace MongoDB.Protocol 4 | { 5 | /// 6 | /// A Message that is to be written to the database. 7 | /// 8 | internal interface IRequestMessage 9 | { 10 | /// 11 | /// Writes the specified stream. 12 | /// 13 | /// The stream. 14 | void Write (Stream stream); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /source/MongoDB/Protocol/MessageBase.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB.Protocol 2 | { 3 | /// 4 | /// Base class for all raw messages 5 | /// 6 | internal abstract class MessageBase 7 | { 8 | /// 9 | /// 10 | /// 11 | protected const int MaximumMessageSize = 1024 * 1024 * 4; 12 | 13 | /// 14 | /// Gets or sets the header. 15 | /// 16 | /// The header. 17 | public MessageHeader Header { get; protected set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /source/MongoDB/Protocol/MsgMessage.cs: -------------------------------------------------------------------------------- 1 | using MongoDB.Bson; 2 | 3 | namespace MongoDB.Protocol 4 | { 5 | /// 6 | /// Deprecated. OP_MSG sends a diagnostic message to the database. 7 | /// The database sends back a fixed resonse. 8 | /// 9 | /// 10 | /// struct { 11 | /// MsgHeader header; // standard message header 12 | /// cstring message; // message for the database 13 | /// } 14 | /// 15 | internal class MsgMessage : RequestMessageBase 16 | { 17 | /// 18 | /// Initializes a new instance of the class. 19 | /// 20 | public MsgMessage() 21 | : base(new BsonWriterSettings()){ 22 | Header = new MessageHeader(OpCode.Msg); 23 | } 24 | 25 | /// 26 | /// Gets or sets the message. 27 | /// 28 | /// The message. 29 | public string Message { get; set; } 30 | 31 | /// 32 | /// Writes the body. 33 | /// 34 | /// The writer. 35 | protected override void WriteBody(BsonWriter writer){ 36 | writer.Write(Message, false); 37 | } 38 | 39 | /// 40 | /// Calculates the size of the body. 41 | /// 42 | /// The writer. 43 | /// 44 | protected override int CalculateBodySize(BsonWriter writer){ 45 | return writer.CalculateSize(Message, false); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /source/MongoDB/Protocol/OpCode.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB.Protocol 2 | { 3 | /// 4 | /// 5 | internal enum OpCode 6 | { 7 | /// 8 | /// Reply to a client request. responseTo is set. 9 | /// 10 | Reply = 1, 11 | /// 12 | /// Generic msg command followed by a string. 13 | /// 14 | Msg = 1000, 15 | /// 16 | /// update document 17 | /// 18 | Update = 2001, 19 | /// 20 | /// insert new document 21 | /// 22 | Insert = 2002, 23 | /// 24 | /// is this used? 25 | /// 26 | GetByOid = 2003, 27 | /// 28 | /// query a collection 29 | /// 30 | Query = 2004, 31 | /// 32 | /// Get more data from a query. See Cursors. 33 | /// 34 | GetMore = 2005, 35 | /// 36 | /// Delete documents 37 | /// 38 | Delete = 2006, 39 | /// 40 | /// Tell database client is done with a cursor. 41 | /// 42 | KillCursors = 2007 43 | } 44 | } -------------------------------------------------------------------------------- /source/MongoDB/Protocol/RequestMessageBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using MongoDB.Bson; 4 | 5 | namespace MongoDB.Protocol 6 | { 7 | /// 8 | /// Description of Message. 9 | /// 10 | internal abstract class RequestMessageBase : MessageBase, IRequestMessage 11 | { 12 | private readonly BsonWriterSettings _bsonWriterSettings; 13 | 14 | /// 15 | /// Initializes a new instance of the class. 16 | /// 17 | /// The bson writer settings. 18 | protected RequestMessageBase(BsonWriterSettings bsonWriterSettings){ 19 | if(bsonWriterSettings == null) 20 | throw new ArgumentNullException("bsonWriterSettings"); 21 | 22 | _bsonWriterSettings = bsonWriterSettings; 23 | } 24 | 25 | /// 26 | /// Writes the specified stream. 27 | /// 28 | /// The stream. 29 | public void Write(Stream stream){ 30 | var header = Header; 31 | var bstream = new BufferedStream(stream); 32 | var writer = new BinaryWriter(bstream); 33 | var bwriter = new BsonWriter(bstream, _bsonWriterSettings); 34 | 35 | Header.MessageLength += CalculateBodySize(bwriter); 36 | if(Header.MessageLength > MaximumMessageSize) 37 | throw new MongoException("Maximum message length exceeded"); 38 | 39 | writer.Write(header.MessageLength); 40 | writer.Write(header.RequestId); 41 | writer.Write(header.ResponseTo); 42 | writer.Write((int)header.OpCode); 43 | writer.Flush(); 44 | WriteBody(bwriter); 45 | bwriter.Flush(); 46 | } 47 | 48 | /// 49 | /// Writes the body. 50 | /// 51 | /// The writer. 52 | protected abstract void WriteBody(BsonWriter writer); 53 | 54 | /// 55 | /// Calculates the size of the body. 56 | /// 57 | /// The writer. 58 | /// 59 | protected abstract int CalculateBodySize(BsonWriter writer); 60 | } 61 | } -------------------------------------------------------------------------------- /source/MongoDB/QueryOptions.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB 2 | { 3 | /// 4 | /// Query options 5 | /// 6 | /// 7 | /// Oplog replay: 8 (internal replication use only - drivers should not implement) 8 | /// 9 | public enum QueryOptions { 10 | /// 11 | /// None 12 | /// 13 | None = 0, 14 | /// 15 | /// Tailable cursor 16 | /// 17 | TailableCursor = 2, 18 | /// 19 | /// Slave OK 20 | /// 21 | SlaveOK = 4, 22 | /// 23 | /// No cursor timeout 24 | /// 25 | NoCursorTimeout = 16 26 | } 27 | } -------------------------------------------------------------------------------- /source/MongoDB/Results/CommandResultBase.cs: -------------------------------------------------------------------------------- 1 | using MongoDB.Attributes; 2 | 3 | namespace MongoDB.Results 4 | { 5 | /// 6 | /// 7 | /// 8 | public abstract class CommandResultBase 9 | { 10 | /// 11 | /// Initializes a new instance of the class. 12 | /// 13 | protected CommandResultBase(){ 14 | ExtendedProperties = new Document(); 15 | } 16 | 17 | /// 18 | /// Gets or sets the server error message. 19 | /// 20 | /// The error message. 21 | [MongoAlias("errmsg")] 22 | public string ErrorMessage { get; set; } 23 | 24 | /// 25 | /// Gets or sets a value indicating whether this is success. 26 | /// 27 | /// true if success; otherwise, false. 28 | [MongoAlias("ok")] 29 | public bool Success { get; set; } 30 | 31 | /// 32 | /// Gets or sets the extended properties. 33 | /// 34 | /// 35 | /// Stores all propertys which are not maped. 36 | /// 37 | /// The extended properties. 38 | public Document ExtendedProperties { get; set; } 39 | } 40 | } -------------------------------------------------------------------------------- /source/MongoDB/Results/FindAndModifyResult.cs: -------------------------------------------------------------------------------- 1 | using MongoDB.Attributes; 2 | 3 | namespace MongoDB.Results 4 | { 5 | internal class FindAndModifyResult : CommandResultBase 6 | { 7 | /// 8 | /// Gets or sets the value. 9 | /// 10 | /// The value. 11 | [MongoAlias("value")] 12 | public T Value { get; set; } 13 | } 14 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/Builders/DictionaryBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB.Serialization.Builders 4 | { 5 | /// 6 | /// 7 | /// 8 | public class DictionaryBuilder : IObjectBuilder 9 | { 10 | private readonly Document _document; 11 | private readonly Type _valueType; 12 | 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// Type of the value. 17 | public DictionaryBuilder(Type valueType) 18 | { 19 | _document = new Document(); 20 | _valueType = valueType; 21 | } 22 | 23 | /// 24 | /// Adds the property. 25 | /// 26 | /// The name. 27 | /// The value. 28 | public void AddProperty(string name, object value) 29 | { 30 | _document.Add(name, value); 31 | } 32 | 33 | /// 34 | /// Builds the object. 35 | /// 36 | /// 37 | public object BuildObject() 38 | { 39 | return _document; 40 | } 41 | 42 | /// 43 | /// Gets the property descriptor. 44 | /// 45 | /// The name. 46 | /// 47 | public PropertyDescriptor GetPropertyDescriptor(string name) 48 | { 49 | return new PropertyDescriptor { Type = _valueType, IsDictionary = false }; 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/Builders/DocumentBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB.Serialization.Builders 2 | { 3 | internal class DocumentBuilder : IObjectBuilder 4 | { 5 | private readonly Document _document; 6 | 7 | /// 8 | /// Initializes a new instance of the class. 9 | /// 10 | public DocumentBuilder() 11 | { 12 | _document = new Document(); 13 | } 14 | 15 | /// 16 | /// Adds the property. 17 | /// 18 | /// The name. 19 | /// The value. 20 | public void AddProperty(string name, object value) 21 | { 22 | _document.Add(name, value); 23 | } 24 | 25 | /// 26 | /// Builds the object. 27 | /// 28 | /// 29 | public object BuildObject() 30 | { 31 | return _document; 32 | } 33 | 34 | /// 35 | /// Gets the type of the property. 36 | /// 37 | /// The name. 38 | /// 39 | public PropertyDescriptor GetPropertyDescriptor(string name) 40 | { 41 | return new PropertyDescriptor(); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/Builders/IObjectBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB.Serialization.Builders 2 | { 3 | internal interface IObjectBuilder 4 | { 5 | void AddProperty(string name, object value); 6 | 7 | object BuildObject(); 8 | 9 | PropertyDescriptor GetPropertyDescriptor(string name); 10 | } 11 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/Builders/PropertyDescriptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB.Serialization.Builders 4 | { 5 | /// 6 | /// 7 | /// 8 | public class PropertyDescriptor 9 | { 10 | /// 11 | /// Gets or sets the type. 12 | /// 13 | /// The type. 14 | public Type Type { get; set; } 15 | 16 | /// 17 | /// Gets or sets a value indicating whether this instance is dictionary. 18 | /// 19 | /// 20 | /// true if this instance is dictionary; otherwise, false. 21 | /// 22 | public bool IsDictionary { get; set; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /source/MongoDB/Serialization/Descriptors/ArrayDescriptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections; 4 | using MongoDB.Bson; 5 | 6 | namespace MongoDB.Serialization.Descriptors 7 | { 8 | internal class ArrayDescriptor : IPropertyDescriptor 9 | { 10 | private readonly Type _elementType; 11 | private readonly IEnumerable _enumerable; 12 | 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The enumerable. 17 | /// Type of the element. 18 | public ArrayDescriptor(IEnumerable enumerable, Type elementType) 19 | { 20 | if (enumerable == null) 21 | throw new ArgumentNullException("enumerable"); 22 | if (elementType == null) 23 | throw new ArgumentNullException("elementType"); 24 | 25 | _elementType = elementType; 26 | _enumerable = enumerable; 27 | } 28 | 29 | /// 30 | /// Gets the properties. 31 | /// 32 | /// 33 | public IEnumerable GetProperties() 34 | { 35 | int i = 0; 36 | foreach (var element in _enumerable) 37 | { 38 | yield return new BsonProperty(i.ToString()) { Value = GetValue(element) }; 39 | i++; 40 | } 41 | } 42 | 43 | /// 44 | /// Gets the value. 45 | /// 46 | /// The value. 47 | /// 48 | private BsonPropertyValue GetValue(object value) 49 | { 50 | var type = _elementType ?? (value == null ? null : value.GetType()); 51 | 52 | return new BsonPropertyValue(type, value, false); 53 | } 54 | 55 | 56 | } 57 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/Descriptors/BsonPropertyValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MongoDB.Serialization.Descriptors 4 | { 5 | internal class BsonPropertyValue 6 | { 7 | public bool IsDictionary { get; private set; } 8 | 9 | public Type Type { get; private set; } 10 | 11 | public object Value { get; private set; } 12 | 13 | public BsonPropertyValue(Type type, object value, bool isDictionary) 14 | { 15 | Type = type; 16 | Value = value; 17 | IsDictionary = isDictionary; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/Descriptors/DictionaryPropertyDescriptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using MongoDB.Bson; 5 | 6 | namespace MongoDB.Serialization.Descriptors 7 | { 8 | /// 9 | /// 10 | /// 11 | public class DictionaryPropertyDescriptor : IPropertyDescriptor 12 | { 13 | private readonly Document _document; 14 | private readonly Type _valueType; 15 | 16 | /// 17 | /// Initializes a new instance of the class. 18 | /// 19 | /// The document. 20 | /// Type of the value. 21 | public DictionaryPropertyDescriptor(Document document, Type valueType) 22 | { 23 | _document = document; 24 | _valueType = valueType; 25 | } 26 | 27 | /// 28 | /// Gets the properties. 29 | /// 30 | /// 31 | public IEnumerable GetProperties() 32 | { 33 | return _document.Select(e => new BsonProperty(e.Key) { Value = new BsonPropertyValue(_valueType, e.Value, false) }); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/Descriptors/DocumentPropertyDescriptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using MongoDB.Bson; 5 | 6 | namespace MongoDB.Serialization.Descriptors 7 | { 8 | internal class DocumentPropertyDescriptor : IPropertyDescriptor 9 | { 10 | private readonly Document _document; 11 | 12 | /// 13 | /// Initializes a new instance of the class. 14 | /// 15 | /// The document. 16 | public DocumentPropertyDescriptor(Document document) 17 | { 18 | if (document == null) 19 | throw new ArgumentNullException("document"); 20 | _document = document; 21 | } 22 | 23 | /// 24 | /// Gets the properties. 25 | /// 26 | /// 27 | public IEnumerable GetProperties() 28 | { 29 | return _document.Select(pair => new BsonProperty(pair.Key) 30 | { 31 | Value = GetValue(pair.Value) 32 | }); 33 | } 34 | 35 | /// 36 | /// Gets the value. 37 | /// 38 | /// The value. 39 | /// 40 | private BsonPropertyValue GetValue(object value) 41 | { 42 | var valueType = value == null ? typeof(Document) : value.GetType(); 43 | return new BsonPropertyValue(valueType, value, false); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/Descriptors/IPropertyDescriptor.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using MongoDB.Bson; 3 | 4 | namespace MongoDB.Serialization.Descriptors 5 | { 6 | internal interface IPropertyDescriptor 7 | { 8 | IEnumerable GetProperties(); 9 | } 10 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/DocumentObjectDescriptorAdapter.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace MongoDB.Serialization 4 | { 5 | /// 6 | /// 7 | /// 8 | public class DocumentObjectDescriptorAdapter : IObjectDescriptor 9 | { 10 | /// 11 | /// Generates the id. 12 | /// 13 | /// The instance. 14 | /// 15 | public object GenerateId(object instance) 16 | { 17 | return Oid.NewOid(); 18 | } 19 | 20 | /// 21 | /// Gets the property value. 22 | /// 23 | /// The instance. 24 | /// Name of the mongo. 25 | /// 26 | public object GetPropertyValue(object instance, string mongoName) 27 | { 28 | return ((Document)instance)[mongoName]; 29 | } 30 | 31 | /// 32 | /// Sets the property value. 33 | /// 34 | /// The instance. 35 | /// Name of the mongo. 36 | /// The value. 37 | public void SetPropertyValue(object instance, string mongoName, object value) 38 | { 39 | ((Document)instance)[mongoName] = value; 40 | } 41 | 42 | /// 43 | /// Gets the mongo property names. 44 | /// 45 | /// The instance. 46 | /// 47 | public IEnumerable GetMongoPropertyNames(object instance) 48 | { 49 | return ((Document)instance).Keys; 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/IObjectDescriptor.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace MongoDB.Serialization 4 | { 5 | /// 6 | /// 7 | /// 8 | public interface IObjectDescriptor 9 | { 10 | /// 11 | /// Generates the id. 12 | /// 13 | /// The instance. 14 | /// 15 | object GenerateId(object instance); 16 | 17 | /// 18 | /// Gets the property value. 19 | /// 20 | /// The instance. 21 | /// Name of the mongo. 22 | /// 23 | object GetPropertyValue(object instance, string mongoName); 24 | 25 | /// 26 | /// Sets the property value. 27 | /// 28 | /// The instance. 29 | /// Name of the mongo. 30 | /// The value. 31 | void SetPropertyValue(object instance, string mongoName, object value); 32 | 33 | /// 34 | /// Gets the mongo property names. 35 | /// 36 | /// The instance. 37 | /// 38 | IEnumerable GetMongoPropertyNames(object instance); 39 | } 40 | } -------------------------------------------------------------------------------- /source/MongoDB/Serialization/ISerializationFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using MongoDB.Bson; 3 | 4 | namespace MongoDB.Serialization 5 | { 6 | /// 7 | /// 8 | /// 9 | public interface ISerializationFactory 10 | { 11 | /// 12 | /// Gets the bson writer settings. 13 | /// 14 | /// Type of the root. 15 | /// 16 | BsonWriterSettings GetBsonWriterSettings(Type rootType); 17 | 18 | /// 19 | /// Gets the name of the collection given the rootType. 20 | /// 21 | /// Type of the root. 22 | /// 23 | string GetCollectionName(Type rootType); 24 | 25 | /// 26 | /// Gets the object descriptor. 27 | /// 28 | /// The type. 29 | /// 30 | IObjectDescriptor GetObjectDescriptor(Type type); 31 | 32 | /// 33 | /// Gets the bson reader settings. 34 | /// 35 | /// Type of the root. 36 | /// 37 | BsonReaderSettings GetBsonReaderSettings(Type rootType); 38 | } 39 | } -------------------------------------------------------------------------------- /source/MongoDB/UpdateFlags.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDB 2 | { 3 | /// 4 | /// Update flags. 5 | /// 6 | /// 7 | /// Bits 2-31 are Reserved and must be set to 0. 8 | /// 9 | public enum UpdateFlags { 10 | /// 11 | /// Default none. 12 | /// 13 | None = 0, 14 | /// 15 | /// If set, the database will insert the supplied object into the collection if no matching document is found. 16 | /// 17 | Upsert = 1, 18 | /// 19 | /// If set, the database will update all matching objects in the collection. Otherwise only updates first matching doc. 20 | /// 21 | MultiUpdate = 2 22 | } 23 | } -------------------------------------------------------------------------------- /source/MongoDB/Util/MongoHash.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Security.Cryptography; 3 | using System.Text; 4 | 5 | namespace MongoDB.Util 6 | { 7 | /// 8 | /// 9 | /// 10 | internal static class MongoHash 11 | { 12 | /// 13 | /// Generate a hash for the specified text. 14 | /// 15 | /// The text. 16 | /// 17 | public static string Generate(string text) 18 | { 19 | var md5 = MD5.Create(); 20 | var hash = md5.ComputeHash(Encoding.Default.GetBytes(text)); 21 | return BitConverter.ToString(hash).Replace("-", "").ToLower(); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /source/MongoDB/Util/ScopedDictionary.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace MongoDB.Util 4 | { 5 | internal class ScopedDictionary 6 | { 7 | private readonly Dictionary _map; 8 | private readonly ScopedDictionary _previous; 9 | 10 | public ScopedDictionary(ScopedDictionary previous) 11 | { 12 | _previous = previous; 13 | _map = new Dictionary(); 14 | } 15 | 16 | public ScopedDictionary(ScopedDictionary previous, IEnumerable> pairs) 17 | : this(previous) 18 | { 19 | foreach(var p in pairs) 20 | _map.Add(p.Key, p.Value); 21 | } 22 | 23 | public void Add(TKey key, TValue value) 24 | { 25 | _map.Add(key, value); 26 | } 27 | 28 | public bool TryGetValue(TKey key, out TValue value) 29 | { 30 | for(var scope = this; scope != null; scope = scope._previous) 31 | if(scope._map.TryGetValue(key, out value)) 32 | return true; 33 | value = default(TValue); 34 | return false; 35 | } 36 | 37 | public bool ContainsKey(TKey key) 38 | { 39 | for(var scope = this; scope != null; scope = scope._previous) 40 | if(scope._map.ContainsKey(key)) 41 | return true; 42 | return false; 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /tools/Benchmark/AssemblyInfo.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: AssemblyTitle("MongoDB.Driver.Benchmark")] 7 | [assembly: AssemblyDescription("")] 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("")] 11 | [assembly: AssemblyCopyright("")] 12 | [assembly: AssemblyTrademark("")] 13 | [assembly: AssemblyCulture("")] 14 | 15 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". 16 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, 17 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. 18 | 19 | [assembly: AssemblyVersion("1.0.*")] 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 | //[assembly: AssemblyDelaySign(false)] 25 | //[assembly: AssemblyKeyFile("")] 26 | -------------------------------------------------------------------------------- /tools/MSBuild.Community.Tasks/ICSharpCode.SharpZipLib.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samus/mongodb-csharp/6397a0f032f24227894afa6ec37e92dd18dd0b3f/tools/MSBuild.Community.Tasks/ICSharpCode.SharpZipLib.dll -------------------------------------------------------------------------------- /tools/MSBuild.Community.Tasks/MSBuild.Community.Tasks.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samus/mongodb-csharp/6397a0f032f24227894afa6ec37e92dd18dd0b3f/tools/MSBuild.Community.Tasks/MSBuild.Community.Tasks.dll --------------------------------------------------------------------------------