Skip to content

Commit

Permalink
Doc: Reworked Interceptors to behave like normal Beans
Browse files Browse the repository at this point in the history
  • Loading branch information
Willian199 committed Oct 26, 2024
1 parent cb8713e commit f26969d
Show file tree
Hide file tree
Showing 17 changed files with 448 additions and 296 deletions.
15 changes: 7 additions & 8 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ class MyModule with DDIModule, PreDestroy {
registerSession<MyLoggingService>(
() => MyLoggingService(ddi.get(qualifier: 'MyService1')),
qualifier: 'MyLoggingSession',
interceptors: [CustomInterceptor.new],
interceptors: {CustomInterceptor},
);

// Register MyLoggingService with dependency on MyService2
registerDependent<MyLoggingService>(
() => MyLoggingService(ddi.get(qualifier: 'MyService2')),
qualifier: 'MyLoggingDependent',
interceptors: [CustomInterceptor.new],
interceptors: {CustomInterceptor},
);

// For events and streams, use the `ddiEvent` and `ddiStream` respectively
Expand All @@ -80,6 +80,8 @@ class MyModule with DDIModule, PreDestroy {
qualifier: 'EventService',
);

registerApplication<CustomInterceptor>(CustomInterceptor.new);

await Future.delayed(const Duration(seconds: 1));
}

Expand Down Expand Up @@ -136,21 +138,18 @@ void main() async {
myService2.doSomething();

// Get an instance of MyLoggingService with qualifier
late final MyLoggingService myLoggingSession =
ddi.get(qualifier: 'MyLoggingSession');
late final MyLoggingService myLoggingSession = ddi.get(qualifier: 'MyLoggingSession');

// Call a method on the MyLoggingService instance
myLoggingSession.logSomething();

// Get another instance of MyLoggingService with different qualifier
late final MyLoggingService myLoggingDependent =
ddi.get(qualifier: 'MyLoggingDependent');
late final MyLoggingService myLoggingDependent = ddi.get(qualifier: 'MyLoggingDependent');
myLoggingDependent.logSomething();

// Add a decorator to uppercase strings
String uppercaseDecorator(String str) => str.toUpperCase();
ddi.registerObject('Hello World',
qualifier: 'authored', decorators: [uppercaseDecorator]);
ddi.registerObject('Hello World', qualifier: 'authored', decorators: [uppercaseDecorator]);

// Will return HELLO WORLD
print(ddi.get(qualifier: 'authored'));
Expand Down
27 changes: 13 additions & 14 deletions lib/src/core/bean/dart_ddi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:dart_ddi/dart_ddi.dart';
import 'package:dart_ddi/src/core/bean/utils/dart_ddi_utils.dart';
import 'package:dart_ddi/src/core/bean/utils/dispose_utils.dart';
import 'package:dart_ddi/src/core/bean/utils/instance_factory_util.dart';
import 'package:dart_ddi/src/core/bean/utils/interceptor_util.dart';
import 'package:dart_ddi/src/core/bean/utils/scope_utils.dart';
import 'package:dart_ddi/src/enum/scopes.dart';
import 'package:dart_ddi/src/exception/bean_not_found.dart';
Expand Down Expand Up @@ -52,7 +53,7 @@ abstract class DDI {
Object? qualifier,
VoidCallback? postConstruct,
ListDecorator<BeanT>? decorators,
ListDDIInterceptor<BeanT>? interceptors,
Set<Object>? interceptors,
FutureOrBoolCallback? registerIf,
bool destroyable = true,
Set<Object>? children,
Expand Down Expand Up @@ -86,7 +87,7 @@ abstract class DDI {
Object? qualifier,
VoidCallback? postConstruct,
ListDecorator<BeanT>? decorators,
ListDDIInterceptor<BeanT>? interceptors,
Set<Object>? interceptors,
FutureOrBoolCallback? registerIf,
bool destroyable = true,
Set<Object>? children,
Expand All @@ -109,6 +110,11 @@ abstract class DDI {
/// - `qualifier`: Optional qualifier name to distinguish between different instances of the same type.
bool isRegistered<BeanT extends Object>({Object? qualifier});

/// Verify if the factory is a Future in [DDI].
///
/// - `qualifier`: Optional qualifier name to distinguish between different instances of the same type.
bool isFuture<BeanT extends Object>({Object? qualifier});

/// Gets an instance of the registered class in [DDI].
///
/// - `qualifier`: Optional qualifier name to distinguish between different instances of the same type.
Expand All @@ -129,8 +135,7 @@ abstract class DDI {
/// Also the [module] class could be the qualifier from the Module Bean.
///
/// - `qualifier`: Optional qualifier name to distinguish between different instances of the same type.
BeanT getComponent<BeanT extends Object>(
{required Object module, Object? qualifier});
BeanT getComponent<BeanT extends Object>({required Object module, Object? qualifier});

/// Gets an instance of the registered class in [DDI].
///
Expand Down Expand Up @@ -176,9 +181,7 @@ abstract class DDI {
///
/// - **Order of Execution:** Decorators are applied in the order they are provided.
/// - **Instaces Already Gets:** No changes any Instances that have been get.
FutureOr<void> addDecorator<BeanT extends Object>(
ListDecorator<BeanT> decorators,
{Object? qualifier});
FutureOr<void> addDecorator<BeanT extends Object>(ListDecorator<BeanT> decorators, {Object? qualifier});

/// Allows to dynamically add a Interceptor.
///
Expand All @@ -188,9 +191,7 @@ abstract class DDI {
/// - **onCreate:** Won't work with Singletons Scope.
/// - **Order of Execution:** Interceptor are applied in the order they are provided.
/// - **Instaces Already Gets:** No changes any Instances that have been get.
void addInterceptor<BeanT extends Object>(
ListDDIInterceptor<BeanT> interceptors,
{Object? qualifier});
void addInterceptor<BeanT extends Object>(Set<Object>? interceptors, {Object? qualifier});

/// Allows to dynamically refresh the Object.
///
Expand All @@ -204,13 +205,11 @@ abstract class DDI {

/// This function adds multiple child modules to a parent module.
/// It takes a list of 'child' objects and an optional 'qualifier' for the parent module.
void addChildrenModules<BeanT extends Object>(
{required Set<Object> child, Object? qualifier});
void addChildrenModules<BeanT extends Object>({required Set<Object> child, Object? qualifier});

/// This function adds a single child module to a parent module.
/// It takes a 'child' object and an optional 'qualifier' for the parent module.
void addChildModules<BeanT extends Object>(
{required Object child, Object? qualifier});
void addChildModules<BeanT extends Object>({required Object child, Object? qualifier});

Set<Object> getChildren<BeanT extends Object>({Object? qualifier});
}
Loading

0 comments on commit f26969d

Please sign in to comment.