diff --git a/MyTaskListApp/DAL/Dal.cs b/MyTaskListApp/DAL/Dal.cs index 3559e10..3adcec7 100644 --- a/MyTaskListApp/DAL/Dal.cs +++ b/MyTaskListApp/DAL/Dal.cs @@ -12,25 +12,41 @@ namespace MyTaskListApp { public class Dal : IDisposable { - //private MongoServer mongoServer = null; private bool disposed = false; - // To do: update the connection string with the DNS name - // or IP address of your server. - //For example, "mongodb://testlinux.cloudapp.net - private string userName = "FILLME"; - private string host = "FILLME"; - private string password = "FILLME"; + //private MongoClient + private readonly MongoClient client; + + //Copy connection string from Azure portal + //Go to Quick Start pane, Choose .NET platform + //Copy the connection string provided there below + private readonly string connectionString = @""; + + //ReadPreference setting + //For LOAD DISTRIBUTION SCENARIO + //we set this to SecondaryPreferred + //The idea is to use secondaries for Read loads + //And Primary/Write region is only used for Write requests + //If there are no Read regions configured for the account + //then read load also goes to Write region. + private readonly ReadPreference readPreference = ReadPreference.SecondaryPreferred; // This sample uses a database named "Tasks" and a //collection named "TasksList". The database and collection //will be automatically created if they don't already exist. - private string dbName = "Tasks"; - private string collectionName = "TasksList"; + private readonly string dbName = "Tasks"; + private readonly string collectionName = "TasksList"; // Default constructor. public Dal() { + MongoClientSettings settings = MongoClientSettings.FromUrl( + new MongoUrl(this.connectionString)); + settings.SslSettings = + new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 }; + + //Initialize the client once and reuse it for all requests + this.client = new MongoClient(settings); } // Gets all Task items from the MongoDB server. @@ -38,7 +54,7 @@ public List GetAllTasks() { try { - var collection = GetTasksCollection(); + var collection = this.GetTasksCollection(); return collection.Find(new BsonDocument()).ToList(); } catch (MongoConnectionException) @@ -50,7 +66,7 @@ public List GetAllTasks() // Creates a Task and inserts it into the collection in MongoDB. public void CreateTask(MyTask task) { - var collection = GetTasksCollectionForEdit(); + var collection = this.GetTasksCollection(); try { collection.InsertOne(task); @@ -63,44 +79,8 @@ public void CreateTask(MyTask task) private IMongoCollection GetTasksCollection() { - MongoClientSettings settings = new MongoClientSettings(); - settings.Server = new MongoServerAddress(host, 10255); - settings.UseSsl = true; - settings.SslSettings = new SslSettings(); - settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12; - - MongoIdentity identity = new MongoInternalIdentity(dbName, userName); - MongoIdentityEvidence evidence = new PasswordEvidence(password); - - settings.Credentials = new List() - { - new MongoCredential("SCRAM-SHA-1", identity, evidence) - }; - - MongoClient client = new MongoClient(settings); - var database = client.GetDatabase(dbName); - var todoTaskCollection = database.GetCollection(collectionName); - return todoTaskCollection; - } - - private IMongoCollection GetTasksCollectionForEdit() - { - MongoClientSettings settings = new MongoClientSettings(); - settings.Server = new MongoServerAddress(host, 10255); - settings.UseSsl = true; - settings.SslSettings = new SslSettings(); - settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12; - - MongoIdentity identity = new MongoInternalIdentity(dbName, userName); - MongoIdentityEvidence evidence = new PasswordEvidence(password); - - settings.Credentials = new List() - { - new MongoCredential("SCRAM-SHA-1", identity, evidence) - }; - MongoClient client = new MongoClient(settings); - var database = client.GetDatabase(dbName); - var todoTaskCollection = database.GetCollection(collectionName); + var database = this.client.GetDatabase(this.dbName); + var todoTaskCollection = database.GetCollection(this.collectionName).WithReadPreference(this.readPreference); return todoTaskCollection; }