kiss_dependencies 3.0.1 copy "kiss_dependencies: ^3.0.1" to clipboard
kiss_dependencies: ^3.0.1 copied to clipboard

A lightweight dependency injection package for Dart that follows the KISS (Keep It Simple, Stupid) principle.

Kiss Dependencies #

A lightweight dependency injection package for Dart that follows the KISS (Keep It Simple, Stupid) principle. Kiss Dependencies provides a streamlined API for managing your application's dependencies with zero external dependencies.

Features #

  • Simple registration and resolution of dependencies
  • Support for named instances using identifiers
  • Factory pattern support with caching capabilities
  • Minimal boilerplate code
  • Type-safe dependency resolution
  • Zero external runtime dependencies
  • Lightweight implementation (~95 lines of code)

Installation #

Add Kiss Dependencies to your pubspec.yaml:

dependencies:
  kiss_dependencies: ^3.0.0

Usage #

Dependency Registration #

You can register dependencies either as lazy singletons (default) or immediate singletons:

Lazy Singleton Registration (Default)

Register dependencies that should be instantiated only when first resolved:

// Register a lazy singleton
register<MyService>(
  () => MyService(),
);

// Register a lazy singleton with an identifier
register<MyService>(
  () => MyService(),
  identifier: 'custom',
);

The lazy singleton will only be created when it's first resolved, which can help with startup performance and memory usage.

Immediate Singleton Registration

Register dependencies that should be instantiated immediately:

// Register an immediate singleton
final myService = MyService();
registerImmediate<MyService>(myService);

// Register an immediate singleton with an identifier
registerImmediate<MyService>(myService, identifier: 'immediate');

Deprecated Functions

// DEPRECATED: Use register() instead
registerLazy<MyService>(() => MyService()); // Now deprecated

Resolving Dependencies #

Resolve your dependencies anywhere in your app:

// Resolve a registered service
final myService = resolve<MyService>();

// Resolve a service with an identifier
final customService = resolve<MyService>(identifier: 'custom');

Using Object Factories #

For cases where you need to create objects with specific context:

// Define and register a factory
register<ObjectFactory<MyObject, BuildContext>>(
  () => ObjectFactory((context) => MyObject(context))
);

// Resolve and use the factory
final factory = resolve<ObjectFactory<MyObject, BuildContext>>();
final myObject = factory.build(context);

Cached Factory Pattern #

When you need to cache objects based on context:

// Register a cached factory
register<ObjectFactoryPermanentCached<MyWidget, String>>(
  () => ObjectFactoryPermanentCached((id) => MyWidget(id))
);

// Resolve the factory and build instances
final cachedFactory = resolve<ObjectFactoryPermanentCached<MyWidget, String>>();
final widget1 = cachedFactory.build('unique_id');

// Subsequent calls return the cached instance
final widget2 = cachedFactory.build('unique_id'); // Returns cached instance

// Clear specific cache entries
cachedFactory.remove('unique_id');

// Clear all cached instances
cachedFactory.clear();

Overriding Dependencies #

You can override existing dependencies for testing or configuration changes:

// Override an existing registration (lazy by default)
overrideDependency<MyService>(() => MyService());

// Override with an identifier
overrideDependency<MyService>(() => MyService(), identifier: 'custom');

// Override with an immediate instance
overrideDependencyImmediate<MyService>(
  MyService(),
  identifier: 'immediate',
);

// DEPRECATED: Use overrideDependency() instead
overrideDependencyLazy<MyService>(() => MyService()); // Now deprecated

Unregistering Dependencies #

You can remove dependencies when they're no longer needed:

// Unregister a dependency
unregister<MyService>();

// Unregister a dependency with an identifier
unregister<MyService>(identifier: 'custom');

Error Handling #

The package provides clear error messages for common scenarios:

// Handling unregistered dependencies
try {
  final unregistered = resolve<UnregisteredService>();
} on UnregisteredDependencyException catch (e) {
  print(e.message); // Prints: Error resolving dependency UnregisteredService with identifier null
}

// Handling duplicate registrations
try {
  register<MyService>(myService);
  register<MyService>(anotherService); // Will throw AlreadyRegisteredException
} on AlreadyRegisteredException catch (e) {
  print(e.message); // Prints appropriate error message
  // Use override() instead to replace existing registration
}

Best Practices #

  1. Register dependencies early in your application lifecycle
  2. Use meaningful identifiers when registering multiple instances of the same type
  3. Clear cached factories when they're no longer needed to prevent memory leaks
  4. Keep your dependency tree simple and avoid circular dependencies

Claude Code Agent #

This package includes a specialized Claude Code agent that reviews your codebase for proper kiss_dependencies usage. The agent helps ensure:

  • Correct dependency injection patterns
  • Proper use of lazy vs immediate registration
  • Identification of missed DI opportunities
  • Best practices for testing with mocks and overrides
  • Detection of anti-patterns and code smells

Using the Agent #

If you're using Claude Code, the agent is automatically available when this package is in your dependencies. Simply ask:

@kiss-dependencies-reviewer please review this codebase

Or for specific reviews:

@kiss-dependencies-reviewer review lib/features/auth/ for DI issues
@kiss-dependencies-reviewer check if we're properly using lazy registration

The agent will scan your code and provide detailed recommendations with file references and code examples.

See .claude/README.md for more details.

Contributing #

Contributions are welcome! Please feel free to submit a Pull Request.

License #

This project is licensed under the MIT License - see below for details:

MIT License

Copyright (c) 2025 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2
likes
150
points
555
downloads

Publisher

verified publisherwearemobilefirst.com

Weekly Downloads

A lightweight dependency injection package for Dart that follows the KISS (Keep It Simple, Stupid) principle.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on kiss_dependencies