Michael Long
Jan 12, 2025

--

Yes, you've simplified view model constructor injection... by writing a LOT of extra code. Some of the use cases can be reused, but the composer is bespoke for each and every view model and service.

You might consider

class ProductListViewModel {

typealias UseCases = ProductUseCase & AnalyticsUseCase & NetworkUseCase

init(factory: UseCases) {

self.productUseCase = ...

...

}

And then creating a single global --or a set of modular--dependency Container(s) that conforms to those cases and all of the others for which you would have created discrete composers.

Said container can be passed into the vm as needed without having to create or manage dozens of custom composers in the code.

That still allows you to create a bespoke version that conforms to those specific protocols for mocks and testing if needed.

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

Responses (4)