Interface and Implementation Inject Issue
See original GitHub issueThank you for making this wonderful library! But, I have some issue with your lib… There is an example.
- Interface
export interface InterfaceClass {
helloWorld(): string;
}
- Implementation
import {InterfaceClass} from "./InterfaceClass";
import {Service} from "typedi";
@Service('helloworld')
export class InterfaceClassImpl implements InterfaceClass {
helloWorld(): string {
return 'Hello World!';
}
}
And I got error with this code
- Test code
import 'reflect-metadata'
import 'mocha';
import {Container} from "typedi";
import {InterfaceClass} from "./InterfaceClass";
import {InterfaceClassImpl} from "./InterfaceClassImpl";
describe('Inject test', () => {
it('We cannot DI', () => {
let interfaceClass: InterfaceClass = Container.get('helloworld');
console.log(interfaceClass.helloWorld())
});
});
- Error code
ServiceNotFoundError: Service "helloworld" was not found, looks like it was not registered in the container. Register it by calling Container.set("helloworld", ...) before using service.
at new ServiceNotFoundError (src/error/ServiceNotFoundError.ts:11:9)
at ContainerInstance.getServiceValue (src/ContainerInstance.ts:277:19)
at ContainerInstance.get (src/ContainerInstance.ts:119:21)
at Function.Container.get (src/Container.ts:107:36)
at Context.it (source/InjectTest.ts:11:52)
But weirdly, I can succes when I append Container.get(InterfaceClassImpl);
- Success Test Code
describe('Inject test', () => {
it('We can DI', () => {
Container.get(InterfaceClassImpl);
let interfaceClass: InterfaceClass = Container.get('helloworld');
console.log(interfaceClass.helloWorld())
});
it('We can DI too', () => {
new InterfaceClassImpl();
let interfaceClass: InterfaceClass = Container.get('helloworld');
console.log(interfaceClass.helloWorld())
});
});
Container.get seems to call new… I means, if we don’t new, we cannot register service. What is the right way to do?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:8
- Comments:7 (1 by maintainers)
Top Results From Across the Web
4 solutions for selective injection when the interface has ...
4 solutions for selective injection when the interface has multiple implementations · 1. Description of the problem · 2. Relatively low-level ...
Read more >A problem implementing Dependency Injection and Interfaces
A problem implementing Dependency Injection and Interfaces · 1. That's because you only have 1 object, and it has state, which you change...
Read more >DI: Injecting interfaces vs actual classes
You can only inject an object that implements an interface. I think what you mean to say is that the object receiving the...
Read more >Interfaces in Angular dependency injection | Damir's Corner
When an instance of the interface is requested, the correct implementation is injected based on the dependency injection configuration.
Read more >Design Patterns Explained – Dependency Injection with Code ...
But that's not an issue because you don't need to implement it. ... It has no dependency on any interface implementation.
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 Free
Top 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

Any updated on it? I’m struggling with same issue. Workaround from @openprojectio doesn’t work for me.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.