Skip to content

Commit c4fe73b

Browse files
committed
task: improve docs for custom persistence options
1 parent 02d2a77 commit c4fe73b

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

README.md

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

1616
```cs
17+
// MongoDbIdentifiable is just a utility base class, could use IIdentifiable<TId> instead
1718
public sealed class Book : MongoDbIdentifiable
1819
{
1920
[Attr]
@@ -63,14 +64,16 @@ public class Startup
6364
}
6465
}
6566
```
67+
6668
Note: If your API project uses only MongoDB (not in combination with EF Core), then instead of
6769
registering all MongoDB resources and repositories individually, you can use:
70+
6871
```cs
6972
public class Startup
7073
{
7174
public IServiceProvider ConfigureServices(IServiceCollection services)
7275
{
73-
// ...
76+
// ...
7477
7578
services.AddJsonApi(facade => facade.AddCurrentAssembly());
7679
services.AddJsonApiMongoDb();
@@ -85,6 +88,60 @@ public class Startup
8588
}
8689
```
8790

91+
### Customise MongoDB persistence options and _id generation
92+
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.
94+
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:
96+
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.
119+
120+
```cs
121+
// in startup
122+
BsonClassMap.RegisterClassMap<Book>(cm =>
123+
{
124+
cm.AutoMap();
125+
cm.MapIdProperty(x => x.Id).SetIdGenerator(StringObjectIdGenerator.Instance);
126+
cm.UnmapMember(x=>x.StringId);
127+
});
128+
```
129+
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:
131+
132+
```cs
133+
services.AddJsonApi(options => {
134+
// Allow us to POST books with already assigned IDs!
135+
options.AllowClientGeneratedIds = true;
136+
}, resources: builder =>
137+
{
138+
builder.Add<Book, string>();
139+
});
140+
services.AddJsonApiMongoDb();
141+
142+
services.AddResourceRepository<MongoDbRepository<Book, string>>();
143+
```
144+
88145
## Development
89146

90147
Restore all NuGet packages with:

0 commit comments

Comments
 (0)