Skip to content

Commit 09678ff

Browse files
committed
doc: fix docs on MongoDbIdentifiable and describe client id generation
1 parent c4fe73b commit 09678ff

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ dotnet add package JsonApiDotNetCore.MongoDb
1414
### Models
1515

1616
```cs
17-
// MongoDbIdentifiable is just a utility base class, could use IIdentifiable<TId> instead
1817
public sealed class Book : MongoDbIdentifiable
1918
{
2019
[Attr]
@@ -90,9 +89,9 @@ public class Startup
9089

9190
### Customise MongoDB persistence options and _id generation
9291

93-
In addition to `MongoDbIdentifiable` your resource classes are free to use any of the MongoDB driver persistence options or inherit from their own base class.
92+
`MongoDbIdentifiable` has some sensible defaults for storing documents with _ids, but these need to be customised if you want client side or string based ids.
9493

95-
For example, you could change the example above so that the `Book` resource has string IDs rather than object ids in the DB, but still have them generated server side:
94+
For example, you could change the example above so that the `Book` resource has string IDs rather than object ids in the DB, (so far still generated server side):
9695

9796
```cs
9897
public class Book : IIdentifiable<string>
@@ -113,25 +112,24 @@ public class Book : IIdentifiable<string>
113112
}
114113
```
115114

116-
Resources just need to inherit from the base `IIdentifiable<string>` interface from JsonApiDotNetCore (or the provided default `MongoDbIdentifiable`) and then just use any of usual [MongoDB Driver mapping code](https://mongodb.github.io/mongo-csharp-driver/2.12/reference/bson/mapping/).
117-
118-
You could also achieve the exact same result using MongoDB `BsonClassMap` [rather than attributes](https://mongodb.github.io/mongo-csharp-driver/2.11/reference/bson/mapping/) so your `Book` does not need any MongoDB specific code like below.
115+
Resources properties can use any of usual [MongoDB Driver mapping code](https://mongodb.github.io/mongo-csharp-driver/2.12/reference/bson/mapping/) so to achieve the same result without decorating your json:api resource classes use `BsonClassMap` [rather than attributes](https://mongodb.github.io/mongo-csharp-driver/2.11/reference/bson/mapping/):
119116

120117
```cs
121118
// in startup
122119
BsonClassMap.RegisterClassMap<Book>(cm =>
123120
{
124121
cm.AutoMap();
125122
cm.MapIdProperty(x => x.Id).SetIdGenerator(StringObjectIdGenerator.Instance);
123+
cm.MapMember(c => c.Name).SetElementName("bookName");
126124
cm.UnmapMember(x=>x.StringId);
127125
});
128126
```
129127

130-
Using `StringObjectIdGenerator` above could then be combined with `AllowClientGeneratedIds` JsonApi setting in `Startup.ConfigureServices` so that IDs can be generated on the client, and will be auto-assigned server side if not provided providing a flexible string based id for the `Book` resource:
128+
The `StringObjectIdGenerator` above can then be combined with `AllowClientGeneratedIds` JsonApi setting in `Startup.ConfigureServices` so that string IDs can be generated on the client, but will be auto-assigned to random strings server side if not provided. This style of ids will be more familiar to developers used to no-sql style databases.
131129

132130
```cs
133131
services.AddJsonApi(options => {
134-
// Allow us to POST books with already assigned IDs!
132+
// Allow us to POST books with already assigned IDs
135133
options.AllowClientGeneratedIds = true;
136134
}, resources: builder =>
137135
{

0 commit comments

Comments
 (0)