I'm experimenting with Nestjs by trying to implement a clean-architecture structure and I'd like to validate my solution because I'm not sure I understand the best way to do it. Please note that the example is almost pseudo-code and a lot of types are missing or generic because they're not the focus of the discussion.
Starting from my domain logic, I might want to implement it in a class like the following:
@Injectable()
export class ProfileDomainEntity {
async addAge(profileId: string, age: number): Promise<void> {
const profile = await this.profilesRepository.getOne(profileId)
profile.age = age
await this.profilesRepository.updateOne(profileId, profile)
}
}
Here I need to get access to the profileRepository
, but following the principles of the clean architecture, I don't want to be bothered with the implementation just now so I write an interface for it:
interface IProfilesRepository {
getOne (profileId: string): object
updateOne (profileId: string, profile: object): bool
}
Then I inject the dependency in the ProfileDomainEntity
constructor and I make sure it's gonna follow the expected interface:
export class ProfileDomainEntity {
constructor(
private readonly profilesRepository: IProfilesRepository
){}
async addAge(profileId: string, age: number): Promise<void> {
const profile = await this.profilesRepository.getOne(profileId)
profile.age = age
await this.profilesRepository.updateOne(profileId, profile)
}
}
And then I create a simple in memory implementation that let me run the code:
class ProfilesRepository implements IProfileRepository {
private profiles = {}
getOne(profileId: string) {
return Promise.resolve(this.profiles[profileId])
}
updateOne(profileId: string, profile: object) {
this.profiles[profileId] = profile
return Promise.resolve(true)
}
}
Now it's time to wiring everything together by using a module:
@Module({
providers: [
ProfileDomainEntity,
ProfilesRepository
]
})
export class ProfilesModule {}
The problem here is that obviously ProfileRepository
implements IProfilesRepository
but it's not IProfilesRepository
and therefore, as far as I understand, the token is different and Nest is not able to resolve the dependency.
The only solution that I've found to this is to user a custom provider to manually set the token:
@Module({
providers: [
ProfileDomainEntity,
{
provide: 'IProfilesRepository',
useClass: ProfilesRepository
}
]
})
export class ProfilesModule {}
And modify the ProfileDomainEntity
by specifying the token to use with @Inject
:
export class ProfileDomainEntity {
constructor(
@Inject('IProfilesRepository') private readonly profilesRepository: IProfilesRepository
){}
}
Is this a reasonable approach to use to deal wit all my dependencies or am I completely off-track? Is there any better solution? I'm new fairly new to all of these things (NestJs, clean architecture/DDD and Typescript as well) so I might be totally wrong here.
Thanks