Git Product home page Git Product logo

ioc-flutter's Introduction

IoC

The most simple IoC Container of Dart and Flutter

If you looking for a package that is light-weight and provide production-ready of inversion of control, then this package is right for you.

This package provides only 2 main api, easy to learn and use, but definitely fits most use case in your flutter project.

If you are a server developer developing small or medium scale project, it's very likely you want to use this package. However, large scale project may need more powerful heavy-weight IoC library.

You can use it in your angular project, but we highly recommend you use dependency injection system provided by angular.

Goal

Keep it minimal, light-weight

Important

  1. The container does not rely on reflection, it's just a Map, so supports Flutter
  2. Dart2 is required to use this package

Feature

  1. Supports singleton
  2. Supports binding to anything
  3. Supports lazy initialization (Developing)
  4. Detects circular dependencies (Developing)

Usage

bind to a string:

import 'package:ioc/ioc.dart';

main() {
  Ioc().bind('MyClass', (ioc) => new MyClass());
  
  // later
  Ioc().use('MyClass'); // you will get an instance of MyClass
  // with generic if you want
  Ioc().use<MyClass>('MyClass');
}

bind to self:

import 'package:ioc/ioc.dart';

main() {
  Ioc().bind(MyClass, (ioc) => new MyClass());
  
  // later
  Ioc().use(MyClass); // you will get an instance of MyClass
}

bind to other:

import 'package:ioc/ioc.dart';

main() {
  Ioc().bind(MyClass, (ioc) => new OtherClass());
  
  // later
  Ioc().use(MyClass); // you will get an instance of OtherClass
}

bind with other dependency

import 'package:ioc/ioc.dart';

main() {
  Ioc().bind('MyClass', (Ioc ioc) {
    OtherClass other = ioc.use<OtherClass>('OtherClass');
    
    return new MyClass(other);
  });
  
  // later
  Ioc().use<MyClass>('MyClass'); // you will get an instance of OtherClass
}

using singleton:

import 'package:ioc/ioc.dart';

class A {
  void someMethod() {}
}

main() {
  // use singleton on one
  Ioc().bind('A', (ioc) => new A(), singleton: true);

  Ioc().use<A>('A').someMethod();

  // use singleton on all
  Ioc().config['singlton'] = true;

  Ioc().use<A>('A').someMethod();
}

using lazy-loading singleton:

import 'package:ioc/ioc.dart';

class A {
  void someMethod() {}
}

main() {
  // use lazy loaded singleton on one
  Ioc().bind('A', (ioc) => new A(), singleton: true, lazy: true);

  // class A will only be instantiated after first .use('A')
  Ioc().use<A>('A').someMethod();

  // use lazy loaded singleton on all
  Ioc().config['lazy'] = true;

  Ioc().use<A>('A').someMethod();
}

ioc-flutter's People

Contributors

copydog avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.