admin 管理员组文章数量: 1086019
Can you remend any great learning resources on inversify in typescript?
I have looked at / and have followed the example but am not grasping how it really works or why I need it.
A great video learning resource would be great or a simple easy beginner example.
Thanks for the help.
Can you remend any great learning resources on inversify in typescript?
I have looked at http://inversify.io/ and have followed the example but am not grasping how it really works or why I need it.
A great video learning resource would be great or a simple easy beginner example.
Thanks for the help.
Share Improve this question asked Dec 1, 2019 at 16:02 Hello-WorldHello-World 9,55524 gold badges90 silver badges157 bronze badges 3- This question seeking a remendation is off topic for this site – Mark Schultheiss Commented Dec 1, 2019 at 16:45
- 3 No. This question was very useful for me. I mean their official docs is only useful for themselves. I tried reading it without any inversify knowledge - and it made 0 sense. – Wajahath Commented Mar 22, 2022 at 18:17
- 1 Mark, your ment should be deprecated – Diego Commented Aug 19, 2022 at 22:30
1 Answer
Reset to default 10The idea of Inversion Of Control, aka dependency injection is that a class hands over control (read: responsibility) for instantiating dependent instances that the class needs to the container that will provide them with these instances instead.
So you would not do something like:
public constructor() {
this._katana = new Katana();
this._shuriken = new Shuriken();
}
I am not going to give a full example, because I would basically be copy-pasting the code that they clearly share on their website in the section 'The Basics'.
They give an example of constructor injection:
public constructor(
@inject(TYPES.Weapon) katana: Weapon,
@inject(TYPES.ThrowableWeapon) shuriken: ThrowableWeapon
) {
this._katana = katana;
this._shuriken = shuriken;
}
This is specifically useful for:
- Testing, since the dependent object can be mocked and injected
- Injecting dependent objects based on variable parameters.
For example, depending on environment you may want to inject a different configuration object with different values. This is just one example.
Constructor injection is usually preferred over property injection, as the library also seems to support this.
Note that what is injected is an interface, not a concrete class type.
So the class just declares that it needs an object of type Weapon / ThrowableWeapon.
The concrete binding happens in inversify.config.ts
:
container.bind<Weapon>(TYPES.Weapon).to(Katana)
So the reason why this is useful is that you have the ability to provide concrete classes at runtime. You don't need to pre-define (hardcode) them in the class.
本文标签: javascriptinversify help required and why do we need itStack Overflow
版权声明:本文标题:javascript - inversify help required and why do we need it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744055304a2525741.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论