SlideShare a Scribd company logo
1 of 24
Download to read offline
ASP.NET MVC Model 的設計與使用
Kevin Tseng
http://kevintsengtw.blogspot.tw
http://mvc.tw
2013 微軟最有價值專家(ASP.NET)
twMVC 聯合創辦人之一
部落格:mrkt 的程式學習筆記
http://kevintsengtw.blogspot.tw
2
講者簡介
http://mvc.tw
MVC 的三個組成部分
Model
可用什麼來做為 Model
ADO.NET
Enterprise Library –
Data Access Application Block
ADO.NET Entity Framework
分層、IoC/DI
3
Agenda
MVC 的三個組成部分
Model, View, Controller
http://mvc.tw
 「資料模型」用於封裝與應用程序的業務邏輯相關的數據以
及對數據的處理方法
 有關「資料處理」的範圍都可屬於Model,包含:
 資料庫的存取操作(讀取、寫入)
 資料結構的定義
 資料格式的驗證
5
Model
http://mvc.tw
 在頁面上顯示或是經由頁面傳回後端都是View所負責
 輸出:把資料輸出並顯示到使用者介面上
 輸入:把使用者所輸入的資料傳回到伺服器端
 有關View的技術還包含:
 HTML / CSS
 Javascript(jQuery, MoonTool, ExtJS …etc)
 JSON / XML
 AJAX
 RIA(Rich Interactive Application, ex:Flash,Silverlight)
6
View
http://mvc.tw
 控制整個系統的業務流程
 依據制定好的規則進行對資料處理以及 View 的使用
 控制器接受外部傳入的資料並與 Model 進行資料處理,最
後再由 View 回應外部的傳入並做顯示
7
Controller
http://mvc.tw 8
MVC – 三者之間的關係
http://mvc.tw 9
MVC – 三者之間的關係
本圖取自《ASP.NET MVC4 網站開發美學》
Model
http://mvc.tw
 在ASP.NET MVC中Model負責所有與「資料」有關的任務
 不處理所有與資料處理無關的事
 不直接處理來自瀏覽器的輸入,也不向瀏覽器產生 HTML 輸
出。
 ASP.NET MVC 架構對可以建置的各種模型物件沒有任何限
制。
11
Model
http://mvc.tw
 ADO.NET Entity Framework
 NHibernate, SubSonic, LLBLGen Pro … etc
以上都是 ORM Solution
也可以用「非 ORM Solution」,但是會非常累…
ADO.NET
Enterprise Library Data Access Application Block
12
可用哪些來做為 Model
http://mvc.tw
 NHibernate是 Hibernate(Java) 的.NET版。
 目前可以支援使用的Database有:
 MS SQL Server
 Oracle
 MySQL
 DB2
 Sybase
 SQLite … etc
13
可用哪些來做為 Model
http://mvc.tw
 使用傳統的 ADO.NET 資料存取方式
 示範操作
14
Model – ADO.NET
http://mvc.tw
 Enterprise Library –
Data Access Application Block
 Enterprise Library 6 – Released 2013/4/26
 .NET 4.5 使用 Enterprise Library 6
.NET 4.0 (3.5) 使用 Enterprise Library 5
 示範操作
15
ADO.NET – EntLib DAAB
http://mvc.tw
 對資料庫的存取操作使用 ADO.NET Entity Framework
 Database First
 示範操作
16
Model – ADO.NET Entity Framework
http://mvc.tw 17
ASP.NET MVC 專案的 Model – 同專案中
http://mvc.tw 18
ASP.NET MVC 專案的 Model – 不同專案
http://mvc.tw
 專案分層架構
 ADO.NET, EntLib DAAB, EF 三合一
 示範操作
19
Model – 分層
http://mvc.tw
 使用介面
「程式的內容是針對介面而寫,而不是針對實作而寫」
 使用 IoC/DI
 IoC/DI Solution:Simple Injector
20
Model – 切換不同的資料存取方式
http://mvc.tw
 DI:Dependency Injection 依賴注入.
 IoC:Inversion of Control 控制反轉.
 控制反轉又稱為依賴注入,在物件導向設計中,一個用來降低物
件之間耦合性的設計原則.
 常見的 IoC/DI Solution:
EntLib Unity, Autofac, Simple Injector, Castle
Windsor, StructureMap, Ninject, Spring.NET,
 IoC Container Benchmark - Performance comparison
21
IoC/DI
http://mvc.tw
 資料庫在 MS SQL Server 與 Oracle 切換
 ADO.NET Entity Framework, ADO.NET, EntLib DAAB
 介面與 IoC/DI
 示範操作
22
Model – 切換資料庫與資料存取方式
聯絡資訊
Blog: http://kevintsengtw.blogspot.tw
G+: http://gplus.to/mrkt_note
twMVC: http://mvc.tw
FB: http://fb.me/twmvc
謝謝各位
• 本投影片所包含的商標與文字皆屬原著作者所有。
• 本投影片使用的圖片皆從網路搜尋。
• 本著作係採用 Creative Commons 姓名標示-非商業性-相同方式分享 3.0 台灣 (中華民國) 授權條款授權。
h t t p : / / m v c . t w

More Related Content

What's hot

What's hot (20)

twMVC#23 | 快速上手 Azure Functions
twMVC#23 | 快速上手 Azure FunctionstwMVC#23 | 快速上手 Azure Functions
twMVC#23 | 快速上手 Azure Functions
 
twMVC#21 | 你所不知道的 Visual Studio
twMVC#21 | 你所不知道的 Visual StudiotwMVC#21 | 你所不知道的 Visual Studio
twMVC#21 | 你所不知道的 Visual Studio
 
twMVC#24 | 開發團隊的敏捷之路(未完成)
twMVC#24 | 開發團隊的敏捷之路(未完成)twMVC#24 | 開發團隊的敏捷之路(未完成)
twMVC#24 | 開發團隊的敏捷之路(未完成)
 
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
 
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
 
如何在實務上使用TDD來開發 twmvc#12
如何在實務上使用TDD來開發 twmvc#12如何在實務上使用TDD來開發 twmvc#12
如何在實務上使用TDD來開發 twmvc#12
 
twMVC#25 | ASP.NET MVC A/B Testing 的眉眉角角
twMVC#25 | ASP.NET MVC A/B Testing 的眉眉角角twMVC#25 | ASP.NET MVC A/B Testing 的眉眉角角
twMVC#25 | ASP.NET MVC A/B Testing 的眉眉角角
 
twMVC#22 | 一個微信專案從0到.000的效能調教之路
twMVC#22 | 一個微信專案從0到.000的效能調教之路twMVC#22 | 一個微信專案從0到.000的效能調教之路
twMVC#22 | 一個微信專案從0到.000的效能調教之路
 
twMVC#29 | 當.Net Core 遇到AWS Lambda
twMVC#29 | 當.Net Core 遇到AWS LambdatwMVC#29 | 當.Net Core 遇到AWS Lambda
twMVC#29 | 當.Net Core 遇到AWS Lambda
 
開發的效能與效率-twMVC#15
開發的效能與效率-twMVC#15開發的效能與效率-twMVC#15
開發的效能與效率-twMVC#15
 
twMVC#28 | visual studio 2017 新功能介紹
twMVC#28 | visual studio 2017 新功能介紹twMVC#28 | visual studio 2017 新功能介紹
twMVC#28 | visual studio 2017 新功能介紹
 
twMVC#19 | opserver監控服務的解決
twMVC#19 | opserver監控服務的解決twMVC#19 | opserver監控服務的解決
twMVC#19 | opserver監控服務的解決
 
輕鬆上手Asp.net web api 2.1-twMVC#14
輕鬆上手Asp.net web api 2.1-twMVC#14輕鬆上手Asp.net web api 2.1-twMVC#14
輕鬆上手Asp.net web api 2.1-twMVC#14
 
twMVC#22 | 什麼鬼的IOC與DI
twMVC#22 | 什麼鬼的IOC與DItwMVC#22 | 什麼鬼的IOC與DI
twMVC#22 | 什麼鬼的IOC與DI
 
Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2
 
twMVC#23 | 一個Mobile App開發、維護與改版的愛恨之路
twMVC#23 | 一個Mobile App開發、維護與改版的愛恨之路twMVC#23 | 一個Mobile App開發、維護與改版的愛恨之路
twMVC#23 | 一個Mobile App開發、維護與改版的愛恨之路
 
一小時可以打造什麼服務Plus twMVC#18
一小時可以打造什麼服務Plus twMVC#18一小時可以打造什麼服務Plus twMVC#18
一小時可以打造什麼服務Plus twMVC#18
 
ASP.NET MVC 善用網路資源快速完打造網站
ASP.NET MVC 善用網路資源快速完打造網站ASP.NET MVC 善用網路資源快速完打造網站
ASP.NET MVC 善用網路資源快速完打造網站
 
twMVC#29 -Learning Machine Learning with Movie Recommendation
twMVC#29 -Learning Machine Learning with Movie RecommendationtwMVC#29 -Learning Machine Learning with Movie Recommendation
twMVC#29 -Learning Machine Learning with Movie Recommendation
 
SignalR實戰技巧 twmvc#17
SignalR實戰技巧 twmvc#17 SignalR實戰技巧 twmvc#17
SignalR實戰技巧 twmvc#17
 

Similar to ASP.NET MVC Model 的設計與使用 twMVC#10

Asp.net+mvc4框架揭秘
Asp.net+mvc4框架揭秘Asp.net+mvc4框架揭秘
Asp.net+mvc4框架揭秘
Zhenhua Tang
 
Mvc 架构
Mvc 架构Mvc 架构
Mvc 架构
cyla993
 
利用 ASP.NET MVC 提升您的 Web 應用程式
利用 ASP.NET MVC 提升您的 Web 應用程式利用 ASP.NET MVC 提升您的 Web 應用程式
利用 ASP.NET MVC 提升您的 Web 應用程式
Chui-Wen Chiu
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
Wade Huang
 

Similar to ASP.NET MVC Model 的設計與使用 twMVC#10 (20)

Asp.net+mvc4框架揭秘
Asp.net+mvc4框架揭秘Asp.net+mvc4框架揭秘
Asp.net+mvc4框架揭秘
 
KSDG-ASP.NET MVC 5 Overview (偽三國誌)
KSDG-ASP.NET MVC 5 Overview (偽三國誌)KSDG-ASP.NET MVC 5 Overview (偽三國誌)
KSDG-ASP.NET MVC 5 Overview (偽三國誌)
 
Mvc 架构
Mvc 架构Mvc 架构
Mvc 架构
 
twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有
 
Asp.Net Mvc 1.0
Asp.Net Mvc 1.0Asp.Net Mvc 1.0
Asp.Net Mvc 1.0
 
利用 ASP.NET MVC 提升您的 Web 應用程式
利用 ASP.NET MVC 提升您的 Web 應用程式利用 ASP.NET MVC 提升您的 Web 應用程式
利用 ASP.NET MVC 提升您的 Web 應用程式
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
 
twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸
 
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
與 Asp.net mvc 的第一次親密接觸 - twMVC#1與 Asp.net mvc 的第一次親密接觸 - twMVC#1
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
 
ASP.NET MVC简介
ASP.NET MVC简介ASP.NET MVC简介
ASP.NET MVC简介
 
ASP.NET MVC Code Templates實戰開發 -twMVC#4
 ASP.NET MVC Code Templates實戰開發 -twMVC#4 ASP.NET MVC Code Templates實戰開發 -twMVC#4
ASP.NET MVC Code Templates實戰開發 -twMVC#4
 
twMVC#04 | ASP.NET MVC - Code Templates實戰開發
twMVC#04 | ASP.NET MVC - Code Templates實戰開發twMVC#04 | ASP.NET MVC - Code Templates實戰開發
twMVC#04 | ASP.NET MVC - Code Templates實戰開發
 
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
 
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
 
Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Single-Page Application Design Principles 101
Single-Page Application Design Principles 101
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
 
Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹
 
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure PlatformtwMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
 

More from twMVC

樞紐分析表之數據分析與統計-twMV#C14
樞紐分析表之數據分析與統計-twMV#C14樞紐分析表之數據分析與統計-twMV#C14
樞紐分析表之數據分析與統計-twMV#C14
twMVC
 
20130823微軟雲端平台開發者日
20130823微軟雲端平台開發者日20130823微軟雲端平台開發者日
20130823微軟雲端平台開發者日
twMVC
 

More from twMVC (18)

雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場
雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場
雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場
 
讓應用程式雲端化 由 Iaa s 邁向 paas 之路-Global Azure Bootcamp 2015 臺北場
讓應用程式雲端化 由 Iaa s 邁向 paas 之路-Global Azure Bootcamp 2015 臺北場讓應用程式雲端化 由 Iaa s 邁向 paas 之路-Global Azure Bootcamp 2015 臺北場
讓應用程式雲端化 由 Iaa s 邁向 paas 之路-Global Azure Bootcamp 2015 臺北場
 
線上遊戲利用 Dev ops 開源工具 chef, docker, vagrant 在microsoft azure 的佈署實踐-Global Azure...
線上遊戲利用 Dev ops 開源工具 chef, docker, vagrant 在microsoft azure 的佈署實踐-Global Azure...線上遊戲利用 Dev ops 開源工具 chef, docker, vagrant 在microsoft azure 的佈署實踐-Global Azure...
線上遊戲利用 Dev ops 開源工具 chef, docker, vagrant 在microsoft azure 的佈署實踐-Global Azure...
 
Azure mobileservice
Azure mobileservice Azure mobileservice
Azure mobileservice
 
前端工程師與室內裝修師傅的相似之處-twMVC#16
前端工程師與室內裝修師傅的相似之處-twMVC#16前端工程師與室內裝修師傅的相似之處-twMVC#16
前端工程師與室內裝修師傅的相似之處-twMVC#16
 
樞紐分析表之數據分析與統計-twMV#C14
樞紐分析表之數據分析與統計-twMV#C14樞紐分析表之數據分析與統計-twMV#C14
樞紐分析表之數據分析與統計-twMV#C14
 
RWD不是你想的那樣 tw mvc#13
RWD不是你想的那樣 tw mvc#13RWD不是你想的那樣 tw mvc#13
RWD不是你想的那樣 tw mvc#13
 
Vs2013新功能介紹 twMVC#11
Vs2013新功能介紹 twMVC#11Vs2013新功能介紹 twMVC#11
Vs2013新功能介紹 twMVC#11
 
架構設計入門 twMVC#11
架構設計入門 twMVC#11架構設計入門 twMVC#11
架構設計入門 twMVC#11
 
Knockout 從零開始,實戰教學 twMVC#10
Knockout 從零開始,實戰教學 twMVC#10Knockout 從零開始,實戰教學 twMVC#10
Knockout 從零開始,實戰教學 twMVC#10
 
20130823微軟雲端平台開發者日
20130823微軟雲端平台開發者日20130823微軟雲端平台開發者日
20130823微軟雲端平台開發者日
 
NFC近場通訊-twMVC#9
NFC近場通訊-twMVC#9NFC近場通訊-twMVC#9
NFC近場通訊-twMVC#9
 
AngularJS 開發 ASP.NET MVC -twMVC#9
AngularJS 開發 ASP.NET MVC -twMVC#9AngularJS 開發 ASP.NET MVC -twMVC#9
AngularJS 開發 ASP.NET MVC -twMVC#9
 
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
 
ASP.NET MVC Web API -twMVC#8
ASP.NET MVC Web API -twMVC#8ASP.NET MVC Web API -twMVC#8
ASP.NET MVC Web API -twMVC#8
 
超。光速 網站最佳化實戰 -twMVC#8
超。光速 網站最佳化實戰 -twMVC#8超。光速 網站最佳化實戰 -twMVC#8
超。光速 網站最佳化實戰 -twMVC#8
 
透過測試提升軟體品質 - twMVC#7
透過測試提升軟體品質 - twMVC#7透過測試提升軟體品質 - twMVC#7
透過測試提升軟體品質 - twMVC#7
 
Windows 市集應用程式 開發心得分享 -twMVC#6
Windows 市集應用程式 開發心得分享 -twMVC#6Windows 市集應用程式 開發心得分享 -twMVC#6
Windows 市集應用程式 開發心得分享 -twMVC#6
 

ASP.NET MVC Model 的設計與使用 twMVC#10