パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.14.2

koin

Dart プロジェクト向けの実用的で軽量な依存性注入フレームワーク。

30日間ダウンロード
52
いいね数
59
Pub ポイント
40

パッケージをインストール

flutter pub add koin

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Koin は、Dart プロジェクト向けの軽量で実用的な依存関係注入(DI)フレームワークであり、アプリケーション内の依存関係の管理を簡素化することを目的としています。依存関係の定義とインジェクションに、明確で簡潔な構文を提供し、モジュール性とテスト可能性を促進します。Koin は関数型およびクラスベースのコンポーネントの両方をサポートしており、シングルトン、ファクトリ、スコープ付きインスタンスを簡単に管理できるようにしています。最小限のボイラープレートで、Flutter および他の Dart アプリケーションにスムーズに統合され、拡張可能で保守しやすいコードベースに最適です。

用途

  • アプリケーション全体の依存関係の管理
  • モック依存関係を使用したテスト
  • 大規模アプリケーションのモジュール化
  • サービスインスタンス化の簡素化
  • 異なる環境(開発、本番)をサポートする
  • 関心事の明確な分離を可能にする

主な機能

  • 最小限のボイラープレート
  • 関数型およびクラスベースの定義
  • シングルトン、ファクトリ、およびスコープバインディング
  • 依存関係の置き換えによる簡単なテスト
  • 遅延初期化の組み込みサポート
  • 軽量で高速な実行時環境

適している対象

  • シンプルなDIを求めるFlutter開発者
  • モジュールアーキテクチャを必要とするプロジェクト
  • テスト性を重視するチーム
  • 複雑な依存関係グラフを持つアプリケーション
  • 大きなDIフレームワークの代替として軽量な選択肢を求める開発者

注意事項

  • 熟達したフレームワークと比べてドキュメントが限られている
  • 小さなコミュニティのサイズ
  • 組み込みの状態管理なし
  • カスタマイズなしでは、非常に大きなエンタープライズアプリケーションには適していません
  • 最適な使用には、DIの概念を理解している必要があります

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Koin.dart

Koin.dart

build codecov Star on Github style: effective dart


An pragmatic and flexible lightweight dependency injection library. This is a port of Koin for Dart projects.

Written in pure Dart, using functional resolution only: no code generation, no reflection.

Package Pub
koin pub package
koin_test pub package
koin_flutter pub package
koin_bloc pub package
koin_devtools pub package

Why should I use Koin?

  • Allows to dispose your objects at the moment that you are no longer using them.

  • It does not depend on the Flutter.

    • The core does not depend on Flutter, so it is possible to use it with any Dart application.
  • Define in which scope a variable can be accessed.

    • The koin scope allows you to define in which part of the widget tree a variable will be accessible
  • Integration by default for Bloc library, but it can be easily used with any state management.

  • Koin DevTools to inspect the state of your objects.

    • Inspect the internal state of each object at any time on a Flutter page.
  • Dependencies are instances only when needed.

    • Its class is instant when used for the first time.
    • Koin has a implementation of Lazy by Kotlin to enhance this functionality.
  • It is not invasive.

    • Insert Koin in your project without changing the structure of your widgets.
  • Facilitates dependency injection by constructor

    • Using dependency injection by constructor you decrease the coupling and make the test easier.
    • Makes it easy to know the dependencies of your components. Just look at your class's constructor to identify how dependencies it uses.

Features

  • Modules
  • Scopes
  • Singleton provider(definition)
  • Factory provider(definition)
  • Scoped provider(definition)
  • Support to multiple bindings
  • Support to named provider(definition)
  • Easy testing
  • Lazy inject
  • Logging
  • Support to parameter injection
  • Integration by default for Bloc library
  • DevTools for state inspection

What Koin.dart is not?

It is not a state manager. Koin does not have any type of state management, use koin with any state manager.

Table Of Contents

Roadmap

  • Improve documentation
  • Add more examples
    • Example of use with Redux, Mobx and RxDart.
    • Example with HTTP server frameworks.
  • Create an external DevTools
  • Add logger plugin for logger

Quick Start

Basic Setup
Dart
dependencies:
  koin: ^[version]
Flutter
dependencies:
  koin: ^[version]
  koin_flutter: ^[version]
Declare a Koin module
// Given some classes 
class Bloc {
  final Repository service;

  Bloc(this.service);

  get state => "Hello";
}

class Repository {}

// just declare your providers(definitions)
final myModule = Module()
  // Declare a single provider(definition) for Bloc class
  ..single((s) => Bloc(s.get()))
  // Declare a single provider(definition) for Repository class
  ..single((s) => Repository());
Starting Koin

Use the startKoin() function to start Koin in your application.

In a Dart app:

void main(List<String> args) {
    startKoin((app){
      app.module(myModule);
    });
  }

In an Flutter app:

void main() {
  startKoin((app) {
    app.module(homeModule);
  });
  runApp(MyApp());
}
Injecting dependencies
class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    // Get a dependency
    final bloc = get<Bloc>();
    return Container(
      child: Text("${bloc.state()}"),
    );
  }
}

Setup

Dart
dependencies:
koin: ^[version]
# Koin for Unit tests
dev_dependencies:
koin_test: ^[version]
Flutter
dependencies:
  koin: ^[version]
  koin_flutter: ^[version]

# Koin for Unit tests
dev_dependencies:
  koin_test: ^[version]
Flutter + Bloc
dependencies:
  koin: ^[version]
  koin_flutter: ^[version]
  koin_bloc: ^[version]

# Koin for Unit tests
dev_dependencies:
  koin_test: ^[version]

Examples

Basic

An simple example in Flutter. Code: Repository

Counter

A more elaborate example using Bloc library as a state management. Code: Repository

Real World

A application to demonstrate the Koin in a real world application.

Features
  • Log in
  • Sign up
  • Loggout
  • Password reset

Code: Repository

DevTools

Koin DevTools allows you to inspect the internal state of the objects created by the providers(definitions).

Usage

Just insert the KoinDevTools Widget somewhere in your application or use showDevTools.

class Page extends StatefulWidget {
  @override
  _PageState createState() => _PageState();
}

class _PageState extends State<Page> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
     /// Just insert the KoinDevTools
      endDrawer: KoinDevTools(),
      body: IconButton(icon: Text('Shod DevTools'), onPressed: () {
        // Or use this
        showDevTools(context);
      },),
    );
  }
}

Ask a Question?

Reporting issues

Found a bug on a specific feature? Open an issue on Github issues

Contribute

Want to help or share a proposal about Koin? problem on a specific feature?

  • Open an issue to explain the issue you want to solve Open an issue
  • After discussion to validate your ideas, you can open a PR or even a draft PR if the contribution is a big one Current PRs

Maintainers

Credits

Dependencies

カテゴリー

依存関係

依存関係一覧 2 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。