UnitGenerator 使用教程

UnitGenerator 使用教程

UnitGenerator C# Source Generator to create value-object, inspired by units of measure. 项目地址: https://gitcode.com/gh_mirrors/un/UnitGenerator

1. 项目介绍

UnitGenerator 是一个 C# 源代码生成器,用于创建受度量单位启发的值对象(Value Object)。它支持所有算术运算符和序列化,旨在帮助开发者更安全、更高效地处理特定类型的数据。UnitGenerator 通过生成代码来确保值对象的类型安全,防止类型间的错误赋值,并支持算术运算和比较操作。

2. 项目快速启动

2.1 安装

首先,通过 NuGet 安装 UnitGenerator:

Install-Package UnitGenerator

2.2 创建值对象

以下是一个简单的示例,展示如何使用 UnitGenerator 创建一个值对象 UserId

using UnitGenerator;

[UnitOf(typeof(int))]
public readonly partial struct UserId
{
}

2.3 使用值对象

生成的 UserId 值对象将自动实现 IEquatable<UserId> 接口,并支持类型安全的比较和赋值操作:

var userId1 = new UserId(123);
var userId2 = new UserId(456);

bool isEqual = userId1 == userId2; // 比较两个 UserId 对象
int primitiveValue = userId1.AsPrimitive(); // 获取原始值

3. 应用案例和最佳实践

3.1 游戏中的 HP 值对象

在游戏中,HP(生命值)通常需要支持算术运算,但不应允许与其他类型混淆。使用 UnitGenerator 可以轻松实现这一点:

[UnitOf(typeof(int), UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.MinMaxMethod)]
public readonly partial struct Hp
{
}

使用示例:

var hp1 = new Hp(100);
var hp2 = new Hp(50);

var newHp = hp1 + hp2; // 支持算术运算
var minHp = Hp.Min(hp1, hp2); // 支持 Min 方法

3.2 数据库序列化

UnitGenerator 还支持多种序列化方式,如 MessagePack、Json 等,确保值对象在序列化和反序列化过程中保持类型安全:

[UnitOf(typeof(int), UnitGenerateOptions.MessagePackFormatter)]
public readonly partial struct UserId
{
}

4. 典型生态项目

UnitGenerator 可以与其他 C# 生态项目无缝集成,如:

  • Entity Framework Core: 支持值对象的自动转换和数据库映射。
  • Dapper: 支持值对象的类型安全查询和结果映射。
  • System.Text.Json: 支持值对象的 JSON 序列化和反序列化。

通过这些集成,UnitGenerator 可以帮助开发者在整个应用栈中保持类型安全,减少潜在的错误。


通过本教程,您应该已经掌握了如何使用 UnitGenerator 创建和使用值对象,并了解了其在实际项目中的应用场景和最佳实践。希望 UnitGenerator 能够帮助您在 C# 开发中更高效、更安全地处理数据。

UnitGenerator C# Source Generator to create value-object, inspired by units of measure. 项目地址: https://gitcode.com/gh_mirrors/un/UnitGenerator

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

你需要的时候你来到最喜爱的国际电视系列在知道的一切 DuckieTV是谷歌Chrome浏览器/歌剧扩展,它是通过一个个性化的电视系列日历负责电视剧成瘾者 DuckieTV确保信息始终是最新的,并为您提供了一个集成的模块性洪流搜索,以帮助找到下载尽可能简单,找到你 集成的扭矩DuckieTorrent客户DuckieTV您在本地的uTorrent/ BitTorrent客户端,您将随时了解下载的进度,而无需切换到其它应用程序 实验铬铸铁的整合甚至允许你串流至您的电视,同时还下载种子视频 从v0.60 DuckieTV也是最后一个有价值的竞争对手为病比尔德通过引入节目自动下载被以11种语言播出和翻译 (英语和德语,西班牙语,法语,意大利语,日本,한국어,荷兰,Purtugese,Русский,瑞典语,简体中文) ---!注:此版本不接管你的“新标签页”页面! ! - 更新日志: ========== * v1.1.3 : Updated translations, many bugfixes and new features. check the changelog at https://github.com/SchizoDuckie/DuckieTV/releases/tag/1.1.3 * v1.00 : Completely revamped user interface (now with 100% more sexyness) - Added support for Tixati, Transmission and qBittorrent torrent clients - Added Strike and RarBG torrent search providers - Added calendar grouping for netflix episode dumps - Initial version of Subtitle search available from episodes panel - Removed Chromecast integration. (Use getvideostream.com and the app!) - Autodownloads now use the configured torrent provider - Revamped the way torrent search engines are created and registered - Shows can be marked as downloaded as well as watched, and downloaded episodes can be highlighted on the calendar - Added Trakt.TV Trending category filters, caching for Trakt.TV trending list - Fixes for DuckieTV standalone: now using frame-less window, open external links in default browser, window and unminimize from tray works in Ubuntu, added upgrade check and notification, and zoom control is now 1:1 with chrome browser - Database performance improvement (including less frequent ratings updates) - added 'Watch on Netflix' button for Netflix shows - numerous other small changes and bugfixes to list * v0.94 : Switched to the new (hopefully more stable) trakt.tv endpoint, Added actor and rating info back to serie details, Fixed KAT Mirror Resolver and custom setting and added back TPB mirror selection to torrent settings, Changed default KAT mirror back to kickass.to, Minor tweaks to auto-download and updatec
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

解然嫚Keegan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值