Backend Systems
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
- MercaditoMovil/MercaditoMovil/Views/Form1.Designer.cs
- MercaditoMovil/MercaditoMovil/Views/Form1.cs
The backend for MercaditoMovil is built using a layered architecture in C# (.NET) and is responsible for business logic, data persistence, and user authentication. The data storage is file-based, utilizing CSV files as a simple database. A supplementary Python script is used for generating the initial dataset, including users, markets, and products, which populates these CSV files.
The core backend functionality revolves around user management, providing services for user login and registration. The system is designed with a separation of concerns, dividing responsibilities into distinct layers: Presentation (WinForms), Application (Services), Domain (Entities and Interfaces), and Infrastructure (Data Repositories). This structure facilitates maintainability and isolates data access logic from business rules.
System Architecture
The application follows a classic layered architecture pattern. The layers communicate in a top-down fashion, where the UI (Presentation) interacts with Application Services via controllers, and the services orchestrate calls to the Infrastructure layer for data operations, all while using entities defined in the Domain layer.
This diagram illustrates the dependency flow between the major components of the backend system.
Sources: MercaditoMovil/Controllers/LoginController.cs, MercaditoMovil.Application/Service/AuthService.cs, MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs, Mercadito.Domain/Entities/User/User.cs, Mercadito.Domain/Interfaces/Interface1.cs, MercaditoMovil.Application/Features/UserManagement/IAuthService.cs, MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py
Data Persistence Layer
The persistence layer is implemented using CSV files, abstracting file I/O operations behind a repository pattern. This design choice avoids the need for a formal database system.
Sources: MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs, MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py
CSV Repository (CsvUserRepository)
The CsvUserRepository class is the concrete implementation for user data access. It reads from and writes to users.csv.
GetByUsername(string username): This method performs a sequential scan of theusers.csvfile. It reads the file line by line, skipping the header, and compares the username field in a case-insensitive manner. It returns aUserobject on the first match ornullif no user is found. It usesTextFieldParserto correctly handle fields enclosed in quotes.Add(User user, out string error): This method appends a new user record to theusers.csvfile. It ensures the directory and file exist, creating them and writing a header if necessary. All string values are quoted and have internal quotes escaped to maintain CSV integrity.
Sources: MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:33-60, 65-103
Data Schema
The data is organized across several CSV files. The Python script create_csvs.py is responsible for generating these files and their content. The following diagram shows the main entities and their relationships as defined by the CSV columns.
This ER diagram outlines the structure of the CSV files and the foreign key relationships between them.
Domain Model
The Domain layer contains the core business entities and interfaces.
Sources: Mercadito.Domain/Entities/User/User.cs, Mercadito.Domain/Interfaces/Interface1.cs
User Entity
The User class is a sealed class representing an application user. All its properties are strings and are guarded against null values in the constructor, defaulting to empty strings.
This class diagram shows the User entity and the interfaces for the repository and authentication service.
The table below details the properties of the User entity.
| Property | Type | Description |
|---|---|---|
UserId | string | Unique identifier for the user (e.g., USR-XXXXXXXX). |
Username | string | The user's login name. |
Password | string | The user's password (stored in plain text). |
FirstName | string | The user's first name. |
LastName1 | string | The user's first last name. |
LastName2 | string | The user's second last name. |
NationalId | string | The user's national identification number. |
Email | string | The user's email address. |
Phone | string | The user's phone number. |
ExactAddress | string | The user's detailed street address. |
Province | string | The province of the user's address. |
Canton | string | The canton of the user's address. |
District | string | The district of the user's address. |
MarketId | string | The ID of the market assigned to the user. Defaults to MKT-000. |
Sources: Mercadito.Domain/Entities/User/User.cs:8-22, 48
Application Services
The Application layer contains the business logic and orchestrates operations between the presentation and data layers.
Authentication Service (AuthService)
AuthService implements the IAuthService interface and provides the core logic for user authentication and registration. It depends on an IUserRepository to interact with the data store.
-
Login(string username, string password, out string message):- Validates that
usernameandpasswordare not empty usingUserValidator. - Retrieves the user from the repository via
_repo.GetByUsername(username). - Compares the provided password with the one stored in the retrieved
Userobject. - Returns
truefor a successful login,falseotherwise, with an appropriate output message.
- Validates that
-
Register(User user, out string message):- Validates the required fields of the
Userobject usingUserValidator. - Checks if a user with the same username already exists by calling
_repo.GetByUsername(user.Username). - If validation passes and the username is unique, it calls
_repo.Add(user, out repoError)to persist the new user. - Returns a success message, which varies depending on whether the user was assigned to a valid market (
MarketId != "MKT-000").
- Validates the required fields of the
Sources: MercaditoMovil.Application/Service/AuthService.cs:19-57, MercaditoMovil.Application/Validators/UserValidator.cs:11-40
The following sequence diagram illustrates the login process.
Initial Data Generation
A Python script, create_csvs.py, is provided to bootstrap the application with a set of realistic, randomly generated data. This script does not have external dependencies and can be run from any location.
Script Functionality
The script performs the following actions in order:
gen_markets(): Writes a predefined list of markets tomarkets.csv.gen_users(): Generates approximately 30 user records, including two fixed users and 28 randomly generated ones, and writes them tousers.csv. It can optionally uselocations.csvto generate more realistic geographic data.gen_producers(): Creates 30 producer records (a mix of individuals and companies) and saves them toproducers.csv.gen_catalog(): Populatesproduct_catalog.csvfrom a hardcoded list of products.gen_producer_products(): Links producers with products from the catalog, assigning random prices and other attributes, and writes the results toproducer_products.csv.
This flowchart shows the main execution flow of the data generation script.
Conclusion
The backend of MercaditoMovil is a pragmatic system designed for a desktop application environment. It employs a clean, layered architecture in C# that separates concerns effectively. The use of CSV files for data persistence, managed through a repository pattern, simplifies deployment and setup. An accompanying Python script for data generation is a key component, enabling rapid development and testing by providing a rich initial dataset. The core functionalities are centered around user authentication and registration, forming a solid foundation for the application's features.