Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ServiceとRepository
Search
ショウノシオリ
April 12, 2017
Programming
36
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ServiceとRepository
2017/4/12 関西PHPユーザーズグループの勉強会での登壇資料です。
ServiceとRepositoryについて自分なりに解釈してみたことをまとめました。
ショウノシオリ
April 12, 2017
More Decks by ショウノシオリ
See All by ショウノシオリ
Nuxt / Vue 開発でやりがちな 「読みづらい」「わかりづらい」コード
sshono1210
0
280
開発チームのリーダーとしてどうあるべきか?
sshono1210
3
1.3k
Nuxt.js のディレクトリ
sshono1210
0
3k
Nuxt.js で SSR 対応する
sshono1210
1
2.3k
array_merge と array_push の違いについて
sshono1210
0
570
全くデザインを勉強したことのないエンジニアが「なるほどデザイン」を読んで少しだけ勉強した話
sshono1210
0
270
Vue.js の methods と computed
sshono1210
0
150
すぐに使える ES2015 の基本構文3つ
sshono1210
0
110
肌で感じたディレクションとマネジメント
sshono1210
0
110
Other Decks in Programming
See All in Programming
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.1k
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
2.3k
LoopHub - ローカルで動く GitHub で、AI と共同開発
jugyo
1
570
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
390
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
310
SREの越境 / SRE Collaboration
y0hgi
2
280
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
150
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
190
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
3.2k
変化を抱擁するドキュメントの作り方 - ビジネスルール駆動開発がもたらす、コードとの新しい関係
ioki
2
220
選挙速報を多くのユーザーへ 届ける Live Activities 設計
hamayokokuririn
0
160
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
120
Featured
See All Featured
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
580
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
590
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
340
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
390
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
KATA
mclloyd
PRO
35
16k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
820
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
290
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?