CSoft.DataPairs.Microsoft 11.0.0

dotnet add package CSoft.DataPairs.Microsoft --version 11.0.0
                    
NuGet\Install-Package CSoft.DataPairs.Microsoft -Version 11.0.0
                    
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="CSoft.DataPairs.Microsoft" Version="11.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSoft.DataPairs.Microsoft" Version="11.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CSoft.DataPairs.Microsoft" />
                    
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 CSoft.DataPairs.Microsoft --version 11.0.0
                    
#r "nuget: CSoft.DataPairs.Microsoft, 11.0.0"
                    
#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 CSoft.DataPairs.Microsoft@11.0.0
                    
#: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=CSoft.DataPairs.Microsoft&version=11.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CSoft.DataPairs.Microsoft&version=11.0.0
                    
Install as a Cake Tool

DataPairs

DataPairs 是使用 SQLite 或 JSON 文件存储键值数据的 .NET 类库。CSoft.DataPairs.Microsoft 使用 System.Text.Json 进行持久化。

安装

dotnet add package CSoft.DataPairs.Microsoft

11.0.0 包含 net8.0、net9.0、net10.0 和 net11.0 目标。

读写数据

DataPair<T> 用一个键绑定一个模型。T 需要是具有公共无参构造函数的类。

using System;
using System.Collections.Generic;
using System.IO;
using DataPairs;
using DataPairs.Interfaces;

var directory = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyApp");
Directory.CreateDirectory(directory);

IDataPair<Data> pair = new DataPair<Data>(
    "Data", Path.Combine(directory, "data.db"), StorageType.SQLite);

var data = await pair.TryGetValueAsync();
data.A = 1.25m;
data.B.Add(2m);
await pair.TryInitOrUpdateAsync(data);

public class Data
{
    public decimal A { get; set; }
    public List<decimal> B { get; set; } = new List<decimal>();
}

键不存在时,TryGetValueAsync() 返回 new T()。修改读取结果后,调用 TryInitOrUpdateAsync() 才会保存。需要删除时调用 await pair.TryRemoveAsync()。

可以复用 DataPair<T> 实例,并在应用初始化时调用 TryGetValueAsync() 预热缓存。缓存按实例维护,其他实例或进程写入的数据可通过新实例读取。

存储方式

选项 path 的含义 省略路径时
StorageType.SQLite SQLite 数据库文件路径 当前工作目录下的 PairsDB.dll
StorageType.File 数据目录,键 Data 写到其下的 Config/Data.json 应用基目录下的 Config
StorageType.Xamarin SQLite 数据库文件路径 Environment.SpecialFolder.Personal 下的 PairsDB.dll

文件模式可使用 new DataPair<Data>("Data", directory, StorageType.File)。构造函数也可以接受 JsonSerializerOptions。SQLite 和文件模式默认都使用 ReferenceHandler.Preserve,SQLite 模式默认还使用 camelCase 字符串枚举。

StorageType.Xamarin 保留个人目录的路径选择。本包的最低目标框架为 .NET 8。

使用 System.Text.Json.Serialization 中的 [JsonInclude] 或 [JsonIgnore] 控制持久化成员。缓存克隆可用 [FastDeepClonerIgnore] 排除字段或属性。

模型包含 IAsyncEnumerable<T> 时,建议先收集到 List<T> 等集合再保存。异步序列可以写入 JSON,但从存储加载后的缓存克隆可能返回空序列。

11.0.0 升级说明

  • 新增 .NET 11 和 EF Core SQLite 11 支持,并更新现有目标的 SQLite 依赖。
  • SQLite 同键写入使用 UPSERT,文件更新使用同目录临时文件替换。已有 SQLite 和 JSON 数据沿用本包的 System.Text.Json 格式。
  • 新实例可直接删除已保存的键。缓存克隆按对象引用处理循环和共享引用。
  • Unix 上新建的文件仅允许所有者读写,更新已有文件时保留其权限位。
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

支持 .NET 11,更新 SQLite 依赖并修复持久化问题