从 C# 和 ASP.NET Core 到 Ruby
录制器:多语言 C# 适配器(dotnet + 生成的驱动程序)。静态方法可以录制。analyze 能识别 ASP.NET Core minimal API(app.MapGet)和基于特性路由的控制器。
从 ASP.NET 到 Rails
| ASP.NET Core | Rails |
|---|---|
app.MapGet("/x/{id}", ...) / [HttpGet("{id}")] |
路由 + 控制器 action |
Results.Ok(x) / return Ok(x) |
render json: x |
Results.NotFound() / NotFound() |
head :not_found(与录制的响应体保持一致) |
CreatedAtAction(...) |
render json: x, status: :created, location: ... |
Razor 页面 / 视图(.cshtml) |
ERB 视图(frontend.md) |
Entity Framework DbContext、LINQ |
ActiveRecord;Where/Select/OrderBy/Include -> where/select/order/includes |
数据注解([Required]、[StringLength]) |
validates |
| Identity | Rails 认证生成器(密码哈希格式不同:需要规划重新哈希) |
IOptions<T> / appsettings.json |
config/*.yml + credentials |
BackgroundService / Hangfire |
Active Job + Solid Queue |
| System.Text.Json 的 camelCase 输出 | 保留 camelCase 键 |
语言结构
- 错误:
ArgumentException/ArgumentNullException/ArgumentOutOfRangeException->ArgumentError;InvalidOperationException->RuntimeError;KeyNotFoundException->KeyError;FormatException(int.Parse)->ArgumentError。 - 可空类型
int?->nil;??->.nil? ? d : x;?.->&.。 - 集合上的 LINQ -> Enumerable(
Aggregate->reduce,GroupBy->group_by,ToDictionary->to_h/index_by,First()在集合为空时抛出异常 ->fetch(0)/ raise)。 - Record / 属性 ->
Data.define/attr_reader。 decimal->BigDecimal,绝不用 Float。
陷阱
- 舍入:
Math.Round(2.5) == 2(默认是银行家舍入!)->round(half: :even);MidpointRounding.AwayFromZero->round。ToString("F2")和字符串插值的格式化使用当前区域设置:小数分隔符可能是,。 - 除非使用
checked,整数溢出会回绕;/向零截断;%的结果符号与被除数相同。 string.Length按 UTF-16 码元计数。string.Split(' ')会保留空项(在 Ruby 中用正则表达式写成split(" ", -1))。Dictionary<K,V>的枚举顺序在实践中是插入顺序,但没有保证:当录制的用例体现了某种顺序时就排序。- 没有指定 kind 的
DateTime是本地时间;按录制结果所示的方式序列化。