2
2
3
3
# Generators
4
4
5
- - TODO: Document usage of the yeoman jadn generator
5
+ You can install the [ Yeoman generators] ( https://github.com/Research-Institute/json-api-dotnet-core-generators )
6
+ to make building applications much easier.
6
7
7
8
## Usage
8
9
9
- - Add Middleware
10
+ You need to do 3 things:
11
+
12
+ - Add Middleware and Services
10
13
- Define Models
11
14
- Define Controllers
12
15
16
+ I recommend reading the details below, but once you're familiar with the
17
+ setup, you can use the Yeoman generator to generate the required classes.
18
+
19
+ ## Middleware and Services
20
+
21
+ Add the following to your ` Startup.ConfigureServices ` method.
22
+ Replace ` AppDbContext ` with your DbContext.
23
+
24
+ ``` csharp
25
+ services .AddJsonApi <AppDbContext >();
26
+ ```
27
+
28
+ Add the middleware to the ` Startup.Configure ` method.
29
+ Note that under the hood, this will call ` app.UseMvc() `
30
+ so there is no need to add that as well.
31
+
32
+ ``` csharp
33
+ app .UseJsonApi ();
34
+ ```
35
+
13
36
## Defining Models
14
37
15
38
Your models should inherit ` Identifiable<TId> ` where ` TId ` is the type of the primary key, like so:
16
39
17
- ```
40
+ ``` csharp
18
41
public class Person : Identifiable <Guid >
19
42
{
20
43
public override Guid Id { get ; set ; }
@@ -26,7 +49,7 @@ public class Person : Identifiable<Guid>
26
49
If you want an attribute on your model to be publicly available,
27
50
add the ` AttrAttribute ` and provide the outbound name.
28
51
29
- ```
52
+ ``` csharp
30
53
public class Person : Identifiable <int >
31
54
{
32
55
public override int Id { get ; set ; }
@@ -38,9 +61,10 @@ public class Person : Identifiable<int>
38
61
39
62
### Relationships
40
63
41
- In order for navigation properties to be identified in the model, they should be labeled as virtual.
64
+ In order for navigation properties to be identified in the model,
65
+ they should be labeled as virtual.
42
66
43
- ```
67
+ ``` csharp
44
68
public class Person : Identifiable <int >
45
69
{
46
70
public override int Id { get ; set ; }
@@ -55,7 +79,7 @@ public class Person : Identifiable<int>
55
79
Dependent relationships should contain a property in the form ` {RelationshipName}Id ` .
56
80
For example, a ` TodoItem ` may have an ` Owner ` and so the Id attribute should be ` OwnerId ` like so:
57
81
58
- ```
82
+ ``` csharp
59
83
public class TodoItem : Identifiable <int >
60
84
{
61
85
public override int Id { get ; set ; }
@@ -73,7 +97,7 @@ public class TodoItem : Identifiable<int>
73
97
You need to create controllers that inherit from ` JsonApiController<TEntity> ` or ` JsonApiController<TEntity, TId> `
74
98
where ` TEntity ` is the model that inherits from ` Identifiable<TId> ` .
75
99
76
- ```
100
+ ``` csharp
77
101
[Route (" api/[controller]" )]
78
102
public class ThingsController : JsonApiController <Thing >
79
103
{
@@ -92,7 +116,7 @@ If your model is using a type other than `int` for the primary key,
92
116
you should explicitly declare it in the controller
93
117
and repository generic type definitions:
94
118
95
- ```
119
+ ``` csharp
96
120
[Route (" api/[controller]" )]
97
121
public class ThingsController : JsonApiController <Thing , Guid >
98
122
{
@@ -120,7 +144,7 @@ NOT /todoItems
120
144
121
145
You can add a namespace to the URL by specifying it in ` ConfigureServices ` :
122
146
123
- ```
147
+ ``` csharp
124
148
services .AddJsonApi <AppDbContext >(
125
149
opt => opt .Namespace = " api/v1" );
126
150
```
@@ -130,7 +154,7 @@ services.AddJsonApi<AppDbContext>(
130
154
If you would like pagination implemented by default, you can specify the page size
131
155
when setting up the services:
132
156
133
- ```
157
+ ``` csharp
134
158
services .AddJsonApi <AppDbContext >(
135
159
opt => opt .DefaultPageSize = 10 );
136
160
```
@@ -146,7 +170,6 @@ add to the service collection in `Startup.ConfigureServices` like so:
146
170
services.AddScoped<IEntityRepository<MyEntity,Guid>, MyEntityRepository>();
147
171
```
148
172
149
-
150
173
## Filtering
151
174
152
175
You can filter resources by attributes using the ` filter ` query parameter.
@@ -180,5 +203,3 @@ I am using DotNetCoreDocs to generate sample requests and documentation.
180
203
4 . ` cd ./src/JsonApiDotNetCoreExample `
181
204
5 . ` dotnet run `
182
205
6 . ` open http://localhost:5000/docs `
183
-
184
-
0 commit comments