abdullahkhanjmm / dotnet-roadmap-2024

0 stars 0 forks source link

Task: Create a Clean Architecture and Implement a REST API with CRUD Operations #11

Closed abdullahkhanjmm closed 2 months ago

abdullahkhanjmm commented 3 months ago

Task: Create a Clean Architecture and Implement a REST API with CRUD Operations

Overview

In this task, you will set up a .NET project using the principles of Clean Architecture. You will create a RESTful API with CRUD operations, ensuring a clear separation of concerns and maintainability of the codebase.

Clean Architecture Layers

  1. Core (Domain) Layer:

    • Entities: Business models that are independent of any framework or technology.
    • Interfaces: Abstractions for services and repositories.
    • Domain Services: Business logic that doesn't belong to a specific entity.
  2. Application Layer:

    • DTOs (Data Transfer Objects): Models for transferring data between layers.
    • Services/Use Cases: Application-specific business logic.
    • Interfaces Implementation: Implementation of domain interfaces.
  3. Infrastructure Layer:

    • Data Access: Repositories and data context for interacting with the database.
    • External Services: Integration with external APIs or services.
    • Configuration: Application configuration and settings.
  4. Presentation (API) Layer:

    • Controllers: API endpoints to handle HTTP requests and responses.
    • Models: API request and response models.
    • Mapping: AutoMapper profiles for converting between domain models and DTOs.

Steps

1. Setup the Project

  1. Install .NET SDK: Ensure you have the latest .NET SDK installed on your machine.
  2. Create a Solution: Open a terminal or command prompt and create a new solution.
    dotnet new sln -n CleanArchitectureDemo
    cd CleanArchitectureDemo

2. Create Project Structure

  1. Create Projects:

    • Core: Contains entities, interfaces, and domain services.
      dotnet new classlib -n CleanArchitectureDemo.Core
      dotnet sln add CleanArchitectureDemo.Core/CleanArchitectureDemo.Core.csproj
    • Application: Contains application services and DTOs.
      dotnet new classlib -n CleanArchitectureDemo.Application
      dotnet sln add CleanArchitectureDemo.Application/CleanArchitectureDemo.Application.csproj
    • Infrastructure: Contains data access implementations and external services.
      dotnet new classlib -n CleanArchitectureDemo.Infrastructure
      dotnet sln add CleanArchitectureDemo.Infrastructure/CleanArchitectureDemo.Infrastructure.csproj
    • API: The presentation layer with controllers and API configurations.
      dotnet new webapi -n CleanArchitectureDemo.API
      dotnet sln add CleanArchitectureDemo.API/CleanArchitectureDemo.API.csproj
  2. Add Project References:

    • Reference Core in Application:
      dotnet add CleanArchitectureDemo.Application/CleanArchitectureDemo.Application.csproj reference CleanArchitectureDemo.Core/CleanArchitectureDemo.Core.csproj
    • Reference Application and Core in Infrastructure:
      dotnet add CleanArchitectureDemo.Infrastructure/CleanArchitectureDemo.Infrastructure.csproj reference CleanArchitectureDemo.Application/CleanArchitectureDemo.Application.csproj
      dotnet add CleanArchitectureDemo.Infrastructure/CleanArchitectureDemo.Infrastructure.csproj reference CleanArchitectureDemo.Core/CleanArchitectureDemo.Core.csproj
    • Reference Application, Core, and Infrastructure in API:
      dotnet add CleanArchitectureDemo.API/CleanArchitectureDemo.API.csproj reference CleanArchitectureDemo.Application/CleanArchitectureDemo.Application.csproj
      dotnet add CleanArchitectureDemo.API/CleanArchitectureDemo.API.csproj reference CleanArchitectureDemo.Core/CleanArchitectureDemo.Core.csproj
      dotnet add CleanArchitectureDemo.API/CleanArchitectureDemo.API.csproj reference CleanArchitectureDemo.Infrastructure/CleanArchitectureDemo.Infrastructure.csproj

3. Implementing Layers

  1. Core Layer:

    • Create entity classes.
    • Define repository interfaces.
  2. Application Layer:

    • Create DTOs for transferring data.
    • Implement application services and business logic.
  3. Infrastructure Layer:

    • Implement repository classes using Entity Framework Core.
    • Configure database context and external service integrations.
  4. API Layer:

    • Create controllers for handling HTTP requests.
    • Define endpoints for CRUD operations.
    • Configure AutoMapper for model mapping.
    • Set up dependency injection for services and repositories.

4. Implement CRUD Operations

  1. Create:

    • Define HTTP POST endpoint in the controller.
    • Implement service method to handle creation logic.
  2. Read:

    • Define HTTP GET endpoints (single item and list) in the controller.
    • Implement service methods to handle fetching data.
  3. Update:

    • Define HTTP PUT endpoint in the controller.
    • Implement service method to handle update logic.
  4. Delete:

    • Define HTTP DELETE endpoint in the controller.
    • Implement service method to handle deletion logic.

Deliverables


This task description provides a clear overview and detailed steps for setting up a .NET project with Clean Architecture and implementing a RESTful API with CRUD operations.

JMMAwais commented 2 months ago

https://github.com/JMMAwais/CleanArchitecture

asifaliwork commented 2 months ago

https://github.com/asifaliwork/CleanArchitectureDemo-net8.0-