Console application domain model
Console application domain model
A domain model represents the information and rules of an application independently of its user interface. In a task manager, a task has a positive ID, a nonblank title and a completion state. The model decides whether a transition is legal; the console decides how to present the result. This lets a future web interface reuse the same rules without copying terminal code.
Use a normal class here: a task has an identity and its completion changes over time. Records are introduced in the next chapter. Keep IDs stable; a position in a list is not an ID. The examples below are separate .NET 10 console programs, not a single combined file.
using System;var task = new WorkItem(7, "Pack samples");Console.WriteLine(task.Done);task.Complete();Console.WriteLine(task.Done);sealed class WorkItem{ public int Id { get; } public string Title { get; } public bool Done { get; private set; } public WorkItem(int id, string title) { if (id <= 0 || string.IsNullOrWhiteSpace(title)) throw new ArgumentException("Invalid task"); Id = id; Title = title; } public void Complete() => Done = true;}The constructor establishes valid initial state. Complete changes the private setter without printing anything. Calling Complete twice leaves the task complete.
Expected output (reasoned, not executed):
FalseTrueBuild on the first example
using System;using System.Collections.Generic;var ids = new List<int>();foreach (int id in new[] { 4, 9, 4 }) Console.WriteLine(TryAdd(ids, id));Console.WriteLine(ids.Count);static bool TryAdd(List<int> ids, int id){ if (id <= 0 || ids.Contains(id)) return false; ids.Add(id); return true;}The list changes only after both checks pass. This small example isolates the ID rule; the complete project stores task objects rather than a second list of IDs.
Expected output (reasoned, not executed):
TrueTrueFalse2Apply the idea to another case
using System;Console.WriteLine(CountOpen(new[] { false, true, false }));Console.WriteLine(CountOpen(Array.Empty<bool>()));static int CountOpen(bool[] completed){ int count = 0; foreach (bool done in completed) if (!done) count++; return count;}CountOpen is a pure calculation: it reads its inputs, does not mutate them and returns a count. The caller owns formatting. Empty input naturally produces zero.
Expected output (reasoned, not executed):
20Common mistakes and design choices
Do not make Id publicly writable after insertion. Changing an identifier breaks lookup assumptions. Do not print inside every domain operation: a returned result can be tested independently. A method that modifies a list is not pure merely because it avoids Console. Treat expected duplicate IDs as ordinary rejection; constructors may still reject invalid use by programmers.
Practice
Add a Reopen operation to WorkItem. Start task 12 incomplete, complete it, reopen it twice, and report its state after each call. Specify whether the operation is idempotent.
Use the manual practice task and knowledge check to verify your understanding. All examples target C# 14 / .NET 10; execution and ByteMind renderer support remain unverified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Console application domain model — knowledge check
Apply the lesson concepts to the following scenarios.
Which operation should own the completion rule?
What should a stable task ID identify?
What does a failed duplicate check change in example 2?
Why can CountOpen be tested without console input?
What does completing an already completed task do in this model?