hmlongco / Factory

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Equivalent of Resolver implement for multiple protocol

hyazel opened this issue · comments

commented

Hello,

I'm currently migrating my code base from Resolver to Factory.

I have some registrations that use the same instance for multiple protocols like this :

main.register { XYZCombinedService() }
    .implements(XYZFetching.self)
    .implements(XYZUpdating.self)

What would be the equivalent for Factory ?

Thanks a lot !

Since they work differently, there's not a direct equivalent. Just make two factories.

    var xyzFetching: Factory<XYZFetching> { self { XYZCombinedService() } }
    var xyzUpdating: Factory<XYZUpdating> { self { XYZCombinedService() } }

Or, if you prefer

    var xyzFetching: Factory<XYZFetching> { self { self.xyzCombined() } }
    var xyzUpdating: Factory<XYZUpdating> { self { self.xyzCombined() } }
    private var xyzCombined: Factory<XYZCombinedService> { self { XYZCombinedService() }.shared }

I'd use the later version if the combined service needs to be cached.

commented

Ok thanks !