Web API Scaffolding

 Key points

What is API ?
What is Scaffolding ?
How to do it ?
Best Practices of API


What is api ?

Application Programming Interface

  • An API is a set of definitions and protocols for building and integrating application software.
  • APIs let your product or service communicate with other products and services without having to know how they’re implemented. This can simplify app development, saving time and money.



What is Scaffolding ?

Scaffolding is a code generation framework

Scaffolding, as used in computing, refers to one of two techniques: The first is a code generation technique related to database access in some model–view–controller frameworks; the second is a project generation technique supported by various tools.

There are two options for scaffolding MVC; Minimal and Full. If you select Minimal, only the NuGet packages and references for ASP.NET MVC are added to your project. If you select the Full option, the Minimal dependencies are added, as well as the required content files for an MVC project. To easily use scaffolding, select Full dependencies.


How to do it?

  • Scaffolding By Database via Entity Framework
  • Scaffolding by open api specification document like swagger Or  other client-side tool like postman


How to do it ?

  1. Create Database or use old created database
  2. Create .NET Core Project – Choose API template while creating project
  3. Install NuGet packages like Entity Framework Core, Entity Framework Core Tools, Entity Framework Core Design, Entity Framework Core SQL, Entity Framework Core SQL Design
  4. Open Package Manage Console and execute command: Scaffold-DbContext "Server=.;Database=SchoolDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
  5. Go to controller folder and add new scaffolded item or Controller. 
  6. Choose API controller with Actions using Entity Framework
  7. Choose Model, Context and DB and Add
  8. Write your business code










Best practices of api

What to consider: Documentation, Stability and Consistency, Flexibility, Security

  1. Ease of Adoption
  2. Use JSON
  3. Use Nouns instead of Verbs -  like - GET /books/123, DELETE /books/123, POST /books, PUT /books/123,  PATCH /books/123
  4.  Name the collections using Plural Nouns
  5. Use resource nesting to show relations or hierarchy
  6.  Error Handling
  7.  Filtering, sorting, paging, and field selection like Filtering: GET /users?country=USA, GET /users?creation_date=2019-11-11, GET /users?creation_date=2019-11-11
  8. Sorting: GET /users?sort=birthdate_date:asc, GET /users?sort=birthdate_date:desc
  9. Paging: GET /users?limit=100, GET /users?offset=2
  10. Versioning https://us6.api.mailchimp.com/3.0/ (major + minor version indication)  https://api.stripe.com/v1/ (major version indication only)   https://developer.github.com/v3/  (major version indication only)   https://api.twilio.com/2010-04-01/ (date based indication)
  11. API Documentation
  12. Using SSL/TLS


Comments

Popular posts from this blog

Using C#9 record and init property in your .NET Framework 4.x, .NET Standard and .NET Core projects

Where you can locate files in Telligent Community DB Server Site?

Easy Steps to migrate from Telligent CS to SP Online