1
1
import 'dart:async' ;
2
2
3
- /// The class to describe a service registration on the get_it locator
4
-
3
+ /// `Environment` is a class that provides preset constants to identify
4
+ /// different types of environments where your app might be running.
5
+ ///
6
+ /// Currently, it provides the following presets:
7
+ /// - `dev` for development environment
8
+ /// - `prod` for production environment
9
+ /// - `test` for testing environment
5
10
class Environment {
6
11
const Environment ._();
7
12
@@ -15,6 +20,21 @@ class Environment {
15
20
static const test = 'test' ;
16
21
}
17
22
23
+ /// `DependencyRegistration` is an abstract class that provides a common structure
24
+ /// for registering dependencies with the `get_it` locator. Different subclasses
25
+ /// provide different registration methods, such as Singleton, LazySingleton, Factory, etc.
26
+ ///
27
+ /// It contains several properties that subclasses can use to configure the registration:
28
+ ///
29
+ /// - `classType` : The concrete class type that should be registered.
30
+ ///
31
+ /// - `asType` : An optional abstract type or interface that `classType` can be registered as.
32
+ ///
33
+ /// - `resolveUsing` : An optional function to generate an instance of `classType` .
34
+ ///
35
+ /// - `dispose` : An optional function that can be called to dispose of the registered instance.
36
+ ///
37
+ /// - `param1` , `param2` : Optional types that can be used by a factory function to create an instance.
18
38
class DependencyRegistration {
19
39
/// The type of the service to register
20
40
final Type ? classType;
@@ -42,7 +62,21 @@ class DependencyRegistration {
42
62
});
43
63
}
44
64
45
- /// Registers the type passed in as a singleton instance in the get_it locator
65
+ /// `Singleton` is a class that extends [DependencyRegistration] . It provides a way
66
+ /// to register an object as a Singleton to the `get_it` locator.
67
+ ///
68
+ /// A Singleton means the object will be instantiated during the first fetch and then will stay
69
+ /// alive in the memory and the same instance will be returned in the subsequent fetches.
70
+ ///
71
+ /// Parameters:
72
+ /// - `classType` : The concrete class to be registered to the `get_it` locator.
73
+ /// - `asType` : An abstract class or interface to map the `classType` to. This is useful when
74
+ /// you want to abstract the concrete implementation and depend on interfaces.
75
+ /// - `resolveUsing` : A callback that resolves the instance. If null, `classType` is instantiated directly.
76
+ /// - `environments` : A set of environment names where this registration should be included.
77
+ /// Useful for conditionally including a service depending on the running environment.
78
+ /// - `instanceName` : An optional instance name that can be used to register multiple objects of the
79
+ /// same type. You will need to fetch the object by instance name from the `get_it` locator.
46
80
class Singleton extends DependencyRegistration {
47
81
const Singleton ({
48
82
Type ? classType,
@@ -59,7 +93,23 @@ class Singleton extends DependencyRegistration {
59
93
);
60
94
}
61
95
62
- /// Registers the type passed in as a LazySingleton instance in the get_it locator
96
+ /// `LazySingleton` is a class that extends [DependencyRegistration] . It provides a way
97
+ /// to register an object as a Lazy Singleton to the `get_it` locator.
98
+ ///
99
+ /// A Lazy Singleton means the object will be instantiated during the first fetch and then will stay
100
+ /// alive in the memory and the same instance will be returned in the subsequent fetches. The key
101
+ /// difference between a [Singleton] and a [LazySingleton] is that a Lazy Singleton is not created
102
+ /// until it is fetched for the first time.
103
+ ///
104
+ /// Parameters:
105
+ /// - `classType` : The concrete class to be registered to the `get_it` locator.
106
+ /// - `asType` : An abstract class or interface to map the `classType` to. This is useful when
107
+ /// you want to abstract the concrete implementation and depend on interfaces.
108
+ /// - `resolveUsing` : A callback that resolves the instance. If null, `classType` is instantiated directly.
109
+ /// - `environments` : A set of environment names where this registration should be included.
110
+ /// Useful for conditionally including a service depending on the running environment.
111
+ /// - `instanceName` : An optional instance name that can be used to register multiple objects of the
112
+ /// same type. You will need to fetch the object by instance name from the `get_it` locator.
63
113
class LazySingleton <T > extends DependencyRegistration {
64
114
const LazySingleton ({
65
115
Type ? classType,
@@ -76,7 +126,19 @@ class LazySingleton<T> extends DependencyRegistration {
76
126
);
77
127
}
78
128
79
- /// Registers the type passed in as a Factory in the get_it locator
129
+ /// `Factory` is a class that extends [DependencyRegistration] . It provides a way
130
+ /// to register an object as a Factory to the `get_it` locator.
131
+ ///
132
+ /// A Factory means that a new instance of the object will be created each time it is fetched.
133
+ ///
134
+ /// Parameters:
135
+ /// - `classType` : The concrete class to be registered to the `get_it` locator.
136
+ /// - `asType` : An abstract class or interface to map the `classType` to. This is useful when
137
+ /// you want to abstract the concrete implementation and depend on interfaces.
138
+ /// - `environments` : A set of environment names where this registration should be included.
139
+ /// Useful for conditionally including a service depending on the running environment.
140
+ /// - `instanceName` : An optional instance name that can be used to register multiple objects of the
141
+ /// same type. You will need to fetch the object by instance name from the `get_it` locator.
80
142
class Factory extends DependencyRegistration {
81
143
const Factory ({
82
144
Type ? classType,
@@ -91,7 +153,21 @@ class Factory extends DependencyRegistration {
91
153
);
92
154
}
93
155
94
- /// Registers the type passed in as a Factory in the get_it locator
156
+ /// `FactoryWithParam` is a class that extends [DependencyRegistration] . It provides a way
157
+ /// to register an object as a Factory with parameter to the `get_it` locator.
158
+ ///
159
+ /// A Factory with parameter means that a new instance of the object will be created each time it
160
+ /// is fetched, and the parameters will be passed to the factory function to create the instance.
161
+ ///
162
+ /// Example:
163
+ /// ```dart
164
+ /// import 'package:stacked_shared/stacked_shared.dart';
165
+ /// class FactoryService {
166
+ /// final String? data1;
167
+ /// final double? data2;
168
+ /// FactoryService(@factoryParam this.data1, {@factoryParam this.data2});
169
+ /// }
170
+ /// ```
95
171
class FactoryWithParam extends DependencyRegistration {
96
172
const FactoryWithParam ({
97
173
Type ? asType,
@@ -117,7 +193,6 @@ class FactoryParam {
117
193
/// with default arguments
118
194
const factoryParam = FactoryParam ._();
119
195
120
- /// Registers the type passed in to be presolved using the function passed in
121
196
@Deprecated ('Use InitializableSingleton instead.' )
122
197
class Presolve extends DependencyRegistration {
123
198
/// The static instance Future function to use for resolving the type registered
@@ -137,7 +212,47 @@ class Presolve extends DependencyRegistration {
137
212
);
138
213
}
139
214
140
- /// Registers the type passed in to be presolved using the function passed in
215
+ /// `InitializableSingleton` is a class that extends [DependencyRegistration] .
216
+ /// It provides a way to register an object as a Singleton to the `get_it` locator.
217
+ /// This means the object will be instantiated during the first fetch and then will stay
218
+ /// alive in the memory and the same instance will be returned in the subsequent fetches.
219
+ ///
220
+ /// When used with a class implementing the `InitializableDependency` interface,
221
+ /// the `init` method of the class will be called upon registration, allowing any
222
+ /// necessary asynchronous initialization logic to be executed before the singleton
223
+ /// instance is fetched for the first time.
224
+ ///
225
+ /// This is useful when you need to perform some setup or initialization tasks (like setting up a
226
+ /// database, making a network request, etc.) before the class can be used.
227
+ ///
228
+ /// Example:
229
+ /// ```dart
230
+ /// class DataCache implements InitializableDependency {
231
+ /// // Implementation details
232
+ ///
233
+ /// @override
234
+ /// Future<void> init() async {
235
+ /// // Initialization logic goes here
236
+ /// }
237
+ /// }
238
+ /// ```
239
+ /// Then register it as:
240
+ /// ```dart
241
+ /// InitializableSingleton(classType: DataCache)
242
+ /// ```
243
+ ///
244
+ /// Parameters:
245
+ /// - `classType` : The concrete class to be registered to the `get_it` locator. This class
246
+ /// should implement `InitializableDependency` interface.
247
+ ///
248
+ /// - `asType` : An abstract class or interface to map the `classType` to. This is useful when
249
+ /// you want to abstract the concrete implementation and depend on interfaces.
250
+ ///
251
+ /// - `environments` : A set of environment names where this registration should be included.
252
+ /// Useful for conditionally including a service depending on the running environment.
253
+ ///
254
+ /// - `instanceName` : An optional instance name that can be used to register multiple objects of the
255
+ /// same type. You will need to fetch the object by instance name from the `get_it` locator.
141
256
class InitializableSingleton extends DependencyRegistration {
142
257
const InitializableSingleton ({
143
258
Type ? classType,
0 commit comments