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
client-side-Blazor で MVVM してみた
Search
ありとみ
July 30, 2019
Technology
0
430
client-side-Blazor で MVVM してみた
Blazor で 超単純な MVVM ハンズオン
ありとみ
July 30, 2019
Tweet
Share
Other Decks in Technology
See All in Technology
米国国防総省のDevSecOpsライフサイクルをAWSのセキュリティサービスとOSSで実現
syoshie
2
1.2k
AI導入の理想と現実~コストと浸透〜
oprstchn
0
140
250627 関西Ruby会議08 前夜祭 RejectKaigi「DJ on Ruby Ver.0.1」
msykd
PRO
2
360
PHP開発者のためのSOLID原則再入門 #phpcon / PHP Conference Japan 2025
shogogg
4
910
データプラットフォーム技術におけるメダリオンアーキテクチャという考え方/DataPlatformWithMedallionArchitecture
smdmts
5
660
なぜ私はいま、ここにいるのか? #もがく中堅デザイナー #プロダクトデザイナー
bengo4com
0
1.2k
Delegating the chores of authenticating users to Keycloak
ahus1
0
130
2025-06-26_Lightning_Talk_for_Lightning_Talks
_hashimo2
2
110
変化する開発、進化する体系時代に適応するソフトウェアエンジニアの知識と考え方(JaSST'25 Kansai)
mizunori
1
260
AWS テクニカルサポートとエンドカスタマーの中間地点から見えるより良いサポートの活用方法
kazzpapa3
2
570
Connect 100+を支える技術
kanyamaguc
0
110
AIとともに進化するエンジニアリング / Engineering-Evolving-with-AI_final.pdf
lycorptech_jp
PRO
0
140
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
37
3.3k
Statistics for Hackers
jakevdp
799
220k
Code Reviewing Like a Champion
maltzj
524
40k
The Pragmatic Product Professional
lauravandoore
35
6.7k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
48
5.4k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
The Cult of Friendly URLs
andyhume
79
6.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Transcript
client-side-Blazor で MVVM してみた 株式会社スタディスト 有冨 隼
⾃⼰紹介 有冨 隼 / Shun Aritomi 株式会社スタディスト開発部 仕事内容 Windows アプリ開発
(WPF)
話すこと Blazor で 超単純な MVVM ハンズオン View への ViewModel を
DI する⽅法 View でコードビハインド使う⽅法 A simple MVVM implementation in client side Blazor. https://itnext.io/a-simple-mvvm- implementation-in-client-side-blazor- 8c875c365435
話さないこと Blazor の概要 MVVM の概要 MVVM における Model 部分の話
Blazor で MVVM しよう
準備 .NET Core 3.0 のインストール Visual Studio 2019 のインストール Visual
Studio の拡張機能 https://marketplace.visualstudio.com/items? itemName=aspnet.blazor
⽬次 View を作る ViewModelを作る BindableBase を作る ViewModel 本体を作る View と
ViewModel を紐付ける @inject を追加する DI コンテナへ登録する コードビハインド
View を作る input:2つ / output:1つ のシンプルな画⾯ Views/Sample.razor @page "/sample" <h1>Sample
Page</h1> <input /> + <input /> = <input readonly />
こんな⾒た⽬
ViewModel を作る (1) BindableBase クラスを作る INotifyPropertyChanged を実装 SetProperty` メソッドを実装 OnPropertyCanged
メソッドを実装 View <-> ViewModel 間の変更通知を実現する 不要でした
ViewModel を作る (2) BindableBase を継承したクラスを作る 継承不要でした
namespace WebApplication.ViewModels { public class SampleViewModel { public int Value1
{ get; set; } public int Value2 { get; set; } public int Result => Value1 + Value2; } }
View と ViewModel を紐付ける (1) View に @inject キーワードで DI
する input 項⽬は @bind キーワードに @ で値をセット output 項⽬は @ で値をセット @page "/sample" @using WebApplication.ViewModels @inject SampleViewModel ViewModel <h1>Sample Page</h1> <input @
[email protected]
/> + <input @
[email protected]
/> = <input readonly
[email protected]
/>
View と ViewModel を紐付ける (2) @inject で⾃動的に DI してもらうため DI
コンテナへの登録も必要 Startup.cs への追記 public void ConfigureServices(IServiceCollection services) { services.AddTransient<SampleViewModel>(); }
View に inject って書くの 嫌じゃない︖
View に inject って書くの 嫌じゃない︖ →実はコードビハインドで書ける
コードビハインド (1) [Viewファイル名].cs ファイルを作る (Views/Sample.razor.cs) View 側で @ で書いたキーワードは属性で記述する using
Microsoft.AspNetCore.Components; using WebApplication.ViewModels; namespace WebApplication.Views { public class SampleBase : ComponentBase { [Inject] public SampleViewModel ViewModel { get; set; } } }
コードビハインド (2) View へ @inherits キーワードで紐付ける @page "/samples" @inherits SampleBase
<h1>Sample Page</h1> <input @
[email protected]
/> + <input @
[email protected]
/> = <input readonly
[email protected]
/>
最終的にこんな感じになる デモ https://github.com/shuntaro4/SampleBlazor
まとめ Blazor でも MVVM できる Blazor でも コードビハインド的にかける まだ preview
だけど、雰囲気良さそう