Skip to content

Commit 294a50c

Browse files
authored
chore: add documentation to all the locator annotation and InitializableDependency (#7)
add documentation to all the locator annotation and inializable_dependency
1 parent 8caeb23 commit 294a50c

File tree

2 files changed

+136
-8
lines changed

2 files changed

+136
-8
lines changed

lib/src/code_generation/locator/initializable_dependency.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/// `InitializableDependency` is an abstract class intended to be implemented by
2+
/// any class which requires asynchronous initialization before being used.
3+
///
4+
/// This is particularly useful when using `InitializableSingleton`. A class implementing
5+
/// `InitializableDependency` signifies that it has asynchronous setup work to be done
6+
/// before it can be used effectively. The setup is done in the `init` method.
7+
///
8+
/// This is particularly useful for classes that depend on resources such as
9+
/// File I/O, Network calls, Database access, etc. which cannot be performed
10+
/// synchronously during object construction.
11+
///
12+
/// This abstract class contains a single method, `init()`, which is expected
13+
/// to contain all asynchronous initialization logic for the implementing class.
114
abstract class InitializableDependency {
215
/// Handles Asynchronously initialization.
316
Future<void> init();

lib/src/code_generation/locator/stacked_locator_annotations.dart

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import 'dart:async';
22

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
510
class Environment {
611
const Environment._();
712

@@ -15,6 +20,21 @@ class Environment {
1520
static const test = 'test';
1621
}
1722

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.
1838
class DependencyRegistration {
1939
/// The type of the service to register
2040
final Type? classType;
@@ -42,7 +62,21 @@ class DependencyRegistration {
4262
});
4363
}
4464

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.
4680
class Singleton extends DependencyRegistration {
4781
const Singleton({
4882
Type? classType,
@@ -59,7 +93,23 @@ class Singleton extends DependencyRegistration {
5993
);
6094
}
6195

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.
63113
class LazySingleton<T> extends DependencyRegistration {
64114
const LazySingleton({
65115
Type? classType,
@@ -76,7 +126,19 @@ class LazySingleton<T> extends DependencyRegistration {
76126
);
77127
}
78128

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.
80142
class Factory extends DependencyRegistration {
81143
const Factory({
82144
Type? classType,
@@ -91,7 +153,21 @@ class Factory extends DependencyRegistration {
91153
);
92154
}
93155

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+
/// ```
95171
class FactoryWithParam extends DependencyRegistration {
96172
const FactoryWithParam({
97173
Type? asType,
@@ -117,7 +193,6 @@ class FactoryParam {
117193
/// with default arguments
118194
const factoryParam = FactoryParam._();
119195

120-
/// Registers the type passed in to be presolved using the function passed in
121196
@Deprecated('Use InitializableSingleton instead.')
122197
class Presolve extends DependencyRegistration {
123198
/// The static instance Future function to use for resolving the type registered
@@ -137,7 +212,47 @@ class Presolve extends DependencyRegistration {
137212
);
138213
}
139214

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.
141256
class InitializableSingleton extends DependencyRegistration {
142257
const InitializableSingleton({
143258
Type? classType,

0 commit comments

Comments
 (0)