-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMongoDBDatabase.cs
More file actions
54 lines (43 loc) · 1.49 KB
/
MongoDBDatabase.cs
File metadata and controls
54 lines (43 loc) · 1.49 KB
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
49
50
51
52
53
54
using System;
using System.Threading;
using System.Threading.Tasks;
using Humanizer;
using ManagedCode.Database.Core;
using ManagedCode.Database.Core.Common;
using MongoDB.Bson;
using MongoDB.Driver;
namespace ManagedCode.Database.MongoDB;
public class MongoDBDatabase : BaseDatabase<IMongoDatabase>
{
private readonly MongoDBOptions _dbOptions;
public MongoDBDatabase(MongoDBOptions dbOptions)
{
_dbOptions = dbOptions;
}
public override Task DeleteAsync(CancellationToken token = default)
{
throw new NotImplementedException();
}
protected override Task InitializeAsyncInternal(CancellationToken token = default)
{
var client = new MongoClient(_dbOptions.ConnectionString);
NativeClient = client.GetDatabase(_dbOptions.DataBaseName);
return Task.CompletedTask;
}
protected override ValueTask DisposeAsyncInternal()
{
return new ValueTask(Task.CompletedTask);
}
protected override void DisposeInternal()
{
}
public MongoDBCollection<TItem> GetCollection<TItem>() where TItem : MongoDBItem, new()
{
if (!IsInitialized) throw new DatabaseNotInitializedException(GetType());
var collectionName = string.IsNullOrEmpty(_dbOptions.CollectionName)
? typeof(TItem).Name.Pluralize()
: _dbOptions.CollectionName;
return new MongoDBCollection<TItem>(
NativeClient.GetCollection<TItem>(collectionName, new MongoCollectionSettings()));
}
}