I'm creating a new project usin Nestjs, which I'm really starting to like. I cannot figure out though, what the right way is for separating Business layer and Application layer.

Following the tutorials on the official Nestjs docs logic is usually separated into modules. This is approach feels natural, except the same module is in charge of controllers and services and/or entities. With this approach, if a feature has to change, only the one module has to be updated, but if I wanted to swap the business layer implementation, or the application layer for the entire project I would have to edit every module to to swap controllers or whatever.

I found this starter project that provides a layered architecture:


The important part is in the following directory structure:

- /backend/src/app
    - application
    - domain
    - host
    - shared

This separetes the domain and application layers into separate directories. But the application modules and services directly reference classes in the domain directory. To me this means that even though the files are physically separated, the two layers are still coupled. To change the domain layer you would have to replace one with the same directory structure and file names. There is no use of an IoC container to easily swap implementations. Am I wrong in this assumption?

I feel I'm running in circles trying to find the sweet-spot between a well-defined architecture and overkill. What I really want is to have the business layer on its own so I could move it to a different framework even and just hook it up there. This project is a rework of an existing project where the chosen framework didn't really work for us, so now we have to write the business layer again. I'd like to avoid that in the future.

When I searched for 'nestjs ioc' I came up with this link: https://docs.nestjs.com/fundamentals/custom-providers

I'm not sure if I understand this 100%, but what I get is, in the application module (which contains the controller) I would have to name my dependencies, so that If I need to swap an implementation, I only have to change the useValue property for the custom provider. What is not clear here, is how I should define the property where it is injected. Is it an interface of the depency?

Sorry for the long post!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.