-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentDbContext.cs
45 lines (38 loc) · 1.29 KB
/
DocumentDbContext.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
using DbdocFramework.Abstracts;
using System.Linq;
using DbdocFramework.Extensions;
using DbdocFramework.MongoDbProvider.Settings;
namespace DbdocFramework
{
public abstract class DocumentDbContext
{
private IProvider Provider { get; }
protected DocumentDbContext(IProvider provider)
{
this.Provider = provider;
Setup();
}
protected DocumentDbContext(MongoDbContextSettings mongoSettings)
: this(new MongoDbProvider.MongoDbProvider(mongoSettings)) { }
public IDbSet<T> Set<T>()
where T : class
{
return this.Provider.GetDbSet<T>();
}
private void Setup()
{
var items = this.GetProperties().Where(p => p.PropertyType.GetGenericTypeDefinition() == typeof(IDbSet<>)).ToArray();
foreach (var prop in items)
{
var modelType = prop.PropertyType.GetGenericArguments()[0];
this.Provider.RegisterModel(modelType);
}
foreach (var prop in items)
{
var modelType = prop.PropertyType.GetGenericArguments()[0];
var instance = this.Provider.GetDbSet(modelType);
prop.SetValue(this, instance);
}
}
}
}