Похожие чаты

My question can be rewritten as: why use dependency injection

instead of static methods?

2 ответов

2 просмотра

Testabilty for one. Like CreditCardService.charge If it is a static method, testing means you actually lose money everytime you charge But if you use an interface, you can create a mock using Mockito or other mocking frameworks

Your rewritten question actually answers your original one)) Controllers are not static to allow DI. There may be other reasons, but this one is the most obvious one. You can't inject properties in classes (instances of Class<MyController>). For example, if you need to use some MyService in controller, you couldn't use it by simple injection as a static field. The holy grail of spring - dependency injection - would be impossible without object instantiation.

Похожие вопросы

Обсуждают сегодня

И ещё - определить, имеет ли он доступ к интернету очень просто - смотрим всех пользователей сети. И да, как оно без сети передаст данные в куда то?
BytePixel 🐧
15
Hey everyone! How can I pass string from Rust to c++ function that expects std::string? When I'm passing CString the c++ functions tell that the string is empty
Jacob
16
А чем вам питонисты не угодили?😂
.
79
Есть какой-нибудь для Delphi/FPC T*Compression(Decompression)Stream на базе LZ4/Zstd/любой другой быстрый(и хорошо сжимающий) алгоритм А ещё лучше в pure pascal А ещё лучше од...
notme
48
Ясн. Но я всеравно к кедам отношусь с опасением. Чисто субьективное кедосозерцание. Что касаемо "восторга изяшности кед" так и не вкурил, хотя много слышал, пока не поставил с...
Артем Записной
30
Я походу случайно сломал свою систему 😅 Хотел увеличить место на системном разделе, переместил раздел EFI и система больше не бутится Можно исправить без переустановки?
Кот Холод (#AW)
10
hi guys, can anyone tell me which location HEX is launched on the 3th of Dec 2019 ? New York? Thanks!
Fleur
11
type TObj = object procedure Init; virtual; end; TObj1 = object(TObj) procedure Init; override; end; procedure TObj1.Init; begin inherited; end; procedur...
Alexander 👋
29
Asus, норм фирма для ноутов?
Артем Записной
11
Hey guys, did you see the new announcement about $Fun? 🔥
Filip Murphy
55
Карта сайта