
How Nullable Types in C# Create More Accurate Domain Models & Better ORM Mappings
When modeling real-world domains in C#, nullable types (string?, DateTime?, bool?, etc.) aren’t just syntax sugar; they’re essential for accurately representing optional data, enforcing business rules, and improving database interactions.
1. Better Domain Clarity
Nullable types help distinguish between:
- "Zero" vs. "Unknown" – int? score = null (not taken) vs. score = 0 (actual zero)
- "Not applicable" vs. "False" – bool? IsApproved = null (pending) vs. false (rejected)
- "Missing data" vs. "Empty string" – string? comment = null (not provided) vs. "" (explicitly blank)
Example (E-Commerce):
public class Product {
public required string Name { get; set; }
public decimal Price { get; set; }
public string? Description { get; set; } // Optional
public int? StockCount { get; set; } // Null = not tracked
public DateTime? DiscontinuedDate { get; set; } // Null = still active
}
2. Cleaner ORM & Database Mapping
Relational databases use NULL extensively, and C# nullables ensure seamless ORM behavior with:
- EF Core / Dapper / NHibernate – Properly maps NULL ↔ null without "magic values" (like -1 or DateTime.MinValue)
- Accurate schema generation – NOT NULL vs. NULL columns align with C# model requirements
- Correct query handling – WHERE column IS NULL works as expected
Example (Employee DB):
public class Employee {
public int Id { get; set; }
public required string Name { get; set; }
public DateTime? TerminationDate { get; set; } // Null = still employed
public decimal? Bonus { get; set; } // Null = not eligible
}
→ EF Core generates the right NULL-allowed columns.
3. Key Takeaways
- Use T? for "optional" domain data (missing, unknown, or N/A states).
- Combine with the required to enforce mandatory fields at compile time.
- Improves ORM reliability – No more hidden InvalidOperationException from unexpected NULLs.
Result? More expressive domain models, fewer bugs, and database mappings that actually reflect real-world scenarios.
Leave a Reply
Your e-mail address will not be published. Required fields are marked *