Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
いまさらだけど ASP.NET MVC のアプリケーション アーキテクチャをおさらいする / ...
Search
Takashi Shinohara
May 22, 2018
Programming
4.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
いまさらだけど ASP.NET MVC のアプリケーション アーキテクチャをおさらいする / Reconsideration for application architecture of ASP.NET MVC
Takashi Shinohara
May 22, 2018
More Decks by Takashi Shinohara
See All by Takashi Shinohara
Microsoft Build 2026 振り返り会 / Recap Microsoft Build 2026
karamem0
0
150
Microsoft 365 Copilot API を試してみる / Trying out Microsoft 365 Copilot APIs
karamem0
0
190
Microsoft 365 の認証と承認を理解する / Understanding Microsoft 365 Authentication and Authorization
karamem0
0
260
Power Automate のパフォーマンス改善レシピ / Power Automate Performance Improvement Recipes
karamem0
0
570
どっちの API SHOW?SharePoint 開発における SharePoint REST API Microsoft Graph API の違い / Which API show? Differences between Microsoft Graph API and SharePoint REST API
karamem0
0
2.5k
Microsoft Ignite 2024 最新情報!Microsoft 365 Agents SDK 概要 / Microsoft Ignite 2024 latest news Microsoft 365 Agents SDK overview
karamem0
0
650
徹底解説!Microsoft 365 Copilot の拡張機能 / Complete guide to Microsoft 365 Copilot extensions
karamem0
1
4.8k
Microsoft Search の Graph コネクタ ハンズオン / Handson for Graph Connector of Microsoft Search
karamem0
0
380
いまだから「検索」を語ろう! SharePoint FAST Search から Microsoft Search セマンティックインデックスまで / Let's talk about search now
karamem0
4
2k
Other Decks in Programming
See All in Programming
AI に Inclusive UI を書かせよう — Design Rules Skill で Compose UI を作り直す
theoriatec2024
1
490
選挙速報を多くのユーザーへ 届ける Live Activities 設計
hamayokokuririn
0
140
Webの地図
yosuke_furukawa
PRO
6
4.5k
スマート反転とウェブアクセシビリティ
camiha
0
210
LoopHub - ローカルで動く GitHub で、AI と共同開発
jugyo
1
550
MVNOの申込からeSIM開通までをiOSアプリでつなぐ- 本人確認・MNP・通信事業者基盤をまたぐ実装
satotakeshi
0
420
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
1
150
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
1.2k
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
3
550
wkhtmltopdfの次どうするか問題2026
willnet
1
690
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
260
ゲームコントローラやキーボードのファームウェアをSwiftで書く
kishikawakatsumi
1
240
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
77
5.5k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
450
Paper Plane (Part 1)
katiecoart
PRO
2
11k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
500
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
260
The Pragmatic Product Professional
lauravandoore
37
7.4k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
660
Joys of Absence: A Defence of Solitary Play
codingconduct
1
520
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Building an army of robots
kneath
307
46k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
420
Transcript
いまさらだけど ASP.NET MVC の アプリケーション アーキテクチャを おさらいする 篠原敬志 (@karamem0)
自己紹介 • 篠原敬志 (Takashi Shinohara) • Microsoft MVP for Office
Development (2018-) • Twitter: @karamem0 • Blog: からめもぶろぐ。(http://blog.karamem0.jp) 2
なんでいまさら • ASP.NET MVC 1.0 のリリースは 2009 年 3 月
• ASP.NET MVC はすでに枯れた技術のはず • なのになんで私はこんなスパゲティ コードと戦ってるんだろう? 3
ということは • ASP.NET MVC でのアプリケーション アーキテクチャってあまり議論されていない? • Ruby on Rails
界隈ではめっちゃ議論されてる • 過去の記事を鵜呑みにしたままなんとなく今日までやってきてない? • 2009~2010 年頃の記事が普通に検索結果のトップに出てくる • 小手先の技術ばかり見ていて全体を掴めてなくない? • それはそれで大事ではあるけれども 4
(本当にいまさらだけど) ASP.NET MVC の アプリケーション アーキテクチャを おさらいしよう 5
ASP.NET MVC の基本 6 • View はユーザー インターフェイスを提供 する •
Controller はユーザーからの入力を処 理し、Model を操作し、View をレン ダリングする • Model はビジネス ロジックを提供する Model Controller View
ASP.NET MVC のアプリケーション レイヤー 7 ユーザー プレゼンテーション レイヤー UI コンポーネント
プレゼンテーション ロジック コンポーネント サービス レイヤー サービス インターフェイス メッセージの種類 ビジネス レイヤー アプリケーション ファサード ビジネス ワークフロー ビジネス コンポーネント ビジネス エンティティ データ レイヤー データ アクセス コンポーネント データ ヘルパー/ ユーティリティ サービス エージェント セキュリティ 運用管理 通信 分野横断的な懸念事項 外部システム データ ソース サービス View Controller Model
Skinny Controller, Fat Model • 本当に大事なのは Model • Controller は
View と Model の橋渡しに過ぎない • Model は複数のレイヤーをまたがる • Model 全体としては Fat だが特定のクラス が Fat になることはない 8
ASP.NET MVC における Model • Model ≠ Entity Model •
スキャフォールディングはアンチ パターン • View と Entity Model が直接結びつくことはありえない • Controller にビジネス ロジックが混在する原因に • ステートレス • トランザクション スクリプト 9
ASP.NET MVC の Model をさらに分解する 10 View Controller Model Repository
View View Model Controller Domain Model Entity Model Service Facade プレゼンテー ション レイヤー サービス レイヤー サービス インターフェイス メッセージの種類 ビジネス レイヤー アプリケーション ファサード ビジネス ワークフロー ビジネス コンポーネント ビジネス エンティティ データ レイヤー データ アクセス コンポーネント データ ヘルパー/ ユーティリティ サービス エージェント UI コンポーネント プレゼンテーション ロジック コンポーネント
Entity Model • 外部データの Entity を表現する • 外部データが RDBMS の場合は
Entity Framework • 外部キーを付ける • 主キーが存在しないテーブルを作成しない • 基本はサロゲート キー (ナチュラル キーも使える) • rowversion 列を付ける • 外部データには Web API や DocumentDB なども 考えられる 11 View Controller Model Repository View View Model Controller Domain Model Entity Model Service Facade
Repository • 外部データへのデータ アクセス操作 (CRUD 操作) を提供する • RDBMS (Entity
Framework) の場合は DbContext クラス • 外部データが RDBMS 以外の場合は自作する • コレクションの場合は IQueryable<T> を返す 12 View Controller Model Repository View View Model Controller Domain Model Entity Model Service Facade
Domain Model • ビジネス ロジックでの Entity を表現する • Entity Model
と 1 対 1 にはならない • RDBMS から変換されるデータであれば SQL の SELECT の結果になる • データは状況によって非正規化される • 不必要なデータは含まれない • データの読み込みと書き込みが同じ Model になる とは限らない (CQRS) 13 View Controller Model Repository View View Model Controller Domain Model Entity Model Service Facade
Service • Entity Model と Domain Model との変換を提供 する •
LINQ to Entities によるクエリ操作を隠蔽する • コレクションの場合は IEnumerable<T> を返す • トランザクションの単位 14 View Controller Model Repository View View Model Controller Domain Model Entity Model Service Facade
View Model • View のレンダリングに必要なデータを格納する構造 化された Entity • 検証のための属性を定義する •
シリアライズ化可能 • ViewBag や ViewData は極力使用しない 15 View Controller Model Repository View View Model Controller Domain Model Entity Model Service Facade
Facade • Controller からの呼び出しの入り口で、1 つ以上の Service を呼び出し ViewModel を返す •
Controller と 1 対 1 で作成する • ビジネス ロジックは含まない 16 View Controller Model Repository View View Model Controller Domain Model Entity Model Service Facade
Controller • インプット データの検証 • Facade の呼び出し • エラー ハンドリング
• Action 以外のメソッドが存在したら負け 17 View Controller Model Repository View View Model Controller Domain Model Entity Model Service Facade
View • ViewModel のバインドと HTML のレンダリング • Razor によるマークアップ •
レイアウト • タグ ヘルパー • 部分ビュー 18 View Controller Model Repository View View Model Controller Domain Model Entity Model Service Facade
ASP.NET MVC の単体テスト 19
単体テストの実施 • それぞれのレイヤーは疎結合になるので単体テストの実施が容易になる • 単体テストを実施するためにレイヤーを分割するのではない • すべてのレイヤーをテストする必要はない • コード カバレッジ
100% を目指さない 20
単体テストのフレームワーク • 依存関係の注入 (Dependency Injection) • Unity (開発終了?) • Autofac
• Grace • Ninject • モック (Mock) • Fakes (Visual Studio Enterprise のみ) • Moq 21
単体テストのスコープ • Service のテストで Repository をモックするのはアンチ パターン • IQueryable<T> ≠
IEnumerable<T> • 発行される SQL が正しいことを確認する (Database.Log プロパティ) • SQL LocalDB + Code First を使うと便利 • Controller のテストでは Service をモックする • 外部データにはアクセスしない • HttpContext もモックしないと駄目だったりする 22
まとめ 23
まとめ • MVC だからといってこれまでのアプリケーション アーキテクチャが大幅に変わるわけ ではない • 本当に大事なのは Model (2
回目) • ASP.NET MVC は Ruby on Rails に影響を受けているので Ruby on Rails でさ んざん議論されてきたことはだいたい ASP.NET MVC にも当てはまる 24
参考リンク • ASP.NET MVC の概要 https://msdn.microsoft.com/ja-jp/library/dd381412.aspx • アプリケーション アーキテクチャ ガイド
2.0 https://msdn.microsoft.com/ja-jp/library/gg998968.aspx • コマンド クエリ責務分離 (CQRS) パターン https://docs.microsoft.com/ja-jp/azure/architecture/patterns/cqrs 25