Skip to content
Open core software, built in Singapore Established 2018  /  Five utilities
Jering
Talk to us

Dependency Injection

Javascript.NodeJS Jering documentation Open source NuGet package
Documentation menu

Jering.Javascript.NodeJS is designed for dependency injection. The registration is short, the service is a singleton and every dependency can be replaced for tests.

Registration

C#
var services = new ServiceCollection();
services.AddNodeJS();
var provider = services.BuildServiceProvider();
var node = provider.GetRequiredService<INodeJSService>();

Lifetime

AddNodeJS registers INodeJSService as a singleton. That is intentional: the Node.js process is expensive to start and cheap to reuse. Inject the interface into your own services instead of resolving it from a static locator.

Testing

Because the API is an interface, a test can replace INodeJSService with a fake that returns a known result. That keeps unit tests fast and removes the Node.js requirement from the test machine.

When to use the static API

StaticNodeJSService wraps the same service for applications that do not use a container. Both APIs are valid. Pick one per application and do not mix them in the same code path.

Replacing internals

File watching, HTTP content creation and process management are separate abstractions. Register a different implementation before building the provider when you need custom behavior.

Reference shelf