Inkslab.Config
1.2.14
dotnet add package Inkslab.Config --version 1.2.14
NuGet\Install-Package Inkslab.Config -Version 1.2.14
<PackageReference Include="Inkslab.Config" Version="1.2.14" />
paket add Inkslab.Config --version 1.2.14
#r "nuget: Inkslab.Config, 1.2.14"
// Install Inkslab.Config as a Cake Addin #addin nuget:?package=Inkslab.Config&version=1.2.14 // Install Inkslab.Config as a Cake Tool #tool nuget:?package=Inkslab.Config&version=1.2.14
“Inkslab”是什么?
Inkslab 是一套简单、高效的轻量级框架(涵盖了对象映射、配置读取、Xml/Json序列化和反序列化、以及自动/定制化依赖注入)。
如何安装?
First, install NuGet. Then, install Inkslab from the package manager console:
PM> Install-Package Inkslab
引包即用?
- 引包即用是指,安装
NuGet
包后,自动注入配置信息。 - 在启动方法中添加如下代码即可:
using (var startup = new XStartup())
{
startup.DoStartup();
}
NuGet 包
Package | NuGet | Downloads | Jane Says <kbd>Markdown</kbd> |
---|---|---|---|
Inkslab | Core universal design. | ||
Inkslab.Map | Type conversion, cloning, mapping. | ||
Inkslab.Config | Read configuration file. | ||
Inkslab.Json | JSON read and write processing. | ||
Inkslab.Net | Request component of HTTP/HTTPS. |
内置功能。
Xml 系列化和反序列化助手。
- 使用方式。
/// <summary> /// 序列化实体。 /// </summary> [XmlRoot("xml")] public class XmlA { /// <summary> /// 忽略字段。 /// </summary> [Ignore] //? 忽略字段。 public int A1 { get; set; } = 100; /// <summary> /// 生成 <![CDATA[{value}]]> /// </summary> public CData A2 { get; set; } /// <summary> /// <inheritdoc/> /// </summary> public string A3 { get; set; } /// <summary> /// <inheritdoc/> /// </summary> public DateTime A4 { get; set; } } /// <summary> /// 反序列化实体。 /// </summary> [XmlRoot("xml")] public class XmlB { /// <summary> /// 忽略字段。 /// </summary> [Ignore] //? 忽略字段。 public int A1 { get; set; } = 100; /// <summary> /// <inheritdoc/> /// </summary> public string A2 { get; set; } /// <summary> /// <inheritdoc/> /// </summary> public string A3 { get; set; } /// <summary> /// <inheritdoc/> /// </summary> public DateTime A4 { get; set; } }
- 序列化。
XmlA x = new XmlA { A1 = 200, A2 = "测试CData节点", A3 = "普通节点", A4 = DateTime.Now }; string xml = XmlHelper.XmlSerialize(x);
- 反序列化。
string xml ="<?xml version=\"1.0\" encoding=\"utf-8\"?><xml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><A2><![CDATA[测试CData节点]]></A2><A3>普通节点</A3><A4>2022-12-03T14:53:29.1218173+08:00</A4></xml>"; XmlB y = XmlHelper.XmlDeserialize<XmlB>(xml);
自定义程序集查找器。
- 自定义【IDirectory】。
public class CustomDirectory : IDirectory { public string[] GetFiles(string path, string searchPattern) => Directory.GetFiles(path, searchPattern); }
- 注入到单列池中。
SingletonPools.TryAdd<IDirectory, CustomDirectory>();
- 自定义【IDirectory】。
配置文件助手。
使用方式:
普通类型。
var value = "config-key".Config<string>(); // 返回结果字符串。
监听类型。
var options = "config-key".Options<string>(); // 返回IOptions<string>配置。 var value = options.Value; // 配置文件发生变化时,“options.Value”会被更新。
默认实现,
PM> Install-Package Inkslab.Config
如需自定义,请参考 Inkslab.Config 文稿。
Json 序列化和反序列化助手。
使用方式:
序列化。
string json = JsonHelper.ToJson(new { Id = KeyGen.Id(), Timestamp = DateTime.Now });
反序列化。
/// <summary> /// 序列化类型。 /// </summary> public class A { /// <summary> /// 不序列化这个属性。 /// </summary> [Ignore] public int A1 { get; set; } = 100; /// <summary> /// <see cref="A2"/> /// </summary> public int A2 { get; set; } /// <summary> /// <see cref="A3"/> /// </summary> public string A3 { get; set; } = string.Empty; /// <summary> /// <see cref="A4"/> /// </summary> public DateTime A4 { get; set; } } string json = "{\"A2\":100,\"A3\":\"A3\",\"A4\":\"2022-12-03 14:17:55.7425309+08:00\"}"; // JSON 字符串。 A a = JsonHelper.Json<A>(json); // 转换实体、匿名类型请参考重载方法。
默认实现,
PM> Install-Package Inkslab.Json
如需自定义,请参考 Inkslab.Json 文稿。
对象映射。
- 使用方式。
FooDto fooDto = Mapper.Map<FooDto>(foo); BarDto barDto = Mapper.Map<BarDto>(bar);
- 默认实现,
PM> Install-Package Inkslab.Map
- 如需自定义或了解高级语法,请参考 Inkslab.Map 文稿。
主键生成器。
- 使用方式。
long id = KeyGen.Id();
- 默认实现:雪花算法。
- 设置机房和机号。
SingletonPools.TryAdd(new KeyOptions(workerId, datacenterId));
- 自定义主键生成器。
- 实现接口。
/// <summary> /// KeyGen 创建器。 /// </summary> public interface IKeyGenFactory { /// <summary> /// 创建。 /// </summary> /// <returns></returns> IKeyGen Create(); }
- 注入实现。
SingletonPools.TryAdd<IMapper, CustomMapper>();
- 正常使用。
单例。
作为单例基类。
public class ASingleton : Singleton<ASingleton> { private ASingleton(){ } } ASingleton singleton = ASingleton.Instance;
作为单例使用。
public class BSingleton { } BSingleton singleton = Singleton<BSingleton>.Instance
绝对单例。
public class CSingleton : Singleton<CSingleton> { private CSingleton(){ } } CSingleton singleton1 = CSingleton.Instance; CSingleton singleton2 = Singleton<CSingleton>.Instance; // 与“singleton1”是同一实例。
单例池。
TryAdd:添加单例实现。
Singleton:获取单例。
单例实现:
单例实现(一)。
- 添加默认支持的单例实现。
SingletonPools.TryAdd<A,B>(); //=> true.
- 在未使用A的实现前,可以刷新单例实现支持。
SingletonPools.TryAdd<A,C>(); //=> true; SingletonPools.TryAdd<A>(new C()); //=> true;
单例实现(二)。
- 添加实例或工厂支持的单例实现。
SingletonPools.TryAdd<A>(new B()); //=> true.
- 在未使用A的实现前,可以被实例或工厂刷新单例实现支持,默认支持方式不被生效。
SingletonPools.TryAdd<A,C>(); //=> false; SingletonPools.TryAdd<A>(new C()); //=> true;
单例使用:
单例使用(一)。
A a = SingletonPools.Singleton<A>();
未提前注入单例实现,会直接抛出
NotImplementedException
异常。单例使用(二)。
A a = SingletonPools.Singleton<A,B>();
未提前注入单例实现,会尝试创建
B
实例。
说明:
TryAdd<T>:使用实例时,使用【公共/非公共】无参构造函数创建实例。
TryAdd<T1,T2>:使用实例时,尽量使用参数更多且被支持的公共构造函数创建实例。
public class A { } public class B { private readonly A a; public B() : this(new A()){ } Public B(A a){ this.a = a ?? throw new ArgumentNullException(nameof(a)); } }
使用单例时,未注入A的单例实现,使用无参构造函数生成实例。
使用单例时,已注入A的单例实现,使用参数
A
的造函数生成实例。
命名规范。
命名方式。
/// <summary> 命名规范。 </summary> public enum NamingType { /// <summary> 默认命名(原样/业务自定义)。 </summary> Normal = 0, /// <summary> 驼峰命名,如:userName。 </summary> CamelCase = 1, /// <summary> 蛇形命名,如:user_name,注:反序列化时也需要指明。 </summary> SnakeCase = 2, /// <summary> 帕斯卡命名,如:UserName。 </summary> PascalCase = 3, /// <summary>短横线命名,如:user-name。</summary> KebabCase = 4 }
命名转换(三种命名可以相互转换)。
指定命名方式。
string named = "name".ToNamingCase(NamingType.CamelCase);
特定命名方式。
帕斯卡命名(又称大驼峰)。
string named = "user_name".ToPascalCase(); // UserName
驼峰命名。
string named = "user_name".ToCamelCase(); // userName
蛇形命名。
string named = "user_name".ToSnakeCase(); // user_name
短横线命名
string named = "user_name".ToKebabCase(); // user-name
字符串语法糖。
string value = "${a + b}".PropSugar(new { A = 1, B = 2 }); //=> value = "3"。
语法说明:
空运算符:A ? B、A ?? B
当A为
null
时,返回B,否则返回A。合并运算符:A + B
当A和B可以参与运算时,返回运算结果。否则转成字符串拼接。
试空合并运算符:A ?+ B
当A为
null
时,返回null
,否则按照【合并运算符】计算A+B的结果。属性后代:A.B 或 A.B.C ...
标星历程图。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
.NET Framework | net461 is compatible. 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 | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.1
- Inkslab (>= 1.2.14)
-
.NETStandard 2.1
- Inkslab (>= 1.2.14)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.0)
- Microsoft.Extensions.Configuration.Json (>= 9.0.0)
-
net6.0
- Inkslab (>= 1.2.14)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.2.14 | 35 | 11/22/2024 |
1.2.13 | 414 | 9/10/2024 |
1.2.12 | 100 | 9/10/2024 |
1.2.11 | 168 | 8/4/2024 |
1.2.10 | 100 | 7/29/2024 |
1.2.9 | 85 | 7/29/2024 |
1.2.8.5 | 2,853 | 5/15/2024 |
1.2.8 | 1,749 | 3/26/2024 |
1.2.7 | 148 | 3/1/2024 |
1.2.5 | 430 | 1/2/2024 |
1.2.4.1 | 268 | 12/20/2023 |
1.2.4 | 183 | 12/12/2023 |
1.2.3 | 150 | 12/12/2023 |
1.2.2 | 135 | 12/12/2023 |
1.2.1.15 | 242 | 11/22/2023 |
1.2.1.14 | 468 | 11/8/2023 |
1.2.1.13 | 239 | 11/3/2023 |
1.2.1.12 | 241 | 11/1/2023 |
1.2.1.11 | 184 | 10/25/2023 |
1.2.1.10 | 129 | 10/24/2023 |
1.2.1.9 | 159 | 10/22/2023 |
1.2.1.8 | 165 | 10/16/2023 |
1.2.1.7 | 184 | 10/12/2023 |
1.2.1.6 | 189 | 10/9/2023 |
1.2.1.4 | 246 | 7/14/2023 |
1.2.1.3 | 156 | 7/14/2023 |
1.2.1.1 | 166 | 7/12/2023 |
1.2.1 | 172 | 7/10/2023 |
1.2.0 | 177 | 7/9/2023 |
1.1.0 | 167 | 7/9/2023 |
1.0.2.9 | 265 | 4/21/2023 |
1.0.2.8 | 202 | 4/21/2023 |
1.0.2.7 | 249 | 3/30/2023 |
1.0.2.5 | 274 | 2/23/2023 |
1.0.2.1 | 286 | 2/7/2023 |
1.0.2 | 300 | 2/3/2023 |
1.0.1 | 354 | 12/20/2022 |
1.0.0 | 337 | 12/13/2022 |