npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

get_it-container

v1.0.5

Published

GetIt is a versatile and lightweight dependency injection container designed for Node.js applications. It simplifies dependency management, promotes code modularity, and enhances testability. Whether you're working on a small project or a large-scale appl

Downloads

14

Readme

GetIt Node.js Package

GetIt is a versatile and lightweight dependency injection container designed for Node.js applications. It simplifies dependency management, promotes code modularity, and enhances testability. Whether you're working on a small project or a large-scale application, GetIt provides a convenient way to organize and resolve dependencies.

Installation

To install GetIt, use npm:

npm install get_it-container

Why GetIt?

1. Dependency Organization and Control

GetIt helps you structure your codebase by centralizing the registration and resolution of dependencies. This promotes cleaner and more modular code, making it easier to understand and maintain.

2. Testability and Mocking

GetIt is particularly beneficial for testing scenarios. By registering factories and singletons, you gain more control over the instantiation of classes, allowing you to replace actual implementations with mocks or stubs during testing. This is crucial for isolating units of code and ensuring reliable and focused testing.

3. Flexible Registration

Registering Factories

I.registerFactory(() => new MyClass());

Factories are useful when you want to create a new instance of a class each time it is requested. This is ideal for managing transient dependencies.

Registering Factories with Parent Class

I.registerFactory(() => new MyDerivedClass(), MyClass);

You can register factories with a specified parent class, ensuring that the resolved instance is a subtype of the parent class.

Registering Lazy Singletons

I.registerLazySingleton(() => new MySingletonClass());

Lazy singletons are instantiated only once, the first time they are requested. Subsequent requests return the same instance. This is useful for managing shared state across your application.

Registering Lazy Singletons with Parent Class

I.registerLazySingleton(() => new MyDerivedSingletonClass(), MySingletonClass);

Similar to factories, you can register lazy singletons with a parent class, ensuring the resolved instance is a subtype of the parent class.

4. Resolving Dependencies

To obtain an instance of a registered class, use the get method:

const instance = I.get(MyClass);

5. Resetting the Container

If needed, you can clear all registered classes in the container:

I.reset();

6. Viewing Registered Classes

To see a summary of the registered classes and their types, you can call the toString method:

console.log(I.toString());

Use Case Scenario

Consider a scenario where you're developing an e-commerce application. You have various components such as a shopping cart, payment processor, and user authentication. GetIt can help organize and manage these dependencies efficiently.

import { GetIt } from 'get_it-container';

const I = GetIt.I;

class ShoppingCart {
  // ...
}

class PaymentProcessor {
  // ...
}

class UserAuthentication {
  // ...
}

// Register dependencies
I.registerLazySingleton(() => new ShoppingCart());
I.registerLazySingleton(() => new PaymentProcessor());
I.registerLazySingleton(() => new UserAuthentication());

// In your application code or services
const shoppingCart = I.get(ShoppingCart);
const paymentProcessor = I.get(PaymentProcessor);
const userAuthentication = I.get(UserAuthentication);

// ...

// In your testing environment
// Replace actual implementations with mocks or stubs
I.registerFactory(() => createMock(ShoppingCart), ShoppingCart);
I.registerFactory(() => createMock(PaymentProcessor), PaymentProcessor);
I.registerFactory(() => createMock(UserAuthentication), UserAuthentication);

// Perform unit tests with controlled dependencies
// ...

/// now you can access all registered dependencies from everywhere
/// by using this container object
export I;

License

This package is licensed under the MIT License - see the LICENSE file for details.


Feel free to further adjust the content according to your preferences.