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
94
肌で感じたディレクションとマネジメント
sshono1210
0
91
Other Decks in Programming
See All in Programming
Rubyと楽しいをつくる / Creating joy with Ruby
chobishiba
0
200
The Ralph Wiggum Loop: First Principles of Autonomous Development
sembayui
0
3.7k
モジュラモノリスにおける境界をGoのinternalパッケージで守る
magavel
0
3.4k
Python’s True Superpower
hynek
0
190
go directiveを最新にしすぎないで欲しい話──あるいは、Go 1.26からgo mod initで作られるgo directiveの値が変わる話 / Go 1.26 リリースパーティ
arthur1
2
450
AI駆動開発の本音 〜Claude Code並列開発で見えたエンジニアの新しい役割〜
hisuzuya
4
460
日本だけで解禁されているアプリ起動の方法
ryunakayama
0
360
2026年は Rust 置き換えが流行る! / 20260220-niigata-5min-tech
girigiribauer
0
220
浮動小数の比較について
kishikawakatsumi
0
370
24時間止められないシステムを守る-医療ITにおけるランサムウェア対策の実際
koukimiura
2
180
Raku Raku Notion 20260128
hareyakayuruyaka
0
430
今更考える「単一責任原則」 / Thinking about the Single Responsibility Principle
tooppoo
3
1.3k
Featured
See All Featured
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
130
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
190
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
130
The Spectacular Lies of Maps
axbom
PRO
1
580
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
120
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
63
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
79
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
110
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
380
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.2k
Reality Check: Gamification 10 Years Later
codingconduct
0
2k
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?