hmlongco / Factory

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.
MIT License
1.71k stars 107 forks source link

Equivalent of Resolver implement for multiple protocol #115

Closed hyazel closed 1 year ago

hyazel commented 1 year ago

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 !

hmlongco commented 1 year ago

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.

hyazel commented 1 year ago

Ok thanks !