Skip to main content

dependency_box 1.0.0 copy "dependency_box: ^1.0.0" to clipboard
dependency_box: ^1.0.0 copied to clipboard

A high-performance, lightweight, and zero-dependency inversion-of-control (IoC) container and dependency injection box for Dart and Flutter

dependency_box #

pub package Star on GitHub GitHub Sponsor License: MIT

A high-performance, lightweight, zero-dependency Inversion-of-Control (IoC) container and service locator for Dart and Flutter applications.

Engineered with a zero-allocation unkeyed lookup path, dependency_box achieves sub-15ns resolution latency while offering hierarchical child scopes, lock-free async initialization deduplication, circular dependency detection, and deterministic lifecycle management.


โœจ Features #

  • ๐ŸŽฏ Zero-Boilerplate Type Inference: Inferred registrations and resolutions without typing <Type> angle brackets.
  • ๐Ÿชถ Zero Runtime Dependencies: Pure Dart code compatible with Flutter, VM, Web, and WebAssembly (WASM).
  • โšก Sub-15ns Resolution: Over 75 Million ops/sec on the hot path via direct Type literal map keys.
  • โณ 6 Registration Lifetimes: Eager singletons, lazy singletons, weak-reference caches, transient factories, parameterized factories, and async singletons.
  • ๐ŸŒณ Hierarchical Child Scopes: Isolated sub-containers (createChild()) that inherit parent registrations.
  • ๐ŸŒ Root-Anchored Shared Singletons: Register singletons from child scopes (addShared*) anchored directly to root.
  • ๐Ÿ”„ Async Concurrency & Cycle Detection: In-flight Future deduplication with automatic reentrancy/circular dependency protection.
  • ๐Ÿงน Automatic Resource Disposal: Clean lifecycle cleanup via DependencyDisposable and inline onDispose hooks.

๐Ÿ“ฆ Getting Started #

Add dependency_box to your pubspec.yaml:

dart pub add dependency_box
# or for Flutter
flutter pub add dependency_box

Import the package:

import 'package:dependency_box/dependency_box.dart';

๐Ÿš€ Quick Start: Zero <Type> Boilerplate #

Unlike traditional service locators where you repeat class names in angle brackets (<Config>, <Client>, <Repository>), dependency_box leverages bidirectional type inference to deduce types automatically from instances, closures, and variable declarations:

import 'package:dependency_box/dependency_box.dart';

class ApiConfig {
  const ApiConfig(this.baseUrl);
  final String baseUrl;
}

class ApiClient {
  ApiClient(this.config);
  final ApiConfig config;
}

class UserRepository {
  UserRepository(this.client);
  final ApiClient client;
  String getUser(String id) => 'User $id from ${client.config.baseUrl}';
}

void main() {
  final dbox = DependencyBox.main();

  // 1. Inferred Registrations: No <Type> brackets needed!
  dbox.addSingleton(const ApiConfig('https://api.example.com'));
  dbox.addLazySingleton((dbox) => ApiClient(dbox.get()));
  dbox.addFactory((dbox) => UserRepository(dbox.get()));

  // 2. Inferred Resolution: Inferred from the variable type
  final UserRepository userRepo = dbox.get();
  print(userRepo.getUser('42')); // User 42 from https://api.example.com
}

๐Ÿ’ก Core Usage & Lifetimes #

1. Registration Lifetimes #

final dbox = DependencyBox.main();

// Eager Singleton (instantiated immediately)
dbox.addSingleton(DatabaseService());

// Lazy Singleton (instantiated once upon first get())
dbox.addLazySingleton((dbox) => AuthService(dbox.get()));

// Weak-Reference Cached Factory (cached while referenced in memory, auto-collected when unreferenced)
dbox.addCachedFactory((dbox) => HeavyImageCache());

// Transient Factory (creates a fresh instance every time get() is called)
dbox.addFactory((dbox) => OrderController(dbox.get()));

// Parameterized Factory (accepts runtime arguments)
dbox.addFactoryWith<QueryEngine, String>((dbox, query) => QueryEngine(query));

// Asynchronous Singleton (deduplicated Future resolution)
dbox.addAsyncSingleton((dbox) async {
  final session = await loadRemoteSession();
  return session;
});

2. Hierarchical Child Scopes & Shared Bindings #

Child scopes inherit dependencies from their parent while keeping local registrations isolated. When the child scope is disposed, its local resources are cleaned up without affecting the parent:

final appBox = DependencyBox.main(name: 'app_root');
appBox.addSingleton(GlobalConfig());

// Create an isolated feature scope
final featureBox = appBox.createChild(name: 'checkout_feature');
featureBox.addSingleton(CheckoutState());

// Register a shared singleton anchored to root from within the child scope
featureBox.addSharedSingleton(SharedAnalyticsSession());

// Dispose scope and clean up scoped dependencies
featureBox.dispose();

3. Lifecycle Cleanup with DependencyDisposable #

Classes implementing DependencyDisposable or registered with custom onDispose hooks are automatically closed when their container is disposed:

class DatabaseClient implements DependencyDisposable {
  @override
  void dispose() {
    print('Database connection closed cleanly.');
  }
}

// Automatically called when dbox.dispose() is executed
dbox.addSingleton(DatabaseClient());

// Or with an inline hook for third-party classes:
dbox.addSingleton(
  StreamController<String>.broadcast(),
  onDispose: (controller) => controller.close(),
);

โšก Performance #

dependency_box is engineered around a zero-allocation unkeyed lookup path using direct Type literal map keys without wrapper objects or reflection layers.

Verified Benchmark Comparison: dependency_box vs. get_it 9.3.0 #

  • Head-to-Head Resolution: dependency_box delivers up to 27.71ร— faster dependency resolution than get_it 9.3.0 (e.g. Singleton: 13 ns vs 362 ns in AOT; 15 ns vs 239 ns in JIT).
  • Container Setup: 2.30ร—โ€“6.35ร— faster initialization and dependency binding setup.
  • Complex Architectures: 13.87ร—โ€“19.74ร— faster across deep dependency graphs and application lifecycles, with weak-reference caching reaching 27.32ร—.
  • Verified Suite Advantage: 5.57ร— geometric-mean speedup in JIT and 6.67ร— in AOT across comprehensive multi-workload benchmarking.

1. JIT Resolution Workloads

[JIT Resolution Workloads]

2. JIT Registration & Scaling

[JIT Registration & Scaling]

3. AOT Resolution Workloads

[AOT Resolution Workloads]

4. AOT Registration & Scaling

[AOT Registration & Scaling]

Tip

A consolidated 4-in-1 chart is also available at doc/benchmark_comparison.svg, along with suite overviews for JIT and AOT.

๐Ÿ”ฌ Reproducing the Benchmarks #

All benchmark suites and measurement harnesses are 100% open source and reproducible:

  • Comparative Benchmark Suite: tool/comparison_benchmark/

    # Run head-to-head microbenchmarks (dependency_box vs get_it 9.3.0)
    dart run tool/comparison_benchmark/bin/compare_benchmark.dart
    
    # Run real-world production architecture simulation suite
    dart run tool/comparison_benchmark/bin/real_world_benchmark.dart
    
  • Standalone Package Microbenchmarks: tool/benchmark/

    dart run tool/benchmark/benchmark.dart
    

Note


Supporting & Contributing #

If you find dependency_box helpful, here are several ways you can support the project:

๐ŸŒŸ Community Support #

  • Star the Repository: Star ahmedm-gh/dart-packages on GitHub to help others discover it.
  • Report Issues & Ideas: Found a bug or need an improvement? Open an issue on GitHub Issues.
  • Contribute: Pull requests for optimizations, documentation, or examples are always welcome.
  • Spread the Word: Recommend dependency_box in your Flutter/Dart teams, blogs, or communities.

โ˜• Sponsorship & Backing #

  • Sponsor on GitHub: If this package brings value to your commercial apps, consider supporting its maintenance via GitHub Sponsors.
  • Buy Me a Coffee: Support development with a one-time coffee or recurring donation via Buy Me a Coffee.

Author & Maintainer #

Created and maintained by AHMED M.

License #

This package is licensed under the MIT License.

5
likes
0
points
1.07k
downloads

Documentation

Documentation

Publisher

unverified uploader

Weekly Downloads

A high-performance, lightweight, and zero-dependency inversion-of-control (IoC) container and dependency injection box for Dart and Flutter

Repository (GitHub)
View/report issues

Topics

#dependency-injection #service-locator #ioc #container #utilities

Funding

Consider supporting this project:

github.com
buymeacoffee.com

License

unknown (license)

More

Packages that depend on dependency_box