Architecture Overview
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 architecture. It is a .NET Core Web API designed to serve as a backend for an e-commerce application. The architecture follows a standard layered pattern, separating concerns into API, Service, and Data Access layers. It uses Entity Framework Core for data persistence with a SQL Server database.
The core components include API controllers for handling HTTP requests, a service layer for business logic (inferred from dependency injection), and a data access layer managed by Entity Framework Core, which includes the DbContext, data models, and entity configurations.
System Components & Request Flow
The application is structured in a classic n-tier architecture. An incoming HTTP request is routed to a controller, which then utilizes a service to perform business operations. The service interacts with the UserDBContext to access the database.
This diagram illustrates the high-level request flow from client to database.
Application Startup & Services
The application's entry point is Program.cs, which configures and launches the web host. It is responsible for setting up dependency injection, middleware, and other essential services.
Service Registration
The following key services are registered in the dependency injection container:
| Service | Registration Method | Purpose |
|---|---|---|
| Controllers | AddControllers() | Registers API controllers. |
| Swagger/OpenAPI | AddEndpointsApiExplorer(), AddSwaggerGen() | Enables API discovery and documentation. |
| DbContext | AddDbContext<UserDBContext>() | Registers the EF Core database context. |
| Session | AddDistributedMemoryCache(), AddSession() | Configures in-memory session management. |
| Custom Services | AddTransient<IUserService, ...>, AddScoped<IProductService, ...>, AddScoped<IOrderService, ...> | Registers the application's business logic services. |
| CORS | UseCors() | Configures Cross-Origin Resource Sharing to allow requests from any origin. |
Sources: Program.cs:12-42, Program.cs:51-55
Middleware Pipeline
The HTTP request pipeline is configured to handle development-time exceptions, Swagger UI, CORS, authorization, and controller mapping.
Sources: Program.cs:45-59
API Layer (Controllers)
The API layer is implemented using ASP.NET Core MVC Controllers. These classes are responsible for handling incoming HTTP requests, validating input, and returning appropriate HTTP responses. The OrderController is a representative example.
Sources: Controllers/Order/OrderController.cs
Order API Endpoints
The OrderController exposes RESTful endpoints for managing orders.
| Method | Route | Description | Request Body DTO |
|---|---|---|---|
POST | api/v1/order | Creates a new order. | CreateOrderRequest |
GET | api/v1/order | Retrieves all orders. | N/A |
GET | api/v1/order/{id} | Retrieves a specific order by its ID. | N/A |
GET | api/v1/order/user/{CustomerID} | Retrieves all orders for a specific customer. | N/A |
PUT | api/v1/order/{id} | Updates an entire order. | UpdateOrderRequest |
PATCH | api/v1/order/{id} | Partially updates an order's status, address, and delivery method. | UpdateOrderStatusRequest |
DELETE | api/v1/order/{id} | Deletes an order by its ID. | N/A |
Sources: Controllers/Order/OrderController.cs:18-129
Create Order Sequence
This diagram shows the sequence of operations when a client creates a new order. The controller receives a CreateOrderRequest DTO, maps it to an Order entity, and passes it to the IOrderService for processing.
Data Access Layer
The data access layer is built using Entity Framework Core. It is responsible for all database interactions.
DbContext
UserDBContext is the heart of the data layer. It represents a session with the database and allows querying and saving data. It defines DbSet properties for each entity, which map to database tables.
Sources: Data/UserDBContext.cs:7-36
Entity Configuration
Instead of using data annotations, the application uses the IEntityTypeConfiguration<T> interface to configure the entity models and their mapping to the database schema. This approach centralizes configuration and keeps the model classes clean. For example, OrderConfiguration defines the table name, primary key, and property constraints for the Order entity.
// Example from OrderConfiguration.cs
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.ToTable("Orders");
builder.HasKey(o => o.OrderID);
builder.Property(o => o.TotalAmount).HasColumnType("decimal(18,2)").IsRequired();
builder.Property(o => o.OrderDate).IsRequired().HasDefaultValueSql("GETDATE()");
// ... other configurations
}
Sources: Configuration/OrderConfiguration.cs:10-28, Data/UserDBContext.cs:15-21
Database Schema
The database schema is defined by the EF Core models and configurations. The following Entity-Relationship diagram illustrates the main tables and their relationships.
Key Data Models
Order Model
The Order entity represents a customer's order.
| Property | Type | Constraints | Description |
|---|---|---|---|
OrderID | int | Primary Key | Unique identifier for the order. |
CustomerID | int | Required, Foreign Key to User | The ID of the customer who placed the order. |
TotalAmount | decimal(18,2) | Required | The total cost of the order. |
OrderDate | DateTime | Required, Default GETDATE() | The date and time the order was placed. |
Status | string | Required, MaxLength(50) | The current status of the order (e.g., "Pending"). |
Address | string | Required, MaxLength(200) | The shipping address for the order. |
DeliveryMethod | string | MaxLength(50) | The chosen delivery method. |
PaymentMethod | string | MaxLength(50) | The chosen payment method. |
OrderItems | ICollection<OrderItem> | Navigation Property | A collection of items included in the order. |
Sources: Configuration/OrderConfiguration.cs:10-28, Migrations/UserDBContextModelSnapshot.cs:91-131
Product Model
The Product entity represents an item available for sale.
| Property | Type | Constraints | Description |
|---|---|---|---|
Id | int | Primary Key | Unique identifier for the product. |
CategoryId | int? | Foreign Key to Category | The category this product belongs to. |
Title | string | Required, MaxLength(100) | A short title for the product. |
Name | string | Required, MaxLength(200) | The full name of the product. |
Price | decimal(18,2) | Required | The price of the product. |
Description | string | MaxLength(500) | A detailed description of the product. |
ProductImageUrl | string | MaxLength(500) | URL for the product's image. |
Sources: Configuration/ProductConfiguration.cs:11-38, Migrations/UserDBContextModelSnapshot.cs:176-221