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
ServiceとRepository
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
ショウノシオリ
April 12, 2017
Programming
0
32
ServiceとRepository
2017/4/12 関西PHPユーザーズグループの勉強会での登壇資料です。
ServiceとRepositoryについて自分なりに解釈してみたことをまとめました。
ショウノシオリ
April 12, 2017
Tweet
Share
More Decks by ショウノシオリ
See All by ショウノシオリ
Nuxt / Vue 開発でやりがちな 「読みづらい」「わかりづらい」コード
sshono1210
0
260
開発チームのリーダーとしてどうあるべきか?
sshono1210
3
1.2k
Nuxt.js のディレクトリ
sshono1210
0
3k
Nuxt.js で SSR 対応する
sshono1210
1
2.3k
array_merge と array_push の違いについて
sshono1210
0
560
全くデザインを勉強したことのないエンジニアが「なるほどデザイン」を読んで少しだけ勉強した話
sshono1210
0
260
Vue.js の methods と computed
sshono1210
0
130
すぐに使える ES2015 の基本構文3つ
sshono1210
0
96
肌で感じたディレクションとマネジメント
sshono1210
0
93
Other Decks in Programming
See All in Programming
Codexに役割を持たせる 他のAIエージェントと組み合わせる実務Tips
o8n
4
1.4k
20260320登壇資料
pharct
0
120
野球解説AI Agentを開発してみた - 2026/02/27 LayerX社内LT会資料
shinyorke
PRO
0
370
PHPで TLSのプロトコルを実装してみる
higaki_program
0
490
ファインチューニングせずメインコンペを解く方法
pokutuna
0
190
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
160
見せてもらおうか、 OpenSearchの性能とやらを!
shunta27
1
140
Takumiから考えるSecurity_Maturity_Model.pdf
gessy0129
1
160
Understanding Apache Lucene - More than just full-text search
spinscale
0
140
Windows on Ryzen and I
seosoft
0
400
Codex CLIのSubagentsによる並列API実装 / Parallel API Implementation with Codex CLI Subagents
takatty
2
500
条件判定に名前、つけてますか? #phperkaigi #c
77web
2
830
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
240
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.2k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
120
Into the Great Unknown - MozCon
thekraken
40
2.3k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
150
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
330
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
150
Optimizing for Happiness
mojombo
378
71k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Mind Mapping
helmedeiros
PRO
1
130
Utilizing Notion as your number one productivity tool
mfonobong
4
270
Transcript
PHP勉強会 ServiceとRepository
自己紹介 ショウノシオリ ◦ 株式会社chatbox ◦ エンジニア ◦ @shoshoでQiita記事 書いてます ◦
Laravel と日々格闘
最近開発したもので出てきた Service と Repository がよくわからなかったので勉強してみました
開発したもの ▷ Laravel 5.4 ▷ ECサイト(管理画面のみ) ▷ フロントは他の会社 ▷ コードの設計は完了済みだった
開発に使う主なもの ▷ View ▷ Route ▷ Controller
と ▷ Service ▷ Repository ▷ Eloquent
DBにアクセスするまでの流れ Controller Service Repository Eloquent DB
DBまわりがなぜ こんなに細かく分けられているんだろう?
Service と Repository は必要なのか?
DBにアクセスするまでの流れが Controller Service Repository Eloquent DB だとすると
2つの問題点 ▷ DBの設計が変わったときに、 Eloquentを 使っているコントローラ全てを修正する必要 が出て来る ◦ めんどう ◦ 大変 ▷
フロントでも同じDB操作をすることがある のに、別々でつくるのはもったいない ◦ ユーザー情報の取得、とか
この2つを解決するのが Service と Repository
DBの設計が変わったときに、Eloquentを使っ ているコントローラ全てを修正する必要が出て 来る 問題
Repositoryが解決 ▷ Eloquentを操作する唯一のクラス。ここか ら先はEloquentではなくEntityとしてデータ を持ち回すようにしている。 ◦ DB変更に伴うEloquentの修正が与える 影響はリポジトリ内に収まる ◦ 修正箇所を見つけやすい
◦ 修正する場所も少なくて済む
フロントでも同じDB操作をすることがあるの に、別々でつくるのはもったいない 問題
Serviceが解決 ▷ 「ユーザー情報を取得したい」といった具体 的な要求に対する処理を提供するクラス。 ◦ 再利用できる ◦ 処理自体を固めることになるので、画面 の仕様変更などに強くなる
なので以下のような役割分担になっている のかな、と
Controller Service Repository Eloquent DB リクエストの受取と 結果を返す、だけ 要求に対するものを 提供するクラス Eloquentを管理する
唯一のクラス DBを操作する
このように 関心事 = ドメイン を中心に開発をすすめる方法が ドメイン駆動設計(DDD) 的な?
なぜこんなに細かくわけられているんだろう? の答えとしては
▷ フロントとの開発をみこしたものだから ▷ 各クラスにしっかり役割分担しておくことで 修正しやすくするため かなぁと思いました
つくる立場であっても 少しずつ設計思想についても学んでいきたいと 思います
Thanks! Any questions?