zef_di_core 5.0.0 copy "zef_di_core: ^5.0.0" to clipboard
zef_di_core: ^5.0.0 copied to clipboard

A dart library which provides abstractions for dependency injection.

example/zef_di_core_example.dart

import 'package:zef_di_core/zef_di_core.dart';

import 'test_classes/implementations/index.dart';
import 'test_classes/interfaces/index.dart';

void main() async {
  // Build the ServiceLocator
  ServiceLocatorBuilder().build();

  // Register an Singleton
  await ServiceLocator.I.registerSingleton(
    Dolphin(),
    interfaces: {Animal, Fish},
  );

  // Register another Singleton
  await ServiceLocator.I.registerSingleton(
    Dolphin(),
    interfaces: {Animal, Fish},
  );

  // Register a Transient
  await ServiceLocator.I.registerTransient(
    (args) async => Whale(),
  );

  // Retrieve the Singleton
  final instance = await ServiceLocator.I.resolve<Dolphin>();

  // Retrieve the instance via the interface
  final interfaceInstance = await ServiceLocator.I.resolve<Animal>();

  // Do something with the instances
  print(instance.runtimeType); // Output: Dolphin
  print(interfaceInstance.runtimeType); // Output: Dolphin
}
1
likes
150
pub points
28%
popularity

Publisher

verified publisherzooper.dev

A dart library which provides abstractions for dependency injection.

Repository (GitHub)
View/report issues

Topics

#dependency-injection #di #injection #ioc #inversion-of-control

Documentation

API reference

Funding

Consider supporting this project:

buymeacoffee.com

License

MIT (LICENSE)

Dependencies

any_of, zef_helpers_lazy, zef_log_core

More

Packages that depend on zef_di_core