CSoft.DataPairs.Newtonsoft
11.0.0
dotnet add package CSoft.DataPairs.Newtonsoft --version 11.0.0
NuGet\Install-Package CSoft.DataPairs.Newtonsoft -Version 11.0.0
<PackageReference Include="CSoft.DataPairs.Newtonsoft" Version="11.0.0" />
<PackageVersion Include="CSoft.DataPairs.Newtonsoft" Version="11.0.0" />
<PackageReference Include="CSoft.DataPairs.Newtonsoft" />
paket add CSoft.DataPairs.Newtonsoft --version 11.0.0
#r "nuget: CSoft.DataPairs.Newtonsoft, 11.0.0"
#:package CSoft.DataPairs.Newtonsoft@11.0.0
#addin nuget:?package=CSoft.DataPairs.Newtonsoft&version=11.0.0
#tool nuget:?package=CSoft.DataPairs.Newtonsoft&version=11.0.0
DataPairs
DataPairs 是使用 SQLite 或 JSON 文件存储键值数据的 .NET 类库。CSoft.DataPairs.Newtonsoft 对应 master 分支,使用 Newtonsoft.Json 进行持久化。源码仓库还提供 System.Text.Json 和 Ceras 分支。
安装
dotnet add package CSoft.DataPairs.Newtonsoft
11.0.0 包含 netstandard2.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)。SQLite 和文件模式的构造函数也可以接受 JsonSerializerSettings。
持久化按 Newtonsoft.Json 的规则处理模型。可用 [JsonProperty] 纳入指定成员,用 [JsonIgnore] 排除成员。缓存克隆可用 [FastDeepClonerIgnore] 排除字段或属性。默认 JSON 会记录类型和引用信息,请读取应用控制的可信数据。
11.0.0 升级说明
- 新增 .NET 11 和 EF Core SQLite 11 支持。SQLite 同键写入使用 UPSERT,文件更新使用同目录临时文件替换。
- 带
path、StorageType和JsonSerializerSettings的构造函数现在会使用传入的路径和设置。文件模式下,如果旧版将文件写到了默认Config,请将对应 JSON 移到指定目录的Config。SQLite 模式下,自定义设置需能读取已保存的 JSON。 - .NET 8 及以上在 Unix 上新建的文件仅允许所有者读写,更新已有文件时保留其权限位。
netstandard2.0为经典 Xamarin 保留旧 SQLite 原生依赖,可能出现NU1903。 SQLite 安全公告
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- FastDeepCloner (>= 1.3.6)
- Microsoft.EntityFrameworkCore.Sqlite (>= 3.1.32)
- Newtonsoft.Json (>= 13.0.4)
-
net10.0
- FastDeepCloner (>= 1.3.6)
- Microsoft.EntityFrameworkCore.Sqlite (>= 10.0.15)
- Newtonsoft.Json (>= 13.0.4)
-
net11.0
- FastDeepCloner (>= 1.3.6)
- Microsoft.EntityFrameworkCore.Sqlite (>= 11.0.0)
- Newtonsoft.Json (>= 13.0.4)
-
net8.0
- FastDeepCloner (>= 1.3.6)
- Microsoft.EntityFrameworkCore.Sqlite (>= 8.0.34)
- Newtonsoft.Json (>= 13.0.4)
-
net9.0
- FastDeepCloner (>= 1.3.6)
- Microsoft.EntityFrameworkCore.Sqlite (>= 9.0.23)
- Newtonsoft.Json (>= 13.0.4)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CSoft.DataPairs.Newtonsoft:
| Package | Downloads |
|---|---|
|
CSoft.Process
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 11.0.0 | 119 | 11/12/2026 | |
| 10.0.0 | 450 | 11/12/2025 | |
| 9.8.6 | 322 | 11/7/2025 | |
| 9.1.1 | 317 | 11/7/2025 | |
| 9.1.0 | 300 | 10/24/2025 | |
| 9.0.2 | 337 | 8/14/2025 | |
| 9.0.1 | 343 | 1/23/2025 | |
| 9.0.0 | 332 | 11/22/2024 | |
| 8.0.2 | 442 | 4/11/2024 | |
| 8.0.1 | 388 | 3/22/2024 | |
| 8.0.0 | 379 | 11/15/2023 | |
| 7.0.2 | 405 | 4/20/2023 | |
| 7.0.1 | 575 | 2/1/2023 | |
| 7.0.0 | 637 | 11/9/2022 | |
| 6.0.6 | 743 | 8/12/2022 | |
| 6.0.5 | 771 | 5/11/2022 | |
| 6.0.4 | 736 | 5/10/2022 | |
| 6.0.3 | 756 | 3/15/2022 | |
| 6.0.2 | 766 | 2/23/2022 | |
| 6.0.1 | 831 | 12/24/2021 |
支持 .NET 11,更新 SQLite 依赖并修复持久化问题