---
description: Following dependency inversion & SRP principle
---

# Injection

📝 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 <mark style="background-color:green;">the main function which is the root of your project</mark> .

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.





2. 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.**

```dart
PulseXInjector injector = PulseXInjector.instance;
```

Then, you register your services and view models.

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

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

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

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

