-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from utopia-dart/feat-refactor
Feat refactor
- Loading branch information
Showing
9 changed files
with
153 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import 'dart:collection'; | ||
|
||
import 'dependency.dart'; | ||
|
||
class Container { | ||
// Store dependencies and instances | ||
final Map<String, Dependency> _dependencies = HashMap(); | ||
final Map<String, dynamic> _instances = HashMap(); | ||
|
||
// Constructor | ||
Container() { | ||
Dependency di = Dependency('di', () => this); | ||
_dependencies[di.name] = di; | ||
} | ||
|
||
// Set a dependency | ||
Container set(Dependency dependency) { | ||
if (_instances.containsKey(dependency.name)) { | ||
_instances.remove(dependency.name); | ||
} | ||
|
||
_dependencies[dependency.name] = dependency; | ||
return this; | ||
} | ||
|
||
// Get a dependency | ||
dynamic get<T>(String name) { | ||
if (!has(name)) { | ||
throw Exception('Failed to find dependency: "$name"'); | ||
} | ||
|
||
final resource = inject(_dependencies[name]!); | ||
if (resource is T) { | ||
return resource; | ||
} | ||
throw Exception('Resource type doesn\'t match'); | ||
} | ||
|
||
// Check if a dependency exists | ||
bool has(String name) { | ||
return _dependencies.containsKey(name); | ||
} | ||
|
||
// Resolve the dependencies of a given injection | ||
dynamic inject(Dependency injection, {bool fresh = false}) { | ||
if (_instances.containsKey(injection.name) && !fresh) { | ||
return _instances[injection.name]; | ||
} | ||
|
||
List<dynamic> arguments = []; | ||
|
||
for (String dependency in injection.dependencies) { | ||
if (_instances.containsKey(dependency)) { | ||
arguments.add(_instances[dependency]); | ||
continue; | ||
} | ||
|
||
if (!_dependencies.containsKey(dependency)) { | ||
throw Exception('Failed to find dependency: "$dependency"'); | ||
} | ||
|
||
arguments.add(get(dependency)); | ||
} | ||
|
||
var resolved = Function.apply(injection.callback, arguments); | ||
_instances[injection.name] = resolved; | ||
return resolved; | ||
} | ||
|
||
// Refresh a dependency | ||
Container refresh(String name) { | ||
if (_instances.containsKey(name)) { | ||
_instances.remove(name); | ||
} | ||
|
||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/// Dependency | ||
/// Define your dependency, name and a callback | ||
class Dependency { | ||
final String name; | ||
final Function callback; | ||
final List<String> dependencies; | ||
|
||
Dependency(this.name, this.callback, {this.dependencies = const []}); | ||
|
||
Dependency inject(String name) { | ||
if (dependencies.contains(name)) { | ||
throw Exception('Dependency already declared for $name'); | ||
} | ||
dependencies.add(name); | ||
return this; | ||
} | ||
} | ||
|
||
/// Another shorter name for Dependency | ||
class D extends Dependency { | ||
D(super.name, super.callback, {super.dependencies}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
name: utopia_di | ||
description: Light & Fast Dart Dependency Injection library. Easy to use and powerful for all kinds of needs, with no external dependencies. | ||
version: 0.2.1 | ||
version: 0.3.0 | ||
repository: https://github.com/utopia-dart/utopia_di | ||
|
||
environment: | ||
sdk: '>=2.19.2 <4.0.0' | ||
|
||
dev_dependencies: | ||
lints: ^3.0.0 | ||
test: ^1.25.2 | ||
lints: ^4.0.0 | ||
test: ^1.25.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters