typedi icon indicating copy to clipboard operation
typedi copied to clipboard

fix: Adding Interface into constructor throw: CannotInjectValueError - you don't use interfaces without service tokens as injection value.

Open joseaburt opened this issue 1 year ago • 2 comments

Description

Guys we are having an issue when we pass interface into a constructor of a service that depedens on a super type.

image

In this image as you can see it is not possible to depend on abstraction it required the same concret class annotated with @Service() annotation.

This is coupling and make testing hard, because for integration testing on my API I need to double the repository to avoid direct database calls or any other 3er party API.

Also it is not possible to change the dependency for a mock one during the testing.

How are you handling this issue guys?

joseaburt avatar Aug 12 '24 08:08 joseaburt

Im having this same issue.

andvalsol avatar Aug 23 '24 00:08 andvalsol

If someone still struggles with this, you can achieve it by using factories:

Image

UserRepository is a @Service-decorated concrete class implementing UserRepositoryInterface.

It is also possible to use factory classes instead of functions (docs)

BmpCorp avatar Nov 16 '25 08:11 BmpCorp