Skip to content

How is the repository set up?

Ronald Johnson edited this page Dec 12, 2024 · 3 revisions

With Minimal API in .NET (often used in ASP.NET Core), the structure can indeed be different since it focuses on simplicity and often eliminates the need for dedicated controllers by handling everything directly in the program's main file or in a very compact structure.

Program.cs

  • Purpose: This is the main entry point of the application. In a Minimal API, Program.cs typically contains the entire setup for routes, services, and middleware.
  • Role: Here, you define the HTTP endpoints, configure the dependency injection (DI) container, and set up any middleware. Each endpoint can be defined with a MapXXX method for handling HTTP requests directly. The methods for how different requests are handled, can be found in ‘Endpoints’.

Endpoints

  • Purpose: If the application is large, developers sometimes split routes or endpoint definitions into separate files for organization.
  • Role: Each file in this folder might handle a specific resource or set of related endpoints, like UserEndpoints.cs or ProductEndpoints.cs. These files register routes and define the logic for handling those routes, keeping the Program.cs file cleaner.

Mapping

  • Purpose: This folder typically holds classes or configuration files that define how data is mapped between different layers or objects.
  • Role: Mapping is essential when you have to transform data from one format or model to another. In .NET, this is commonly done with a library like AutoMapper. A Mapping/ folder may contain mapping profiles that define how data from one type (like a User entity) is converted into another type (like a UserDto). This layer is useful for abstracting the transformations required to map database models to API responses and vice versa.
  • Typical Contents:
    • Mapping profiles: Files with classes like UserMappingProfile.cs or ProductMappingProfile.cs. These contain configuration to map properties between two types.
    • Sometimes these mappings are also created for automating data transfer between layers, such as database models and DTOs. This way, the API can control exactly what data is exposed to the client.

Data

  • Purpose: Handles database interaction. This could include the database context (e.g., AppDbContext.cs in Entity Framework Core) and any configuration for connecting to the database.
  • Role: You’ll often find the logic for database operations here, such as defining tables and their relationships. It may also include database migrations, seed data, and repository patterns if the application follows that structure.

Entities

  • Purpose: Defines the data structures or models used by the application. These are typically classes that represent the main entities in your database or the data contracts for your API.
  • Role: Each model represents a specific data entity, such as User, Product, or Order, and is used throughout the application to handle and transport data. These models may also include data annotations for validation or serialization attributes.

DTOs (Data Transfer Objects)

  • Purpose: DTOs define the data structure that is sent and received by the API. They can simplify and optimize the data exchanged between the client and the server.
  • Role: DTOs act as intermediaries between your models and the client. For example, you might have UserDto for transferring user data to the client and CreateUserDto for data received in a user creation request. This helps decouple your models from the API contract and provides a layer of validation.

Note

In some instances, depending on the payload a DTO cannot be mapped properly to an entity. In the post endpoint of pictograms for example, the IFormFile prevents direct mapping and thus must be mapped manually later in the endpoint.

Extensions

  • Purpose: This folder may contain extension methods for configuring services, middleware, or other application components.
  • Role: Instead of configuring everything directly in Program.cs, you can create reusable configuration methods here. For instance, ConfigureSwagger or ConfigureDatabase methods could be used to set up Swagger or database services cleanly.

Properties

  • Purpose: This folder contains project properties, most notably launchSettings.json which is used to configure the environment for different launch profiles.
  • Role: Configures how the API should behave in different environments (e.g., development, production). It may specify things like the default URL for launching the application, and debug settings, and it can be modified depending on the runtime environment.

Migrations

  • Purpose: This folder is specifically used for database migrations, which help manage changes to the database schema over time.
  • Role: In .NET (often with Entity Framework Core), the Migrations/ folder contains files that represent individual changes to the database schema, such as adding new tables, modifying columns, or creating relationships. Each migration file contains the logic to apply and rollback changes. This folder is essential for versioning the database schema and updating it in sync with the application code.
  • Typical Contents:
    • Files with names like 20230101010101_InitialCreate.cs, where each migration class describes a change.
    • A DbContextModelSnapshot.cs file, which represents the current state of the model and helps Entity Framework determine what changes are necessary for future migrations.

appsettings.json

  • Purpose: A configuration file containing settings for the application, such as database connection strings, API keys, and other configuration options.
  • Role: Used to define environment-specific configurations. It can be read within the application, and you can set up appsettings.Development.json or appsettings.Production.json files to manage different settings for various environments.