Structural Pattern for ASP.NET API

A very common problem that every beginner faces while they start a project. The question on their mind is how to arrange, name, group the files in my project. Yes, I faced this problem too, that’s why Today I will write about the repository pattern or the Structural Pattern I follow while developing an API (application programming interface). First, we learn what is an API in short.

API just in short how the back end of an application handles the HTTP request and Response. API also defines the formation of data that will be received and sent by it. We can Develop API as we want but there are many good outlines or architecture which gives an API a good share. Recently REST API architecture is very famous. REST API can handle many Request or calls from different end points, above all It is very flexible. 

My project structure will be based on REST API with ASP.NET, so if you have some prior knowledge in ASP.NET then you will fill more comfortable about Let's dig into it…

First, I start with Root folder name same as the app_name

Root Folder: MyApp

Inside MyApp folder create two more folder named

    1.  MyApp.API and

    2.      MyApp.SPA

MyApp.API contains the ASP.NET web application project files.

MyApp.API contains the ASP.NET Web API project’s following files:

All the listed files are created automatically by dotnet SDK

Now I will add some folders, then explain the role of each folder in this director with example.

  1. Data
  2. Dtos
  3. Helpers
  4. EntityConfigurations
  5. Models

Folders and Description: 
Controllers: This folder contains controllers which are .cs classes such as UserController.cs. Those classes handle incoming HTTP requests and send responses back to the caller. 

Models: The Models folder contains the classes that represent the application model. Example: In the database table exists User (id, username, address) so in application there will be an application model with the same name and similar name properties as User table field.

 Data: This folder stores files or classes with different roles. List of files types it stores listed:

1.      DataContext. – DataContext is more like a bridge that connects databases and applications for data transfer. In other words, DataContext connects the database to the application and supplies data to every part of the application.

2.      Interface Repository / IRepository: IUserRepository, IAuthRepository. – Repository is just an interface that we create (it is not part of API or ASP.NET or .NET). It allows you to “decouple” our repositories from real implementations.

3.      Repository: UserRepository, AuthRepository. – Repository classes contain methods that query the data supplied by DataContext. This method is called whenever data is needed. Sometimes the same query needs to apply in multiple parts of application. To prevent repeating the same code, it is better to store the code in a repository and use it to introduce DRY to the code.

4.      Seed: This class injects dummy data in the database when the database is created for the first time.

Dtos: Dto stand for Data Transfer Object. This folder contains classes. Dto class creates objects which can be sent to the user. It is easy to understand with example: Suppose user A visits user B’s profile for detailed information, in this situation API sends data form User Model but User Model may contain credential data like password that can be shared with others. Therefore, first our API needs to map User Model’s data to a Dto that can be sent to User A.

EntityConfigurations: To define the Model classes properties or describing relation between Model classes or Entities sometimes developers use Fluent API to configure. Model or Entity configuration can be done in DataContext but when configuration code becomes bulky then it is better to separate into another file named EntityConfigurations.

Helpers: As the name this folder contains classes or files that is absolutely for helping the API. Those files are not part of API or ASP.NET or .NET.

This Repository Pattern is easy for beginners. There are plenty of Architecture to follow. But I recommend beginners don’t use complex architecture without knowing it’s uses. It may cost your application performance. But The architecture I just mention you It is good for any kinds of API and easy to understand. One thing worth mention is Folder name can be as we want. In this article folder named by following convention of naming. And also, there is no roles that you must have to follow this this way to structure your files you can do it as your way. Just don’t let your code WET, keep as DRY as possible.

Comments

Post a Comment