メインコンテンツへ移動

Inventory and Orders API for .NET C# and MySQL

最後の 1 点を二重に売らず、変更を繰り返さず、データベース接続を溢れさせない ASP.NET Core と MySQL のコード。

  • .NET SDK
  • C#
  • ASP.NET Core
  • EF Core
  • MySql.EntityFrameworkCore

スクリーンショット

スクリーンショット 1 / 11:タイトル カード: Inventory and Orders API、.NET C# のソース コード。技術の一覧と、最後の 1 点への 2 件の注文を示すコード ウィンドウがあり、1 件は 201 Created、もう 1 件は 409 Conflict です。

タイトル カード: Inventory and Orders API、.NET C# のソース コード。技術の一覧と、最後の 1 点への 2 件の注文を示すコード ウィンドウがあり、1 件は 201 Created、もう 1 件は 409 Conflict です。

買い切り価格

  • ₹999+GST(お支払い時)
  • $19
  • お支払いが確認されると、すぐにライブラリからダウンロードできます。
  • このプロジェクトの今後のリリースもすべて、追加費用なしで含まれます。
最新リリース
1.0.0 · 2026/11/06
ライセンス
Source Code Licence

概要

ASP.NET Core、EF Core、MySQL で作られた在庫・注文 API の完全なソース コードです。カテゴリ、商品、在庫の移動、注文を 20 のエンドポイントで扱います。2 人の購入者が最後の 1 点を同時に買うことはできず、再試行で変更が繰り返されることもなく、リクエストが殺到してもデータベース接続は溢れません。6 つの手順で自分のコンピューターで動かせます。

在庫管理のサンプルの多くは、2 人が同じ瞬間に最後の 1 点を買うとき、クライアントが応答を受け取れず再試行するとき、トラフィックの急増がデータベースに届くときに問題が起きます。Inventory and Orders API for .NET C# and MySQL は、そうした場面のための完全なコードです。結果はすべて MySQL 9.7 での実際の実行から得たものです。

API でできること

  • カテゴリと商品。 商品の検索、絞り込み、並べ替え、ページ分割、アーカイブと復元ができ、発注点以下の商品も一覧できます。
  • 在庫。 入荷と修正を記録し、商品ごとのすべての変更履歴を確認できます。
  • 注文。 注文を作成し、出荷済みにするかキャンセルします。キャンセルすると在庫は戻ります。
  • 20 のエンドポイントと 2 つのヘルス チェック。 .http ファイルにすべてのエンドポイントのリクエストがあり、Development では Scalar の対話型ドキュメントを使えます。

デモが壊れる瞬間のための設計

  • 購入者は 2 人、最後の 1 点。 1 つのステートメントが行をロックし、数量を確認して在庫を取ります。同じ瞬間に送った 2 件の注文は、1 件が 201 Created、もう 1 件が 409 Conflict になりました。
  • 再試行で変更は繰り返されません。 注文と在庫の変更は Idempotency-Key を受け付けます。同じ注文を 2 回送っても同じ注文が返り、在庫は 1 回だけ取られました。
  • リクエストが殺到してもプロセスは止まりません。 MySql.Data の既定のプールでは、起動直後の API への 150 件の同時リクエストで、26 回中 17 回プロセス全体が終了しました。この API は接続プールが許す数だけリクエストを同時に実行し、同じ負荷で 8 回中 8 回とも問題なく完了しました。同時に送った 600 件の注文はすべて 5.5 秒で作成され、在庫も正確でした。
  • 編集が互いを上書きすることはありません。 古いバージョンの商品に基づく変更は 409 になり、注文を出荷済みまたはキャンセルにできるのは 1 回だけです。
  • MySQL が停止しているときは明確に応答します。 クライアントはスタック トレースなしで Retry-After 付きの 503 を受け取り、MySQL が戻れば API はそのまま動き続けます。

丁寧に作られた MySQL 接続

  • 暗号化。 セットアップでは SslMode=Required を使い、README で VerifyFull への移行方法を説明しています。Development 以外では、サーバーを検証していない場合に起動時に警告します。
  • 最小権限。 行の読み取りと変更だけができる API 用のアカウントと、マイグレーション用のアカウントを分けています。
  • サーバー側の時間制限。 既定では、ステートメントが行ロックを待つのは最長 5 秒、SELECT の実行は最長 30 秒です。MySql.Data の非同期呼び出しは CommandTimeout で止まらないため、サーバー側で制限しています。
  • 機能する再試行。 Oracle のプロバイダーは、一覧にない MySQL エラーを一時的なものとして扱いません。このコードはそれらを列挙し、再試行のたびにクリーンな接続から始めます。
  • マイグレーションは独立した手順。 5 つの EF Core マイグレーションが 5 つのテーブルを作成し、アプリが自動でマイグレーションを行うことはありません。

拡張しやすいコード

  • 明快な構成。 機能ごとにフォルダーを分けた Minimal API で、API とデータのプロジェクトを分けたソリューションです。
  • 厳格なビルド。 警告はエラーとして扱い、推奨アナライザーを有効にし、すべてのパッケージのバージョンを 1 つのファイルで管理しています。
  • 一貫したエラーと安全なログ。 すべてのエラーは problem details 形式で返し、Development 以外のログは JSON で、パスワードがログに出ることはありません。
  • シークレットはコードの外に。 ローカルの MySQL には .env、開発中は user-secrets、それ以外では環境変数を使います。

含まれないもの

API のコードだけなので、何を追加すればよいかがはっきりしています。

  • UI はありません。 JSON で応答します。
  • サインインはありません。 独自の認証やゲートウェイを前段に置いてください。
  • テスト プロジェクトはありません。 上記の結果は MySQL で実行して得たもので、アーカイブに含まれる自動テストによるものではありません。
  • ホスティングは含まれません。 デプロイ方法はお任せします。コードが実行環境に求めることは README にまとめています。

6 つの手順で実行

  1. ローカルのパスワードを作成します。
  2. Docker Compose で MySQL を起動します。
  3. API とマイグレーションにそれぞれのアカウントを用意します。
  4. テーブルを作成します。
  5. 接続文字列を保存します。
  6. API を起動し、ドキュメントを開きます。

README には macOS と Linux、PowerShell 用のすべてのコマンドがあります。How to Set Up and Run the Inventory and Orders API (新しいタブで開きます)(英語)では、各手順と表示される内容を紹介しています。macOS 上のクリーンなコピーから、zsh と PowerShell 7 でテストしました。

作り方を学ぶ

全 5 回の無料チュートリアル シリーズ How to Connect a .NET C# Web API to a MySQL Database (新しいタブで開きます)(英語)で、接続、マイグレーション、データの読み書き、トランザクションとロック、このコードの背景にある本番チェックリストを解説しています。

無料チュートリアルシリーズ

How to Connect a .NET C# Web API to a MySQL Database

作りながら学びたい方へ。ブログの無料ステップガイドで、このプロジェクトの作り方を順を追って紹介しています。

シリーズを読む (新しいタブで開きます)

手に入るもの

アーカイブの中身が一目でわかります。

  • 完全なソース コードのバージョン 1.0.0: API プロジェクトとデータ プロジェクトからなる .NET 10 ソリューション
  • カテゴリ、商品、在庫、注文の 20 のエンドポイントと、liveness と readiness のヘルス チェック
  • 5 つの MySQL テーブルを作成する 5 つの EF Core マイグレーション
  • 最後の 1 点を二重に売らない注文処理と、注文と在庫の変更での Idempotency-Key 対応
  • 接続プールに合わせた同時実行リクエストの上限、クライアントごとの上限、リクエスト サイズの上限
  • Oracle の MySQL プロバイダー向けに作られた再試行戦略とサーバー側の時間制限
  • 開発用に MySQL 9.7 を実行する compose.yaml と .env.example
  • すべてのエンドポイントのリクエストを収めた .http ファイルと、Development での Scalar ドキュメント
  • macOS、Linux、PowerShell のセットアップ コマンド、すべての設定、本番前に行うことをまとめた README

使用している技術

開発と動作確認に使った実行環境とバージョンです。

  • .NET SDK10.0.403
  • C#14
  • ASP.NET Core10.0.14
  • EF Core10.0.14
  • MySql.EntityFrameworkCore10.0.12
  • MySql.Data26.10.0
  • Scalar.AspNetCore2.19.0
  • dotnet-ef10.0.14
  • MySQL9.7.4
  • Docker Compose5.6.1

アーカイブの中身

展開したあとに手元に残るファイル構成です。

inventory-orders-api-dotnet-csharp-mysql/

  • src/
  • .editorconfig
  • .env.example
  • .gitattributes
  • .gitignore
  • compose.yaml
  • Directory.Build.props
  • Directory.Packages.props
  • dotnet-tools.json
  • global.json
  • README.md

必要になるもの

動かすために必要なアカウントやサービスです。あとから驚くことはありません。

  • .NET 10 SDK
  • ローカルの MySQL 用の Docker Compose (Docker Desktop またはその他のインストール)
  • 本番用の MySQL サーバー: Docker 上の MySQL 9.7 LTS でテストしており、マネージド サービスではテストしていません
  • API は公開状態のため、信頼できないものに公開する前に独自のサインインまたはゲートウェイ
  • プロセスが終了したときに再起動するホスティング: TLS ハンドシェイクが止まると、MySql.Data はどのコードでも捕捉できない例外を発生させます
  • 任意: VS Code または Visual Studio

リリース

これまでのすべてのバージョンを新しい順に並べています。

  1. 1.0.0最新

    完全なソース コードの最初のリリースです。

    • ASP.NET Core と MySQL による在庫・注文 API で、カテゴリ、商品、在庫、注文の 20 のエンドポイントがあります。
    • 2 人の購入者が最後の 1 点を同時に買うことはできず、同じ Idempotency-Key で繰り返したリクエストが変更を繰り返すこともありません。
    • リクエストが殺到しても、データベース接続を溢れさせずに順番を待ちます。
    • 最小権限の MySQL アカウント、暗号化された接続、EF Core マイグレーション、アーカイブから API の実行までを案内する README を含みます。

Source Code Licence

利用できる範囲

What you may and may not do with the source code you buy.

ライセンスの全文を読む
  1. What this licence covers

This licence governs every file included in a product you buy from Dewiride Store: source code, configuration, database scripts, sample data, documentation and any other file in the download. It takes effect when your payment is confirmed and applies to every release of that product that reaches your library. Files that reach your library through an annual updates membership are governed in exactly the same way.

It forms part of the Terms of Sale and Use. Where a product page states different or additional licence terms for that product, those terms prevail for that product. The membership page may likewise state different or additional licence terms for the files a membership carries, and those terms then prevail for those files.

  1. The licence we grant you

We grant you a perpetual, worldwide, non-exclusive, non-transferable licence to use, reproduce, adapt, translate and modify the files, and to include the result in your own work, on the conditions set out below.

Perpetual means it does not expire. Worldwide means it is not limited to any country. Non-exclusive means we license the same product to other customers. Non-transferable means you cannot pass it on to someone else.

The licence is granted to you — either you personally, or the organisation you identified when buying. Everyone working within that organisation may use the files for that organisation’s projects. Nobody outside it may.

  1. What you may do
  • Use and change the files in your own projects, commercial or otherwise, with no limit on the number of projects.
  • Build a product or a service on them and charge your own customers for it.
  • Deploy the result for a client, as part of work you are doing for that client.
  • Distribute the result in compiled, deployed or otherwise non-source form.
  • Keep as many back-up copies as you reasonably need.
  • Study, observe and test how the code works, and obtain the information you need to make it work with your own software.

The last two are yours by law under sections 52(1)(aa) to (ac) of the Indian Copyright Act, 1957, and nothing in this licence is intended to take them away.

  1. What you may not do
  • Resell, sublicense, redistribute or publish the files themselves, in whole or in substantial part, in source form.
  • Include them in a template, starter kit, course, tutorial repository or component library that you sell or give away, where the code is the thing being supplied.
  • Share your account, your download links or your invoices with anyone outside your organisation.
  • Publish the files in a public repository, or in any other place where they can be obtained without buying them.
  • Remove or obscure the copyright and attribution notices in the source files.
  • Use the files to build something whose main purpose is to compete with Dewiride Store as a supplier of the same code.

The distinction that matters is simple: you may sell what you build with the code; you may not sell the code.

  1. Third-party components

Our products depend on open-source libraries, frameworks and services that we did not write. Those remain under their own licences, which we do not and cannot change. Where a product includes or requires such a component, its licence is named in the product’s documentation, and complying with it is your responsibility.

  1. Releases

Every release of a product you own is covered by this same licence and appears in your library at no extra cost. A release you have already downloaded remains licensed to you even if we later stop publishing the product. A release that reached your library through an annual updates membership is licensed to you on these same terms, and stays licensed after the period that carried it has ended.

  1. Ownership

We keep ownership of the files and of the copyright in them. You own whatever you create with them: your changes, your additions and the product you build are yours.

  1. If this licence ends

This licence ends if you materially breach it and do not put the breach right within thirty days of us asking you to in writing. It also ends if your payment is reversed or refunded, because the purchase it depends on no longer stands. Where a membership period is refunded or reversed, the licence ends for the releases that period carried and for nothing else: anything you bought separately, or that another period carried, is unaffected.

If it ends, you must stop using the files and delete the copies you hold. Work you had already delivered to a client before that point may continue in use, so that ending this licence never breaks something your own customer relies on.

  1. No warranty beyond the description

The warranties and limits in section 19 and section 20 of the Terms of Sale and Use apply to this licence as well.

  1. Governing law

This licence is governed by the law of India, and section 24 of the Terms of Sale and Use applies to any dispute about it. If a translation and the English version differ in meaning, the English version governs.