Core Features
Relevant source files
The following files were used as context for generating this wiki page:
- MercaditoMovil/MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py
- MercaditoMovil/MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs
- MercaditoMovil/Mercadito.Domain/Entities/User/User.cs
- MercaditoMovil/MercaditoMovil.Application/Service/AuthService.cs
- MercaditoMovil/MercaditoMovil/Controllers/LoginController.cs
- MercaditoMovil/MercaditoMovil.Application/Validators/UserValidator.cs
- MercaditoMovil/Mercadito.Domain/Interfaces/Interface1.cs
- MercaditoMovil/MercaditoMovil.Application/Features/UserManagement/IAuthService.cs
- MercaditoMovil/MercaditoMovil/Program.cs
This document outlines the architecture and implementation of the user authentication system and the underlying data management strategy for the MercaditoMovil application. The system is designed around a layered architecture with a CSV-based data persistence mechanism, bootstrapped by a Python script.
The core functionality includes user registration and login, which are handled through a series of services, repositories, and validators. The data, including users, producers, and markets, is stored in CSV files located in the MercaditoMovil.Infrastructure/DataFiles/ directory.
System Architecture
The user management feature follows a layered architectural pattern, separating concerns between presentation, application logic, domain modeling, and data access.
The following diagram illustrates the high-level components and their interactions.
This diagram shows the dependency flow from the UI down to the data files.
Sources: MercaditoMovil/Controllers/LoginController.cs, MercaditoMovil.Application/Service/AuthService.cs, Mercadito.Domain/Interfaces/Interface1.cs, MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs, MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py
Key Components
| Component | Description | Source File |
|---|---|---|
LoginController | A bridge between the WinForms UI and the application services, handling user input for login and registration. | MercaditoMovil/Controllers/LoginController.cs |
AuthService | Implements the core business logic for user login and registration, coordinating validation and repository access. | MercaditoMovil.Application/Service/AuthService.cs |
UserValidator | Provides static methods to validate user input for required fields during login and registration. | MercaditoMovil.Application/Validators/UserValidator.cs |
User Entity | Represents the application user, containing all user-related properties. | Mercadito.Domain/Entities/User/User.cs |
IUserRepository | Defines the contract for data persistence operations related to users (GetByUsername, Add). | Mercadito.Domain/Interfaces/Interface1.cs |
CsvUserRepository | The concrete implementation of IUserRepository that persists user data to a CSV file. | MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs |
create_csvs.py | A Python script responsible for generating all necessary CSV data files with mock data. | MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py |
Data Model and Persistence
The application's data is stored in a set of CSV files. A Python script, create_csvs.py, is responsible for creating and populating these files with initial data. This script defines the schema for all data entities.
Sources: MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py
ER Diagram
The following diagram shows the relationships between the main data entities as defined by the CSV generation script.
This ER diagram visualizes the schema of the CSV files and their foreign key relationships.
Sources: MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py:180,250,323
User Data (users.csv)
The users.csv file stores information about application users. The CsvUserRepository class is responsible for reading from and writing to this file.
Sources: MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:15, MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py:170
| Column | C# Property | Description | Source |
|---|---|---|---|
UserId | UserId | Unique identifier for the user (e.g., USR-XXXXXXXX). | User.cs:9 |
Username | Username | The user's login name. | User.cs:10 |
Password | Password | The user's password (stored in plain text). | User.cs:11 |
FirstName | FirstName | User's first name. | User.cs:12 |
FirstLastName | LastName1 | User's first last name. | User.cs:13 |
SecondLastName | LastName2 | User's second last name. | User.cs:14 |
NationalId | NationalId | National identification number. | User.cs:15 |
Email | Email | User's email address. | User.cs:16 |
Phone | Phone | User's phone number. | User.cs:17 |
Address | ExactAddress | User's physical address. | User.cs:18 |
Province | Province | Province of residence. | User.cs:19 |
Canton | Canton | Canton of residence. | User.cs:20 |
District | District | District of residence. | User.cs:21 |
MarketId | MarketId | The ID of the market assigned to the user based on location. | User.cs:22 |
The CsvUserRepository performs a sequential scan to find a user by username. New users are appended to the end of the file.
// MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:36-39
public User? GetByUsername(string username)
{
// ...
while (!parser.EndOfData)
{
// ...
if (string.Equals(u.Username, username, StringComparison.OrdinalIgnoreCase))
return u;
}
return null;
}
Sources: MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:36-57
Authentication Flow
User Login
The login process is initiated by the LoginController, which passes credentials to the AuthService. The service first validates the inputs and then queries the CsvUserRepository to find a matching user and verify the password.
The sequence diagram below details the login process.
This diagram shows the sequence of calls during a user login attempt.
Sources: MercaditoMovil/Controllers/LoginController.cs:22, MercaditoMovil.Application/Service/AuthService.cs:23-37, MercaditoMovil.Application/Validators/UserValidator.cs:11, MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:36
User Registration
User registration follows a similar pattern. The AuthService validates the new user's data, checks for duplicate usernames, and then uses the CsvUserRepository to persist the new user.
The sequence diagram below details the registration process.
This diagram shows the sequence of calls during a user registration attempt.
Sources: MercaditoMovil/Controllers/LoginController.cs:30, MercaditoMovil.Application/Service/AuthService.cs:42-63, MercaditoMovil.Application/Validators/UserValidator.cs:24, MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:68
Summary
The user authentication and data management system of MercaditoMovil is a straightforward implementation based on a layered architecture. It relies on CSV files for data storage, which are initially created by a dedicated Python script. The C# backend handles business logic for login and registration through a service layer that interacts with a repository responsible for low-level file I/O. This design, while simple, effectively decouples the application logic from the data persistence mechanism.
Sources: MercaditoMovil.Application/Service/AuthService.cs, MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs, MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py