Skip to content

task: improve docs for custom persistence options #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

alastairtree
Copy link

Small update to the readme

README.md Outdated
public class Book : IIdentifiable<string>
{
// If Id=null generate a random string ID using the MongoDB driver
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
Copy link
Contributor

@bart-degreed bart-degreed Feb 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to consider changing MongoDbIdentifiable to use this instead of [BsonId] [BsonRepresentation(BsonType.ObjectId)] if @mrnkr agrees this is better. In any case, The MongoDB fluent mapping API can overrule these using AutoMap, making it merely just a default for convenience.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default of BsonType.ObjectId may be sensible as it is the most performant in MongoDB because it is the binary representation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know, then I think we should leave that the default.

README.md Outdated
Comment on lines 130 to 144
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:

```cs
services.AddJsonApi(options => {
// Allow us to POST books with already assigned IDs!
options.AllowClientGeneratedIds = true;
}, resources: builder =>
{
builder.Add<Book, string>();
});
services.AddJsonApiMongoDb();

services.AddResourceRepository<MongoDbRepository<Book, string>>();
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is basic JsonApiDotNetCore functionality, which is already documented at https://www.jsonapi.net/usage/options.html#client-generated-ids. I don't see why it needs to be mentioned here, or am I missing something that makes this work differently for MongoDB that requires explanation? Note we have MongoDB-tests for this and all seem to work fine without any special handling.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The combination of mongoDB features (string ids generated server side) and json api features (optional client side id generation) working in tandem was hard to discover for me and took me a few passes to figure out which was why I was offering some docs up to save others the same hassle but accept what you say.

I think it is MongoDB specific because the library is defaulted to the (performant) binary object id. If you are trying to do client side id generation with AllowClientGeneratedIds then users (me!) may assume they can pass any string id since that is how the API appears. However, in reality the API requires you to pass a 12 byte unique generated object id, and throws an non-obvious exception if you just pass something like "123".

Using mongoDB StringObjectIdGenerator and json:api AllowClientGeneratedIds are a nice fit together in the document db ecosystem because it is common to have client-assigned document random string keys as the default that can be overridden with a unique string if I know the id I want to assign. This differs from relational SQL ecosystem where auto-increment database-assigned ids are the default, and client side key assignment is the exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. But then we should have tests backing that scenario, if we're going to advise on doing that. Can you add some? They would demonstrate using the fluent mapping overrides too, which is nice.

@alastairtree
Copy link
Author

Thanks for the useful feedback, please re-review - have tweaked the docs based on the comments.

@bart-degreed
Copy link
Contributor

Superseded by #14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants