-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Named Service doesn`t work #652
Comments
I have same problem. I mentioned here ISSUE |
@michaeldouglasdev I think you have a little problem in your This snippet works as expected for me @Service({id: 'ClassB'})
export class ClassB {
execute() {
console.log('execute Class B')
}
}
@Service({id: 'ClassA'})
export class ClassA {
execute() {
const classB = Container.get<ClassB>("ClassB");
classB.execute();
}
}
const inst = Container.get<ClassA>('ClassA');
inst.execute(); |
@michaeldouglasdev Sorry, I was on the develop branch... Tested it on master and I cannot reproduce it with this snippet @Service('ClassB')
export class ClassB {
execute() {
console.log('execute Class B')
}
}
@Service('ClassA')
export class ClassA {
execute() {
const classB = Container.get<ClassB>("ClassB");
classB.execute();
}
}
const inst = Container.get<ClassA>('ClassA');
inst.execute(); Could you provide a reproduction repo? |
In my case, Named Service that in other files are failed to be discovered. There are 6 Services. Relative to the test case file,
Test cases and resultsdescribe("Test", () => {
it("TestSameFile", () => {
assert.equal(Container.get(TestSameFile).run(), true)
})
it("TestSibling", () => {
assert.equal(Container.get(TestSibling).run(), true)
})
it("TestSub", () => {
assert.equal(Container.get(TestSub).run(), true)
})
it("TestSameFileNamed", () => {
assert.equal(Container.get<any>('TestSameFileNamed').run(), true)
})
it("TestSiblingNamed", () => {
assert.equal(Container.get<any>('TestSiblingNamed').run(), true)
})
it("TestSubNamed", () => {
assert.equal(Container.get<any>('TestSubNamed').run(), true)
})
})
@Service()
export class TestSameFile {
public run(): boolean {
return true;
}
}
@Service('TestSameFileNamed')
export class TestSameFileNamed {
public run(): boolean {
return true;
}
} Only two were failed to discovered.Which are Named and not in the same file.directory structurewhole projecthttps://github.com/jeesim2/test-typedi/blob/main/app/test.spec.ts Is there anything that I missed? |
@jeesim2 typedi does not run a full filesystem check for services. Your example fails because those named services are never imported thus the decorator never runs so it does not get registered. Can you try your example but instead of using any, you actually reference the classes? |
@attilaorosz It also fails when I give type to the
Named service only can be discovered when it is defined at same file with caller. BTW, My usecase is that, |
Hello, guys! I solved it this way, it wasn't quite what I wanted, but that's how I'm doing it
|
Not sure I follow this statement - it should be looking for all items marked as @service ??? That is the point of the typedi registration process ? |
(btw, this is causing quite a few issues moving from 0.8.0 to 0.10.0) |
Closing this in favor or #636 |
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. |
Description
Named Service doesn`t work, but works with Container.set.
TypeDI version: 0.10.0
I've tried using version 0.8.0, but it doesn't work either.
I don't want to inject via constructor with private, as I want to use the Stategy Design Pattern and that way it wouldn't work, at least as far as I know.
Error message:
Service with "ClassB" identifier was not found in the container. Register it before usage via explicitly calling the "Container.set" function or using the "@service()" decorator.
Minimal code-snippet showcasing the problem
Expected behavior
Expected to return a Class B instance
Actual behavior
Does not return a Class B instance.
Error message:
Service with "ClassB" identifier was not found in the container. Register it before usage via explicitly calling the "Container.set" function or using the "@service()" decorator.
The text was updated successfully, but these errors were encountered: