Feature request: dependency injection everywhere
See original GitHub issueI’d like to use dependency injection in pipe
s, filter
s and global guard
s as well.
Usecase: business validation with AOP, separated to the business logic
For example: The UserAdminController
receives a UserCreateCommand
, which I can validate it with the class-validator
in a pipe
so it has to be filled with the right data, but I cannot use a service to check is there any user with the same email address.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
New dependency injection features in .NET 6
The "problem" with IAsyncDisposable is that everywhere that "cleans up" IDisposable objects by calling IDisposable on them now also needs to ...
Read more >Design Patterns Explained – Dependency Injection with Code ...
Dependency injection is a programming technique that makes a class independent of its dependencies. It achieves that by decoupling the usage ...
Read more >Dependency Injection in Swift using latest Swift features
Dependency Injection using latest Swift features allows you to mock data, and write tests easily without 3rd party dependencies.
Read more >Contexts and Dependency Injection - Quarkus
Quarkus DI solution (also called ArC) is based on the Contexts and Dependency Injection for Java 2.0 specification. However, it is not a...
Read more >ASP.NET Core Dependency Injection Best Practices, Tips ...
Because it makes the dependencies implicit. That means it's not possible to see the dependencies easily while creating an instance of the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @peterreisz, Since guards determine which route can be accessed, maybe it’s even better place than interceptor to provide this type of logic. Referring to your question, yes, there’s a reason. All of these 4 building blocks - pipes, filters, interceptors, and guards have their specific responsibilities. The interceptors and guards are combined with modules, while pipes and filters are running outside the module zone. The pipes task is to validate types, object structure or to map data based on some specific conditions. The pipe is not the proper place to select from the database or call any service. On the other hand, the interceptors shouldn’t validate the object schema or just decorate the data. If the override is necessary, it must be caused because of the e.g. database call. Same guards, which in your case should take over the validation responsibility.
After reading several times the documentation, I feel that some things are missing.
Yet
After reading this, I had the feeling that I could create controller-scoped pipes and use dependency injection, but it does not seem possible (not a good choice in my case anyway).
I’m a bit lost here. My use case is simple: use class-transformer and class-validator to cast a JSON to a class instance, and throw an error if the JSON does not validate the class schema. However I need to log this (using a logger service) and to throw translated errors (based on the user language) in case of error (TranslatorService + access to connected user information).
I do not want to use the pipe for all my routes, I’m not using
useGlobalPipes
.Moreover, I did not find any example of pipe registration in a module, which should/could be possible if I’m reading this correctly :
I may just switch to an interceptor instead of a pipe, it seems to cover more or less the same use cases, and can inject dependencies (such as my loggerService and translationService).
Edit: The problem with interceptors is that they don’t receive the metatype like the pipes do.