Jump to content

Layered

From Knowledge Base

Layered Architecture

Layered architecture is a software design approach that organizes system components into distinct, self-contained layers, each with a specific responsibility, promoting [separation of concerns](https://en.wikipedia.org/wiki/Separation_of_concerns) and maintainability.

Overview

This is a possible implementation with Domain-Driven Design (DDD) as "North Star", .NET, Entity Framework Core (EF Core) and a REST API 😅👍. A well-structured layered architecture helps in maintaining a clean and maintainable codebase.

Summary

  • The Domain Layer encapsulates the core business logic using entities, value objects, and domain services (Entity classes).
  • The Infrastructure Layer handles data access using EF Core and external service interactions (Repository classes).
  • The Application Layer orchestrates business operations (Handler and Query classes).
  • The API (Presentation) Layer manages HTTP requests and responses (Controller classes).
  • Cross-cutting concerns like logging, caching, and configuration are handled as needed.
  • In this Database-First approach, Entity Framework Core (EF Core) is used to generate entity classes and contexts from an existing database schema.


Simple Layered Architecture (4 Layers)

1. Presentation (API) Layer This layer handles the HTTP requests and responses, serving as the entry point for your API.

  • Controllers: Receive HTTP requests, validate inputs, and interact directly with the application's services.
  • Dependency Injection: Configuration classes to inject handlers, queries, entities, context, etc.

2. Application Layer This layer contains the application logic.

  • Application Services: Coordinate the application's behavior by using the Domain Entities and Services.
  • DTOs (Data Transfer Objects): Define input and output models for API endpoints. DTOs simplify data transfer, enhance security, and optimize performance by providing a structured and controlled means of exposing and transferring data between layers as needed.
  • Mapping: From entities to DTOs, this can be done automatically (for example, AutoMapper) or manually.

3. Domain Layer This layer represents the core of your application and contains the business logic.

  • Entities: Define the domain objects and their behavior.
  • Value Objects: Represent immutable values without an identity.
  • Domain Services: Implement domain-specific logic that doesn't naturally fit into entities or value objects.

4. Infrastructure Layer This layer provides implementations for data access and external services.

  • Entity Framework Core (EF Core): Implement database access and ORM here.
  • Repositories: Define interfaces for data access and provide concrete implementations using EF Core.
  • External Services: Implement clients for external services (e.g., third-party APIs).

These Layers can be represented by own folders or also own projects, that depends on the scaling.

Sophisticated (6 Layers)

1. API (Presentation) Layer This layer handles user interactions and exposes the REST API. The RESTful API endpoints are built with the ASP.NET Core Web API.

  • Controllers: Receive HTTP requests, validate inputs, and orchestrate the interaction with the application's services.
  • DTOs (Data Transfer Objects): Used to define the input and output models for API endpoints.
  • Authentication and Authorization: Implement authentication and authorization mechanisms here.

2. Application Layer This layer contains the application logic, often called the "Application Services" in DDD.

  • Application Services: Coordinate the application's behavior by using the Domain Services and Entities.
  • Mappings: Convert DTOs to Domain Entities and vice versa.
  • Validation: Perform validation of input data and enforce business rules.

3. Domain Layer This layer represents the core of your application and contains the business logic.

  • Entities: Define the domain objects and their behavior.
  • Value Objects: Represent immutable values without an identity.
  • Domain Services: Implement domain-specific logic that doesn't naturally fit into entities or value objects.
  • Aggregates: Define aggregates to group related entities and enforce consistency boundaries.

4. Infrastructure Layer This layer provides implementations for data access, external services, and other infrastructure concerns.

  • Entity Framework Core (EF Core): Implement database access and ORM here.
  • Repositories: Define interfaces for data access and provide concrete implementations using EF Core.
  • External Services: Implement clients for external services (e.g., third-party APIs).
  • Caching, Logging, and Configuration: Handle cross-cutting concerns here.

5. Persistence Layer This sub-layer is responsible for managing database access and migrations.

  • DbContext: Implement EF Core's DbContext for managing the database schema and CRUD operations.
  • Migrations: Use EF Core migrations to evolve the database schema.

6. Cross-cutting Concerns

  • Logging: Implement logging for monitoring and debugging.
  • Caching: Add caching mechanisms where necessary.
  • Configuration: Centralize application configuration settings.

ASP.NET Architecture

Error creating thumbnail: Unable to save thumbnail to destination

https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures