-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDbFactory.cs
48 lines (40 loc) · 1.51 KB
/
MongoDbFactory.cs
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
38
39
40
41
42
43
44
45
46
47
48
using System.Security.Authentication;
using Collections.API.Factories.Interfaces;
using MongoDB.Driver;
namespace Collections.API.Factories
{
/// <summary>
/// The mongo db factory
/// </summary>
public class MongoDbFactory : IMongoDbFactory
{
/// <summary>
/// The MongoDB connection string
/// </summary>
private readonly string connectionString;
/// <summary>
/// The MongoDB collection
/// </summary>
private readonly string collection;
/// <summary>
/// Initializes a new instance of the <see cref="MongoDbFactory"/> class.
/// </summary>
public MongoDbFactory(string ConnectionString, string CollectionName)
{
this.connectionString = ConnectionString;
this.collection = CollectionName;
}
/// <summary>
/// Connects to Document DB collection.
/// </summary>
/// <returns><see cref="IMongoCollection{TDocument}"/> of collection from DB</returns>
public IMongoCollection<TInterface> ConnectToCollection<TInterface>()
{
MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(this.connectionString));
settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
var mongoClient = new MongoClient(settings);
var db = mongoClient.GetDatabase(this.collection);
return db.GetCollection<TInterface>(this.collection);
}
}
}