Dependency Injection oversimplified. Inspired by Koin, with focus on scopes.

Overview

Dart

Dependency Injection oversimplified. Inspired by Koin, with focus on scopes.

Features

  • Define your dependencies in a syntax as close to DSL as possible in Dart
  • Use scopes to control the life-cycle of your objects (relying on the GC)
  • Semi-automatic dependency resolution of dependencies
  • NO MAGIC, all achieved by just regular Dart code, no unnecessary code generation

Principles

  • Use factory to get a new instance every time you request one
  • Use scoped to get an instance that is persistent during the lifetime of your scope
  • Scopes are super general, which means that if you want a singleton, you just create a scoped in a scope that you'll have around forever
  • No built-in global/static container, if you want one, manage it for yourself
  • Scopes and Modules are basically the same, they are your containers
  • They live in a hierarchy, so first you create the widest scope, and as you create more and more below each other, they get narrower specific to a part of your application
  • If your dependency is not available in your current scope, Darin will try to resolve it in all the parent scopes, that's how it is wider as you go up
  • Your modules are going to have owners. Owners are conceptually required to understand scopes are tied to their life-cycle. A module should always be thrown away sooner than its owner, otherwise it's a memory leak

Getting started

Import the package (published to pub.dev soon):

dependencies:
  darin:
    git: https://github.com/TamasBarta/darin.git

Usage

Create your module like this:

final module = Module((module) => module
    ..factory<Interface1>((module) => Implementation1())
    ..scoped<Interface2>((module) => Implementation2(module.get<Interface1>()))
    ..scope<Interface4>((scope) => scope
        ..factory<Interface4>((module) => Implementation4(module.get<Interface3>()))
        ..scoped<Interface5>((module) => Implementation5(
            module.get<Interface3>(),
            module.get<Interface4>(),
        ))
    )
);

You can separate dependencies of your app's specific parts into multiple modules (even across packages), and combine them in a single place:

final module = Module.fromModules([
    moduleFromAPackage(),
    moduleFromBPackage(),
    moduleFromCPackage(),
]);

Use this module as your dependency injection container. You can use it as a service locator, but I would suggest avoiding that, and define all your classes' providers in your modules and submodules/scopes, and just get a root object, and let Darin take care of resolving dependencies. If you go this route, don't forget to utilize getting factories (coming soon), or creating sub-scopes.

To get your dependencies:

final MyDep = module.get<MyDep>();

To get narrower scopes:

// Owner will be `existingObject`, and will be available scoped in
// the submodule
final submodule = module.scope<MyDep>(existingObject);

// or

// Owner of the scope will be provided by `module`, and will be
// available as scoped in the submodule
final submodule = module.scopeProvided(MyDep);

Roadmap

  • Basic injection with scope support
  • Flutter integration via InheritedWidget
  • Set/Map support
  • Parameters for factories
  • Get the providers themselves similarly to getting the dependencies

Additional information

For integration with Flutter, see Darin Flutter (soon).

For Koin users

Koin is designed for you to be able to get your dependency injection container anywhere in the application, where you don't have a reference to it. My guess would be that this is done this way so you can use val myDep by inject<MyDep>() in your Android Activities, where you don't have any reference you could utilize to pass the context/scope down there, so a static accessor is required. This is not the case in Flutter, because in Flutter you actually control how your screens are created, and what you do have in your BuildContexts. For this reason I didn't include anything static that in turn would allow you to rely on a global reference. There are static functions for following Flutter patterns (think of SomethingFluttery.of(BuildContext)), but those rely on a BuildContext, so you don't use any global reference even behind the curtains. This is for a good reason, because you could always have parallel modules set up for different reasons, and they wouldn't overwrite each other with a Darin.start() or something similar as in Koin.

You might also like...

Your best flutter coding friend. All in one; state management, navigation management(with dynamic routing), local storage, localization, dependency injection, cool extensions with best usages and with the support of best utilities!

okito Your best flutter coding friend. All in one; state management, navigation management(with dynamic routing), local storage, dependency injection,

Jul 10, 2022

Dependency Injection is a great design pattern that allows us to eliminate rigid dependencies between elements and it makes the application more flexible

Dependency Injection is a great design pattern that allows us to eliminate rigid dependencies between elements and it makes the application more flexible

GetX lib DI pattern Dependency Injection is a great design pattern that allows us to eliminate rigid dependencies between elements and it makes the ap

Feb 1, 2022

Arisprovider - A mixture between dependency injection (DI) and state management, built with widgets for widgets

A mixture between dependency injection (DI) and state management, built with wid

Jan 9, 2022

MVC pattern for flutter. Works as state management, dependency injection and service locator.

MVC pattern for flutter. Works as state management, dependency injection and service locator.

MVC pattern for flutter. Works as state management, dependency injection and service locator. Model View Controller Here's a diagram describing the fl

Dec 12, 2022

Flutter getx template - A Flutter Template using GetX package for State management, routing and Dependency Injection

Flutter getx template - A Flutter Template using GetX package for State management, routing and Dependency Injection

Flutter GetX Template (GetX, Dio, MVVM) This Flutter Template using GetX package

Aug 27, 2022

App to showcase demo for how to have Clean Architecture in Flutter with Get_It for dependency injection

App to showcase demo for how to have Clean Architecture in Flutter with Get_It for dependency injection

App to showcase demo for how to have Clean Architecture in Flutter with Get_It for dependency injection, BLoC for state maintainence and Hive for persisting data into database.

Mar 19, 2022

[Flutter SDK V.2] - Youtube Video is a Flutter application built to demonstrate the use of Modern development tools with best practices implementation like Clean Architecture, Modularization, Dependency Injection, BLoC, etc.

[Flutter SDK V.2] - Youtube Video is a Flutter application built to demonstrate the use of Modern development tools with best practices implementation like Clean Architecture, Modularization, Dependency Injection, BLoC, etc.

[Flutter SDK V.2] - Youtube Video is a Flutter application built to demonstrate the use of Modern development tools with best practices implementation like Clean Architecture, Modularization, Dependency Injection, BLoC, etc.

Jan 2, 2023

A simple dependency injection plugin for Flutter and Dart.

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

Dec 13, 2022

Clean Architecture + TDD + SOLID + Dependency Injection + GitFlow + Mobx

 Clean Architecture + TDD + SOLID + Dependency Injection + GitFlow + Mobx

Clean Architecture + TDD + SOLID + Dependency Injection + GitFlow + Mobx Flutter Interview Challenge This app is part of an interview process. It took

Dec 28, 2022

A Dart dependency injection library aimed to be flexible, predictable and easy to use.

dino Dino is a Dart dependency injection library with optional code generation. It was inspired by DI in .NET and aimed to be flexible, predictable an

Dec 20, 2022

🎬 Ditonton App is a Flutter application built to demonstrate the use of modern development tools with best practices implementation like Modularization, BLoC, Dependency Injection, Firebase Analytics & Crashlytics, Sqlite, Testing, CI/CD, etc.

🎬 Ditonton App is a Flutter application built to demonstrate the use of modern development tools with best practices implementation like Modularization, BLoC, Dependency Injection, Firebase Analytics & Crashlytics, Sqlite, Testing, CI/CD, etc.

Ditonton App Features Movies (Now Playing, Popular, Top Rated) TV Show (On The Air, Popular, Top Rated) Watchlist Movies & TV Show Search Movies & TV

Aug 12, 2023

Ndiscopes - Scopes for NDI Sources in Windows

Ndiscopes - Scopes for NDI Sources in Windows

NDIScopes An open source Windows application to display several diffrent Scopes

Dec 18, 2022

A new Flutter widget that add support for AndroidTV app. Using keyboard to control focus node.

A new Flutter widget that add support for AndroidTV app. Using keyboard to control focus node.

flutter_tv_autofocus A new Flutter widget that add support for AndroidTV app. Using keyboard to control focus node. Getting Started Wrap your flutter

Aug 18, 2022

Main focus is to show dynamic operation not supported in Stateless Widget of Flutter

 Main focus is to show dynamic operation not supported in Stateless Widget of Flutter

A new Flutter project. It will count the number of donut(Though increment doesnot take place Stateless Widget). Main focus is to show dynamic operation not supported in Stateless Widget of Flutter.

Sep 9, 2022

Serene is a white noise app developed with Flutter. It helps you meditate, sleep better, focus, relax and be calm.

Serene is a white noise app developed with Flutter. It helps you meditate, sleep better, focus, relax and be calm.

Serene - Relax, Meditate and Sleep A white noise app developed with Flutter. It helps you meditate, sleep better, focus, relax and be calm. Screenshot

Dec 6, 2022

Snoozed, open source Flutter app. A focus oriented To-Do list with skippable tasks.

Snoozed, open source Flutter app. A focus oriented To-Do list with skippable tasks.

Snoozed: Skippable To Do list Download the App Apple App Store Google Play Store Video Tutorial YouTube video link Stack Front-end: Flutter Back-end:

Jan 5, 2023

Provides Dart Build System builder for creating Injection pattern using annotations.

Provides Dart Build System builder for creating Injection pattern using annotations. Gate generator The core package providing generators using annoat

Dec 20, 2022

a sample flutter app using Injection, routing and simple authentication follows clean code and best practices

a sample flutter app using Injection, routing and simple authentication follows clean code and best practices

Flutter Clean Project A sample flutter app using Injection, routing and simple authentication follows clean code and best practices Features Cleaned f

Jan 2, 2023

A CLI for syncing Dart dependency versions between pubspec.yaml and pubspec.lock files.

lockpick A CLI for syncing Dart dependency versions between pubspec.yaml and pubspec.lock files. 🔒 Usage # Activate lockpick pub global activate lock

Oct 17, 2022
Owner
Tamás Barta
Tamás Barta
This is a Flutter project inspired by sustainability and powered by the community

This is a Flutter project inspired by sustainability and powered by the community. TrashTrack is a demo of a social platform with geolocation features that brings people a new way of collaboration.

David 0 Oct 31, 2021
Chopper is an http client generator using source_gen and inspired from Retrofit.

Chopper Chopper is an http client generator for Dart and Flutter using source_gen and inspired by Retrofit. Documentation Installation Please refer to

Hadrien Lejard 632 Dec 31, 2022
A mock library for Dart inspired by mockito

?? mocktail This repository contains mocking libraries for Dart inspired by mockito. package:mocktail A Dart mocking library which simplifies mocking

Felix Angelov 430 Dec 25, 2022
Releases for the Delta-y app: A scrum-inspired personal productivity app for data-nerds with goals

Delta-y Releases This repository contains the public releases for the Delta-y app, which is currently closed source. Resources delta-y-flyer.pdf Video

Delta-y 2 Dec 10, 2021
Modern and elegant test framework for Flutter, inspired by Cypress

flutter_modern_test: Modern, elegant and productive test framework for Flutter, inspired by Cypress GitHub: https://github.com/fzyzcjy/flutter_modern_

fzyzcjy 14 Oct 19, 2022
Medikkare is a doctor appointment app. Written in Flutter. Inspired by Dribbble.

Medikkare is a doctor appointment app. Written in Flutter. Inspired by Dribbble.

Hyunjae Park 76 Jan 1, 2023
Ethers: A dart library that connects to interact with the Ethereum blockchain and inspired by ethers.js

Ethers For Flutter Ethers is a dart library that connects to interact with the Ethereum blockchain and inspired by ethers.js. Thanks to web3dart which

Hanggi 12 Oct 30, 2022
Pet Adoption App concept built with Flutter inspired by Aman UX

?? Adopt Me ?? Pet Adoption App concept built with Flutter inspired by Aman UX.

martinoyovo 38 Dec 8, 2022
Dart-dependency-injection - A simple example of how to use dependency injection with dart

This is a simple example of how to use dependency injection with dart. In this e

Rafael Alves 0 Feb 3, 2022
Compile-time dependency injection for Dart and Flutter

package:inject Compile-time dependency injection for Dart and Flutter, similar to Dagger. NOTE: This is not an official Google or Dart team project. E

Google 863 Dec 31, 2022