All Projects → jonsamwell → flutter_simple_dependency_injection

jonsamwell / flutter_simple_dependency_injection

Licence: MIT License
A super simple dependency injection implementation for flutter that behaviours like any normal IOC container and does not rely on mirrors

Programming Languages

dart
5743 projects
Batchfile
5799 projects

Projects that are alternatives of or similar to flutter simple dependency injection

TVToday
iOS TV Shows app with TMDb Api. RxSwift, MVVM, Clean Architecture. Tuist + Swift Package Manager
Stars: ✭ 27 (-70.33%)
Mutual labels:  dependency-injection
pythondi
Python lightweight dependency injection library
Stars: ✭ 26 (-71.43%)
Mutual labels:  dependency-injection
EvaEngine.js
A micro service development engine for node.js
Stars: ✭ 31 (-65.93%)
Mutual labels:  dependency-injection
framework
The target of this framework is to help in all the needs of a full featured API REST, from top to bottom, and at the same time having the best possible performance and security.
Stars: ✭ 88 (-3.3%)
Mutual labels:  dependency-injection
ControllerAutowire
[DEPRECATED] Use Controller autowiring by default since Symfony 3.3
Stars: ✭ 24 (-73.63%)
Mutual labels:  dependency-injection
opyoid
Dependency injection library for Python
Stars: ✭ 34 (-62.64%)
Mutual labels:  dependency-injection
x5 webview flutter
一个基于腾讯x5引擎的webview flutter插件,简化集成,一行代码打开视频播放,暂时只支持android使用
Stars: ✭ 90 (-1.1%)
Mutual labels:  flutter-plugin
http middleware
A middleware library for Dart's http library.
Stars: ✭ 38 (-58.24%)
Mutual labels:  flutter-plugin
flutter paystack
💳 A robust Flutter plugin for making payments via Paystack Payment Gateway. Completely supports Android and iOS
Stars: ✭ 146 (+60.44%)
Mutual labels:  flutter-plugin
flutter tensorflow lite
A Flutter plugin to access TensorFlow Lite apis.
Stars: ✭ 75 (-17.58%)
Mutual labels:  flutter-plugin
A-Complete-Guide-To-Flutter
This repo contains all the small snippets related to Flutter Apps. Most of the projects/apps are deployed on Flutter Web using GitHub Actions CI Pipeline.
Stars: ✭ 70 (-23.08%)
Mutual labels:  flutter-plugin
cross connectivity
A Flutter plugin for handling Connectivity and REAL Connection state in the mobile, web and desktop platforms. Supports iOS, Android, Web, Windows, Linux and macOS.
Stars: ✭ 27 (-70.33%)
Mutual labels:  flutter-plugin
brisk-ioc
fast light brisk ioc/di container on nodejs; Node下快速 轻量的IoC/DI容器,依赖注入,配合装饰器使用
Stars: ✭ 12 (-86.81%)
Mutual labels:  dependency-injection
ToDoApp
📱My android playground app - Simple and Fastest todo app - developing to cover most android concepts, simple logic can make me focus more on framework
Stars: ✭ 28 (-69.23%)
Mutual labels:  dependency-injection
Medicine
Code-driven component injection toolkit for Unity.
Stars: ✭ 28 (-69.23%)
Mutual labels:  dependency-injection
simple-container
Yet another DI container
Stars: ✭ 12 (-86.81%)
Mutual labels:  dependency-injection
SocketHook
Socket hook is an injector based on EasyHook (win only) which redirect the traffic to your local server.
Stars: ✭ 38 (-58.24%)
Mutual labels:  dependency-injection
Resolver
🎊 A simple resolver in Swift
Stars: ✭ 15 (-83.52%)
Mutual labels:  dependency-injection
flutter qq ads
🔥🔥🔥 Flutter 广告插件 -- 优量汇 、广点通、腾讯广告(支持开屏、插屏、激励视频、Banner、信息流、视频贴片)
Stars: ✭ 51 (-43.96%)
Mutual labels:  flutter-plugin
iap
Flutter plugin for interacting with iOS StoreKit and Android Billing Library
Stars: ✭ 18 (-80.22%)
Mutual labels:  flutter-plugin

flutter_simple_dependency_injection

A simple dependency injection plugin for Flutter and Dart.

This implementation does not rely on the dart reflection apis (mirrors) and favours a simple factory based approach. This increases the performance and simplicity of this implementation.

  • Support for multiple injectors (useful for unit testing or code running in isolates)
  • Support for types and named types
  • Support for singletons
  • Support simple values (useful for configuration parameters like api keys or urls)

Any help is appreciated! Comment, suggestions, issues, PR's!

Getting Started

In your flutter or dart project add the dependency:

dependencies:
  ...
  flutter_simple_dependency_injection: any

For help getting started with Flutter, view the online documentation.

Usage example

Import flutter_simple_dependency_injection

import 'package:flutter_simple_dependency_injection/injector.dart';

Injector Configuration

As this injector relies on factories rather than reflection (as mirrors in not available in Flutter) each mapped type needs to provide a factory function. In most cases this can just be a simple new object returned function. In slightly more advanced scenarios where the type to be created relies on other types an injector instances is passed into the factory function to allow the type of be created to get other types it depends on (see below for examples).

import 'package:flutter_simple_dependency_injection/injector.dart';

void main() {
  // it is best to place all injector initialisation work into one or more modules
  // so it can act more like a dependency injector than a service locator.
  // The Injector implements a singleton pattern. You can get a singleton injector instance
  // just by calling Injector().
  final injector = ModuleContainer().initialise(Injector());

  // NOTE: it is best to architect your code so that you never need to
  // interact with the injector itself.  Make this framework act like a dependency injector
  // by having dependencies injected into objects in their constructors.  That way you avoid
  // any tight coupling with the injector itself.

  // Basic usage, however this kind of tight couple and direct interaction with the injector
  // should be limited.  Instead prefer dependencies injection.

  // simple dependency retrieval and method call
  injector.get<SomeService>().doSomething();

  // get an instance of each of the same mapped types
  final instances = injector.getAll<SomeType>();
  print(instances.length); // prints '3'

  // passing in additional arguments when getting an instance
  final instance =
      injector.get<SomeOtherType>(additionalParameters: {"id": "some-id"});
  print(instance.id); // prints 'some-id'
}

class ModuleContainer {
  Injector initialise(Injector injector) {
    injector.map<Logger>((i) => Logger(), isSingleton: true);
    injector.map<String>((i) => "https://api.com/", key: "apiUrl");
    injector.map<SomeService>(
        (i) => SomeService(i.get<Logger>(), i.get<String>(key: "apiUrl")));

    // note that you can configure mapping in a fluent programming style too
    injector.map<SomeType>((injector) => SomeType("0"))
            ..map<SomeType>((injector) => SomeType("1"), key: "One")
            ..map<SomeType>((injector) => SomeType("2"), key: "Two");

    injector.mapWithParams<SomeOtherType>((i, p) => SomeOtherType(p["id"]));

    return injector;
  }
}

class Logger {
  void log(String message) => print(message);
}

class SomeService {
  final Logger _logger;
  final String _apiUrl;

  SomeService(this._logger, this._apiUrl);

  void doSomething() {
    _logger.log("Doing something with the api at '$_apiUrl'");
  }
}

class SomeType {
  final String id;
  SomeType(this.id);
}

class SomeOtherType {
  final String id;
  SomeOtherType(this.id);
}

Remove mappings

You can remove a mapped a factory/instance at any time:

  injector.removeMapping<SomeType>();

  injector.removeMapping<SomeType>(key: 'some_key');

  injector.removeAllMappings<SomeType>();

Multiple Injectors

The Injector class has a factory constructor that by default returns the default singleton instance of the injector. In most cases this will be enough. However, you can pass a name into this factory constructor to return another isolated injector that is independent from the default injector. Passing in a new injector name will create the injector if it has not be retrieved before.

  final defaultInjector = Injector();
  final isolatedInjector = Injector("Isolated");

To destroy an injector instance call its dispose method. The particular instance of the injector with all mappings will be removed.

  Injector().dispose();
  Injector("Isolated").dispose();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].