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
// MongoDbIdentifiable is just a utility base class, could use IIdentifiable<TId> instead
18
17
publicsealedclassBook : MongoDbIdentifiable
19
18
{
20
19
[Attr]
@@ -90,48 +89,33 @@ public class Startup
90
89
91
90
### Customise MongoDB persistence options and _id generation
92
91
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 and overridden if you want client side or string based ids.
94
93
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).
96
95
97
-
```cs
98
-
publicclassBook : IIdentifiable<string>
99
-
{
100
-
// If Id=null generate a random string ID using the MongoDB driver
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.
96
+
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 string based ids you have to [override the json:api resource class attributes](https://mongodb.github.io/mongo-csharp-driver/2.11/reference/bson/mapping/) using `BsonClassMap`:
119
97
120
98
```cs
121
-
// in startup
99
+
// in startup change to string generated ids for MongoDbIdentifiable
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:
114
+
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.
131
115
132
116
```cs
133
117
services.AddJsonApi(options=> {
134
-
// Allow us to POST books with already assigned IDs!
118
+
// Allow us to POST books with already assigned IDs
0 commit comments