Skip to content

Commit

Permalink
Doc: Fix Singleton behavior with register method (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Willian199 authored Oct 12, 2024
1 parent 9eba9ef commit 67570c8
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 59 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/src/core/bean/dart_ddi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
114 changes: 62 additions & 52 deletions lib/src/core/bean/dart_ddi_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,18 @@ class _DDIImpl implements DDI {
FutureOrBoolCallback? registerIf,
bool destroyable = true,
Set<Object>? 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<BeanT>(clazz, decorators);

postConstruct?.call();

if (clazz is DDIModule) {
clazz.moduleQualifier = effectiveQualifierName;
}

_beans[effectiveQualifierName] = FactoryClazz<BeanT>.singleton(
clazzInstance: clazz,
}) {
return register<BeanT>(
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<PostConstruct>) {
return DartDDIUtils.runFutureOrPostConstruct(clazz);
}
}
),
qualifier: qualifier,
registerIf: registerIf,
);
}

@override
Expand Down Expand Up @@ -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) {
Expand All @@ -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<BeanT>(factoryClazz, effectiveQualifierName);
} else {
_beans[effectiveQualifierName] = factoryClazz;
}
}
}

Future<void> _applySingleton<BeanT extends Object>(
FactoryClazz<BeanT> 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<BeanT>(clazz, factoryClazz.decorators);

factoryClazz.postConstruct?.call();

if (clazz is DDIModule) {
clazz.moduleQualifier = effectiveQualifierName;
}

_beans[effectiveQualifierName] = FactoryClazz<BeanT>.singleton(
clazzInstance: clazz,
interceptors: factoryClazz.interceptors,
destroyable: factoryClazz.destroyable,
children: factoryClazz.children,
);

if (clazz is PostConstruct) {
return clazz.onPostConstruct();
} else if (clazz is Future<PostConstruct>) {
return DartDDIUtils.runFutureOrPostConstruct(clazz);
}
}

Expand Down
6 changes: 5 additions & 1 deletion lib/src/data/factory_clazz.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@ final class FactoryClazz<BeanT extends Object> {
});

factory FactoryClazz.singleton({
required BeanT clazzInstance,
BeanT? clazzInstance,
Function? clazzRegister,
ListDDIInterceptor<BeanT>? interceptors,
bool destroyable = true,
Set<Object>? children,
ListDecorator<BeanT>? decorators,
}) {
return FactoryClazz<BeanT>._(
scopeType: Scopes.singleton,
destroyable: destroyable,
clazzInstance: clazzInstance,
interceptors: interceptors,
children: children,
clazzRegister: clazzRegister,
decorators: decorators,
);
}

Expand Down
10 changes: 10 additions & 0 deletions lib/src/exception/factory_not_allowed.dart
Original file line number Diff line number Diff line change
@@ -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.';
}
}
9 changes: 3 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 67570c8

Please sign in to comment.