Factory icon indicating copy to clipboard operation
Factory copied to clipboard

Using Factory between different targets in a package and app

Open LucasVanDongen opened this issue 7 months ago • 3 comments

Use Case

I'm working on improving my build times and previews speed and reliability by turning all of my major features into Packages. The strategy is to hide every dependency behind a protocol, and then only export 3rd party dependencies when I'm returning the actual implementation, but not when I'm running Previews.

This is the structure:

let package = Package(
    name: "Navigation",
    platforms: [.iOS(.v17)],
    products: [
        .library(
            name: "Navigation",
            targets: [
                implementations,
                ui
            ]
        ),
        .library(name: "Previews", targets: [previews]) // needed to run my previews, should not be imported
    ],
    dependencies: [
        .package(
            url: "https://github.com/apple/swift-async-algorithms/",
            exact: "1.0.0"
        ),
        .package(
            name: "SharedUI",
            path: "../SharedUI"
        ),
        .package(
            url: "https://github.com/mapbox/mapbox-navigation-ios.git",
            from: "3.0.0"
        ),
        .package(
            url: "https://github.com/mapbox/search-ios.git",
            from: "2.0.0"
        )
    ],
    targets: [
        .target( // The actual implementations of my protocols, using third party dependencies
            name: implementations,
            dependencies: [
                .targetItem(
                    name: protocols,
                    condition: .none
                ),
                mapboxSearch,
                mapboxNavigationCore
            ],
            path: "Sources/Implementations"
        ),
        .target( // The protocols I expose for these implementations, not requiring 3rd party dependencies
            name: protocols,
            dependencies: [
                .targetItem(
                    name: model,
                    condition: .none
                )
            ],
            path: "Sources/Protocols"
        ),
        .target( // The Model implementations I share everywhere (requests, state, enums etcetera)
            name: model,
            path: "Sources/Model"
        ),
        .target( // The UI, only gets protocols injected, has no clue about any big dependency
            name: ui,
            dependencies: [
                .targetItem(name: protocols, condition: .none),
                sharedUI
            ],
            path: "Sources/UI"
        ),
        .target( // Mocks only know about the lightweight protocols and perhaps Models
            name: mocks,
            dependencies: [
                .targetItem(name: protocols, condition: .none)
            ],
            path: "Sources/Mocks"
        ),
        .target( // Specific target to put my previews in. Previews only use the UI, protocols and lightweight mocks
            name: previews,
            dependencies: [
                .targetItem(name: ui, condition: .none),
                .targetItem(name: protocols, condition: .none),
                .targetItem(name: mocks, condition: .none)
            ],
            path: "Sources/Previews"
        ),
        .testTarget(
            name: "NavigationTests",
            dependencies: [.targetItem(name: implementations, condition: .none)]
        )
    ]
)

This works great and I'm back to snappy Previews because the only thing I build is View / ViewModel code, some lightweight MockSpies (generated by Sourcery).

However I have no idea how to fit Factory into this. The ViewModels and Views expect registrations in Containers but I cannot give those yet because they're different between the App that is consuming my UI target and my Previews that inject the mocks instead.

So the UI relies on the App itself to register these dependencies (coming from the Implementations target) but the Previews module has its own registrations. The UI module doesn't understand this and fails to build.

Solution Direction

I'm looking for a way where I can "promise" the UI module that the dependencies will be set at run-time, in a compile-time safe way, by the target or app that consumes it. I tried messing around with protocols but couldn't get anything to work. The only thing I saw possible so far is injecting the dependencies Container manually.

LucasVanDongen avatar Jul 07 '24 10:07 LucasVanDongen