1.0.5 • Published 6 months ago

get_it-container v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

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.
1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago