injector 1.0.3

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 83

Injector

We have been using this injector in all our projects for quite a while now. It does not replace a complex dependency injection framework like Dagger, but it provides the basics that most apps need. Feature requests are welcomed!

Internally the injector is a singleton that stores instances and builders in a Map.

Use registerDependency<Type>() to register the dependency. Never ever try to register or get dependencies without the generic type! Dart allows it, but we don't ;)

All instances are lazy loaded, meaning that at the time you request the dependency the instance is created. Therefore you are registering a builder function that hands you a injector that you HAVE TO USE when getting child dependencies.

Get your dependency using getDependency<Type>().

Usage

A simple usage example:

// Register a dependency (Every time a new instance)
injector.registerDependency<Car>((injector) {
      var engine = injector.getDependency<Engine>();
      var fuel = injector.getDependency<Fuel>();
      var driver = injector.getDependency<Driver>();
      
      return CarImpl(engine,fuel,driver);
    });
    
// Register a singleton
 injector.registerSingleton<Car>((injector) {
          var engine = injector.getDependency<Engine>();
          var fuel = injector.getDependency<Fuel>();
          var driver = injector.getDependency<Driver>();
          
          return CarImpl(engine,fuel,driver);
        });
    
// Register your dependencies / singletons in the  __main.dart__ file

Features and bugs

Please file feature requests and bugs at the issue tracker.

1.0.3

  • Change license from BSD-3 to Apache 2

1.0.2

  • Upgrade analysis_options.yaml
  • Add missing Cast ("as T")
  • Add Test - Cycle dependencies

1.0.1

  • Upgrade pubspec.yaml (Change dart sdk constraint)
  • Update README.md
  • Update Description

1.0.0

  • Create package,
  • Add Injector class
  • Add unit tests for "Injector"
  • Add example - How to deal with the injector / dependency injector

example/injector_example.dart

import 'package:injector/injector.dart';
import 'package:meta/meta.dart';

class Engine {
  String capacity = "";
}

abstract class Car {
  bool drive();

  bool stop();
}

class CarImpl extends Car {
  final Engine engine;

  CarImpl({@required this.engine});

  @override
  bool drive() {
    return true;
  }

  @override
  bool stop() {
    return true;
  }
}

abstract class Database {
  void add(String text);
  void remove(int id);
}

class TikkrDatabase extends Database {
  TikkrDatabase() {
    _initialize(_createTableString());
  }

  void _initialize(String createTableString) {
    //Create the Database
  }

  String _createTableString() {
    return "CREATE TABLE USER";
  }

  @override
  void add(String text) {
    //Do something
  }

  @override
  void remove(int id) {
    //Do something
  }
}

void main() {
  Injector injector = Injector();

  //Register a dependency
  injector.registerDependency<Engine>((_) => Engine());

  injector.registerDependency<Car>((Injector injector) {
    var engine = injector.getDependency<Engine>();
    return CarImpl(engine: engine);
  });

  //Maybe you want to register a class and you need it as a singleton
  injector.registerSingleton<Database>((_) => TikkrDatabase());
}

//Now you can easily get your dependencies / singletons with one line
class WebView {
  Database database;
  Car customerCar;

  WebView() {
    Injector injector = Injector();
    database = injector.getDependency<Database>();
    customerCar = injector.getDependency<Car>();
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  injector: ^1.0.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:injector/injector.dart';
  
Version Uploaded Documentation Archive
1.0.3 Aug 30, 2018 Go to the documentation of injector 1.0.3 Download injector 1.0.3 archive
1.0.2 Aug 27, 2018 Go to the documentation of injector 1.0.2 Download injector 1.0.2 archive
1.0.1 Jul 26, 2018 Go to the documentation of injector 1.0.1 Download injector 1.0.1 archive
0.1.2 Aug 30, 2018 Go to the documentation of injector 0.1.2 Download injector 0.1.2 archive
0.0.1 Jul 5, 2018 Go to the documentation of injector 0.0.1 Download injector 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
65
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
83
Learn more about scoring.

We analyzed this package on Sep 18, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.3

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:injector/injector.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <3.0.0
meta ^1.1.5 1.1.6
Dev dependencies
test 1.3.0