Best of Dependency InjectionJanuary 2025

  1. 1
    Article
    Avatar of codigeeCodigee·1y

    Don't mix bloc with get_it

    Mixing Bloc with dependency injection tools like get_it requires careful consideration to avoid issues with state management. Using singletons for BLoC can disrupt its lifecycle, causing unexpected state persistence and errors. Instead, use registerFactory to ensure clean state instances. For better integration, use RepositoryProvider for dependencies within the widget tree.

  2. 2
    Article
    Avatar of tiaThis is Angular·1y

    Angular's inject function: a couple of years later...

    Angular's `inject` function, introduced in Angular 14, offers an alternative to constructors for injecting dependencies. It simplifies inheritance, reduces boilerplate code, eliminates the need for parameter decorators, and provides more concise and type-safe code. However, it can lead to scattered and hidden dependencies, making testing and code comprehension more challenging. Understanding the Injection Context is crucial for using `inject` effectively. Despite some concerns, `inject` has proven useful and is recommended over constructor parameters for consistent coding practices.

  3. 3
    Article
    Avatar of tiaThis is Angular·1y

    Dynamic Service Instantiation In Angular

    Dynamic service instantiation in Angular can be leveraged to defer the creation of services based on user selections, thus optimizing performance. This approach is useful when integrating third-party services or when runtime decision-making is needed. Examples include dynamic formatters, file upload handlers, notification services, and authentication providers. Using Angular's Injector, services are created only when required, avoiding the overhead of unnecessary initializations.