2 ms·
I do a slight modification of the approach: I pass everything in, but if you need related functions passed in, it makes sense to pass them in as a service objec
by necovek 3y ago
I do a slight modification of the approach: I pass everything in, but if you need related functions passed in, it makes sense to pass them in as a service object containing those functions.
Those service objects really have no side effects (constructor basically simply keeps context in), allowing for smaller function signatures, especially for, by nature, side-effect stuff (logging, caching...). Basically, saves on passing both "get_from_cache" and "set_cache" functions in as you pass CacheService instance in.
But the premise is the same: keep code functional, allowing for easy testing and clear implementation.
The one thing I disagree is that this does not allow testing against the real thing: you simply have a test that confirms dependency and simulated fake (function or service) have the same basic behaviour your code expects. If the real thing works using an external service, you can decide if you want to test that as a system test (I run those ocassionally) or with mocks.