Frontend Components
Relevant source files
The following files were used as context for generating this wiki page:- MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs
- Mercadito.Domain/Entities/User/User.cs
- MercaditoMovil.Application/Service/AuthService.cs
- MercaditoMovil/Controllers/LoginController.cs
- MercaditoMovil.Application/Validators/UserValidator.cs
- Mercadito.Domain/Interfaces/Interface1.cs
- MercaditoMovil.Application/Features/UserManagement/IAuthService.cs
- MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py
- MercaditoMovil/Program.cs
- MercaditoMovil/Views/Form1.Designer.cs
- MercaditoMovil/Views/Form1.cs
This document outlines the architecture and implementation of the user authentication and management system within the MercaditoMovil project. This system is responsible for handling user registration, login, data validation, and persistence. The backend is built using a layered architecture in .NET, with user data stored in CSV files.
The application's entry point is a Windows Forms application, but the core logic for user management is decoupled from the UI, residing in separate Application, Domain, and Infrastructure layers.
System Architecture
The user management feature follows a clean architecture pattern, separating concerns into distinct layers. The Presentation layer (Windows Forms) interacts with the Application layer, which contains business logic. The Application layer uses interfaces defined in the Domain layer to interact with the Infrastructure layer, which handles data persistence.
This diagram illustrates the high-level interaction between the major components.
Class Relationships
The following diagram shows the dependency and implementation relationships between the key classes and interfaces involved in authentication.
Sources: MercaditoMovil.Application/Features/UserManagement/IAuthService.cs, Mercadito.Domain/Interfaces/Interface1.cs, MercaditoMovil.Application/Service/AuthService.cs, MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs, MercaditoMovil/Controllers/LoginController.cs, MercaditoMovil.Application/Validators/UserValidator.cs
Data Model and Persistence
User Entity
The User entity represents an application user. It is a plain C# class with properties for user data. The constructor ensures that all string properties are non-null, defaulting to empty strings if null is passed.
Sources: Mercadito.Domain/Entities/User/User.cs
| Property | Type | Description |
|---|---|---|
UserId | string | Unique identifier for the user. |
Username | string | The user's login name. |
Password | string | The user's password (stored in plain text). |
FirstName | string | User's first name. |
LastName1 | string | User's first last name. |
LastName2 | string | User's second last name. |
NationalId | string | National identification number. |
Email | string | User's email address. |
Phone | string | User's phone number. |
ExactAddress | string | User's full street address. |
Province | string | Province of residence. |
Canton | string | Canton of residence. |
District | string | District of residence. |
MarketId | string | ID of the market associated with the user. Defaults to MKT-000. |
Sources: Mercadito.Domain/Entities/User/User.cs:7-41
CSV Storage
User data is stored in a CSV file located at MercaditoMovil.Infrastructure/DataFiles/People/users.csv. The CsvUserRepository class is responsible for all read and write operations on this file.
The structure of the users.csv file is defined by a header row.
This diagram represents the columns in the users.csv file.
Sources: MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:15-16, MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py:149-150
CsvUserRepository
This class implements the IUserRepository interface and handles the persistence of User entities in a CSV file.
| Method | Description |
|---|---|
GetByUsername(string username) | Performs a sequential scan of the CSV file to find a user by their username (case-insensitive). It uses Microsoft.VisualBasic.FileIO.TextFieldParser to correctly handle quoted fields. |
Add(User user, out string error) | Appends a new user record to the CSV file. It ensures the file and its directory exist, and writes the header if the file is new. |
EnsureFile() | A private helper that creates the directory and file if they don't exist. It also prepends the header to an existing file if it's missing. |
Q(string value) | A private static helper to quote and escape a string value for safe inclusion in a CSV row. |
Sources: MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs
Business Logic
Authentication Service (AuthService)
The AuthService class orchestrates the login and registration processes. It depends on IUserRepository for data access and UserValidator for input validation.
Sources: MercaditoMovil.Application/Service/AuthService.cs
Login Flow
The login process validates user input, retrieves the user from the repository, and compares the provided password with the stored password.
Registration Flow
The registration process validates the new user's data, checks for duplicate usernames, and then adds the new user to the repository.
Input Validation (UserValidator)
The static UserValidator class provides centralized validation logic for user-related operations.
ValidateLogin: Checks thatusernameandpasswordare not null or whitespace.ValidateRegister: Checks that theUserobject is not null and that required fields (Username,Password,FirstName,NationalId,Email) are not empty.
Sources: MercaditoMovil.Application/Validators/UserValidator.cs:11-42
Presentation Layer
The presentation layer is a Windows Forms application.
Program.cs: The main entry point for the application, which initializes and runsForm1.Form1.cs/Form1.Designer.cs: A basic, currently empty, Windows Form. It is intended to be the main window of the application.LoginController.cs: Acts as a bridge between the UI (forms) and theAuthService. It translates UI events into calls to the application service and returns results to be displayed.
Sources: MercaditoMovil/Program.cs, MercaditoMovil/Views/Form1.cs, MercaditoMovil/Controllers/LoginController.cs
Data Generation
A Python script, create_csvs.py, is provided to generate mock data for the application, including the users.csv file.
Key functions in this script related to user generation:
gen_users(): Orchestrates the creation of user data, including two fixed users and 28 randomly generated ones.rand_username(): Generates a plausible username from a first and last name.rand_email(): Generates a plausible email address.cr_id(): Generates a realistic 9-digit national ID number.pick_market(): Assigns a market ID based on the user's location.
The script writes the generated data to MercaditoMovil.Infrastructure/DataFiles/People/users.csv.
Sources: MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py:148-210