Share
Embed
iOS Conf SG - 2022 - Dependency Injection at scale
Building a feature in an app consisting of 95+ modules is complicated and leads to dependency management hell. Splitting a module into a public interface and a separate implementation framework for the interface allows to create simpler deps management and improve build times.
https://www.youtube.com/watch?v=a1KvjxNVEq8&list=PLED4k3CZkY9R9mhRW5V74gS9cVTp28CbK
Maciej Piotrowski
January 21, 2022
Transcript
41 slides
-
9+ years iOS development Mobile Core Team @ allegro “I
make devs’ lives easier” Maciej Piotrowski -
20 million customers each month one of top ten most
visited e-commerce sites worldwide mobile apps big chunk of traf fi c the most popular product search engine in Poland -
OVERVIEW: Steve provides you a convenient way to solve daily
👨💻 development problems and complete repetitive tasks. USAGE: steve <subcommand> OPTIONS: --version Shows the version -h, --help Shows help information SUBCOMMANDS: fixme Fixes most common issues that developers encounter generate Allows to generate modules for Allegro app bazel Use this command to invoke Bazel with parameters focus Allows to focus on selected modules from Allegro workspace test Runs tests for the given project and scheme Media: https://www.macrumors.com/2011/10/05/steve-jobs-has-passed-away OVERVIEW: Steve provides you a convenient way to solve daily 👨💻 development problems and complete repetitive tasks. USAGE: steve <subcommand> OPTIONS: --version Shows the version -h, --help Shows help information SUBCOMMANDS: fixme Fixes most common issues that developers encounter generate Allows to generate modules for Allegro app bazel Use this command to invoke Bazel with parameters focus Allows to focus on selected modules from Allegro workspace test Runs tests for the given project and scheme -
January 2021 78 modules 77 standard modules 1 utility module
January 2022 91 standard modules 26 utility modules 117 modules -
how to inject dependencies to ? 117 modules NetworkClient .shared
Core Cart ItemPage Payments SearchEngine Lists … Address OLD import Networking import Networking import Networking import Networking import Networking import Networking import Networking -
Cart ItemPage Payments SearchEngine Lists … Address Networking Interface
Networking NEW import NetworkingInterface import NetworkingInterface import NetworkingInterface import NetworkingInterface import NetworkingInterface import NetworkingInterface how to inject dependencies to ? 117 modules -
January 2022 91 private modules 91 public modules 26 utility
modules January 2021 78 modules 77 standard modules 1 utility module 208 modules -
The Build of iOS Conf SG 2021: Speeding up the
build process of a monolithic application 208 modules -
Cart ItemPage Payments SearchEngine Lists … Address Networking Interface
Networking ./steve focus Networking NEW Networking Interface -
Cart Interface ItemPage Interface Payments Interface SearchEngine
Interface Lists Interface … Address Interface Cart ItemPage Payments SearchEngine Lists … Address NEW -
Cart Interface ItemPage Interface Payments Interface SearchEngine
Interface Lists Interface … Address Interface Cart ItemPage Payments SearchEngine Lists … Address NEW -
Cart Interface ItemPage Interface Payments Interface SearchEngine
Interface Lists Interface … Address Interface Networking Interface Networking Cart ItemPage Payments SearchEngine Lists … Address NEW -
import Networking func injectProductPageDependencies() { let container = ProductPage.DependencyContainer.default container.apiClient
= Networking.NetworkClient.shared // … } OLD // Module: Core Media: https://giphy.com/gifs/cbs-indianajones-harrisonford-raidersofthelostark-TGogvP0s0QESHmyYi9 -
protocol NetworkingClient { func get(_ resource: Resource) func post(_ resource:
Resource) } OLD // Module: Networking -
protocol NetworkingClient { func get(_ resource: Resource) func post(_ resource:
Resource) } // Module: NetworkingInterface NEW -
import NetworkingInterface class ModuleConfiguration: ModuleConfiguring { func registerImplementations(in registry: Registry)
{ registry.register(type: NetworkingClient.self, service: NetworkClient()) // Module: Networking NEW -
import NetworkingInterface class ModuleConfiguration: ModuleConfiguring { func registerImplementations(in registry: Registry)
{ registry.register(type: NetworkingClient.self, service: NetworkClient()) } // Module: Networking NEW Media: https://giphy.com/gifs/cbs-indianajones-harrisonford-raidersofthelostark-TGogvP0s0QESHmyYi9 -
import NetworkingInterface class ModuleConfiguration: ModuleConfiguring { func resolveDependencies(with resolver: Resolver)
{ guard let networking = resolver.resolve(type: NetworkingClient.self) // Module: ProductPage NEW -
import NetworkingInterface class ModuleConfiguration: ModuleConfiguring { func resolveDependencies(with resolver: Resolver)
{ guard let networking = resolver.resolve(type: NetworkingClient.self) } } // Module: ProductPage NEW Media: https://giphy.com/gifs/cbs-indianajones-harrisonford-raidersofthelostark-TGogvP0s0QESHmyYi9 -
let container: Registry & Resolver = // … modules.forEach {
$0.registerImplementations(in: container) } modules.forEach { $0.resolveDependencies(with: container) } // Module: Application NEW -
// Module: DependencyInjection NEW class DependencyContainer: Registry, Resolver { func
register<T>(type: T.Type, service: Any) { dependencies[DependencyKey(type)] = service -
// Module: DependencyInjection NEW class DependencyContainer: Registry, Resolver { func
register<T>(type: T.Type, service: Any) { dependencies[DependencyKey(type)] = service } } -
// Module: DependencyInjection NEW class DependencyContainer: Registry, Resolver { }
func resolve<T>(type: T.Type) -> T? { return dependencies[DependencyKey(type)] as? T } -
NEW // Summary: New Dependency Injection at scale private public
Cart Interface ItemPage Interface Payments Interface SearchEngine Interface Lists Interface … Address Interface Cart ItemPage Payments SearchEngine Lists … Address Media: https://www.macrumors.com/2011/10/05/steve-jobs-has-passed-away -
NEW // Summary: New Dependency Injection at scale private public
Media: https://giphy.com/gifs/cbs-indianajones-harrisonford-raidersofthelostark-TGogvP0s0QESHmyYi9 func application(_:, didFinishLaunchingWithOptions:) -> Bool { modules.forEach { $0.registerImplementations(in: container) } modules.forEach { $0.resolveDependencies(with: container) } } -
• lighter workspace • light dependency injection framework • intermodule
dependencies on public interfaces • private modules inject implementations of interfaces • module extracts what it needs from the dependencies container // Summary: New Dependency Injection at scale Media: https://giphy.com/gifs/cbs-indianajones-harrisonford-raidersofthelostark-TGogvP0s0QESHmyYi9 -
allegro.tech Speeding up iOS builds with Bazel Speeding up warm
builds in Xcode Maciej Piotrowski twitter.com/macpiotrowski89 swifting.io