Files
puaros/packages/guardian/examples/repository-pattern/good-interface-in-use-case.ts
imfozilbek 0534fdf1bd feat: add repository pattern validation (v0.5.0)
Add comprehensive Repository Pattern validation to detect violations
and ensure proper domain-infrastructure separation.

Features:
- ORM type detection in repository interfaces (25+ patterns)
- Concrete repository usage detection in use cases
- Repository instantiation detection (new Repository())
- Domain language validation for repository methods
- Smart violation reporting with fix suggestions

Tests:
- 31 new tests for repository pattern detection
- 292 total tests passing (100% pass rate)
- 96.77% statement coverage, 83.82% branch coverage

Examples:
- 8 example files (4 bad patterns, 4 good patterns)
- Demonstrates Clean Architecture and SOLID principles
2025-11-24 20:11:43 +05:00

75 lines
1.5 KiB
TypeScript

/**
* ✅ GOOD EXAMPLE: Repository interface in use case
*
* Use case depends on repository interface, not concrete implementation.
* This follows Dependency Inversion Principle.
*/
class CreateUser {
constructor(private readonly userRepo: IUserRepository) {}
async execute(data: CreateUserRequest): Promise<UserResponseDto> {
const user = User.create(Email.from(data.email), data.name)
await this.userRepo.save(user)
return UserMapper.toDto(user)
}
}
interface IUserRepository {
save(user: User): Promise<void>
findByEmail(email: Email): Promise<User | null>
}
class Email {
private constructor(private readonly value: string) {}
static from(value: string): Email {
if (!value.includes("@")) {
throw new Error("Invalid email")
}
return new Email(value)
}
getValue(): string {
return this.value
}
}
class User {
static create(email: Email, name: string): User {
return new User(email, name)
}
private constructor(
private readonly email: Email,
private readonly name: string,
) {}
getEmail(): Email {
return this.email
}
getName(): string {
return this.name
}
}
interface CreateUserRequest {
email: string
name: string
}
interface UserResponseDto {
email: string
name: string
}
class UserMapper {
static toDto(user: User): UserResponseDto {
return {
email: user.getEmail().getValue(),
name: user.getName(),
}
}
}