Data Management
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/Features/UserManagement/IAuthService.cs
- MercaditoMovil/MercaditoMovil.Application/Validators/UserValidator.cs
- MercaditoMovil/Mercadito.Domain/Interfaces/Interface1.cs
- MercaditoMovil/MercaditoMovil/Program.cs
- MercaditoMovil/MercaditoMovil/Views/Form1.cs
- MercaditoMovil/MercaditoMovil/Views/Form1.Designer.cs
The data management strategy for MercaditoMovil is centered around a collection of CSV files that serve as the application's database. This approach facilitates easy bootstrapping and development without the need for a formal database server. The system includes a Python script for generating initial datasets and a data access layer in C# for reading from and writing to these files.
All data files are generated and stored within the MercaditoMovil.Infrastructure/DataFiles/ directory, organized into subdirectories for different data domains like People, Markets, and Commerce. The application interacts with this data through a repository pattern, abstracting the file-based storage from the application logic.
Sources: MercaditoMovil/MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py:18-20, MercaditoMovil/MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs
Data Generation
A Python script, create_csvs.py, is responsible for generating all the necessary CSV files with mock data. This script has no external dependencies and can be run to populate the DataFiles directory. It creates structured data for users, markets, producers, and product catalogs.
The script's main execution flow is as follows:
- Generate
markets.csv. - Load optional location data from
locations.csvto generate more realistic user addresses. - Generate
users.csvwith a mix of fixed and randomly generated users. - Generate
producers.csvwith a mix of individual and company producers. - Generate
product_catalog.csvfrom a predefined list. - Generate
producer_products.csv, linking producers to catalog items with random pricing.
Sources: MercaditoMovil/MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py:410-418
The following diagram illustrates the data generation process executed by the script.
Diagram showing the sequence of function calls in create_csvs.py to generate the data files.
Sources: MercaditoMovil/MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py:410-418
Data Storage and Schema
Data is persisted in several CSV files. The relationships between the main entities are established through foreign key identifiers (e.g., MarketId, UserId).
The diagram below shows the entity relationships between the core data files.
ER diagram illustrating the relationships between Users, Markets, Producers, and Products as defined in the CSV generation script.
Sources: MercaditoMovil/MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py
users.csv
Stores application user information.
| Column | Description |
|---|---|
UserId | Unique identifier (e.g., USR-XXXXXXXX). |
Username | User's login name. |
Password | User's password (stored in plain text). |
FirstName | User's first name. |
FirstLastName | User's first last name. |
SecondLastName | User's second last name. |
NationalId | National identification number. |
Email | User's email address. |
Phone | User's phone number. |
Address | User's exact address. |
Province | Province of residence. |
Canton | Canton of residence. |
District | District of residence. |
MarketId | Foreign key to the markets.csv file. |
Sources: MercaditoMovil/MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py:170-172, MercaditoMovil/MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:14-15
markets.csv
Contains a list of farmers' markets, primarily within the Greater Metropolitan Area (GAM) of Costa Rica.
| Column | Description |
|---|---|
MarketId | Unique identifier (e.g., MKT-XXX). |
MarketName | The full name of the market. |
Province | Province where the market is located. |
Canton | Canton where the market is located. |
District | District where the market is located. |
Sources: MercaditoMovil/MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py:68-69
producers.csv
Stores information about product producers, who can be individuals or companies.
| Column | Description |
|---|---|
ProducerId | Unique identifier (e.g., PR-XXX). |
ProducerCode | A business-facing code (e.g., PDCR-XXX). |
NationalId | National ID (personal or corporate). |
Name | Full name of the person or company. |
Email | Contact email. |
Phone | Contact phone number. |
MarketId | Foreign key to the associated market. |
UserId | Optional foreign key to the users.csv file if the producer is also a user. |
IsActive | Boolean flag (true/false). |
Sources: MercaditoMovil/MercaditoMovil.Infrastructure/Tools/Scripts/create_csvs.py:245-249
Data Access Layer
The application uses a repository pattern to abstract data operations. The IUserRepository interface defines the contract for user data management, and CsvUserRepository provides the concrete implementation that interacts with users.csv.
Sources: MercaditoMovil/Mercadito.Domain/Interfaces/Interface1.cs, MercaditoMovil/MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs
This diagram shows the class relationships in the data access and application service layers.
Class diagram showing the dependency of AuthService on the IUserRepository interface, which is implemented by CsvUserRepository.
Sources: MercaditoMovil/Mercadito.Domain/Interfaces/Interface1.cs, MercaditoMovil/MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs, MercaditoMovil/MercaditoMovil.Application/Service/AuthService.cs, MercaditoMovil/Mercadito.Domain/Entities/User/User.cs
User Retrieval and Authentication
User retrieval is performed by a sequential scan of the users.csv file. The Login process demonstrates the interaction between the controller, service, and repository.
The following sequence diagram details the login flow:
Sequence diagram for the user login process, from UI interaction to CSV file scanning.
Sources: MercaditoMovil/MercaditoMovil/Controllers/LoginController.cs:22-27, MercaditoMovil/MercaditoMovil.Application/Service/AuthService.cs:23-37, MercaditoMovil/MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:36-63
The GetByUsername method uses Microsoft.VisualBasic.FileIO.TextFieldParser to correctly handle comma-delimited fields that may be enclosed in quotes. It reads the file line by line, skipping the header, until it finds a matching username (case-insensitive).
Sources: MercaditoMovil/MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:39-42, 58-59
User Registration
New users are added by appending a new row to the users.csv file. The CsvUserRepository.Add method ensures the file and its directory exist, creates a header if the file is new, and then appends the formatted user data as a single line.
// MercaditoMovil/MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:80-95
var line = string.Join(",",
Q(user.UserId),
Q(user.Username),
Q(user.Password),
Q(user.FirstName),
Q(user.LastName1),
Q(user.LastName2),
Q(user.NationalId),
Q(user.Email),
Q(user.Phone),
Q(user.ExactAddress),
Q(user.Province),
Q(user.Canton),
Q(user.District),
Q(user.MarketId));
writer.WriteLine(line);
Code snippet showing how a User object is serialized into a CSV row.
Sources: MercaditoMovil/MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:80-95
The Q helper method is used to enclose each field in double quotes and escape any existing double quotes within the field's value, ensuring data integrity.
Sources: MercaditoMovil/MercaditoMovil.Infrastructure/Repositories/CsvUserRepository.cs:176-182
Domain Entities
User Entity
The MercaditoMovil.Domain.Entities.User class is the primary data model for an application user. It is an immutable class whose properties are set only at construction. The constructor provides null-safety by defaulting any null input strings to string.Empty, except for marketId which defaults to "MKT-000".
| Property | Type | Description |
|---|---|---|
UserId | string | Unique identifier. |
Username | string | User's login name. |
Password | string | User's password. |
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 exact address. |
Province | string | Province of residence. |
Canton | string | Canton of residence. |
District | string | District of residence. |
MarketId | string | ID of the user's preferred market. |
Sources: MercaditoMovil/Mercadito.Domain/Entities/User/User.cs:8-53