SlideShare a Scribd company logo
2015/09/11
Repository Pattern
3-tier Architecture
1
Database
Entity Framework
Business Service
Controller
Data
Access
Layer
Service
Layer
Presentation
Layer
2
如果程式已完成 90% 時,高層忽
然下令不准用 Entity Framework,
要改用 Nhibernate。
(上線時間不變)
Challenge
3
• 爆肝加班
• 落人幫忙
• 擺爛
• 離職
Solution
Repository Pattern
4
圖片來源:Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application
Definition
5
Martin Fowler 定義:Repository 是領域層與資料對應層之間
的媒介,如同存在記憶體中的物件集合。用戶端會建構查詢規
格(query specification),並傳遞給 Repository 以執行特定
的資料操作.....概念上,Repository 將資料與其相關操作封裝
成物件集合,以便用貼近物件導向的方式來存取資料。
Martin Fowler,軟體工程師,也是一個軟體開
發方面的著作者和國際知名演說家,專注於物
件導向分析與設計,統一建模語言,領域建模,
以及敏捷軟體開發方法,包括極限編程。
Definition
6
將 DAL 以 Repository 封裝,外部
透過 Repository 來取資料,無須
理會資料來源的種類。
SQL Server
程式架構
7
Controllers
Presentation
Layer
Views View Models
Domain
Model
EF DbContext
EF DbSet<T>
Entity Framework
HTML
JavaScript
AJAX
User
Interface
Repositories
Biz
Entities
EF
Entities
Business Service
Business
Layer
Business Utilities / Helpers
Web API
Comm
on
Utilities
定義 Repository 的 interface
8
實作 Repository interface
9
實作 Repository interface
10
使用 Repository
11
使用 Repository
12
13
使用 Repository
14
抽換 Repository
使用 Repository 的理由
15
• 利於單元測試
• 避免上層服務直接依賴 Entity Framework,以
便抽換成其他 DAL 技術
• 可搭配 Unit of Works 來進行交易(Transaction)
不使用 Repository 的理由
16
• 利於單元測試
− 沒有 Repository 還是可以做 unit test,對象
改為商業邏輯層(前提是要具備 N-tier 架構)
• 避免上層服務直接依賴 Entity Framework,以
便抽換成其他 DAL 技術
− 實務上更換 DAL、DB 的機會不大
• 可搭配 Unit of Works 來進行交易(Transaction)
− Entity Framework 已提供類似功能

More Related Content

PDF
10. 資料永續與交換
PDF
Chapter 4 models
PPTX
Ch03 請求與回應
PPTX
Ch07 使用 JSTL
PDF
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
PDF
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
PDF
CH04:認識物件
PPTX
CH1. 簡介 Web 應用程式
10. 資料永續與交換
Chapter 4 models
Ch03 請求與回應
Ch07 使用 JSTL
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
CH04:認識物件
CH1. 簡介 Web 應用程式

What's hot (10)

PDF
Servlet & JSP 教學手冊第二版 - 課後練習解答
PPTX
Ch13 整合 Spring MVC/Security
PPTX
Ch09 整合資料庫
PDF
5. 從模組到類別
PPTX
Ch02 撰寫與設定 Servlet
PDF
Servlet & JSP 教學手冊第二版 - 第 7 章:使用 JSTL
PDF
Ch09 整合資料庫
PDF
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
PPTX
9. meta-programming
PDF
整合資料庫
Servlet & JSP 教學手冊第二版 - 課後練習解答
Ch13 整合 Spring MVC/Security
Ch09 整合資料庫
5. 從模組到類別
Ch02 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版 - 第 7 章:使用 JSTL
Ch09 整合資料庫
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
9. meta-programming
整合資料庫
Ad

Viewers also liked (20)

PDF
Repository and Unit Of Work Design Patterns
PDF
Generic repository pattern with ASP.NET MVC and Entity Framework
PPTX
Repository design pattern in laravel - Samir Poudel
PPTX
Dapper performance
PPTX
Dependency injection in asp.net core
PDF
Laravel 5 In Depth
PPT
A clean repository pattern in ef
PDF
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
PPTX
Domain Driven Design in Rails
PDF
Real World Dependency Injection - phpday
PPTX
EF6 or EF Core? How Do I Choose?
PPTX
A Tour of EF Core's (1.1) Most Interesting & Important Features
PPTX
ASP.NET MVC 4 Request Pipeline Internals
PDF
Difference between wcf and asp.net web api
PPTX
Generic Repository Pattern with ASP.NET MVC and EF
PPTX
ASP.NET MVC 4 - Routing Internals
PPTX
C# 6.0 - DotNetNotts
PDF
Day02 01 Advance Feature in C# DH TDT
PDF
DDD Repository
PPTX
Real World Asp.Net WebApi Applications
Repository and Unit Of Work Design Patterns
Generic repository pattern with ASP.NET MVC and Entity Framework
Repository design pattern in laravel - Samir Poudel
Dapper performance
Dependency injection in asp.net core
Laravel 5 In Depth
A clean repository pattern in ef
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Domain Driven Design in Rails
Real World Dependency Injection - phpday
EF6 or EF Core? How Do I Choose?
A Tour of EF Core's (1.1) Most Interesting & Important Features
ASP.NET MVC 4 Request Pipeline Internals
Difference between wcf and asp.net web api
Generic Repository Pattern with ASP.NET MVC and EF
ASP.NET MVC 4 - Routing Internals
C# 6.0 - DotNetNotts
Day02 01 Advance Feature in C# DH TDT
DDD Repository
Real World Asp.Net WebApi Applications
Ad

Introduction the Repository Pattern