Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 29 additions & 49 deletions MyTaskListApp/DAL/Dal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,49 @@ 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.
public List<MyTask> GetAllTasks()
{
try
{
var collection = GetTasksCollection();
var collection = this.GetTasksCollection();
return collection.Find(new BsonDocument()).ToList();
}
catch (MongoConnectionException)
Expand All @@ -50,7 +66,7 @@ public List<MyTask> 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);
Expand All @@ -63,44 +79,8 @@ public void CreateTask(MyTask task)

private IMongoCollection<MyTask> 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<MongoCredential>()
{
new MongoCredential("SCRAM-SHA-1", identity, evidence)
};

MongoClient client = new MongoClient(settings);
var database = client.GetDatabase(dbName);
var todoTaskCollection = database.GetCollection<MyTask>(collectionName);
return todoTaskCollection;
}

private IMongoCollection<MyTask> 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<MongoCredential>()
{
new MongoCredential("SCRAM-SHA-1", identity, evidence)
};
MongoClient client = new MongoClient(settings);
var database = client.GetDatabase(dbName);
var todoTaskCollection = database.GetCollection<MyTask>(collectionName);
var database = this.client.GetDatabase(this.dbName);
var todoTaskCollection = database.GetCollection<MyTask>(this.collectionName).WithReadPreference(this.readPreference);
return todoTaskCollection;
}

Expand Down