Skip to content

Commit 7471cba

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

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

README.md

Lines changed: 14 additions & 30 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,48 +89,33 @@ 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 and overridden 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

97-
```cs
98-
public class Book : IIdentifiable<string>
99-
{
100-
// If Id=null generate a random string ID using the MongoDB driver
101-
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
102-
[Attr]
103-
public virtual string Id { get; set; }
104-
105-
// override the attribute name in the db
106-
[BsonElement("bookName")]
107-
[Attr]
108-
public string Name { get; set; }
109-
110-
// all json:api resources need this
111-
[BsonIgnore]
112-
public string StringId { get => Id; set => Id = value; }
113-
}
114-
```
115-
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.
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`:
11997

12098
```cs
121-
// in startup
99+
// in startup change to string generated ids for MongoDbIdentifiable
100+
BsonClassMap.RegisterClassMap<MongoDbIdentifiable>(cm =>
101+
{
102+
cm.MapIdProperty(x => x.Id)
103+
.SetIdGenerator(StringObjectIdGenerator.Instance);
104+
});
105+
106+
// optionally you can also change the mapping for resources here
122107
BsonClassMap.RegisterClassMap<Book>(cm =>
123108
{
124109
cm.AutoMap();
125-
cm.MapIdProperty(x => x.Id).SetIdGenerator(StringObjectIdGenerator.Instance);
126-
cm.UnmapMember(x=>x.StringId);
110+
cm.MapProperty(x => x.Name).SetElementName("bookName");
127111
});
128112
```
129113

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:
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.
131115

132116
```cs
133117
services.AddJsonApi(options => {
134-
// Allow us to POST books with already assigned IDs!
118+
// Allow us to POST books with already assigned IDs
135119
options.AllowClientGeneratedIds = true;
136120
}, resources: builder =>
137121
{

0 commit comments

Comments
 (0)