패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.14.2

koin

Dart 프로젝트용 실용적이고 가벼운 종속성 주입 프레임워크입니다.

30일 다운로드
52
좋아요
59
Pub 점수
40

패키지 설치

flutter pub add koin

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

Koin은 Dart 프로젝트를 위한 가벼우면서도 실용적인 의존성 주입(DI) 프레임워크로, 애플리케이션 내에서 종속성 관리를 간소화하도록 설계되었습니다. 의존성을 정의하고 주입하는 데 깔끔하고 간결한 구문을 제공하여 모듈성과 테스트 가능성을 촉진합니다. Koin은 함수형 및 클래스 기반 컴포넌트를 모두 지원하며, 싱글톤, 팩토리, 스코프 인스턴스를 쉽게 관리할 수 있게 해줍니다. 최소한의 반복 코드로 플러터와 기타 Dart 애플리케이션에 원활하게 통합되어 확장성 있고 유지보수 가능한 코드베이스에 이상적입니다.

사용 사례

  • 애플리케이션 전반의 종속성 관리
  • 모의 종속성으로 테스트
  • 대규모 애플리케이션의 모듈화
  • 서비스 인스턴스화 간소화
  • 다른 환경(개발, 운영) 지원
  • 관심사의 명확한 분리를 가능하게 하기

주요 기능

  • 최소한의 보일러플레이트
  • 기능적 및 클래스 기반 정의
  • 싱글톤, 팩토리, 및 스코프 바인딩
  • 의존성 교체를 통한 간편한 테스트
  • 지연 초기화를 위한 내장 지원
  • 경량이고 빠른 런타임

적합한 대상

  • 간단한 DI를 찾는 플러터 개발자
  • 모듈러 아키텍처가 필요한 프로젝트
  • 테스트 가능성에 중점을 둔 팀
  • 복잡한 종속성 그래프를 가진 애플리케이션
  • 더 큰 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 개

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.