The Clean Architecture in php



Download 2,26 Mb.
Pdf ko'rish
bet73/179
Sana24.06.2021
Hajmi2,26 Mb.
#100337
1   ...   69   70   71   72   73   74   75   76   ...   179
Bog'liq
The-Clean-Architecture-in-PHP-Kristopher-Wilson

Using Setter Injection
Using Setter Injection, we would update the mechanism responsible for instantiating the
CustomerController
object (either a routing or dispatching process in our framework) to call
some new set methods and provide the object with its dependencies:
¹⁸
http://www.natpryce.com/articles/000783.html


Dependency Injection
46
$controller
=
new
CustomerController();
$controller
->
setCustomerRepository
(
new
CustomerRepository());
$customer
=
$controller
->
viewAction
();
And of course, the
CustomerController
would be updated to have this
setCustomerRepository
method:
class
CustomerController
{
protected
$repository
;
public function
setCustomerRepository
(CustomerRepository
$repo
) {
$this
->
repository
=
$repo
;
}
public function
viewAction
() {
$customer
=
$this
->
repository
->
getById
(
1001
);
return
$customer
;
}
}
By the time the
viewAction()
method is called, this class should have been injected an instance
of
CustomerRepository
through the
setCustomerRepository
method. Now the processes of
retrieving dependencies has been completely removed from the
CustomerController
class.
When testing, we can see how much easier it is to mock the repository to provide a stable testing
state:
$repository
=
new
MockCustomerRepository([
1001 =>
(
new
Customer())
->
setName
(
'ACME Corp'
)
]);
$controller
=
new
CustomerController();
$controller
->
setCustomerRepository
(
$repository
);
$customer
=
$controller
->
viewAction
();
assertEquals(
'ACME Corp'
,
$customer
->
getName
());
There is still one drawback here, though, and that is that using setter injection does not make the
dependencies required. This puts us in a situation that can lead to hard-to-detect defects when
we forget to inject some dependency:
$controller
=
new
CustomerController();
$customer
=
$controller
->
viewAction
();
This code will throw errors as, without calling the
setCustomerRepository()
method, the class
$repository
variable will be null when it is used. Given how small this application is, we’ll
likely find the problem easily, but in a larger system with more involved code, this could lead to
a rough time debugging where something went wrong.


Dependency Injection
47

Download 2,26 Mb.

Do'stlaringiz bilan baham:
1   ...   69   70   71   72   73   74   75   76   ...   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