Lesson 7 of 8
Mini Project: Task Manager - Part 4 (OOP Architecture)
Restructure the Task Manager with a full OOP architecture - a generic base repository, concrete Task and User repositories, a TaskService injected with interfaces, and an abstract BaseNotifier with Email and Console implementations. The system is now testable with in-memory fakes.
Task Manager Part 4 - OOP Architecture
// src/domain/entities.ts - domain value base class
abstract class Entity<T extends { id: number }> {
constructor(protected readonly props: T) {}
get id(): number { return this.props.id; }
equals(other: Entity<T>): boolean { return this.props.id === other.props.id; }
}
// src/domain/task.ts
class TaskEntity extends Entity<Task> {
get title() { return this.props.title; }
get status() { return this.props.status; }
get priority() { return this.props.priority; }
complete(): TaskEntity {
return new TaskEntity({ ...this.props, status: "done", updatedAt: new Date() });
}
assignTo(userId: number): TaskEntity {
return new TaskEntity({ ...this.props, updatedAt: new Date() });
}
toPlain(): Task { return { ...this.props }; }
}
// src/repositories/taskRepository.ts
interface ITaskRepository {
findById(id: number): Promise<TaskEntity | null>;
findAll(filter?: { status?: TaskStatus; assigneeId?: number }): Promise<TaskEntity[]>;
save(task: TaskEntity): Promise<TaskEntity>;
delete(id: number): Promise<void>;
}
class InMemoryTaskRepository implements ITaskRepository {
private map = new Map<number, Task>();
async findById(id: number): Promise<TaskEntity | null> {
const t = this.map.get(id);
return t ? new TaskEntity(t) : null;
}
async findAll({ status, assigneeId } = {}): Promise<TaskEntity[]> {
return [...this.map.values()]
.filter((t) => (!status || t.status === status))
.map((t) => new TaskEntity(t));
}
async save(task: TaskEntity): Promise<TaskEntity> {
this.map.set(task.id, task.toPlain());
return task;
}
async delete(id: number): Promise<void> {
this.map.delete(id);
}
}
// src/services/taskService.ts
class TaskService {
constructor(
private readonly taskRepo: ITaskRepository,
private readonly notifier: Notifier,
private readonly logger: Logger,
) {}
async createTask(input: CreateTaskInput, creatorId: number): Promise<TaskEntity> {
const task = new TaskEntity({
id: Date.now(),
...input,
status: "todo",
assignee: null,
dueDate: null,
tags: [],
description: null,
createdAt: new Date(),
updatedAt: new Date(),
});
const saved = await this.taskRepo.save(task);
this.logger.info("Task created", { taskId: saved.id, creatorId });
return saved;
}
async completeTask(taskId: number, userId: number): Promise<TaskEntity> {
const task = await this.taskRepo.findById(taskId);
if (!task) throw new Error(`Task ${taskId} not found`);
const completed = await this.taskRepo.save(task.complete());
await this.notifier.notify(userId, `Task "${task.title}" marked complete.`);
return completed;
}
}
// src/bootstrap.ts - wiring
const task Manager = (() => {
const repo = new InMemoryTaskRepository();
const logger = new ConsoleLogger();
const notifier = new ConsoleNotifier(logger);
return new TaskService(repo, notifier, logger);
})();Common Mistakes
- Mixing domain logic and infrastructure in entities - `TaskEntity.complete()` should not talk to the database. Domain logic stays in the entity; persistence stays in the repository.
- Using mutable class properties for domain state - returned new instances (`task.complete()` returns a new `TaskEntity`) rather than mutating `this.props`. Immutable domain objects prevent unexpected state changes.
- Not testing with in-memory fakes - the in-memory repository is the key testing asset. Any service test should use it to avoid touching a real database. Build it once, use it everywhere in tests.
Tip
Tip
Practice Mini Project Task Manager Part 4 OOP Architecture in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Mini Project: Task Manager - Part 4 (OOP Architecture)
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Task Manager Part 4 OOP Architecture from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Mini Project Task Manager Part 4 OOP Architecture is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.
Key Takeaways
- Restructure the Task Manager with a full OOP architecture - a generic base repository, concrete Task and User repositories, a TaskService injected with interfaces, and an abstract BaseNotifier with Email and Console implementations.
- Mixing domain logic and infrastructure in entities - `TaskEntity.complete()` should not talk to the database. Domain logic stays in the entity; persistence stays in the repository.
- Using mutable class properties for domain state - returned new instances (`task.complete()` returns a new `TaskEntity`) rather than mutating `this.props`. Immutable domain objects prevent unexpected state changes.
- Not testing with in-memory fakes - the in-memory repository is the key testing asset. Any service test should use it to avoid touching a real database. Build it once, use it everywhere in tests.
Finished this lesson?