Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Minimal API と DI の良い組み合わせ
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Yuta Matsumura
November 28, 2022
Technology
2.9k
1
Share
Minimal API と DI の良い組み合わせ
.NET Conf Recap Fukuoka での登壇資料です。
https://msdevjp.connpass.com/event/264482/
Yuta Matsumura
November 28, 2022
More Decks by Yuta Matsumura
See All by Yuta Matsumura
エージェントスキルを作って自分のインプットに役立てよう
tsubakimoto_s
0
350
やさしいとこから始めるGitHubリポジトリのセキュリティ
tsubakimoto_s
3
2.3k
GitHub Copilot CLI で Azure Portal to Bicep
tsubakimoto_s
0
360
使って学ぼう MCP (と GitHub Codespaces)
tsubakimoto_s
1
280
GitHub Copilot CLI を使いやすくしよう
tsubakimoto_s
1
350
AspireとGitHub Modelsで作るお手軽AIアプリ
tsubakimoto_s
0
94
Memories of GitHub Universe & San Francisco
tsubakimoto_s
0
72
私のMCPの使い方
tsubakimoto_s
0
190
これからはじめる Model Context Protocol
tsubakimoto_s
0
92
Other Decks in Technology
See All in Technology
マルチプロダクトの信頼性を効率良く保っていくために
kworkdev
PRO
0
160
Introduction to Sansan for Engineers / エンジニア向け会社紹介
sansan33
PRO
6
74k
EBS暗号化に失敗してEC2が動かなくなった話
hamaguchimmm
2
200
AIでAIをテストする - 音声AIエージェントの品質保証戦略
morix1500
1
120
自分のハンドルは自分で握れ! ― 自分のケイパビリティを増やし、メンバーのケイパビリティ獲得を支援する ― / Take the wheel yourself
takaking22
1
910
目的ファーストのハーネス設計 ~ハーネスの変更容易性を高めるための優先順位~
gotalab555
8
2.2k
ARIA Notifyについて
ryokatsuse
1
120
Standards et agents IA : un tour d’horizon de MCP, A2A, ADK et plus encore
glaforge
0
170
Data Hubグループ 紹介資料
sansan33
PRO
0
2.9k
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
3.1k
最初の一歩を踏み出せなかった私が、誰かの背中を押したいと思うようになるまで / give someone a push
mii3king
0
160
The Journey of Box Building
tagomoris
4
2.6k
Featured
See All Featured
From π to Pie charts
rasagy
0
160
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
150
[SF Ruby Conf 2025] Rails X
palkan
2
960
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
260
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
210
The Mindset for Success: Future Career Progression
greggifford
PRO
0
310
A designer walks into a library…
pauljervisheath
211
24k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
1.9k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
350
Code Reviewing Like a Champion
maltzj
528
40k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.8k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.8k
Transcript
#dotnetconf Minimal API と DI の良い組み合わせ 2022/11/25 .NET Conf Recap
Fukuoka Yuta Matsumura
#dotnetconf 松村 優大 (Yuta Matsumura) Chief Technical Architect (C#, PHP,
Azure) Microsoft MVP (Developer Technologies) #fukuten #devblogradio の中の人 @tsubakimoto_s Currently working for
#dotnetconf 本日お話しすること 1. ASP.NET Core Minimal API とは 2. これまでの
Web API 開発との違い 3. これから始める Minimal API 4. Minimal API x Dependency Injection 5. .NET 7 の Minimal API の新機能
#dotnetconf ASP.NET Core Minimal API とは • ASP.NET Core 6.0
で登場した機能 • 依存関係が最小限の Web API を作成 • ルーティングで構成するエンドポイント定義
#dotnetconf ASP.NET Core Minimal API とは • ASP.NET Core 6.0
で登場した機能 • 依存関係が最小限の Web API を作成 • ルーティングで構成するエンドポイント定義
#dotnetconf 最小限の依存関係 前提 (.NET 6.0 ~ / ASP.NET Core 6.0
~) 最小化されたホスティングモデル • Startup.cs が Program.cs に統合された • トップレベルステートメント • global using により using の記述量が減る
#dotnetconf #dotnetconf Program.cs
#dotnetconf #dotnetconf $ dotnet run info: Microsoft.Hosting.Lifetime[14] Now listening on:
http://localhost:5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production info: Microsoft.Hosting.Lifetime[0] Content root path: C:¥Users¥yuta¥...¥MinimalConsole
#dotnetconf ルーティングで構成するエンドポイント定義 HTTP メソッド エンドポイント定義 HTTP GET MapGet HTTP POST
MapPost HTTP PUT MapPut HTTP DELETE MapDelete など https://learn.microsoft.com/ja-jp/aspnet/core/fundamentals/routing
#dotnetconf #dotnetconf Program.cs
#dotnetconf これまでの Web API 開発との違い • MVC の構成に合わせて実装する必要あり • Controller
クラス (ControllerBase クラス 要継承) • ApiController 属性 : API 用のコントローラーである目印 • Route 属性 : ルーティング構成 リソース数が増えると必要な Controller も増える https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api#differences-between-minimal-apis-and-apis-with-controllers
#dotnetconf これから始める Minimal API
#dotnetconf Swagger を構成しましょう .NET では Swashbuckle というライブラリを用いることで、 アプリケーションに Swagger を導入することができます。
https://github.com/domaindrivendev/Swashbuckle.AspNetCore
#dotnetconf ルーティングで構成するエンドポイント定義 HTTP メソッド エンドポイント定義 HTTP GET MapGet HTTP POST
MapPost HTTP PUT MapPut HTTP DELETE MapDelete など https://learn.microsoft.com/ja-jp/aspnet/core/fundamentals/routing
#dotnetconf #dotnetconf
#dotnetconf #dotnetconf Minimal API (Program.cs) DB (Usersテーブル) DbContext
#dotnetconf #dotnetconf データアクセスは 依存関係の解決 (DI) を介する
#dotnetconf #dotnetconf
#dotnetconf #dotnetconf ▼グルーピング可能
#dotnetconf #dotnetconf
#dotnetconf #dotnetconf
#dotnetconf #dotnetconf
#dotnetconf #dotnetconf パラメーターの取得元を 明示したい場合
#dotnetconf #dotnetconf API認証
#dotnetconf Minimal API x Dependency Injection
#dotnetconf “Fat Program.cs” 実際の API では様々なビジネスロジックが登場します。 しかし、これらを Program.cs に書くと複雑になります。 •
パラメーターのバリデーション • ログ出力 • データストアの CRUD • 他サービスとの連携 • HTTP レスポンスの構成
#dotnetconf Dependency Injection の活用 ビジネスロジック部分は専用のクラスで管理しましょう。 ※単一責任の原則 (SRP) ASP.NET Core では
Dependency Injection の仕組みがサポー トされているため、インターフェースで依存関係を構成する ことで、結果的に「テストしやすいAPI」となります。 https://learn.microsoft.com/ja-jp/aspnet/core/fundamentals/dependency-injection
#dotnetconf #dotnetconf ▶
#dotnetconf #dotnetconf
#dotnetconf .NET 7 の Minimal API の新機能 https://learn.microsoft.com/ja-jp/aspnet/core/release-notes/aspnetcore-7.0
#dotnetconf Rate Limit Middleware レート制限(時間内のリクエスト試行回数の制限)を ASP.NET Core 単体で構成することが可能となった。 レート制限に達した HTTP
リクエストに対しては HTTP 429 (Too Many Requests) のレスポンスを行う。
#dotnetconf Minimal API エンドポイントのフィルター エンドポイント前後で処理を 実行することが可能となった。
#dotnetconf 配列と文字列値のバインド ヘッダーやクエリ文字列に指定された値を配列にバインドす ることが可能となった。
#dotnetconf AsParametersAttribute 引数のバインドをクラスに置き換え可能となった。 ▼
#dotnetconf OpenAPI の機能強化 Microsoft.AspNetCore.OpenApi (nuget) を使用すると、 Minimal API のエンドポイントにて OpenAPI
定義を設定する ことが可能となった。
#dotnetconf ルートグループ リソース単位で API グルーピングする機能。 グループレベルで認証構成などが可能。
#dotnetconf おまけ 開発をラクにするちょっとしたツールたち
#dotnetconf dotnet-httprepl コマンドラインで使用する Web API クライアント。
#dotnetconf dotnet user-jwts 開発環境で使用する JWT (JSON Web Token) を発行するため のコマンドラインツール。
JWT に関する情報はユーザーシークレットに保管される。
#dotnetconf Appendix • .NET Conf での Minimal API セッション •
https://www.youtube.com/watch?v=uoVeZzKl6WQ (2021) • https://www.youtube.com/watch?v=HXHwtEjQoyM (2022) • Minimal API サンプルコード • https://github.com/tsubakimoto/dotnetconf-minimal-api-sample • https://github.com/captainsafia/TrainingApi • ドキュメント • https://learn.microsoft.com/ja-jp/aspnet/core/fundamentals/minimal-apis • https://learn.microsoft.com/ja-jp/aspnet/core/release-notes/aspnetcore-7.0 • https://learn.microsoft.com/ja-jp/aspnet/core/web-api/http-repl/ • https://learn.microsoft.com/ja-jp/aspnet/core/security/authentication/jwt-authn
#dotnetconf ご清聴ありがとうございました