11# Step-By-Step Guide to a Running API
22
3- The most basic use case leverages Entity Framework.
3+ The most basic use case leverages Entity Framework Core.
44The shortest path to a running API looks like:
55
66- Create a new web app
@@ -33,45 +33,45 @@ Install-Package JsonApiDotnetCore
3333```
3434
3535### Define Models
36-
36+
3737Define your domain models such that they implement ` IIdentifiable<TId> ` .
38- The easiest way to do this is to inherit ` Identifiable `
38+ The easiest way to do this is to inherit from ` Identifiable `
3939
4040``` c#
4141public class Person : Identifiable
42- {
42+ {
4343 [Attr (" name" )]
4444 public string Name { get ; set ; }
4545}
4646```
4747
4848### Define DbContext
49-
49+
5050Nothing special here, just an ordinary ` DbContext `
5151
5252```
5353public class AppDbContext : DbContext
5454{
5555 public AppDbContext(DbContextOptions<AppDbContext> options)
5656 : base(options) { }
57-
57+
5858 public DbSet<Person> People { get; set; }
5959}
6060```
6161
6262### Define Controllers
63-
64- You need to create controllers that inherit from ` JsonApiController<TEntity > ` or ` JsonApiController<TEntity , TId> `
65- where ` TEntity ` is the model that inherits from ` Identifiable<TId> `
63+
64+ You need to create controllers that inherit from ` JsonApiController<T > ` or ` JsonApiController<T , TId> `
65+ where ` T ` is the model that inherits from ` Identifiable<TId> `
6666
6767``` c#
6868public class PeopleController : JsonApiController <Person >
6969{
7070 public PeopleController (
71- IJsonApiContext jsonApiContext ,
72- IResourceService < Person > resourceService ,
73- ILoggerFactory loggerFactory )
74- : base (jsonApiContext , resourceService , loggerFactory )
71+ IJsonApiOptions jsonApiOptions ,
72+ ILoggerFactory loggerFactory ,
73+ IResourceService < Person > resourceService )
74+ : base (jsonApiOptions , loggerFactory , resourceService )
7575 { }
7676}
7777```
@@ -81,24 +81,26 @@ public class PeopleController : JsonApiController<Person>
8181Finally, add the services by adding the following to your Startup.ConfigureServices:
8282
8383``` c#
84- public IServiceProvider ConfigureServices (IServiceCollection services )
84+ // This method gets called by the runtime. Use this method to add services to the container.
85+ public void ConfigureServices (IServiceCollection services )
8586{
86- // add the db context like you normally would
87+ // Add the Entity Framework Core DbContext like you normally would
8788 services .AddDbContext <AppDbContext >(options =>
88- { // use whatever provider you want, this is just an example
89+ {
90+ // Use whatever provider you want, this is just an example
8991 options .UseNpgsql (GetDbConnectionString ());
90- }, ServiceLifetime . Transient );
92+ });
9193
92- // add jsonapi dotnet core
94+ // Add JsonApiDotNetCore
9395 services .AddJsonApi <AppDbContext >();
94- // ...
9596}
9697```
9798
98- Add the middleware to the Startup.Configure method. Note that under the hood,
99- this will call ` app.UseMvc( ) ` so there is no need to add that as well.
99+ Add the middleware to the Startup.Configure method. Note that under the hood,
100+ this will call ` app.UseRouting() ` and ` app.UseEndpoints(... )` so there is no need to add that as well.
100101
101102``` c#
103+ // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
102104public void Configure (IApplicationBuilder app )
103105{
104106 app .UseJsonApi ();
@@ -110,19 +112,19 @@ public void Configure(IApplicationBuilder app)
110112One way to seed the database is in your Configure method:
111113
112114``` c#
113- public void Configure (
114- IApplicationBuilder app ,
115- AppDbContext context )
115+ public void Configure (IApplicationBuilder app , AppDbContext context )
116116{
117117 context .Database .EnsureCreated ();
118- if (context .People .Any () == false )
118+
119+ if (! context .People .Any ())
119120 {
120- context .People .Add (new Person {
121+ context .People .Add (new Person
122+ {
121123 Name = " John Doe"
122124 });
123125 context .SaveChanges ();
124126 }
125- // ...
127+
126128 app .UseJsonApi ();
127129}
128130```
0 commit comments