Project Structure
Relevant source files
The following files were used as context for generating this wiki page:
This document provides a technical overview of the WEBAPI project structure. The project is an ASP.NET Core Web API designed to serve as a backend for an e-commerce application. It follows a layered architecture pattern, separating concerns into distinct components: API Controllers for handling HTTP requests, a Service layer for business logic, and a Data layer for database interactions using Entity Framework Core.
The core functionalities include user management (registration, login, password reset), order processing, and product management. The system is configured to use a SQL Server database and includes features like dependency injection, Swagger for API documentation, and CORS for cross-origin requests.
Core Components & Startup
The application's entry point and configuration are defined in Program.cs. This file is responsible for setting up the web application builder, registering services for dependency injection, and configuring the HTTP request pipeline.
Sources: Program.cs
Service Registration (Dependency Injection)
Services are registered in the container to be injected into other components, such as controllers. Key services include:
- Controllers: Adds services for API controllers.
- Swagger/OpenAPI: For API exploration and documentation.
- DbContext:
UserDBContextis registered with a connection string to a SQL Server database. - Session Management: Configures session state with a 5-minute idle timeout.
- Custom Services: Application-specific services like
IUserService,IProductService, andIOrderServiceare registered with their concrete implementations.IUserServiceis registered as transient, whileIProductServiceandIOrderServiceare scoped.
Sources: Program.cs:13-40
The following diagram illustrates the dependency injection setup for the main services.
This diagram shows the services being registered in the dependency injection container.
Sources: Program.cs:13-40
HTTP Request Pipeline
The middleware pipeline is configured to handle incoming HTTP requests.
- Swagger: In the development environment, Swagger UI is enabled for API testing.
- CORS: A permissive CORS policy is applied, allowing any origin, method, and header.
- Authorization: Enables authorization middleware.
- Controller Mapping: Maps incoming requests to controller actions.
Sources: Program.cs:45-62
API Layer (Controllers)
The Controllers directory contains classes that handle incoming HTTP requests, process input, and return responses. They act as the interface between the client and the application's business logic. Controllers depend on service layer interfaces (e.g., IUserService, IOrderService) to perform operations.
Sources: Controllers/Login/UserController.cs, Controllers/Order/OrderController.cs
Key API Endpoints
The following table summarizes some of the main endpoints available in the API.
| Method | Route | Description | Controller |
|---|---|---|---|
POST | /api/v1/user/register | Registers a new user. | UserController |
POST | /api/v1/user/login | Authenticates a user. | UserController |
POST | /api/v1/user/forgot-password | Initiates the password reset process. | UserController |
POST | /api/v1/user/reset-password | Resets the user's password with a valid token. | UserController |
POST | /api/v1/order | Creates a new order. | OrderController |
GET | /api/v1/order | Retrieves all orders. | OrderController |
GET | /api/v1/order/user/{CustomerID} | Retrieves all orders for a specific customer. | OrderController |
PUT | /api/v1/order/{id} | Updates an existing order. | OrderController |
Sources: Controllers/Login/UserController.cs:19-195, Controllers/Order/OrderController.cs:18-125
User Registration Flow
The sequence diagram below illustrates the process for registering a new user via the /api/v1/user/register endpoint.
This diagram shows the interaction between the client, controller, service, and database during user registration.
Sources: Controllers/Login/UserController.cs:64-92
Data Layer
The data layer is responsible for all data persistence and retrieval, abstracting the database from the rest of the application. It is implemented using Entity Framework Core.
Data Models (Entities)
The Models directory contains Plain Old CLR Objects (POCOs) that represent the database tables. These entities are used by UserDBContext to query and save data.
Sources: Models/User.cs, Models/Order.cs, Models/OrderItem.cs, Models/Product.cs, Models/Category.cs, Models/Admin.cs
User Model
The User entity represents a customer or user of the system.
| Property | Type | Constraints | Description |
|---|---|---|---|
Id | int | Primary Key | Unique identifier for the user. |
Email | string | Required, MaxLength(100) | User's email address. |
UserName | string | Required, MaxLength(50) | Unique username. |
Password | string | Required, MaxLength(256) | User's password (stored as plain text). |
Role | bool | Required | User role (e.g., admin or regular user). |
ResetToken | string? | Nullable | Token for password reset functionality. |
ResetTokenExpiration | DateTime? | Nullable | Expiration date for the reset token. |
Sources: Configuration/UserConfiguration.cs:11-30, Migrations/UserDBContextModelSnapshot.cs:220-256
Database Context (UserDBContext)
UserDBContext.cs is the EF Core DbContext class. It acts as the bridge between the domain models and the database.
- DbSets: It defines
DbSet<T>properties for each entity (User,Order,Product, etc.), which represent the collections of entities in the database. - Model Configuration: The
OnModelCreatingmethod is overridden to apply entity configurations from separate classes (e.g.,UserConfiguration,OrderConfiguration). This keeps the DbContext class clean and organizes configuration logic. - Seeding: It calls a
Seed()extension method to populate the database with initial data, such as a default user.
Sources: Data/UserDBContext.cs:10-41, Seeders/DatabaseSeeder.cs:9-22
Entity Configurations
The Configuration directory contains classes that implement IEntityTypeConfiguration<T>. These classes use the EF Core Fluent API to configure the database schema for each entity, defining table names, primary keys, column types, lengths, relationships, and constraints.
For example, OrderConfiguration.cs defines:
- The table name as "Orders".
OrderIDas the primary key.- Constraints like
IsRequiredandHasMaxLengthfor properties. - The default value for
OrderDateusingHasDefaultValueSql("GETDATE()"). - The relationship between
OrderandOrderItems.
Sources: Configuration/OrderConfiguration.cs:11-30, Configuration/UserConfiguration.cs:9-31, Configuration/ProductConfiguration.cs:11-41, Configuration/CategoryConfiguration.cs:9-24
Database Schema
The following Entity-Relationship diagram illustrates the relationships between the main tables in the database, as defined by the models and their configurations.
This ER diagram shows the database schema, including tables for users, orders, products, and their relationships.
Sources: Migrations/UserDBContextModelSnapshot.cs, Models/Order.cs:19-22, Models/OrderItem.cs:12-20
Summary
The WEBAPI project is a well-structured ASP.NET Core application that effectively separates concerns into distinct layers. The use of dependency injection, a dedicated service layer, and Entity Framework Core with a repository-like pattern (via DbContext and services) creates a maintainable and scalable backend system. The clear definition of data models, configurations, and API controllers provides a solid foundation for building and extending e-commerce functionalities.