Skip to content

Instantly share code, notes, and snippets.

@nicolasdanelon
Created June 2, 2026 19:38
Show Gist options
  • Select an option

  • Save nicolasdanelon/311d214c9128c5ff08fdaec93e353237 to your computer and use it in GitHub Desktop.

Select an option

Save nicolasdanelon/311d214c9128c5ff08fdaec93e353237 to your computer and use it in GitHub Desktop.
class APIUserController {
constructor(private service: UserService) {}
update(): void {
const id = 4;
const name = "Nicolás";
this.service.setUserName(id, name);
}
show(): void {
const name = this.service.getUserName(4);
}
}
class User {
constructor(private name: string) {}
static findById(id: number): User {
return new User("Nico");
}
getName(): string {
return this.name;
}
setName(name: string): void {
this.name = name;
}
}
class UserRepository {
updateUserName(id: number, name: string): void {
const user = User.findById(id);
user.setName(name);
// persistir cambios
}
getUserName(id: number): string {
return User.findById(id).getName();
}
}
class UserService {
constructor(private repo: UserRepository) {}
private ensureNonEmptyName(name: string): void {
if (!name.trim()) {
throw new Error("Name cannot be empty");
}
}
setUserName(id: number, name: string): void {
this.ensureNonEmptyName(name);
this.repo.updateUserName(id, name);
}
getUserName(id: number): string {
return this.repo.getUserName(id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment