You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+101-7Lines changed: 101 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -148,7 +148,8 @@ When using the `AddMongo()` method, multiple conventions are added automatically
148
148
-`EnumRepresentationConvention(BsonType.String)`, so changing an enum member name is a breaking change
149
149
-`DateTime` and `DateTimeOffset` are serialized as `DateTime` instead of the default Ticks or (Ticks, Offset). In MongoDB, DateTime only supports precision up to the milliseconds. If you need more precision, you need to set the serializer at property level.
150
150
151
-
## Declaring and using MongoDB documents and collections
151
+
## Declaring Mongo Documents
152
+
### With Attributes Decoration
152
153
153
154
The process doesn't deviate much from the standard way of declaring and using MongoDB collections in C#. However, there are two additional steps:
154
155
@@ -163,14 +164,89 @@ public class PersonDocument : IMongoDocument
163
164
}
164
165
```
165
166
167
+
### With Configuration
168
+
169
+
In certain scenarios, like in Domain Driven Design (DDD), one would like to persist their Domain Aggregates as is in the Document Database. These Domain objects are not aware of how they are persisted. They cannot be decorated with Persistence level attributes (ie `[MongoCollection()]`), nor can they implement `IMongoDocument`.
170
+
171
+
You can configure your Object to Database mapping throught `IMongoCollectionConfiguration<TDocument>` instead.
Since the Configuration approach uses reflection to find the implementations of `IMongoCollectionConfiguration<T>` during the startup, we have to tell the library that we opt-in the Configuration mode by calling AddCollectionConfigurations and pass it the Assemblies where you can locate the Configurations.
Refer back to the [getting started section](#getting-started) to learn how to resolve `IMongoCollection<TDocument>` from the dependency injection services.
167
200
201
+
## Extensions
168
202
We also provide `IAsyncEnumerable<TDocument>` extensions on `IAsyncCursor<TDocument>` and `IAsyncCursorSource<TDocument>`, eliminating the need to deal with cursors. For example:
You can use Mongo Attributes for Property Mapping, or BsonClassMaps. However, if you are using Configuration, you probably do not want to use Attributes on your Models.
211
+
212
+
### With Attributes
213
+
214
+
```csharp
215
+
[MongoCollection("people")]
216
+
publicclassPersonDocument : IMongoDocument
217
+
{
218
+
[BsonId]
219
+
[BsonRepresentation(BsonType.ObjectId)]
220
+
publicstringId { get; set; }
221
+
222
+
[BsonElement("n")]
223
+
publicstringName { get; set; } =string.Empty;
224
+
}
225
+
```
226
+
227
+
[Mapping Models with Attributes](https://www.mongodb.com/docs/drivers/csharp/v2.19/fundamentals/serialization/poco/)
[Mapping Models with ClassMaps](https://www.mongodb.com/docs/drivers/csharp/v2.19/fundamentals/serialization/class-mapping/)
249
+
174
250
## Logging and distributed tracing
175
251
176
252
**Workleap.Extensions.Mongo** supports modern logging with `ILogger` and log level filtering. MongoDB commands can be logged at the `Debug` level and optionally with their BSON content only if you set `MongoClientOptions.Telemetry.CaptureCommandText` to `true`.
@@ -189,6 +265,21 @@ By default, some commands such as `isMaster`, `buildInfo`, `saslStart`, etc., ar
189
265
190
266
We provide a mechanism for you to declare your collection indexes and ensure they are applied to your database. To do this, declare your indexes by implementing a custom `MongoIndexProvider<TDocument>`:
At this stage, nothing will happen. To actually create or update the index, you need to inject our `IMongoIndexer` service and then call one of its `UpdateIndexesAsync()` method overloads, for example:
thrownewArgumentException(documentType+" must be decorated with "+nameof(MongoCollectionAttribute)+" or be registered by a "+typeof(IMongoCollectionConfiguration<>).MakeGenericType(documentType).Name);
0 commit comments