從 C# 與 ASP.NET Core 到 Ruby
Recorder:polyglot C# adapter(dotnet + 產生的 driver)。static 方法可以錄製。ASP.NET Core minimal API(app.MapGet)與使用屬性路由的 controller 會由 analyze 找出。
從 ASP.NET 到 Rails
| ASP.NET Core | Rails |
|---|---|
app.MapGet("/x/{id}", ...) / [HttpGet("{id}")] |
路由 + controller action |
Results.Ok(x) / return Ok(x) |
render json: x |
Results.NotFound() / NotFound() |
head :not_found(要符合錄製下來的 body) |
CreatedAtAction(...) |
render json: x, status: :created, location: ... |
Razor pages / views(.cshtml) |
ERB view(frontend.md) |
Entity Framework DbContext、LINQ |
ActiveRecord;Where/Select/OrderBy/Include -> where/select/order/includes |
Data annotations([Required]、[StringLength]) |
validates |
| Identity | Rails authentication generator(密碼雜湊不同:請規劃重新雜湊) |
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。 - Nullable
int?->nil;??->.nil? ? d : x;?.->&.。 - 集合上的 LINQ -> Enumerable(
Aggregate->reduce,GroupBy->group_by,ToDictionary->to_h/index_by,First()在集合為空時會拋出例外 ->fetch(0)/ raise)。 - Record / property ->
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 中用 regex 搭配split(" ", -1))。Dictionary<K,V>的列舉順序實務上是插入順序,但並無保證:當錄製案例顯示出某種順序時,請排序。- 沒有指定 kind 的
DateTime是本地時間;請依錄製結果所顯示的方式序列化。