22
33# Generators
44
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.
67
78## Usage
89
9- - Add Middleware
10+ You need to do 3 things:
11+
12+ - Add Middleware and Services
1013- Define Models
1114- Define Controllers
1215
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+
1336## Defining Models
1437
1538Your models should inherit ` Identifiable<TId> ` where ` TId ` is the type of the primary key, like so:
1639
17- ```
40+ ``` csharp
1841public class Person : Identifiable <Guid >
1942{
2043 public override Guid Id { get ; set ; }
@@ -26,7 +49,7 @@ public class Person : Identifiable<Guid>
2649If you want an attribute on your model to be publicly available,
2750add the ` AttrAttribute ` and provide the outbound name.
2851
29- ```
52+ ``` csharp
3053public class Person : Identifiable <int >
3154{
3255 public override int Id { get ; set ; }
@@ -38,9 +61,10 @@ public class Person : Identifiable<int>
3861
3962### Relationships
4063
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.
4266
43- ```
67+ ``` csharp
4468public class Person : Identifiable <int >
4569{
4670 public override int Id { get ; set ; }
@@ -55,7 +79,7 @@ public class Person : Identifiable<int>
5579Dependent relationships should contain a property in the form ` {RelationshipName}Id ` .
5680For example, a ` TodoItem ` may have an ` Owner ` and so the Id attribute should be ` OwnerId ` like so:
5781
58- ```
82+ ``` csharp
5983public class TodoItem : Identifiable <int >
6084{
6185 public override int Id { get ; set ; }
@@ -73,7 +97,7 @@ public class TodoItem : Identifiable<int>
7397You need to create controllers that inherit from ` JsonApiController<TEntity> ` or ` JsonApiController<TEntity, TId> `
7498where ` TEntity ` is the model that inherits from ` Identifiable<TId> ` .
7599
76- ```
100+ ``` csharp
77101[Route (" api/[controller]" )]
78102public class ThingsController : JsonApiController <Thing >
79103{
@@ -92,7 +116,7 @@ If your model is using a type other than `int` for the primary key,
92116you should explicitly declare it in the controller
93117and repository generic type definitions:
94118
95- ```
119+ ``` csharp
96120[Route (" api/[controller]" )]
97121public class ThingsController : JsonApiController <Thing , Guid >
98122{
@@ -120,7 +144,7 @@ NOT /todoItems
120144
121145You can add a namespace to the URL by specifying it in ` ConfigureServices ` :
122146
123- ```
147+ ``` csharp
124148services .AddJsonApi <AppDbContext >(
125149 opt => opt .Namespace = " api/v1" );
126150```
@@ -130,7 +154,7 @@ services.AddJsonApi<AppDbContext>(
130154If you would like pagination implemented by default, you can specify the page size
131155when setting up the services:
132156
133- ```
157+ ``` csharp
134158 services .AddJsonApi <AppDbContext >(
135159 opt => opt .DefaultPageSize = 10 );
136160```
@@ -146,7 +170,6 @@ add to the service collection in `Startup.ConfigureServices` like so:
146170services.AddScoped<IEntityRepository<MyEntity,Guid>, MyEntityRepository>();
147171```
148172
149-
150173## Filtering
151174
152175You can filter resources by attributes using the ` filter ` query parameter.
@@ -180,5 +203,3 @@ I am using DotNetCoreDocs to generate sample requests and documentation.
1802034 . ` cd ./src/JsonApiDotNetCoreExample `
1812045 . ` dotnet run `
1822056 . ` open http://localhost:5000/docs `
183-
184-
0 commit comments