scope_injector

Last updated:

0 purchases

scope_injector Image
scope_injector Images
Add to Cart

Description:

scope injector

Features #

✅ Scope system
✅ Qualifier system
✅ Module System
✅ Factory system for non singleton dependencies
✅ Dependency inversion principle

Getting started #
Installation #
Add the following to your pubspec.yaml file:
dependencies:
scope_injector: ^1.0.1
copied to clipboard
Usage #
First is necessary to create a class that extends from Module and override the onProvide method.
Inside this method you can use the provide method to register your dependencies.
class RepositoryDiModule extends Module {
RepositoryDiModule(super.scopedState);

@override
void onProvide() {
provide<ProjectRepository>(() => ProjectRepositoryImp(inject()));
provide<UserRepository>(() => UserRepositoryImp(inject(), inject()));

// provide with qualifier
provide<PaymentRepository>(() => PaymentRepositoryImpA(inject()), qualifier: 'payment.a');
provide<PaymentRepository>(() => PaymentRepositoryImpB(inject()), qualifier: 'payment.b');

// provide a non singleton dependency, by default all dependencies are singletons
provide<PaymentService>(() => PaymentServiceImp(inject()), singleton: false);
}
}
copied to clipboard
Then you have to create a stateful widget with a state that extends from ScopedState and override
the getModules method. And inject your dependencies by using the inject method.
class ScopedWidget extends StatefulWidget {
const ExampleWidget({Key? key}) : super(key: key);

@override
State<ScopedWidget> createState() => _ScopedWidgetState();
}

class _ScopedWidgetState extends ScopedState<ScopedWidget> {

// inject your dependencies here
late final ProjectRepository projectRepository = inject();
late final UserRepository userRepository = inject();
late final PaymentRepository paymentRepository = inject(qualifier: 'payment.a');

@override
Widget build(BuildContext context) {
return const Placeholder();
}

// initialize the modules here
@override
List<Module> getModules() => [RepositoryDiModule(this)];

}
copied to clipboard
Finally you can use the inject method to get your dependencies.
class ChildWidget extends StatefulWidget {
const ChildWidget({Key? key}) : super(key: key);

@override
State<ChildWidget> createState() => _ChildWidgetState();
}

class _ChildWidgetState extends State<ChildWidget> {

// inject your dependencies here
late final ProjectRepository projectRepository = inject();

@override
Widget build(BuildContext context) {
return const Placeholder();
}
}
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.