simplest_service_locator
simplest_service_locator
is a lightweight and straightforward service locator for Dart, providing singleton, lazy singleton, and factory registration capabilities.
Features
- Register and retrieve singletons, lazy singletons, and factory instances.
- Simple API for managing dependencies.
- Ensures only one instance of each service is registered.
Installation
Add simplest_service_locator
to your pubspec.yaml
file:
dependencies:
simplest_service_locator: latest
Then, run pub get
to install the package.
Usage
Import the library:
import 'package:simplest_service_locator/simplest_service_locator.dart';
Example
void main() {
final serviceLocator = SimplestServiceLocator.instance();
// Register a singleton
serviceLocator.registerSingleton<MyService>(MyService());
// Register a lazy singleton
serviceLocator.registerLazySingleton<MyLazyService>(() => MyLazyService());
// Register a factory
serviceLocator.registerFactory<MyFactoryService>(() => MyFactoryService());
// Retrieve the singleton instance
final myService = serviceLocator.get<MyService>();
myService.doSomething();
// Retrieve the lazy singleton instance
final myLazyService = serviceLocator.get<MyLazyService>();
myLazyService.doSomethingElse();
// Retrieve a new instance from the factory
final myFactoryService = serviceLocator.get<MyFactoryService>();
myFactoryService.doAnotherThing();
// Clear all registered services
serviceLocator.clear();
}
API
SimplestServiceLocator
Methods
-
factory SimplestServiceLocator.instance()
: Returns the singleton instance ofSimplestServiceLocator
. Creates a new instance if none exists. -
bool isRegistered<T extends Object>()
: Checks if a service of typeT
is registered. -
void registerSingleton<T extends Object>(T instance)
: Registers a singleton instance of typeT
.- Throws
ServiceAlreadyRegisteredException
if a service of typeT
is already registered.
- Throws
-
void registerLazySingleton<T extends Object>(T Function() factory)
: Registers a lazy singleton instance of typeT
, created by the provided factory function. -
void registerFactory<T extends Object>(T Function() factory)
: Registers a factory function for creating instances of typeT
. -
T get<T extends Object>()
: Retrieves the registered service of typeT
. If the service is a factory or a lazy singleton, it invokes the factory to create the instance.- Throws
ServiceNotRegisteredException
if no service of typeT
is registered.
- Throws
-
void clear()
: Clears all registered services.
Exceptions
ServiceAlreadyRegisteredException
Thrown when trying to register a service that is already registered.
ServiceNotRegisteredException
Thrown when trying to retrieve a service that is not registered.
Libraries
- simplest_service_locator
- Simple service locator package developed in Dart. Its purpose is to provide
an API similar to
get_it
, but make sure it is easier to understand, and has less bloat associated with it.