Product & Category Data Models
Relevant source files
The following files were used as context for generating this wiki page:The Product and Category data models form the core of the product catalog system within the WEBAPI application. These models define the structure for storing product information and organizing products into distinct categories. They are implemented as C# classes and mapped to database tables using Entity Framework Core.
The relationship between these models is a one-to-many association, where a single Category can contain multiple Product entities. The database schema, constraints, and relationships are configured using EF Core's Fluent API, ensuring data integrity and consistency. These models are ultimately exposed through a RESTful API for client consumption.
Entity Relationship Diagram
The following diagram illustrates the relationships between the Category, Product, and OrderItem entities in the database. A Category has many Products, and a Product can be part of many OrderItems.
Product Model
The Product model represents an item available for sale in the application.
Sources: Models/Product.cs, Configuration/ProductConfiguration.cs
Properties
The properties of the Product model are defined in Models/Product.cs and configured in Configuration/ProductConfiguration.cs.
| Property | Type | Database Type | Constraints | Description |
|---|---|---|---|---|
Id | int | int | Primary Key, Identity | Unique identifier for the product. |
Title | string | nvarchar(100) | Required, MaxLength(100) | The main title of the product. |
Name | string | nvarchar(200) | Required, MaxLength(200) | The full name of the product. |
Description | string | nvarchar(500) | MaxLength(500) | A detailed description of the product. |
Url | string | nvarchar(200) | MaxLength(200) | A URL slug for the product page. |
Gender | string | nvarchar(10) | MaxLength(10) | Target gender for the product (e.g., "Male", "Female"). |
Price | decimal | decimal(18,2) | Required | The price of the product. |
ProductImageUrl | string | nvarchar(500) | MaxLength(500) | URL for the product's image. |
CategoryId | int? | int | Nullable, Foreign Key | Foreign key referencing the Category. |
Sources: Models/Product.cs:5-13, Configuration/ProductConfiguration.cs:13-36, Migrations/UserDBContextModelSnapshot.cs:163-201
Database Configuration (ProductConfiguration)
The ProductConfiguration class uses the EF Core Fluent API to define the database schema for the Product entity. It sets the table name to "Product" and configures constraints for various properties.
// Configuration/ProductConfiguration.cs
public class ProductConfiguration : IEntityTypeConfiguration<Product>
{
public void Configure(EntityTypeBuilder<Product> builder)
{
builder.ToTable("Product");
builder.HasKey(p => p.Id);
builder.Property(p => p.Title)
.IsRequired()
.HasMaxLength(100);
// ... other properties
builder.Property(p => p.Price)
.IsRequired()
.HasColumnType("decimal(18,2)");
}
}
Sources: Configuration/ProductConfiguration.cs:8-39
Category Model
The Category model is used to group related products together.
Sources: Models/Category.cs, Configuration/CategoryConfiguration.cs
Properties
| Property | Type | Database Type | Constraints | Description |
|---|---|---|---|---|
Id | int | int | Primary Key, Identity | Unique identifier for the category. |
Name | string | nvarchar(100) | Required, MaxLength(100) | The name of the category. |
Description | string | nvarchar(500) | MaxLength(500) | A description of the category. |
Products | ICollection<Product> | N/A | Navigation Property | A collection of products belonging to this category. |
Sources: Models/Category.cs:9-14, Configuration/CategoryConfiguration.cs:13-22, Migrations/UserDBContextModelSnapshot.cs:64-83
Database Configuration (CategoryConfiguration)
Similar to the product, the CategoryConfiguration class defines the schema for the Category entity, setting the table name to "Categories" and applying constraints.
// Configuration/CategoryConfiguration.cs
public class CategoryConfiguration : IEntityTypeConfiguration<Category>
{
public void Configure(EntityTypeBuilder<Category> builder)
{
builder.ToTable("Categories");
builder.HasKey(c => c.Id);
builder.Property(c => c.Name)
.IsRequired()
.HasMaxLength(100);
builder.Property(c => c.Description)
.HasMaxLength(500);
}
}
Sources: Configuration/CategoryConfiguration.cs:8-25
Relationship and Database Context
Class Diagram
This diagram shows the C# classes and their relationship. Category holds a collection of Products.
Database Context (UserDBContext)
The UserDBContext is the EF Core database context class that manages the entities. It includes DbSet properties for Products and Categories and applies their respective configurations in the OnModelCreating method. This is where the application's data models are registered with EF Core.
// Data/UserDBContext.cs
public class UserDBContext : DbContext
{
// ... constructor ...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// ... other configurations ...
modelBuilder.ApplyConfiguration(new ProductConfiguration());
modelBuilder.ApplyConfiguration(new CategoryConfiguration());
// ... other configurations ...
}
// DbSet for each entity
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
}
Sources: Data/UserDBContext.cs:16, 18, 33, 35
The relationship between Product and Category is explicitly defined in the EF Core model snapshot. A product has an optional foreign key CategoryId, and a category can have many products.
Sources: Migrations/UserDBContextModelSnapshot.cs:253-257
API Endpoints for Products
The ProductController provides HTTP endpoints for performing CRUD (Create, Read, Update, Delete) operations on Product entities.
Source: [Controllers/LoadProduct/ProductController.cs]
| Method | Route | Description |
|---|---|---|
GET | api/v1/product | Retrieves a list of all products. |
GET | api/v1/product/{id} | Retrieves a single product by its ID. |
GET | api/v1/product/new | Retrieves a list of new products. |
POST | api/v1/product | Adds a new product. |
PUT | api/v1/product/{id} | Updates an existing product. |
DELETE | api/v1/product/{id} | Deletes a product by its ID. |
Sources: Controllers/LoadProduct/ProductController.cs:11-80
API Request Flow
This diagram shows the flow of a request to fetch all products.
Summary
The Product and Category models are fundamental data structures in this application, providing a well-defined schema for the product catalog. Their implementation using C# classes, combined with EF Core's configuration-based mapping, creates a robust and maintainable system for managing product data. The relationships are clearly defined, and the data is made accessible through a clean RESTful API, demonstrating a standard and effective architecture for a web API backend.