搜尋套件、儲存庫與 APP 分類

套件詳情v5.1.0

scope

一個依賴注入(控制反轉)庫。支援巢狀作用域和相同類型的多個值。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
25.4萬
喜歡數
14
Pub 分數
150

安裝套件

flutter pub add scope

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Scope 是一個用於 Flutter 和 Dart 的依賴注入(控制反轉)庫,可實現結構化、階層式的依賴管理。它支援巢狀作用域以隔離相依性,並允許相同類型的多個實例,非常適合模組化應用。憑藉乾淨的 API 設計與最少的樣板程式碼,Scope 可簡化應用程式各部分之間的狀態管理和服務實例化。它可在所有主要平台(包括 Android、iOS、Web、macOS、Windows 和 Linux)上順暢運作。

適用情境

  • 模組化應用架構
  • 使用隔離依賴項進行測試
  • 管理共用與作用域服務
  • 多實例服務處理
  • 跨平台應用開發

主要特色

  • 巢狀作用域支援
  • 同一類型的多個值
  • 最小化樣板程式碼
  • Platform-agnostic
  • 乾淨的 API 設計
  • 與現有程式碼的輕鬆整合

適合對象

  • 大規模 Flutter 應用
  • 可測試的架構
  • 微服務類應用模組
  • 需要依賴隔離的團隊
  • 開發者避開複雜的依賴注入框架

使用建議

  • 新使用者的學習曲線
  • 與成熟的替代方案相比,文件較少
  • 最適合具有複雜依賴關係樹的應用程式

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Scope

Scope provides Inversion of Control using the dependency injection (DI) pattern for Dart applications.

Scope allows you to inject values into a scope and then 'use' those dependencies from any method (or constructor) called within that scope.

Scope is incredibly easy to use with no additional plumbing required.

Scope is not a replacement for the likes of Provider. Provider does dependency injection for your BuildContext whilst Scope provides DI for your call stack.

For Java developers Scope provides similar functionality to thread local variables.

Scope supports nested scopes; overload or add additional values at each level.

Supporters

Scope is supported by OnePub the private Dart package repository.

https://onepub.dev

The best way to understand Scope is with an example:

import 'package:scope/scope.dart';
import 'package:money2/money2.dart';

const ageKey = ScopeKey<int>();
const heightKey = ScopeKey<int>();
const incomeKey = ScopeKey<Money>();
const countKey = ScopeKey<int>();

void main() async {
    /// create a Scope
    final scope = Scope()
    
    /// inject values
    ..value<int>(ageKey, 18)
    ..value<int>(heightKey, 182) // centimetres
    /// single value from factory method
    ..single<Money>(incomeKey, () => calcIncome)
    /// sequence of values from factory method.
    ..sequence<int>(countKey, () => tracker);
    
    /// run some code within the Scope
    await scope.run(() async => a();
}
void a() {
    print('Your height is ${use(heightKey)}');
    printRealWorth();
}

/// `use` the injected values
void printRealWorth() {
    print('You are the ${use(countKey)} person to ask');
    print('You are ${use(ageKey)} years old');
    print('You earn ${use(incomeKey} per hour.');
}

Money calcIncome() {
    final age = use(ageKey);
    if (age < 18) return Money.parse(r'$10.00');

    return Money.parse(r'$20.00');
}

var count = 0;
int tracker()
{
    return count++;
}

See the Scope documentation at:

https://scope.onepub.dev

The best practice guide is worth a read:

https://scope.onepub.dev/guides/best-practice

Authors: 
Philipp Schiffmann <philippschiffmann93@gmail.com>
S. Brett Sutton

Scope is a reimagining of Philipp's zone_id package. All credit goes to Phillipp's original implementation without which Scope wouldn't exist.

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案