Skip to content

Commit 97e3c9f

Browse files
author
Bart Koelman
committed
Update README.md
1 parent 57fbd33 commit 97e3c9f

File tree

1 file changed

+39
-55
lines changed

1 file changed

+39
-55
lines changed

README.md

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,76 +15,60 @@ dotnet add package JsonApiDotNetCore.MongoDb
1515
### Models
1616

1717
```c#
18+
#nullable enable
19+
20+
[Resource]
1821
public class Book : MongoIdentifiable
1922
{
2023
[Attr]
21-
public string Name { get; set; }
24+
public string Name { get; set; } = null!;
2225
}
2326
```
2427

25-
### Controllers
28+
### Middleware
2629

2730
```c#
28-
public class BooksController : JsonApiController<Book, string>
29-
{
30-
public BooksController(IJsonApiOptions options, ILoggerFactory loggerFactory,
31-
IResourceService<Book, string> resourceService)
32-
: base(options, loggerFactory, resourceService)
33-
{
34-
}
35-
}
36-
```
31+
// Program.cs
3732
38-
### Middleware
33+
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
3934

40-
```c#
41-
public class Startup
35+
// Add services to the container.
36+
37+
builder.Services.AddSingleton<IMongoDatabase>(_ =>
4238
{
43-
public IServiceProvider ConfigureServices(IServiceCollection services)
44-
{
45-
services.AddSingleton<IMongoDatabase>(_ =>
46-
{
47-
var client = new MongoClient("mongodb://localhost:27017");
48-
return client.GetDatabase("ExampleDbName");
49-
});
50-
51-
services.AddJsonApi(resources: builder =>
52-
{
53-
builder.Add<Book, string>();
54-
});
55-
services.AddJsonApiMongoDb();
56-
57-
services.AddResourceRepository<MongoRepository<Book, string>>();
58-
}
59-
60-
public void Configure(IApplicationBuilder app)
61-
{
62-
app.UseRouting();
63-
app.UseJsonApi();
64-
app.UseEndpoints(endpoints => endpoints.MapControllers());
65-
}
66-
}
39+
var client = new MongoClient("mongodb://localhost:27017");
40+
return client.GetDatabase("ExampleDbName");
41+
});
42+
43+
builder.Services.AddJsonApi(resources: resourceGraphBuilder =>
44+
{
45+
resourceGraphBuilder.Add<Book, string>();
46+
});
47+
48+
builder.Services.AddJsonApiMongoDb();
49+
50+
builder.Services.AddResourceRepository<MongoRepository<Book, string>>();
51+
52+
// Configure the HTTP request pipeline.
53+
54+
app.UseRouting();
55+
app.UseJsonApi();
56+
app.MapControllers();
57+
58+
app.Run();
6759
```
60+
6861
Note: If your API project uses only MongoDB (not in combination with EF Core), then instead of
6962
registering all MongoDB resources and repositories individually, you can use:
63+
7064
```c#
71-
public class Startup
72-
{
73-
public IServiceProvider ConfigureServices(IServiceCollection services)
74-
{
75-
// ...
76-
77-
services.AddJsonApi(facade => facade.AddCurrentAssembly());
78-
services.AddJsonApiMongoDb();
79-
80-
services.AddScoped(typeof(IResourceReadRepository<>), typeof(MongoRepository<>));
81-
services.AddScoped(typeof(IResourceReadRepository<,>), typeof(MongoRepository<,>));
82-
services.AddScoped(typeof(IResourceWriteRepository<>), typeof(MongoRepository<>));
83-
services.AddScoped(typeof(IResourceWriteRepository<,>), typeof(MongoRepository<,>));
84-
services.AddScoped(typeof(IResourceRepository<>), typeof(MongoRepository<>));
85-
services.AddScoped(typeof(IResourceRepository<,>), typeof(MongoRepository<,>));
86-
}
87-
}
65+
builder.Services.AddJsonApi(facade => facade.AddCurrentAssembly());
66+
builder.Services.AddJsonApiMongoDb();
67+
68+
builder.Services.AddScoped(typeof(IResourceReadRepository<,>), typeof(MongoRepository<,>));
69+
builder.Services.AddScoped(typeof(IResourceWriteRepository<,>), typeof(MongoRepository<,>));
70+
builder.Services.AddScoped(typeof(IResourceRepository<,>), typeof(MongoRepository<,>));
71+
8872
```
8973

9074
## Limitations

0 commit comments

Comments
 (0)