Pulse-X State Management
  • 📍Pulse-X Overview
    • 🛠️Use Tip
  • STATE MANAGEMENT
    • 💡Simple data
    • 🎢Collection data
    • 🌊Stream data
    • 🔮Future data
  • DEPENDENCY MANAGEMENT
    • 💉Injection
  • 🛣️ROUTE MANAGEMENT
    • 🚂Routing
  • EXAMPLES
    • 🍁Example Projects
  • CONTACT
    • 👨‍💻How to reach me
Powered by GitBook
On this page
  1. DEPENDENCY MANAGEMENT

Injection

Following dependency inversion & SRP principle

📝 Dependency injection is a technique that makes a class independent of its dependencies. It decouples the usage of an object from its creation. This helps you to follow 2 SOLID principles - Dependency Inversion & Single Responsibility Principles.

Usage

Here, I will show you only the usage.

You first need to register your view models and services in the main function which is the root of your project .

Pulse-X provides you with two types of register:

  1. register

The register method is used to register a dependency in the DI container. When you use register, the DI container creates a new instance of the registered dependency each time it is requested. In other words, a new instance is created for every injection point, ensuring that each consumer receives a unique instance. This can be useful when you want to have different instances of a class for each consumer.

  1. register lazy singleton

The registerLazySingleton method is used to register a dependency as a lazy singleton in the DI container. When you use registerLazySingleton, the DI container creates a single instance of the registered dependency when it is first requested. Subsequent requests for that dependency will return the same instance that was created initially. This ensures that all injection points receive the same instance, making it suitable for scenarios where you want to share the same instance of a class across the application.

You can access PulseXInjector instance like this. Global variable declaration is preferred.

PulseXInjector injector = PulseXInjector.instance;

Then, you register your services and view models.

injector.registerLazySingleton(() => Service());
injector.registerLazySingleton(
      () => ViewModel(),
);

Then, if you wanna get your view model or service. Use like this.

final viewModel = injector.find<ViewModel>();

If your view model or service has not been registered yet, Pulse-X will throw error!

PreviousFuture dataNextRouting

Last updated 2 years ago

💉