![]() ![]() if you try to resolve a type that was not previously registered.if you register the same type under the same name a second time.Ignoring KiwiErrors in development mode #īy default kiwi throws an KiwiError in the following cases: You can remove all the registered types by calling the clear method: container.clear() Unregister the Sith type that was registered under the name DartVader. You can unregister a factory/instance at any time: // Unregisters the Sith type. That's why kiwi comes with a generator □! Unregistering # KiwiContainer container = KiwiContainer() Ĭontainer.registerFactory((c) => ServiceB(c.resolve())) įor services with a lot of dependencies, it can be tedious to write that sort of code. Registers a complex factory by resolving the dependency For registering the service, you can then use the c parameter we saw earlier to resolve the value. If you have a service that depends on another, you have to add the dependency in the constructor. You can also resolve a type like that: Sith theSith = container('DartVader') If it was registered with a superclass under a name, you can get its value like this: Sith theSith = container.resolveAs('DartVader') If it was registered with a superclass, you can get its value like this: Sith theSith = container.resolveAs() If it was registered under a name, you can get its value like this: Sith theSith = container.resolve('DartVader') You can get the instance registered for a type like this: Sith theSith = container.resolve() container.registerSingleton((c) => Sith('Anakin', 'Skywalker')) Singletons are registered like factories but they are called only once: the first time we get their value. Note: the c parameter is the instance of the KiwiContainer, we will see later how it can be useful. If you want to register an factory under a supertype, you have to specify both of them: container.registerFactory((c) => Sith('Anakin', 'Skywalker'), name: 'DartVader') You can also give a name to a specific factory: container.registerFactory((c) => Sith('Anakin', 'Skywalker'), name: 'DartVader') īy default factories are registered under the return type of the factory. Factories container.registerFactory((c) => Sith('Anakin', 'Skywalker')) In the above example Character is a supertype of Sith. The subtype will be detected automatically: container.registerInstance(Sith('Anakin', 'Skywalker'), name: 'DartVader') You have only need to add the super type in the generics. If you want to register an instance under a supertype: You can also give a name to a specific instance: container.registerInstance(Sith('Anakin', 'Skywalker'), name: 'DartVader') īy default instances are registered under their type. Kiwi can register simple instances like that: container.registerInstance(Sith('Anakin', 'Skywalker')) You can register 3 kinds of objects: Instances It works like a lot of IoC containers: you can register a factory under a type, and then resolve the type to get a value. If you want different containers, you can create scoped ones easily: KiwiContainer container = KiwiContainer.scoped() Note: I promise you, even if this is looking like a constructor, you will always end up with the same instance □. The KiwiContainer is implemented as a singleton, you can access the single instance like this: KiwiContainer container = KiwiContainer() This is where all your instances and factories are stored. The core of kiwi is the KiwiContainer class. In your library add the following import: import 'package:kiwi/kiwi.dart' Configuration #Īdd kiwi to pubspec.yaml under the dependencies field. If you are looking for the kiwi_generator configuration, you can find documentation here. This section is only about kiwi which contains the IoC container and the annotations. While code generation allows you to code faster, it comes with extra configuration on you side (to be setup only one time). This package can be used with, or without code generation. IMPORTANT: Dart2 is required to use this package. The container does not rely on reflection, it's just a Map, so it's fast. A simple yet efficient IoC container for Dart and Flutter. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |