mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-28 15:26:53 +05:00
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
99 lines
2.1 KiB
TypeScript
99 lines
2.1 KiB
TypeScript
/**
|
|
* ✅ GOOD EXAMPLE: Dependency Injection
|
|
*
|
|
* Use case receives dependencies through constructor.
|
|
* This makes code testable and follows SOLID principles.
|
|
*/
|
|
|
|
class CreateUser {
|
|
constructor(
|
|
private readonly userRepo: IUserRepository,
|
|
private readonly emailService: IEmailService,
|
|
) {}
|
|
|
|
async execute(data: CreateUserRequest): Promise<UserResponseDto> {
|
|
const user = User.create(Email.from(data.email), data.name)
|
|
await this.userRepo.save(user)
|
|
await this.emailService.sendWelcomeEmail(user.getEmail())
|
|
return UserMapper.toDto(user)
|
|
}
|
|
}
|
|
|
|
interface IUserRepository {
|
|
save(user: User): Promise<void>
|
|
findByEmail(email: Email): Promise<User | null>
|
|
}
|
|
|
|
interface IEmailService {
|
|
sendWelcomeEmail(email: Email): Promise<void>
|
|
}
|
|
|
|
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(),
|
|
}
|
|
}
|
|
}
|
|
|
|
class UserRepository implements IUserRepository {
|
|
async save(_user: User): Promise<void> {
|
|
console.warn("Saving user to database")
|
|
}
|
|
|
|
async findByEmail(_email: Email): Promise<User | null> {
|
|
return null
|
|
}
|
|
}
|
|
|
|
class EmailService implements IEmailService {
|
|
async sendWelcomeEmail(email: Email): Promise<void> {
|
|
console.warn(`Sending welcome email to ${email.getValue()}`)
|
|
}
|
|
}
|