question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: dependency injection everywhere

See original GitHub issue

I’d like to use dependency injection in pipes, filters and global guards 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:closed
  • Created 6 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilmysliwieccommented, Sep 25, 2017

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.

1reaction
RDeluxecommented, Jan 2, 2018

After reading several times the documentation, I feel that some things are missing.

The global pipes don’t belong to any scope. They live outside modules, thus as a result - they can’t inject dependencies.

Yet

Pipes, same as exception filters can be method-scoped, controller-scoped and global-scoped. Additionally, a pipe may be param-scoped

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 :

Let’s assume that we have a ValidationPipe registered in the SharedModule. This SharedModule is imported into root module. We can pick the ValidationPipe instance using following syntax:

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found