DEV Community

Gourav Kadu
Gourav Kadu

Posted on

Structure Of .Net API

Hey this blog will give you overall understanding/and feel of the working of API from Controller -> Services -> Database, if you don't know what Controller or Services don't worry just refer below image.

Image description

The Client send request through UI(User Interface) then the request is sent to controller redirected to services to perform operation and retrieve/store data in Database and vice versa . This may get you a rough idea of how it works.

  • Controllers are in API layer.
  • Services are in Business layer.
  • Data is in Database Layer.

API Layer

The API layer is responsible for handling incoming HTTP requests and returning responses. In an ASP.NET Core API, Controllers are implemented as classes that derive from the ControllerBase class. Each controller is responsible for handling requests for a specific set of endpoints.

The API layer typically includes the following components:

  • Endpoints: Endpoints are the URLs that the API exposes. Each endpoint corresponds to a method on the controller class.

  • Actions: Actions are the methods on the controller class that handle incoming requests. They are responsible for processing the input, invoking the appropriate service methods, and returning the response.

  • Routing: Routing is the process of mapping incoming requests to the appropriate controller and action. It is typically configured using attributes on the controller and action methods.

Business Layer

The services layer is responsible for encapsulating the business logic of the application. Services are typically implemented as singleton classes that are registered with the Dependency Injection (DI) container. The DI container is responsible for creating and managing the instances of the services.

The services layer typically includes the following components:

  • Interfaces: Interfaces define the contracts for the services. They specify the methods that the service provides and the inputs and outputs of those methods.

  • Implementations: Implementations are the concrete classes that implement the interfaces. They contain the actual implementation of the service methods.

  • Dependency Injection: The DI container is responsible for managing the dependencies between the services and other components of the application. It is used to inject the required services into the controllers and other components that require them.

Database Layer

The database layer is responsible for handling the interactions between the application and the database. In an ASP.NET Core API, this layer is typically implemented using an Object-Relational Mapping (ORM) tool such as Entity Framework Core. The ORM provides an abstraction layer over the database, allowing developers to interact with the database using code instead of SQL queries.

The database layer typically includes the following components:

  • Entities: Entities are classes that represent the tables in the database. Each entity corresponds to a table, and the properties of the entity correspond to the columns in the table.

Here is how a Entity is written "User"

namespace YourNamespace.Models
    public class User
        public int UserId { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Email { get; set; }

        public string Password { get; set; }

        // Other properties and navigation properties can be added here
Enter fullscreen mode Exit fullscreen mode

  • DbContext: The DbContext is a class that represents the database as a whole. It provides a way to query and manipulate the entities in the database. Install "Microsoft.EntityFrameworkCore" from Nuget package manager.
using Microsoft.EntityFrameworkCore;

namespace YourNamespace.Data
    public class YourDbContext : DbContext
        public YourDbContext(DbContextOptions<YourDbContext> options) : base(options)

        public DbSet<User> Users { get; set; }

        // Other DbSets can be added here for other entities

        protected override void OnModelCreating(ModelBuilder modelBuilder)
            // Configure entity mappings here

Enter fullscreen mode Exit fullscreen mode

  • Migrations: Migrations are scripts that update the database schema. They are used to create new tables, add or remove columns, and make other changes to the database. Before running migration command add nuget package.


Once you install these packages run


This is just an Rough Explanation to make you feel the flow of the WebAPI project.
Stick around for more detailed explanations and Codes.

Top comments (0)