simply_injector 0.1.1

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

Simply Injector

Simply Injector is a dart package inspired and based on Simple Injector library.

It is an easy-to-use dependency injection (DI) library and is easily integrated with Flutter.

It’s easy to implement the dependency injection pattern with loosely coupled components using Simply Injector Dart Package.

Configuration

Add simply_injector to pubspec.yaml under the dependencies field.

dependencies:
  simply_injector: ^latest_version

Import

Add the following import in your library :

import 'package:simply_injector/simply_injector.dart';

Usage

Simply Injector's main type is the Container. An instance of Container is used to register mappings between each abstraction (service) and its corresponding implementation (component).

Tip: You should typically create a single Container instance for the whole application (one instance per app domain).

It's possible create an instance of Container by some ways:

var container = new SimplyInjector.Container();

or

var container = new Container();

or using a library prefix to avoid conflict with Flutter's *Container' class:

import 'package:simply_injector/simply_injector.dart' as si;
...
si.Container container = new si.Container();

The first way it's so much elegantly ;)

Example

This is the main source of complete example project at Simply Injector GitHub repository:

import 'package:simply_injector/simply_injector.dart';

import 'CancelOrder.dart';
import 'CancelOrderHandler.dart';
import 'EventPublisher.dart';
import 'FileLogger.dart';
import 'Guid.dart';
import 'IEventPublisher.dart';
import 'ILogger.dart';
import 'IOrderRepository.dart';
import 'SqlOrderRepository.dart';

main()
{
  // 1. Create a new Simple Injector container
  var container = new SimplyInjector.Container();

  // 2. Configure the container (register)
  container.register<IOrderRepository, SqlOrderRepository>( () => new SqlOrderRepository(container.get<ILogger>()));
  container.register<ILogger, FileLogger>( () => new FileLogger(), Lifestyle.Singleton );
  container.register<IEventPublisher, EventPublisher>( () => new EventPublisher(), Lifestyle.Singleton );

  container.register<CancelOrderHandler, CancelOrderHandler>(()
    => new CancelOrderHandler(
      container.get<IOrderRepository>(),
      container.get<ILogger>(),
      container.get<IEventPublisher>() 
    )
  );

  // 3. Verify your configuration
  container.verify();

  // 4. Use the container
  var handler = container.getInstance<CancelOrderHandler>();

  var args = ['0123456789'];
  var orderId = Guid.Parse(args[0]);
  var command = new CancelOrder(orderId);

  handler.Handle(command);
}

0.1.1

  • Health issues and suggestions changes.

0.1.0

  • Initial version, created by Luciano Rodrigues

example/main.dart

import 'package:simply_injector/simply_injector.dart';

import 'CancelOrder.dart';
import 'CancelOrderHandler.dart';
import 'EventPublisher.dart';
import 'FileLogger.dart';
import 'Guid.dart';
import 'IEventPublisher.dart';
import 'ILogger.dart';
import 'IOrderRepository.dart';
import 'SqlOrderRepository.dart';

main() {
  // 1. Create a new Simple Injector container
  var container = new SimplyInjector.Container();

  // 2. Configure the container (register)
  container.register<IOrderRepository, SqlOrderRepository>(
      () => new SqlOrderRepository(container.get<ILogger>()));
  container.register<ILogger, FileLogger>(
      () => new FileLogger(), Lifestyle.Singleton);
  container.register<IEventPublisher, EventPublisher>(
      () => new EventPublisher(), Lifestyle.Singleton);

  container.register<CancelOrderHandler, CancelOrderHandler>(() =>
      new CancelOrderHandler(container.get<IOrderRepository>(),
          container.get<ILogger>(), container.get<IEventPublisher>()));

  // 3. Verify your configuration
  container.verify();

  // 4. Use the container
  var handler = container.getInstance<CancelOrderHandler>();

  var args = ['0123456789'];
  var orderId = Guid.Parse(args[0]);
  var command = new CancelOrder(orderId);

  handler.Handle(command);
}

Use this package as a library

1. Depend on it

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


dependencies:
  simply_injector: ^0.1.1

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:simply_injector/simply_injector.dart';
  
Version Uploaded Documentation Archive
0.1.1 Nov 28, 2018 Go to the documentation of simply_injector 0.1.1 Download simply_injector 0.1.1 archive
0.1.0 Nov 27, 2018 Go to the documentation of simply_injector 0.1.0 Download simply_injector 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
17
Health:
Code health derived from static analysis. [more]
89
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
55
Learn more about scoring.

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

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

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

Health issues and suggestions

Document public APIs (-9.72 points)

35 out of 36 API elements (library, class, field or method) have no adequate dartdoc content. Good documentation improves code readability and discoverability through search.

Fix lib/src/ContainerOptions.dart. (-1 points)

Analysis of lib/src/ContainerOptions.dart reported 2 hints:

line 26 col 19: The member 'isLocked' can only be used within instance members of subclasses of 'package:simply_injector/src/Container.Common.dart'.

line 26 col 41: The member 'hasRegistrations' can only be used within instance members of subclasses of 'package:simply_injector/src/Container.Common.dart'.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.1.6 1.1.6
Dev dependencies
test ^1.0.0