benxu.AppPlatform.Firebase.Firestore 3.0.2

dotnet add package benxu.AppPlatform.Firebase.Firestore --version 3.0.2
                    
NuGet\Install-Package benxu.AppPlatform.Firebase.Firestore -Version 3.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="benxu.AppPlatform.Firebase.Firestore" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="benxu.AppPlatform.Firebase.Firestore" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="benxu.AppPlatform.Firebase.Firestore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add benxu.AppPlatform.Firebase.Firestore --version 3.0.2
                    
#r "nuget: benxu.AppPlatform.Firebase.Firestore, 3.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package benxu.AppPlatform.Firebase.Firestore@3.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=benxu.AppPlatform.Firebase.Firestore&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=benxu.AppPlatform.Firebase.Firestore&version=3.0.2
                    
Install as a Cake Tool

benxu.AppPlatform.Firebase.Firestore

Firebase Cloud Firestore 實作套件,提供雲端 NoSQL 資料庫功能,支援文件操作、即時監聽、批次寫入和交易。

功能特色

  • 文件操作: 建立、讀取、更新、刪除文件 (CRUD)
  • 集合查詢: 支援多種查詢條件和排序
  • 即時監聽: 監聽文件和集合的即時變更
  • 批次操作: 一次性執行多個寫入操作
  • 交易支援: 保證資料一致性的交易操作
  • 離線支援: 內建離線持久化功能

安裝

dotnet add package benxu.AppPlatform.Firebase.Firestore

快速開始

1. 設定服務

// 方式 1: 使用 Bootstrap
builder.UseAppPlatform(options =>
{
    options.UseFirestore(firestore =>
    {
        firestore.EnableOfflinePersistence = true;
        firestore.CacheSizeMegabytes = 100;
    });
});

// 方式 2: 手動註冊
builder.Services.AddFirestore(options =>
{
    options.EnableOfflinePersistence = true;
});

2. 定義資料模型

重要: 資料模型必須實作 IFirestoreObject 介面,並使用 [FirestoreProperty] 屬性標記要序列化的屬性。

using Plugin.Firebase.Firestore;

public class User : IFirestoreObject
{
    [FirestoreProperty("name")]
    public string Name { get; set; } = "";

    [FirestoreProperty("age")]
    public int Age { get; set; }

    [FirestoreProperty("email")]
    public string Email { get; set; } = "";

    [FirestoreProperty("createdAt")]
    public DateTimeOffset CreatedAt { get; set; }
}

注意事項:

  • 類別必須是 public,否則反射無法正確存取
  • [FirestoreProperty] 中的名稱會成為 Firestore 文件中的欄位名稱
  • 重要: 時間欄位必須使用 DateTimeOffset(不是 DateTime),否則會發生類型轉換錯誤
  • 支援的類型包括: string, int, long, double, bool, DateTimeOffset, 陣列, 字典等

3. 使用服務

@inject IFirestoreService Firestore

// 建立文件
var user = new User { Name = "張三", Age = 25 };
await Firestore.SetDocumentAsync("users", "user123", user);

// 讀取文件
var result = await Firestore.GetDocumentAsync<User>("users", "user123");
if (result.IsSuccess)
{
    Console.WriteLine($"Name: {result.Data.Name}");
}

// 查詢集合
var queryResult = await Firestore.QueryCollectionAsync<User>(
    "users",
    q => q.WhereGreaterThan("Age", 18)
          .OrderBy("Name")
          .Limit(10)
);

// 即時監聽
var listener = Firestore.ListenToDocument<User>(
    "users",
    "user123",
    onChanged: user => Console.WriteLine($"Updated: {user?.Name}"),
    onError: ex => Console.WriteLine($"Error: {ex.Message}")
);

// 取消監聽
listener.Dispose();

設定選項

選項 預設值 說明
EnableOfflinePersistence true 是否啟用離線持久化
CacheSizeMegabytes 100 快取大小限制(MB)
EnableSsl true 是否啟用 SSL
EmulatorHost null 模擬器位址(例如: "10.0.2.2:8080")
EnableLogging false 是否啟用日誌
TimeoutSeconds 30 預設逾時時間(秒)

支援平台

  • Android 21.0+
  • iOS 14.0+

依賴

  • benxu.AppPlatform.Core
  • Plugin.Firebase.Firestore (3.1.3)

授權

MIT License

Product Compatible and additional computed target framework versions.
.NET net10.0-android36.0 is compatible.  net10.0-ios26.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on benxu.AppPlatform.Firebase.Firestore:

Package Downloads
benxu.AppPlatform.MAUI.Bootstrap

Bootstrap package for benxu App Platform. Provides fluent API for one-line service registration and lifecycle management.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.2 82 1/17/2026
3.0.0 85 1/13/2026
2.0.0 85 1/11/2026
1.0.0 94 12/28/2025