InversifyJS | lightweight inversion of control container | Dependency Injection library
kandi X-RAY | InversifyJS Summary
kandi X-RAY | InversifyJS Summary
InversifyJS is a lightweight inversion of control (IoC) container for TypeScript and JavaScript apps. An IoC container uses a class constructor to identify and inject its dependencies. InversifyJS has a friendly API and encourages the usage of the best OOP and IoC practices.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of InversifyJS
InversifyJS Key Features
InversifyJS Examples and Code Snippets
Community Discussions
Trending Discussions on InversifyJS
QUESTION
I have a react-native application frontend using inversify-js.
I've structured some service classes to use IOC (using an inversify container), such services intended to be shared as just one singleton instances among other services. They have an init/destroy method to help to clear up the services' internal state.
The init/destroy mechanism works fine but on top of that, it would be nice to have a way to "clear" the inversify container singletons, to rebuild all my singleton services from scratch.
Example:
src/services/A.ts
...ANSWER
Answered 2021-Mar-26 at 17:32The solution I suggest in the other answer is to simply ditch the current container
and create a fresh instance, very clean and easy to understand.
But then I look into the source code and find that such feature is already included.
container.unbindAll()
it is!
This API unconditionally resets the container to a fresh state (almost fresh, explain later) without the need to create a new instance. Link to source code.
I said "almost fresh" earlier, this is because container also has a less known (at least to me) feature called snapshots. Turns out you can even have multiple snapshots of binding setups stored in one container.
Interesting 🤔. So if you make a snapshot of a empty container before registering any binding, and restore it later, it’s effective a "reset".
Lastly, container.unload(containerModule)
is totally valid, not hacky at all. May call this one selective reset 😂
If you read the source code you’ll see, under the hood of all these methods, it’s all about modifying the internal _bindingDictionary
. That’s where all the bindings are stored.
QUESTION
I think I found my real problem, inversify
and everything else works fine.
In the original post I omitted some part of my code because I didn't think they were causing the problem.
In my ICitiesRepo
implementation I've a method ensureDb
that makes sure to init some typeorm
properties, since this method must be async I couldn't just call it in the constructor so should call it before any CRUD operation, then I created a decorator that has the purpose to call ensureDb
before any other method in the class:
ANSWER
Answered 2020-Nov-12 at 16:49You're almost there. You're getting undefined
because this
in your decorator is undefined and should be referring to an instantiated instance of your target. Changing that definition to a regular function call instead of an arrow function should do the trick.
QUESTION
I'm currently trying to implement a base class which has multiple properties. All but one property are being injected via the constructor using InversifyJS's @Inject tag. I'm also getting an instance via the container.Get() function in the constructor. When I run my application everything is fine, but when the application recieves a request, the app crashes without an error.
Base class
...ANSWER
Answered 2020-May-01 at 13:07In order for container.get()
to work, must be bound to the container at some point. In your composition root (where you set up your container) you can create this binding:
QUESTION
Is there any way to autowire (automatically register) singleton services to a C# DI container (Microsoft.Extensions.DependencyInjection) by annotation?
E.g. something like ProvidedIn option in @Injectable() annotation in Angular, @injectable() annotation in InversifyJS (Node.js), autowiring in Spring (Java) or autowiring in Symfony Framework (PHP)?
See Angular example below:
...ANSWER
Answered 2020-Apr-25 at 11:08No, and that's actually by-design.
The whole point of DI is that there are no surprises with how your program is configured: everything you configure in ConfigureServices
(your in your case, SetupDi
) is exactly what you get at runtime. By using attributes to configure DI then that would introduce "non-local effects" and it would be much harder to track-down bugs introduced by incorrect or misconfigured dependencies caused by an errant attribute.
(To that extent, I disagree with Angular's design - but that's off-topic).
(I also feel that .NET Core's DI system is also imperfect - too many necessary details are hidden behind DI injection extension-methods that you need to use ILSpy or Reflector to peer into).
As a workaround, you can "test" your DI services at app startup to ensure everything is configured by reflecting over every IService
in your project and trying to instantiate the implementation.
Here's the code I use in my ASP.NET and ASP.NET Core projects to verify DI is thoroughly configured:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install InversifyJS
Reflect metadata
Map
Promise (Only required if using provider injection)
Proxy (Only required if using activation handlers)
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page