Plug-n-play implementation of IResourceRepository<TResource, TId>
allowing you to use MongoDB with your JsonApiDotNetCore APIs.
dotnet add package JsonApiDotNetCore.MongoDb
public sealed class Book : IIdentifiable<string>
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[Attr]
public string Id { get; set; }
[Attr]
public string Name { get; set; }
[BsonIgnore]
public string StringId { get => Id; set => Id = value; }
}
public sealed class BooksController : JsonApiController<Book, string>
{
public BooksController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Book, string> resourceService)
: base(options, loggerFactory, resourceService)
{
}
}
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IMongoDatabase>(sp =>
{
var client = new MongoClient(Configuration.GetSection("DatabaseSettings:ConnectionString").Value);
return client.GetDatabase(Configuration.GetSection("DatabaseSettings:Database").Value);
});
services.AddResourceRepository<MongoDbRepository<Book>>();
services.AddJsonApi(resources: builder =>
{
builder.Add<Book, string>();
});
}
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseJsonApi();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
}
- Relationships are not supported