Decomposing My .Net API (Part I)

What’s in the boilerplate?

$ dotnet new webapi -n my-api
I exclude obj from my editor, don’t worry about it

Program.cs

  1. Line 9, 10 references to our appsettings.json and appsettings.Development.json files.
  2. Line 13 add environmental variables, for example, we can set the environment into Production or Development in the command line.
  3. Line 15–18 specifies that we can use the command line argument to pass in some settings.
  4. Line 20–24 uses a web-specific builder to introduce ChainedConfigurationProvider which is the IConfiguration Configuration in Startup.cs

Startup.cs

appsettings.json

*.csproj

launchSettings.json

WeatherForecast related files

Design for this API

Not a precise depiction of some concepts.
  1. The database is where you store the data.
  2. To use the database, you will need to map your models/entities to the database using a Data Context. (User model to the User table in DB)
  3. At this stage, you can use data context to persist data into the database, however, code will be repetitive and complicate for certain operations. Repository pattern is therefore used to encapsulate operations on persistent data.
  4. Models are the data objects you designed for your application.
  5. Data Transfer Objects (DTOs) define how the data will be sent over the network.
  6. Controllers decide what to send back to users when they make requests.

Models folder

CollectionGundam.cs

Like.cs

Data folder

DataContext.cs

CollectorRepository.cs and ICollectorRepository.cs

DTOs folder

  1. Remove circular reference (User refer to Photo which then refers to User)
  2. Hide particular properties that clients are not supposed to view (User has password data which should not be sent back to other users)

Controllers Folder

UsersController.cs

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store