Plug-n-play implementation of IResourceRepository<TResource, TId>
allowing you to use MongoDB with your JsonApiDotNetCore APIs.
dotnet add package JsonApiDotNetCore.MongoDb
public class Book : MongoIdentifiable
{
[Attr]
public string Name { get; set; }
}
public 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>(_ =>
{
var client = new MongoClient("mongodb://localhost:27017");
return client.GetDatabase("ExampleDbName");
});
services.AddJsonApi(resources: builder =>
{
builder.Add<Book, string>();
});
services.AddJsonApiMongoDb();
services.AddResourceRepository<MongoRepository<Book, string>>();
}
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseJsonApi();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
}
Note: If your API project uses only MongoDB (not in combination with EF Core), then instead of registering all MongoDB resources and repositories individually, you can use:
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// ...
services.AddJsonApi(facade => facade.AddCurrentAssembly());
services.AddJsonApiMongoDb();
services.AddScoped(typeof(IResourceReadRepository<>), typeof(MongoRepository<>));
services.AddScoped(typeof(IResourceReadRepository<,>), typeof(MongoRepository<,>));
services.AddScoped(typeof(IResourceWriteRepository<>), typeof(MongoRepository<>));
services.AddScoped(typeof(IResourceWriteRepository<,>), typeof(MongoRepository<,>));
services.AddScoped(typeof(IResourceRepository<>), typeof(MongoRepository<>));
services.AddScoped(typeof(IResourceRepository<,>), typeof(MongoRepository<,>));
}
}
- JSON:API relationships are currently not supported. You can use complex object graphs though, which are stored in a single document.
Have a question, found a bug or want to submit code changes? See our contributing guidelines.
After each commit to the master branch, a new prerelease NuGet package is automatically published to AppVeyor at https://ci.appveyor.com/nuget/jsonapidotnetcore-mongodb. To try it out, follow the next steps:
- In Visual Studio: Tools, NuGet Package Manager, Package Manager Settings, Package Sources
- Click +
- Name: AppVeyor JADNC MongoDb, Source: https://ci.appveyor.com/nuget/jsonapidotnetcore-mongodb
- Click Update, Ok
- Open the NuGet package manager console (Tools, NuGet Package Manager, Package Manager Console)
- Select AppVeyor JADNC MongoDb as package source
- Run command:
Install-Package JonApiDotNetCore -pre
To build the code from this repository locally, run:
dotnet build
You don't need to have a running instance of MongoDB on your machine to run tests. Just type the following command in your terminal:
dotnet test
If you want to run the examples and explore them on your own you are going to need that running instance of MongoDB. If you have docker installed you can launch it like this:
run-docker-mongodb.ps1
And then to run the API:
dotnet run --project src/Examples/GettingStarted
Alternatively, to build and validate the code, run all tests, generate code coverage and produce the NuGet package:
Build.ps1