# ASP .NET MVC Study Note ###### tags: `NTUST-CC` ### 介紹 什麼是MVC?,MVC是一種”設計模式”,不是程式語言也不是一種框架。把應用程式架構區分為三個部分,分別為Model(模型)、View(視圖)和Controller(控制器)。 符合MVC架構的狀態下,1.外部發出一個request請求後,2.routing指定適當的路徑,接著進到controller,3.controller作為views和model的協調處理者,必須給予對方回應。4.控制器調用view,5.在瀏覽器中呈現視圖。model指的是應用邏輯層,負責應用的邏輯運算與方法操作。 整個專案的分層式架構,主要是資料顯示層 -> 資料連接層(商業邏輯) -> 服務層 -> 資料庫層  #### View 在頁面上顯示或是經由頁面傳回後端都是View所負責,View的呈現分為輸出和輸入,輸出:把資料輸出並顯示到使用者介面上,輸入:把使用者所輸入的資料傳回到伺服器端。  #### Controller Controller 主要是負責回應對 ASP .NET MVC 網站提出的要求。 ### 範例 這邊test為這次的範例,原則上一個.cshtml只會對應到一個Controller。 1. View Layer 這邊假設我要做一個填寫姓名的表單,會希望可以在DB上去新增、刪除及搜尋做一些API操作。 2. Model.cs 剛提到要依填寫姓名的表單,就需要姓名`Name`跟英文姓名`NameEn`的參數。 ```javascript= namespace HRVote.Domain { public class test { public string Name { get; set; } public string NameEn { get; set; } } } ``` 3. Controller 此為testController的程式碼,在上方紅框來說屬於結構化流程。 Controller下有Service層。Service 是很常見的設計模式,通常會把商業邏輯寫在 Service 層,才不會讓 Controller 過於笨重。  4. Service Layer 剛剛提到一般直接的寫法是Controller底下寫商業邏輯,以下面的服務(商業邏輯)來說,我要在前面定義的名字參數上做新增、修改、刪除和搜尋名稱的動作。 但假設我別的API也要使用同樣的服務,這樣只能將複製貼上到另一個Controller裡面。  因此為了方便維護系統,將Service從Controller分離出來形成自己Layer  **testService.cs** ```javascript= using AutoMapper; using HRVote.Domain; using HRVote.Repository.Interface; using HRVote.Service.Interface; using System.Collections.Generic; using System.Linq; namespace HRVote.Service { public class testService : ItestService { private readonly ItestRepository testRepository; private readonly IMapper mapper; public testService(ItestRepository testRepository, IMapper mapper) { this.testRepository = testRepository; this.mapper = mapper; } public void Add(string index) { throw new System.NotImplementedException(); } public void Delete(string index) { throw new System.NotImplementedException(); } public string GetSutdentInfo(string studentid) { var reslut = testRepository.Where(item=>item.Name== studentid).FirstOrDefault(); var aa = testRepository.Where(item=>item.NameEn== studentid).FirstOrDefault(); var customersResponse = mapper.Map<StudentInfo>(aa); return reslut.NameEn; } public void Put(string index) { throw new System.NotImplementedException(); } public List<test> Search(string index) { throw new System.NotImplementedException(); } } } ``` 5. Repository Layer 在Controller直接去使用ApplicationDbContext對資料庫進行處理不是好的做法,所以要把這種方式做改變,不要直接在Controller中直接對ApplicationDbContext進行資料操作,這裡有一個重要的觀念是”關注點分離(SOC)”。以下對”關注點分離”稍微解釋一下,資料操作就是一種關注點,而資料驗證、流程控制都是關注點,當把這些關注點都放在一起的話,會覺得很混亂,所以我們可以把對資料庫做操作的部分給抽離出來,這就叫做”關注點分離”。 把資料庫抽離出來,通常會使用一種設計模式”Repository Pattern”,而這個模式就是用來處理資料操作,資料操作基本上一定有”CRUD”也就是Create(建立)、Read(讀取)、Update(更新)和Delete(刪除),所以我們就去為每個Model類別建立專屬的Repository類別,每個Repository類別只專注於自己所負責的Model的資料操作,不會干涉到其他資料類別的操作。 建立Repository還有一個優點是可重複使用,像是取得某筆資料、取得全部資料,這些資料操作會經常重複使用,可以將這些重複性高的方法建立在Repository,所以如果取得的資料有變動的話,就只需要修改Repository的方法就可以了。 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up