From 67570c835f7c139fa968dfc93189c885a69a45c4 Mon Sep 17 00:00:00 2001 From: Willian Marchesan <40872392+Willian199@users.noreply.github.com> Date: Sat, 12 Oct 2024 19:07:27 -0300 Subject: [PATCH] Doc: Fix Singleton behavior with register method (#18) --- CHANGELOG.md | 4 + lib/src/core/bean/dart_ddi.dart | 1 + lib/src/core/bean/dart_ddi_impl.dart | 114 +++++++++++---------- lib/src/data/factory_clazz.dart | 6 +- lib/src/exception/factory_not_allowed.dart | 10 ++ pubspec.yaml | 9 +- 6 files changed, 85 insertions(+), 59 deletions(-) create mode 100644 lib/src/exception/factory_not_allowed.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e673da..5ab6b01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.7.2 + +* Fix Singleton and Object behavior when registering with the `register` method. + ## 0.7.1 * Added support for registering a custom factory class with the `register` method. Note: Factories with parameters are not yet supported. diff --git a/lib/src/core/bean/dart_ddi.dart b/lib/src/core/bean/dart_ddi.dart index c88c2bc..e7f80fe 100644 --- a/lib/src/core/bean/dart_ddi.dart +++ b/lib/src/core/bean/dart_ddi.dart @@ -7,6 +7,7 @@ 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'; import 'package:dart_ddi/src/exception/duplicated_bean.dart'; +import 'package:dart_ddi/src/exception/factory_not_allowed.dart'; import 'package:dart_ddi/src/exception/future_not_accept.dart'; import 'package:dart_ddi/src/exception/module_not_found.dart'; import 'package:dart_ddi/src/typedef/typedef.dart'; diff --git a/lib/src/core/bean/dart_ddi_impl.dart b/lib/src/core/bean/dart_ddi_impl.dart index 7894567..5769cd0 100644 --- a/lib/src/core/bean/dart_ddi_impl.dart +++ b/lib/src/core/bean/dart_ddi_impl.dart @@ -13,59 +13,18 @@ class _DDIImpl implements DDI { FutureOrBoolCallback? registerIf, bool destroyable = true, Set? children, - }) async { - bool shouldRegister = true; - - if (registerIf != null) { - if (registerIf is BoolCallback) { - shouldRegister = registerIf(); - } else { - shouldRegister = await registerIf(); - } - } - - if (shouldRegister) { - final Object effectiveQualifierName = qualifier ?? BeanT; - - if (_beans[effectiveQualifierName] != null) { - throw DuplicatedBeanException(effectiveQualifierName.toString()); - } - - late BeanT clazz; - - if (clazzRegister is BeanT Function()) { - clazz = clazzRegister(); - } else { - clazz = await clazzRegister(); - } - - if (interceptors != null) { - for (final interceptor in interceptors) { - clazz = interceptor().aroundConstruct(clazz); - } - } - - clazz = DartDDIUtils.executarDecorators(clazz, decorators); - - postConstruct?.call(); - - if (clazz is DDIModule) { - clazz.moduleQualifier = effectiveQualifierName; - } - - _beans[effectiveQualifierName] = FactoryClazz.singleton( - clazzInstance: clazz, + }) { + return register( + factoryClazz: FactoryClazz.singleton( + clazzRegister: clazzRegister, + children: children, interceptors: interceptors, + decorators: decorators, destroyable: destroyable, - children: children, - ); - - if (clazz is PostConstruct) { - return clazz.onPostConstruct(); - } else if (clazz is Future) { - return DartDDIUtils.runFutureOrPostConstruct(clazz); - } - } + ), + qualifier: qualifier, + registerIf: registerIf, + ); } @override @@ -146,6 +105,10 @@ class _DDIImpl implements DDI { Object? qualifier, FutureOrBoolCallback? registerIf, }) async { + if (factoryClazz.scopeType == Scopes.object) { + throw FactoryNotAllowedException(BeanT.toString()); + } + bool shouldRegister = true; if (registerIf != null) { @@ -163,7 +126,54 @@ class _DDIImpl implements DDI { throw DuplicatedBeanException(effectiveQualifierName.toString()); } - _beans[effectiveQualifierName] = factoryClazz; + if (factoryClazz.scopeType == Scopes.singleton) { + if (factoryClazz.clazzRegister == null || + factoryClazz.clazzInstance != null) { + throw FactoryNotAllowedException(effectiveQualifierName.toString()); + } + return _applySingleton(factoryClazz, effectiveQualifierName); + } else { + _beans[effectiveQualifierName] = factoryClazz; + } + } + } + + Future _applySingleton( + FactoryClazz factoryClazz, Object effectiveQualifierName) async { + late BeanT clazz; + + if (factoryClazz.clazzRegister is BeanT Function()) { + clazz = factoryClazz.clazzRegister!() as BeanT; + } else { + clazz = (await factoryClazz.clazzRegister!()) as BeanT; + } + + if (factoryClazz.interceptors != null) { + for (final interceptor in factoryClazz.interceptors!) { + clazz = interceptor().aroundConstruct(clazz); + } + } + + clazz = + DartDDIUtils.executarDecorators(clazz, factoryClazz.decorators); + + factoryClazz.postConstruct?.call(); + + if (clazz is DDIModule) { + clazz.moduleQualifier = effectiveQualifierName; + } + + _beans[effectiveQualifierName] = FactoryClazz.singleton( + clazzInstance: clazz, + interceptors: factoryClazz.interceptors, + destroyable: factoryClazz.destroyable, + children: factoryClazz.children, + ); + + if (clazz is PostConstruct) { + return clazz.onPostConstruct(); + } else if (clazz is Future) { + return DartDDIUtils.runFutureOrPostConstruct(clazz); } } diff --git a/lib/src/data/factory_clazz.dart b/lib/src/data/factory_clazz.dart index 8933b76..d10d6b2 100644 --- a/lib/src/data/factory_clazz.dart +++ b/lib/src/data/factory_clazz.dart @@ -47,10 +47,12 @@ final class FactoryClazz { }); factory FactoryClazz.singleton({ - required BeanT clazzInstance, + BeanT? clazzInstance, + Function? clazzRegister, ListDDIInterceptor? interceptors, bool destroyable = true, Set? children, + ListDecorator? decorators, }) { return FactoryClazz._( scopeType: Scopes.singleton, @@ -58,6 +60,8 @@ final class FactoryClazz { clazzInstance: clazzInstance, interceptors: interceptors, children: children, + clazzRegister: clazzRegister, + decorators: decorators, ); } diff --git a/lib/src/exception/factory_not_allowed.dart b/lib/src/exception/factory_not_allowed.dart new file mode 100644 index 0000000..6fad7f0 --- /dev/null +++ b/lib/src/exception/factory_not_allowed.dart @@ -0,0 +1,10 @@ +/// [FactoryNotAllowedException] is an exception that is thrown when the facotry isn't valid and must be correct. +class FactoryNotAllowedException implements Exception { + const FactoryNotAllowedException(this.cause); + final String cause; + + @override + String toString() { + return 'The Factory is not valid for Type $cause.'; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 79fc46f..a0ab403 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,21 +1,18 @@ name: dart_ddi description: "A Dependency Injection package, with Qualifier, Decorators, Interceptors, Events and more. Inspired by Java CDI and get_it." -version: 0.7.1 +version: 0.7.2 homepage: https://github.com/Willian199/dart_ddi issue_tracker: https://github.com/Willian199/dart_ddi/issues tags: - dependency - package - - version - - injection + - inject - management - - java topics: - dependency-injection - di - - injection - - java + - inject - cdi environment: