Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
sidinaz committed Nov 30, 2019
0 parents commit 5f0d512
Show file tree
Hide file tree
Showing 16 changed files with 668 additions and 0 deletions.
74 changes: 74 additions & 0 deletions .gitignore
@@ -0,0 +1,74 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.packages
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
10 changes: 10 additions & 0 deletions .metadata
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 856a90e67c9284124d44d2be6c785bacd3a1c772
channel: beta

project_type: package
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,3 @@
## [0.0.1] - TODO: Add release date.

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions LICENSE
@@ -0,0 +1 @@
TODO: Add your license here.
14 changes: 14 additions & 0 deletions README.md
@@ -0,0 +1,14 @@
# daggerito

A new Flutter package project.

## Getting Started

This project is a starting point for a Dart
[package](https://flutter.dev/developing-packages/),
a library module containing code that can be shared easily across
multiple Flutter or Dart projects.

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
4 changes: 4 additions & 0 deletions lib/daggerito.dart
@@ -0,0 +1,4 @@
library daggerito;

export 'package:daggerito/src/di/component.dart';
export 'package:daggerito/src/dependency_container.dart';
35 changes: 35 additions & 0 deletions lib/src/definition.dart
@@ -0,0 +1,35 @@
import 'dependency_container.dart';

/// Signature for a builder which creates an object of type [T].
typedef T Factory<T>(DependencyContainer container);

class Definition<T> {
Definition.instance(this.object)
: instanceBuilder = null,
_oneTime = false;

Definition.factory(this.instanceBuilder) : _oneTime = false;

Definition.singleton(this.instanceBuilder) : _oneTime = true;

final Factory<T> instanceBuilder;
T object;
bool _oneTime = false;

T get(DependencyContainer container) {
if (_oneTime && instanceBuilder != null) {
object = instanceBuilder(container);
_oneTime = false;
}

if (object != null) {
return object;
}

if (instanceBuilder != null) {
return instanceBuilder(container);
}

return null;
}
}
53 changes: 53 additions & 0 deletions lib/src/dependency_container.dart
@@ -0,0 +1,53 @@
import 'definition.dart';



/// A simple service container.
class DependencyContainer {
/// Creates a scoped container.
DependencyContainer()
: namedProviders = Map<String, Map<Type, Definition<Object>>>();

final Map<String, Map<Type, Definition<Object>>> namedProviders;

/// Whether ignoring assertion errors in the following cases:
/// * if you register the same type under the same name a second time.
/// * if you try to resolve or unregister a type that was not
/// previously registered.
///
/// Defaults to true.
bool silent = true;

/// Colaborators
List<DependencyContainer> collaborators = [];
/// Removes all instances and builders from the container.
///
/// After this, the container is empty.
void clear() {
namedProviders.clear();
}

void setProvider<T>(String name, Definition<T> provider) {
assert(
silent ||
(!namedProviders.containsKey(name) ||
!namedProviders[name].containsKey(T)),
assertRegisterMessage<T>('already', name),);

namedProviders.putIfAbsent(name, () => Map<Type, Definition<Object>>())[T] =
provider;
}

String assertRegisterMessage<T>(String word, String name) {
return 'The type $T was $word registered${name == null ? '' : ' for the name $name'}';
}
}

//

extension DependencyContainerExtensions on DependencyContainer{
void collaborate(List<DependencyContainer> containers){
this.collaborators = [...this.collaborators, ...containers];
}
}
27 changes: 27 additions & 0 deletions lib/src/di/component.dart
@@ -0,0 +1,27 @@
import 'package:daggerito/src/dependency_container.dart';


abstract class Component {
DependencyContainer container();
}

abstract class Module {
void register(DependencyContainer container);
}

abstract class ChildComponent implements Component {
final DependencyContainer _container;

ChildComponent(
Component parentComponent, {
Module module,
}) : _container = DependencyContainer()
..collaborate(
[parentComponent.container()],
) {
if (module != null) module.register(_container);
}

@override
DependencyContainer container() => _container;
}
60 changes: 60 additions & 0 deletions lib/src/register.dart
@@ -0,0 +1,60 @@
import 'dependency_container.dart';
import 'definition.dart';

extension DependencyContainerExtensions on DependencyContainer{

/// Registers an instance into the container.
///
/// An instance of type [T] can be registered with a
/// supertype [S] if specified.
///
/// If [tag] is set, the instance will be registered under this name.
/// To retrieve the same instance, the same name should be provided
/// to [DependencyContainer.resolve].
void registerInstance<S, T extends S>(
S instance, {
String tag,
}) {
setProvider(tag, Definition<S>.instance(instance));
}

/// Registers a factory into the container.
///
/// A factory returning an object of type [T] can be registered with a
/// supertype [S] if specified.
///
/// If [tag] is set, the factory will be registered under this name.
/// To retrieve the same factory, the same name should be provided
/// to [DependencyContainer.resolve].
void register<S, T extends S>(
Factory<S> factory, {
String tag,
}) {
setProvider(tag, Definition<S>.factory(factory));
}

/// Registers a factory that will be called only only when
/// accessing it for the first time, into the container.
///
/// A factory returning an object of type [T] can be registered with a
/// supertype [S] if specified.
///
/// If [tag] is set, the factory will be registered under this name.
/// To retrieve the same factory, the same name should be provided
/// to [DependencyContainer.resolve].
void registerSingleton<S, T extends S>(
Factory<S> factory, {
String tag,
}) {
setProvider(tag, Definition<S>.singleton(factory));
}

/// Removes the entry previously registered for the type [T].
///
/// If [tag] is set, removes the one registered for that name.
void unregister<T>([String tag]) {
assert(silent || (namedProviders[tag]?.containsKey(T) ?? false),
assertRegisterMessage<T>('not', tag));
namedProviders[tag]?.remove(T);
}
}
37 changes: 37 additions & 0 deletions lib/src/resolve.dart
@@ -0,0 +1,37 @@
import 'dependency_container.dart';
import 'definition.dart';

extension DependencyContainerExtensions on DependencyContainer{
/// Attemps to resolve the type [T].
///
/// If [tag] is set, the instance or builder registered with this
/// name will be get.
///
/// See also:
///
/// * [DependencyContainer.registerFactory] for register a builder function.
/// * [DependencyContainer.registerInstance] for register an instance.
T resolve<T>([String tag]) => _resolve([this,...this.collaborators],tag);

T _resolve<T>(List<DependencyContainer> containers ,[String tag]) {
if(containers.length == 0)
return null;

T value;

containers.forEach((dc){
Map<Type, Definition<Object>> providers = dc.namedProviders[tag];

if(providers != null && value == null){
value = providers[T]?.get(this);
}
if(value == null){
value = _resolve(dc.collaborators, tag);
}
});
return value;

}

T call<T>([String tag]) => resolve<T>(tag);
}

0 comments on commit 5f0d512

Please sign in to comment.