The settings used to access a database.
        public void TestEquals()
        {
            var settings = new MongoDatabaseSettings();
            var clone = settings.Clone();
            Assert.IsTrue(clone.Equals(settings));

            settings.Freeze();
            clone.Freeze();
            Assert.IsTrue(clone.Equals(settings));

            clone = settings.Clone();
            clone.GuidRepresentation = GuidRepresentation.PythonLegacy;
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.ReadConcern = ReadConcern.Majority;
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.ReadPreference = ReadPreference.Secondary;
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.WriteConcern = WriteConcern.W2;
            Assert.IsFalse(clone.Equals(settings));
        }
        public void TestAll() {
            var settings = new MongoDatabaseSettings(
                "database",
                MongoCredentials.Create("username", "password"),
                GuidRepresentation.CSharpLegacy,
                SafeMode.Create(5, TimeSpan.FromSeconds(5)),
                true
            );

            Assert.AreEqual("database", settings.DatabaseName);
            Assert.AreEqual(MongoCredentials.Create("username", "password"), settings.Credentials);
            Assert.AreEqual(GuidRepresentation.CSharpLegacy, settings.GuidRepresentation);
            Assert.AreEqual(SafeMode.Create(5, TimeSpan.FromSeconds(5)), settings.SafeMode);
            Assert.AreEqual(true, settings.SlaveOk);

            Assert.IsFalse(settings.IsFrozen);
            var hashCode = settings.GetHashCode();
            var stringRepresentation = settings.ToString();
            Assert.AreEqual(settings, settings);

            settings.Freeze();
            Assert.IsTrue(settings.IsFrozen);
            Assert.AreEqual(hashCode, settings.GetHashCode());
            Assert.AreEqual(stringRepresentation, settings.ToString());
        }
 public void TestConstructorArgumentChecking()
 {
     var settings = new MongoDatabaseSettings(_server, "");
     Assert.Throws<ArgumentNullException>(() => { new MongoDatabase(null, settings); });
     Assert.Throws<ArgumentNullException>(() => { new MongoDatabase(_server, null); });
     Assert.Throws<ArgumentOutOfRangeException>(() => { new MongoDatabase(_server, settings); });
 }
        public void TestEquals()
        {
            var settings = new MongoDatabaseSettings();
            var clone = settings.Clone();
            Assert.IsTrue(clone.Equals(settings));

            settings.Freeze();
            clone.Freeze();
            Assert.IsTrue(clone.Equals(settings));

            clone = settings.Clone();
            clone.GuidRepresentation = GuidRepresentation.PythonLegacy;
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.OperationTimeout = TimeSpan.FromMilliseconds(20);
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.ReadPreference = ReadPreference.Secondary;
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.WriteConcern = WriteConcern.W2;
            Assert.IsFalse(clone.Equals(settings));
        }
 public static IMongoDatabase GetDatabase(string database, MongoDatabaseSettings dbSettings = null)
 {
     if(string.IsNullOrWhiteSpace(database))
         throw new ArgumentNullException("database");
     
     return Client.GetDatabase(database, dbSettings);
 }
        public void TestAll()
        {
            var server = MongoServer.Create();
            var settings = new MongoDatabaseSettings(server, "database")
            {
                Credentials = MongoCredentials.Create("username", "password"),
                SafeMode = SafeMode.Create(5, TimeSpan.FromSeconds(5)),
                ReadPreference = ReadPreference.Primary
            };

            Assert.AreEqual("database", settings.DatabaseName);
            Assert.AreEqual(MongoCredentials.Create("username", "password"), settings.Credentials);
            Assert.AreEqual(GuidRepresentation.CSharpLegacy, settings.GuidRepresentation);
            Assert.AreEqual(SafeMode.Create(5, TimeSpan.FromSeconds(5)), settings.SafeMode);
            Assert.AreEqual(ReadPreference.Primary, settings.ReadPreference);

            Assert.IsFalse(settings.IsFrozen);
            var hashCode = settings.GetHashCode();
            var stringRepresentation = settings.ToString();
            Assert.AreEqual(settings, settings);

            settings.Freeze();
            Assert.IsTrue(settings.IsFrozen);
            Assert.AreEqual(hashCode, settings.GetHashCode());
            Assert.AreEqual(stringRepresentation, settings.ToString());
        }
        public void TestEquals()
        {
            var settings = new MongoDatabaseSettings();
            var clone = settings.Clone();
            Assert.IsTrue(clone.Equals(settings));

            settings.Freeze();
            clone.Freeze();
            Assert.IsTrue(clone.Equals(settings));

            clone = settings.Clone();
            clone.GuidRepresentation = GuidRepresentation.PythonLegacy;
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.ReadPreference = ReadPreference.Secondary;
            Assert.IsFalse(clone.Equals(settings));

#pragma warning disable 618
            clone = settings.Clone();
            clone.SafeMode = SafeMode.W2;
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.SlaveOk = !settings.SlaveOk;
            Assert.IsFalse(clone.Equals(settings));
#pragma warning restore

            clone = settings.Clone();
            clone.WriteConcern = WriteConcern.W2;
            Assert.IsFalse(clone.Equals(settings));
        }
 public void TestConstructor()
 {
     var settings = new MongoDatabaseSettings();
     Assert.AreEqual(GuidRepresentation.Unspecified, settings.GuidRepresentation);
     Assert.AreEqual(null, settings.ReadPreference);
     Assert.AreEqual(null, settings.WriteConcern);
 }
Beispiel #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnitOfWork"/> class.
 /// </summary>
 public UnitOfWork()
 {
     const string dbName = "GuestBook";
     const string connectionString = "mongodb://localhost"; //TODO:USer App setting helper
     var client = new MongoClient(connectionString);
     var server = client.GetServer();
     var settings = new MongoDatabaseSettings();
     _database = server.GetDatabase(dbName, settings);//TODO:User app sertting helper
 }
Beispiel #10
0
        public static void Register(string databaseName, MongoDatabaseSettings settings = null)
        {
            if (_instance != null)
            {
                throw new Exception("Instance has been generated. Please call method DataStore.Instance for using");
            }

            _instance = new Lazy<DataStore>(() => new DataStore(databaseName, settings));
        }
        public void TestConstructor()
        {
            var settings = new MongoDatabaseSettings();
            Assert.AreEqual(GuidRepresentation.Unspecified, settings.GuidRepresentation);
            Assert.AreEqual(null, settings.ReadPreference);
#pragma warning disable 618
            Assert.AreEqual(null, settings.SafeMode);
#pragma warning restore
            Assert.AreEqual(null, settings.WriteConcern);
        }
Beispiel #12
0
 public static MongoDatabase GetMongoDatabase()
 {
     var url = GetUrl();
     var settings = new MongoDatabaseSettings
     {
         GuidRepresentation = GuidRepresentation.Standard,
         WriteConcern = WriteConcern.Acknowledged
     };
     var mongoDatabase = new MongoClient(url).GetServer().GetDatabase(url.DatabaseName, settings);
     return mongoDatabase;
 }
 public void Setup()
 {
     var settings = new MongoDatabaseSettings();
     settings.ApplyDefaultValues(new MongoClientSettings());
     _operationExecutor = new MockOperationExecutor();
     _subject = new MongoDatabaseImpl(
         new DatabaseNamespace("foo"),
         settings,
         Substitute.For<ICluster>(),
         _operationExecutor);
 }
 public void TestFrozenCopy()
 {
     var server = MongoServer.Create();
     var settings = new MongoDatabaseSettings(server, "database");
     var frozenCopy = settings.FrozenCopy();
     var secondFrozenCopy = frozenCopy.FrozenCopy();
     Assert.AreNotSame(settings, frozenCopy);
     Assert.AreSame(frozenCopy, secondFrozenCopy);
     Assert.AreEqual(false, settings.IsFrozen);
     Assert.AreEqual(true, frozenCopy.IsFrozen);
 }
 public MongoDatabaseImplTests()
 {
     var settings = new MongoDatabaseSettings();
     settings.ApplyDefaultValues(new MongoClientSettings());
     _operationExecutor = new MockOperationExecutor();
     _subject = new MongoDatabaseImpl(
         new Mock<IMongoClient>().Object,
         new DatabaseNamespace("foo"),
         settings,
         new Mock<ICluster>().Object,
         _operationExecutor);
 }
 public void TestClone()
 {
     // set everything to non default values to test that all settings are cloned
     var settings = new MongoDatabaseSettings
     {
         GuidRepresentation = GuidRepresentation.PythonLegacy,
         ReadPreference = ReadPreference.Secondary,
         WriteConcern = WriteConcern.W2
     };
     var clone = settings.Clone();
     Assert.IsTrue(clone.Equals(settings));
 }
 public override MongoDatabase GetDatabase(string databaseName, MongoDatabaseSettings databaseSettings)
 {
     if (databaseName == null)
     {
         throw new ArgumentNullException("databaseName");
     }
     if (databaseSettings == null)
     {
         throw new ArgumentNullException("databaseSettings");
     }
     return new ProfiledMongoDatabase(this, databaseName, databaseSettings);
 }
        public void TestAll()
        {
            var settings = new MongoDatabaseSettings
            {
                GuidRepresentation = GuidRepresentation.PythonLegacy,
                ReadPreference = ReadPreference.Primary,
                WriteConcern = WriteConcern.Acknowledged
            };

            Assert.AreEqual(GuidRepresentation.PythonLegacy, settings.GuidRepresentation);
            Assert.AreSame(ReadPreference.Primary, settings.ReadPreference);
            Assert.AreSame(WriteConcern.Acknowledged, settings.WriteConcern);
        }
 public void Setup()
 {
     var settings = new MongoCollectionSettings();
     var dbSettings = new MongoDatabaseSettings();
     dbSettings.ApplyDefaultValues(new MongoServerSettings());
     settings.ApplyDefaultValues(dbSettings);
     _operationExecutor = new MockOperationExecutor();
     _subject = new MongoCollectionImpl<BsonDocument>(
         new CollectionNamespace("foo", "bar"),
         settings,
         Substitute.For<ICluster>(),
         _operationExecutor);
 }
 public void TestClone()
 {
     // set everything to non default values to test that all settings are cloned
     var settings = new MongoDatabaseSettings(_server, "database")
     {
         Credentials = MongoCredentials.Create("username", "password"),
         GuidRepresentation = GuidRepresentation.PythonLegacy,
         ReadPreference = ReadPreference.Secondary,
         WriteConcern = WriteConcern.W2
     };
     var clone = settings.Clone();
     Assert.IsTrue(clone.Equals(settings));
 }
        public void TestConstructor()
        {
            var settings = new MongoDatabaseSettings(_server, "database");
            Assert.AreEqual("database", settings.DatabaseName);
            Assert.AreEqual(null, settings.Credentials);
            Assert.AreEqual(MongoDefaults.GuidRepresentation, settings.GuidRepresentation);
            Assert.AreEqual(ReadPreference.Primary, settings.ReadPreference);
#pragma warning disable 618
            Assert.AreEqual(new SafeMode(false) { W = 1 }, settings.SafeMode);
            Assert.AreEqual(false, settings.SlaveOk);
#pragma warning restore
            Assert.AreEqual(WriteConcern.Acknowledged, settings.WriteConcern);
        }
        public void TestAll()
        {
            var settings = new MongoDatabaseSettings
            {
                GuidRepresentation = GuidRepresentation.PythonLegacy,
                OperationTimeout = TimeSpan.FromMilliseconds(20),
                ReadPreference = ReadPreference.Primary,
                WriteConcern = WriteConcern.Acknowledged
            };

            Assert.AreEqual(GuidRepresentation.PythonLegacy, settings.GuidRepresentation);
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), settings.OperationTimeout);
            Assert.AreSame(ReadPreference.Primary, settings.ReadPreference);
            Assert.AreSame(WriteConcern.Acknowledged, settings.WriteConcern);
        }
        public void TestAll()
        {
            var settings = new MongoDatabaseSettings
            {
                GuidRepresentation = GuidRepresentation.PythonLegacy,
                ReadPreference = ReadPreference.Primary,
                WriteConcern = WriteConcern.Acknowledged
            };

            Assert.AreEqual(GuidRepresentation.PythonLegacy, settings.GuidRepresentation);
            Assert.AreSame(ReadPreference.Primary, settings.ReadPreference);
#pragma warning disable 618
            Assert.AreEqual(new SafeMode(true), settings.SafeMode);
#pragma warning restore
            Assert.AreSame(WriteConcern.Acknowledged, settings.WriteConcern);
        }
        public void TestAllObsolete()
        {
#pragma warning disable 618
            var settings = new MongoDatabaseSettings(_server, "database")
            {
                GuidRepresentation = GuidRepresentation.PythonLegacy,
                ReadPreference = ReadPreference.Primary,
                WriteConcern = WriteConcern.Acknowledged
            };

            Assert.AreEqual("database", settings.DatabaseName);
            Assert.AreEqual(GuidRepresentation.PythonLegacy, settings.GuidRepresentation);
            Assert.AreSame(ReadPreference.Primary, settings.ReadPreference);
            Assert.AreEqual(new SafeMode(true), settings.SafeMode);
            Assert.AreEqual(false, settings.SlaveOk);
            Assert.AreSame(WriteConcern.Acknowledged, settings.WriteConcern);
#pragma warning restore
        }
        public void TestAll()
        {
            var settings = new MongoDatabaseSettings
            {
                Credentials = MongoCredentials.Create("username", "password"),
                GuidRepresentation = GuidRepresentation.PythonLegacy,
                ReadPreference = ReadPreference.Primary,
                WriteConcern = WriteConcern.Acknowledged
            };

            Assert.AreEqual(MongoCredentials.Create("username", "password"), settings.Credentials);
            Assert.AreEqual(GuidRepresentation.PythonLegacy, settings.GuidRepresentation);
            Assert.AreSame(ReadPreference.Primary, settings.ReadPreference);
#pragma warning disable 618
            Assert.AreEqual(new SafeMode(false) { W = 1 }, settings.SafeMode);
#pragma warning restore
            Assert.AreSame(WriteConcern.Acknowledged, settings.WriteConcern);
        }
        public void TestAll()
        {
            var settings = new MongoDatabaseSettings(_server, "database")
            {
                Credentials = MongoCredentials.Create("username", "password"),
                GuidRepresentation = GuidRepresentation.PythonLegacy,
                ReadPreference = ReadPreference.Primary,
                WriteConcern = WriteConcern.Errors
            };

            Assert.AreEqual("database", settings.DatabaseName);
            Assert.AreEqual(MongoCredentials.Create("username", "password"), settings.Credentials);
            Assert.AreEqual(GuidRepresentation.PythonLegacy, settings.GuidRepresentation);
            Assert.AreSame(ReadPreference.Primary, settings.ReadPreference);
#pragma warning disable 618
            Assert.AreEqual(new SafeMode(true), settings.SafeMode);
            Assert.AreEqual(false, settings.SlaveOk);
#pragma warning restore
            Assert.AreSame(WriteConcern.Errors, settings.WriteConcern);
        }
Beispiel #27
0
 public MongoDatabase(MongoServer server, MongoDatabaseSettings settings)
     : this(server, settings.DatabaseName, settings)
 {
 }
 /// <summary>
 /// 获取数据库
 /// </summary>
 /// <param name="name"></param>
 /// <param name="settings"></param>
 /// <returns></returns>
 public virtual IMongoDatabase GetDatabase(string name, MongoDatabaseSettings settings = null)
 {
     return(Context.GetDatabase(name, settings));
 }
        // constructors
        public MongoDatabaseImpl(IMongoClient client, DatabaseNamespace databaseNamespace, MongoDatabaseSettings settings, ICluster cluster, IOperationExecutor operationExecutor)
        {
            _client            = Ensure.IsNotNull(client, nameof(client));
            _databaseNamespace = Ensure.IsNotNull(databaseNamespace, nameof(databaseNamespace));
            _settings          = Ensure.IsNotNull(settings, nameof(settings)).Freeze();
            _cluster           = Ensure.IsNotNull(cluster, nameof(cluster));
            _operationExecutor = Ensure.IsNotNull(operationExecutor, nameof(operationExecutor));

            _messageEncoderSettings = new MessageEncoderSettings
            {
                { MessageEncoderSettingsName.GuidRepresentation, _settings.GuidRepresentation },
                { MessageEncoderSettingsName.ReadEncoding, _settings.ReadEncoding ?? Utf8Encodings.Strict },
                { MessageEncoderSettingsName.WriteEncoding, _settings.WriteEncoding ?? Utf8Encodings.Strict }
            };
        }
        /// <inheritdoc/>
        public IMongoDatabase GetDatabase(string name)
        {
            var settings = new MongoDatabaseSettings();

            return(GetDatabase(name, settings));
        }
Beispiel #31
0
 /// <summary>
 /// Gets the database.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>An implementation of a database.</returns>
 public IMongoDatabase GetDatabase(string name, MongoDatabaseSettings settings)
 {
     return(new MongoDatabaseImpl(new DatabaseNamespace(name), settings, _cluster, _operationExecutor));
 }
Beispiel #32
0
 // public methods
 /// <summary>
 /// Gets a MongoDatabase instance representing a database. See also GetServer.
 /// </summary>
 /// <param name="databaseSettings">The settings to use with this database.</param>
 /// <returns>An instance of MongoDatabase.</returns>
 public MongoDatabase GetDatabase(MongoDatabaseSettings databaseSettings)
 {
     return(GetServer().GetDatabase(databaseSettings));
 }
 /// <inheritdoc />
 public abstract IMongoDatabase GetDatabase(string name, MongoDatabaseSettings settings = null);
        // constructors
        public MongoDatabaseImpl(IMongoClient client, DatabaseNamespace databaseNamespace, MongoDatabaseSettings settings, ICluster cluster, IOperationExecutor operationExecutor)
        {
            _client            = Ensure.IsNotNull(client, nameof(client));
            _databaseNamespace = Ensure.IsNotNull(databaseNamespace, nameof(databaseNamespace));
            _settings          = Ensure.IsNotNull(settings, nameof(settings)).Freeze();
            _cluster           = Ensure.IsNotNull(cluster, nameof(cluster));
            _operationExecutor = Ensure.IsNotNull(operationExecutor, nameof(operationExecutor));

            _messageEncoderSettings = new MessageEncoderSettings
            {
                { MessageEncoderSettingsName.ReadEncoding, _settings.ReadEncoding ?? Utf8Encodings.Strict },
                { MessageEncoderSettingsName.WriteEncoding, _settings.WriteEncoding ?? Utf8Encodings.Strict }
            };
#pragma warning disable 618
            if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
            {
                _messageEncoderSettings.Add(MessageEncoderSettingsName.GuidRepresentation, _settings.GuidRepresentation);
            }
#pragma warning restore 618
        }
Beispiel #35
0
 // constructors
 public MongoDatabaseImpl(IMongoClient client, DatabaseNamespace databaseNamespace, MongoDatabaseSettings settings, ICluster cluster, IOperationExecutor operationExecutor)
 {
     _client            = Ensure.IsNotNull(client, nameof(client));
     _databaseNamespace = Ensure.IsNotNull(databaseNamespace, nameof(databaseNamespace));
     _settings          = Ensure.IsNotNull(settings, nameof(settings)).Freeze();
     _cluster           = Ensure.IsNotNull(cluster, nameof(cluster));
     _operationExecutor = Ensure.IsNotNull(operationExecutor, nameof(operationExecutor));
 }
Beispiel #36
0
 // constructors
 public MongoDatabaseImpl(IMongoClient client, DatabaseNamespace databaseNamespace, MongoDatabaseSettings settings, ICluster cluster, IOperationExecutor operationExecutor)
 {
     _client            = Ensure.IsNotNull(client, "client");
     _databaseNamespace = Ensure.IsNotNull(databaseNamespace, "databaseNamespace");
     _settings          = Ensure.IsNotNull(settings, "settings").Freeze();
     _cluster           = Ensure.IsNotNull(cluster, "cluster");
     _operationExecutor = Ensure.IsNotNull(operationExecutor, "operationExecutor");
 }
Beispiel #37
0
        /// <summary>
        /// Gets a MongoDatabase instance representing a database on this server. Only one instance
        /// is created for each combination of database settings.
        /// </summary>
        /// <param name="databaseName">The name of the database.</param>
        /// <returns>A new or existing instance of MongoDatabase.</returns>
        public virtual MongoDatabase GetDatabase(string databaseName)
        {
            var databaseSettings = new MongoDatabaseSettings();

            return(GetDatabase(databaseName, databaseSettings));
        }
Beispiel #38
0
 public virtual MongoDatabase GetDatabase(MongoDatabaseSettings databaseSettings)
 {
     return(GetDatabase(databaseSettings.DatabaseName, databaseSettings));
 }
Beispiel #39
0
 public virtual MongoDatabase this[MongoDatabaseSettings databaseSettings]
 {
     get { return(GetDatabase(databaseSettings)); }
 }
        /// <inheritdoc/>
        public sealed override IMongoDatabase GetDatabase(string name, MongoDatabaseSettings settings = null)
        {
            settings = settings == null ?
                new MongoDatabaseSettings() :
                settings.Clone();

            settings.ApplyDefaultValues(_settings);

            return new MongoDatabaseImpl(this, new DatabaseNamespace(name), settings, _cluster, _operationExecutor);
        }
 public MongoDatabase(MongoServer server, string name, MongoDatabaseSettings settings)
     : this(server, name, settings, new DefaultLegacyOperationExecutor())
 {
 }
 // internal methods
 internal void ApplyDefaultValues(MongoDatabaseSettings databaseSettings)
 {
     if (!_assignIdOnInsert.HasBeenSet)
     {
         AssignIdOnInsert = MongoDefaults.AssignIdOnInsert;
     }
     if (!_guidRepresentation.HasBeenSet)
     {
         GuidRepresentation = databaseSettings.GuidRepresentation;
     }
     if (!_readEncoding.HasBeenSet)
     {
         ReadEncoding = databaseSettings.ReadEncoding;
     }
     if (!_readPreference.HasBeenSet)
     {
         ReadPreference = databaseSettings.ReadPreference;
     }
     if (!_writeConcern.HasBeenSet)
     {
         WriteConcern = databaseSettings.WriteConcern;
     }
     if (!_writeEncoding.HasBeenSet)
     {
         WriteEncoding = databaseSettings.WriteEncoding;
     }
 }
 public void TestGetDatabase()
 {
     var settings = new MongoDatabaseSettings { ReadPreference = ReadPreference.Primary };
     var database = _server.GetDatabase("test", settings);
     Assert.AreEqual("test", database.Name);
     Assert.AreEqual(ReadPreference.Primary, database.Settings.ReadPreference);
 }
Beispiel #44
-1
        private DataStore(string databaseName, MongoDatabaseSettings settings = null)
        {
            _client = new MongoClient();
            _database = _client.GetDatabase(databaseName, settings);

            DatabaseName = databaseName;
        }