@@ -15,76 +15,62 @@ dotnet add package JsonApiDotNetCore.MongoDb
15
15
### Models
16
16
17
17
``` c#
18
+ #nullable enable
19
+
20
+ [Resource ]
18
21
public class Book : MongoIdentifiable
19
22
{
20
23
[Attr ]
21
- public string Name { get ; set ; }
24
+ public string Name { get ; set ; } = null ! ;
22
25
}
23
26
```
24
27
25
- ### Controllers
28
+ ### Middleware
26
29
27
30
``` 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
37
32
38
- ### Middleware
33
+ #nullable enable
39
34
40
- ``` c#
41
- public class Startup
35
+ WebApplicationBuilder builder = WebApplication .CreateBuilder (args );
36
+
37
+ // Add services to the container.
38
+
39
+ builder .Services .AddSingleton <IMongoDatabase >(_ =>
42
40
{
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
- }
41
+ var client = new MongoClient (" mongodb://localhost:27017" );
42
+ return client .GetDatabase (" ExampleDbName" );
43
+ });
44
+
45
+ builder .Services .AddJsonApi (resources : resourceGraphBuilder =>
46
+ {
47
+ resourceGraphBuilder .Add <Book , string ?>();
48
+ });
49
+
50
+ builder .Services .AddJsonApiMongoDb ();
51
+
52
+ builder .Services .AddResourceRepository <MongoRepository <Book , string ?>>();
53
+
54
+ // Configure the HTTP request pipeline.
55
+
56
+ app .UseRouting ();
57
+ app .UseJsonApi ();
58
+ app .MapControllers ();
59
+
60
+ app .Run ();
67
61
```
68
- Note: If your API project uses only MongoDB (not in combination with EF Core), then instead of
62
+
63
+ Note: If your API project uses MongoDB only (so not in combination with EF Core), then instead of
69
64
registering all MongoDB resources and repositories individually, you can use:
65
+
70
66
``` 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
- }
67
+ builder .Services .AddJsonApi (facade => facade .AddCurrentAssembly ());
68
+ builder .Services .AddJsonApiMongoDb ();
69
+
70
+ builder .Services .AddScoped (typeof (IResourceReadRepository <,>), typeof (MongoRepository <,>));
71
+ builder .Services .AddScoped (typeof (IResourceWriteRepository <,>), typeof (MongoRepository <,>));
72
+ builder .Services .AddScoped (typeof (IResourceRepository <,>), typeof (MongoRepository <,>));
73
+
88
74
```
89
75
90
76
## Limitations
0 commit comments