@@ -15,76 +15,60 @@ 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
+ WebApplicationBuilder builder = WebApplication . CreateBuilder ( args );
39
34
40
- ``` c#
41
- public class Startup
35
+ // Add services to the container.
36
+
37
+ builder .Services .AddSingleton <IMongoDatabase >(_ =>
42
38
{
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 ();
67
59
```
60
+
68
61
Note: If your API project uses only MongoDB (not in combination with EF Core), then instead of
69
62
registering all MongoDB resources and repositories individually, you can use:
63
+
70
64
``` 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
+
88
72
```
89
73
90
74
## Limitations
0 commit comments