hvpaiva / clean-architecture-nestjs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UseCase depends on Framework

af-owe opened this issue · comments

My understanding is that the application layer should not know about the framework. But the use-cases know about NestJS, so isn't this a violation of the very basic idea of Clean Architecture? I am still looking for a good way to handle this, but even outside of NestJS, many Dependency Injection libraries depend on decorators provided by a framework. And therefore we will see imports from frameworks in the application layer all the time...

Custom providers should work just fine.

@Module({
  imports: [],
  controllers: [PostsController],
  providers: [
    {
      provide: PostsUseCases
      inject: [IUsersRepository],
      useFactory: (usersRepository: IUsersRepository) => new PostsUseCases(usersRepository)
    },
    { 
      provide: IUsersRepository, 
      useClass: UsersRepository 
    },
  ],
})
export class PostsModule {}

My understanding is that the application layer should not know about the framework. But the use-cases know about NestJS, so isn't this a violation of the very basic idea of Clean Architecture? I am still looking for a good way to handle this, but even outside of NestJS, many Dependency Injection libraries depend on decorators provided by a framework. And therefore we will see imports from frameworks in the application layer all the time...

I totally agree with you, OWE-AF. In a more purist view, the framework should only be in the Infrastructure layer.

That said, in contrast, most frameworks I've worked with either don't allow or make it very unpleasant to do so. So I'm in favor of some kind of "conditional purism" (lol).

To be fair, I haven't worked with Nest and JS in a few years, and maybe today I would do something a little different or have a better answer.

However, in many cases, you may want to create a monitoring or error handling interface, even some framework abstraction, to segregate these operations and implement them with some infrastructure components. But in most cases, it's just not worth it and purism doesn't pay off.

Thanks for explanation 👍