-
Notifications
You must be signed in to change notification settings - Fork 84
/
MongoDbContext.cs
153 lines (140 loc) · 6.41 KB
/
MongoDbContext.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using MongoDB.Driver;
using MongoDbGenericRepository.Attributes;
using MongoDbGenericRepository.Utils;
using System.Linq;
using System.Reflection;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Serializers;
using MongoDbGenericRepository.Internals;
namespace MongoDbGenericRepository
{
/// <summary>
/// The MongoDb context
/// </summary>
public class MongoDbContext : IMongoDbContext
{
/// <summary>
/// The IMongoClient from the official MongoDB driver
/// </summary>
public IMongoClient Client { get; }
/// <summary>
/// The IMongoDatabase from the official MongoDB driver
/// </summary>
public IMongoDatabase Database { get; }
/// <summary>
/// The constructor of the MongoDbContext, it needs an object implementing <see cref="IMongoDatabase"/>.
/// </summary>
/// <param name="mongoDatabase">An object implementing IMongoDatabase</param>
public MongoDbContext(IMongoDatabase mongoDatabase)
{
// Avoid legacy UUID representation: use Binary 0x04 subtype.
InitializeGuidRepresentation();
Database = mongoDatabase;
Client = Database.Client;
}
/// <summary>
/// The constructor of the MongoDbContext, it needs a connection string and a database name.
/// </summary>
/// <param name="connectionString">The connections string.</param>
/// <param name="databaseName">The name of your database.</param>
public MongoDbContext(string connectionString, string databaseName)
{
InitializeGuidRepresentation();
Client = new MongoClient(connectionString);
Database = Client.GetDatabase(databaseName);
}
/// <summary>
/// Initialise an instance of a <see cref="IMongoDbContext"/> using a connection string
/// </summary>
/// <param name="connectionString"></param>
public MongoDbContext(string connectionString)
: this(connectionString, new MongoUrl(connectionString).DatabaseName)
{
}
/// <summary>
/// The constructor of the MongoDbContext, it needs a connection string and a database name.
/// </summary>
/// <param name="client">The MongoClient.</param>
/// <param name="databaseName">The name of your database.</param>
public MongoDbContext(MongoClient client, string databaseName)
{
InitializeGuidRepresentation();
Client = client;
Database = client.GetDatabase(databaseName);
}
/// <summary>
/// Returns a collection for a document type. Also handles document types with a partition key.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="partitionKey">The optional value of the partition key.</param>
public virtual IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
{
return Database.GetCollection<TDocument>(GetCollectionName<TDocument>(partitionKey));
}
/// <summary>
/// Drops a collection, use very carefully.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
public virtual void DropCollection<TDocument>(string partitionKey = null)
{
Database.DropCollection(GetCollectionName<TDocument>(partitionKey));
}
/// <summary>
/// Sets the Guid representation of the MongoDB Driver.
/// </summary>
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
public virtual void SetGuidRepresentation(GuidRepresentation guidRepresentation)
{
// GuidRepresentation and GuidRepresentationMode will be removed in the next major release of the MongoDB Driver.
// We can safely replace this with RepositorySerializationProvider.Instance.RegisterSerializer once we upgrade to the next major release.
#pragma warning disable CS0618
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
RepositorySerializationProvider.Instance.RegisterSerializer(new GuidSerializer(guidRepresentation));
#pragma warning restore CS0618
}
/// <summary>
/// Extracts the CollectionName attribute from the entity type, if any.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <returns>The name of the collection in which the TDocument is stored.</returns>
protected virtual string GetAttributeCollectionName<TDocument>()
{
return (typeof(TDocument).GetTypeInfo()
.GetCustomAttributes(typeof(CollectionNameAttribute))
.FirstOrDefault() as CollectionNameAttribute)?.Name;
}
/// <summary>
/// Initialize the Guid representation of the MongoDB Driver.
/// Override this method to change the default GuidRepresentation.
/// </summary>
protected virtual void InitializeGuidRepresentation()
{
// by default, avoid legacy UUID representation: use Binary 0x04 subtype.
SetGuidRepresentation(GuidRepresentation.Standard);
}
/// <summary>
/// Given the document type and the partition key, returns the name of the collection it belongs to.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="partitionKey">The value of the partition key.</param>
/// <returns>The name of the collection.</returns>
protected virtual string GetCollectionName<TDocument>(string partitionKey)
{
var collectionName = GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>();
if (string.IsNullOrEmpty(partitionKey))
{
return collectionName;
}
return $"{partitionKey}-{collectionName}";
}
/// <summary>
/// Very naively pluralizes a TDocument type name.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <returns>The pluralized document name.</returns>
protected virtual string Pluralize<TDocument>()
{
return (typeof(TDocument).Name.Pluralize()).Camelize();
}
}
}