Skip to main content

Overview

The Customer model stores comprehensive customer information including personal details, contact information, and identification. This entity is used for customer relationship management, order tracking, and personalized service delivery.

Class Definition

namespace SupermarketWEB.Models
{
    public class Customer
    {
        public int Id { get; set; }
        public string Document { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public DateTime Birthday { get; set; }
        public string PhoneNumber { get; set; }
        public string Email { get; set; }
    }
}

Properties

Id
int
required
Primary key and unique identifier for the customer. Auto-generated by the database.
Document
string
required
Customer’s identification document number. Could be ID card, passport, driver’s license, or other official identification depending on the region.
FirstName
string
required
Customer’s first name or given name.
LastName
string
required
Customer’s last name or family name.
Address
string
required
Customer’s residential or mailing address. Full address including street, city, state, and postal code.
Birthday
DateTime
required
Customer’s date of birth. Stored as a DateTime value in the database. Can be used for age verification, birthday promotions, or demographic analysis.
PhoneNumber
string
required
Customer’s contact phone number. Stored as string to accommodate various phone number formats and international numbers.
Email
string
required
Customer’s email address for communication, receipts, and marketing purposes.

Data Types

The Customer model uses specific data types for each field:
// Text-based information
public string Document { get; set; }      // Alphanumeric ID
public string FirstName { get; set; }     // Text
public string LastName { get; set; }      // Text
public string Address { get; set; }       // Multi-line text
public string PhoneNumber { get; set; }   // Numeric string with formatting
public string Email { get; set; }         // Email format

Usage Examples

Creating a New Customer

var customer = new Customer
{
    Document = "12345678",
    FirstName = "John",
    LastName = "Smith",
    Address = "123 Main Street, New York, NY 10001",
    Birthday = new DateTime(1985, 3, 20),
    PhoneNumber = "+1-555-0123",
    Email = "[email protected]"
};

context.Customers.Add(customer);
await context.SaveChangesAsync();

Updating Customer Information

var customer = await context.Customers.FindAsync(customerId);
if (customer != null)
{
    customer.Address = "456 Oak Avenue, Brooklyn, NY 11201";
    customer.PhoneNumber = "+1-555-0199";
    customer.Email = "[email protected]";
    await context.SaveChangesAsync();
}

Searching Customers

// Search by document
var customer = await context.Customers
    .FirstOrDefaultAsync(c => c.Document == documentNumber);

// Search by email
var customer = await context.Customers
    .FirstOrDefaultAsync(c => c.Email == email);

// Search by name
var customers = await context.Customers
    .Where(c => c.FirstName.Contains(searchTerm) || 
                c.LastName.Contains(searchTerm))
    .ToListAsync();

Getting Customer Full Name

// In a service or view model
public string GetFullName(Customer customer)
{
    return $"{customer.FirstName} {customer.LastName}";
}

// Using LINQ projection
var customerNames = await context.Customers
    .Select(c => new {
        c.Id,
        FullName = c.FirstName + " " + c.LastName,
        c.Email
    })
    .ToListAsync();

Birthday Queries

// Get customers with birthdays this month
var currentMonth = DateTime.Today.Month;
var birthdayCustomers = await context.Customers
    .Where(c => c.Birthday.Month == currentMonth)
    .OrderBy(c => c.Birthday.Day)
    .ToListAsync();

// Get customers by age range
var minDate = DateTime.Today.AddYears(-65);
var maxDate = DateTime.Today.AddYears(-18);
var adultCustomers = await context.Customers
    .Where(c => c.Birthday >= maxDate && c.Birthday <= minDate)
    .ToListAsync();

Validation Example

public bool ValidateCustomer(Customer customer)
{
    if (string.IsNullOrWhiteSpace(customer.FirstName))
        return false;
    
    if (string.IsNullOrWhiteSpace(customer.LastName))
        return false;
    
    if (!IsValidEmail(customer.Email))
        return false;
    
    if (customer.Birthday > DateTime.Today)
        return false;
    
    return true;
}

private bool IsValidEmail(string email)
{
    try
    {
        var addr = new System.Net.Mail.MailAddress(email);
        return addr.Address == email;
    }
    catch
    {
        return false;
    }
}

Database Schema

When migrated to the database, the Customer table has the following structure:
ColumnTypeNullableKey
IdintNoPrimary Key (Identity)
Documentnvarchar(MAX)No
FirstNamenvarchar(MAX)No
LastNamenvarchar(MAX)No
Addressnvarchar(MAX)No
Birthdaydatetime2(7)No
PhoneNumbernvarchar(MAX)No
Emailnvarchar(MAX)No
Consider adding indexes on frequently queried fields like Document and Email for better performance:
modelBuilder.Entity<Customer>()
    .HasIndex(c => c.Document)
    .IsUnique();

modelBuilder.Entity<Customer>()
    .HasIndex(c => c.Email);

Data Annotations Enhancement

While the current model doesn’t include data annotations, you can enhance it with validation attributes:
using System.ComponentModel.DataAnnotations;

public class Customer
{
    public int Id { get; set; }
    
    [Required]
    [StringLength(50)]
    public string Document { get; set; }
    
    [Required]
    [StringLength(100)]
    public string FirstName { get; set; }
    
    [Required]
    [StringLength(100)]
    public string LastName { get; set; }
    
    [Required]
    [StringLength(500)]
    public string Address { get; set; }
    
    [Required]
    [DataType(DataType.Date)]
    public DateTime Birthday { get; set; }
    
    [Required]
    [Phone]
    [StringLength(20)]
    public string PhoneNumber { get; set; }
    
    [Required]
    [EmailAddress]
    [StringLength(200)]
    public string Email { get; set; }
}

Best Practices

  1. Unique Identifiers: Ensure Document and Email are unique per customer
  2. Data Validation: Validate email format and phone number format before saving
  3. Date Handling: Use UTC for Birthday if customers span multiple time zones
  4. Privacy: Implement proper security measures for storing personal information
  5. GDPR Compliance: Implement data retention and deletion policies
  6. Phone Format: Store phone numbers with country codes for international support
  7. Address Normalization: Consider breaking Address into separate fields (Street, City, State, Zip) for better querying
  8. Email Verification: Implement email verification workflow for accuracy

Relationships

The Customer model currently operates as an independent entity without foreign key relationships to other models. In a complete e-commerce system, you might extend this to include:
  • Orders collection (one-to-many)
  • Loyalty program membership
  • Shopping cart
  • Payment methods

Build docs developers (and LLMs) love