The Clean Architecture in php


class CustomerController



Download 2,26 Mb.
Pdf ko'rish
bet78/179
Sana24.06.2021
Hajmi2,26 Mb.
#100337
1   ...   74   75   76   77   78   79   80   81   ...   179
Bog'liq
The-Clean-Architecture-in-PHP-Kristopher-Wilson

class
CustomerController
{
protected
$repository
;
protected
$responseFactory
;
public function
__construct
(
CustomerRepository
$repo
,
ResponseFactory
$factory
) {
$this
->
repository
=
$repo
;
$this
->
responseFactory
=
$factory
;
}
public function
viewAction
() {
$customer
=
$this
->
repository
->
getById
(
1001
);
$response
=
$this
->
responseFactory
->
create
(
$this
->
params
(
'context'
));
$response
->
setData
(
'customer'
,
$customer
);
return
$response
;
}
}


Dependency Injection
50
Previously, our dependency was the specific response we were returning. Now, however, it’s this
new
ResponseFactory
class that builds a response for us.
The important thing here is that we are abstracting out the logic that determines how to build
the dependency, and instead depending on that abstraction. We’re also injecting that in so that
if the logic or methodology ever changes, we just have to change the implementation instead of
updating an unknown number of locations that were previously building their own responses.
Handling Many Dependencies
It’s very easy to let dependency injection get out of control:
public function
__construct
(
CustomerRepository
$customerRepository
,
ProductRepository
$productRepository
,
UserRepository
$userRepository
,
TaxService
$taxService
,
InvoiceFactory
$invoiceFactory
,
ResponseFactory
$factory
,
// ...
) {
// ...
}
This is usually indicative of having a class that violates the Single Responsibility Principle and
does too much and contains too much logic. There’s no hard rule about how many dependencies
a class can have. Generally, the fewer the better, however, it’s entirely possible to go too far in
the other direction as well, and end up with a hierarchy of tiny classes that are hard to follow,
refactor and test.
After understanding the concepts of this book, you can start to use the “Feels Bad” policy of
knowing when to refactor a bad situation into good, healthy code.

Download 2,26 Mb.

Do'stlaringiz bilan baham:
1   ...   74   75   76   77   78   79   80   81   ...   179




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish