0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ASP.net MVVM それぞれの役割

Last updated at Posted at 2025-10-14

MVVM(MVC)

M(Model)

データ項目毎にプロパティを作る。DBとのマッピングがされる。

V(View)

Razorを用いて、Controllerから渡されたModel毎のテンプレートを使って、ユーザーが実際に見るhtmlを作る

C(Controller)

View(ブラウザ)からもらったデータをModelに渡す役割、逆にマッピングなどがされ、整形されたModelを、Viewに渡す役割もある

VM(ViewModel)

実際に、Modelに応じて、Viewテンプレートを作成した場合、場合によってはテンプレートが膨大になってしまう。そこで、ModelはあくまでDBとの型に応じたstringやintなどにして、ViewModelにinputやselectなどのUIに応じたモデルを作成し、Viewにテンプレートも作成する(通常ViewModelの名前とテンプレートの名前は同じだが、UIHintを用いて、別の名前でも使えるようにする)
そのため、ViewとViewModelのマッピングが必要になってくる。
例としてViewModelにValueというプロパティを持たせ、Modelとマッピングを行う

"M""V""C"ではないが、採用されるもの

Service

実際にControllerにビジネスロジックを記述すると、Controllerの責任が大きくなる。そのため、ControllerはViewとModelの橋渡しのみで、ビジネスロジックをServiceに記述するのが一般的

Repository

Serviceから呼ばれ、DB操作を行う。Controllerの責任を減らすために存在する。entityFrameworkなどを用いて、ModelとDBのマッピングを行う。

Areas

大規模なシステムだと、Areasディレクトリを作って、その中にMVVM(MVC)それぞれを置く、Areasを用いる場合、Actionメソッドの引数に記述する必要がある

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?