square / Cleanse

Lightweight Swift Dependency Injection Framework
Other
1.78k stars 90 forks source link

How this differ from SwiftInject? #121

Closed kaushalyap closed 4 years ago

kaushalyap commented 4 years ago

I would like to know how this library differ from SwiftInject? What advantages Cleanse have over SwiftInject?

sebastianv1 commented 4 years ago

@kaushalyap That's a great question, and it might even be a good idea for us to write up a little section in the README explaining how Cleanse differs from other DI frameworks out there.

I won't speak too much about the particulars of what SwiftInject does and doesn't provide, and tell you about the motivation and goals behind Cleanse. Cleanse was influenced by Google's Guice DI framework and aims to provide the same feature set as established frameworks like Guice and Dagger, but with a strong focus on creating idiomatic Swift APIs and leveraging the language's static type system. For example, when registering dependencies, we code-gen factory functions that allow you simply call binder.bind(CoffeeMachine.self).to(factory: CoffeeMachine.init) where CoffeeMachine.init expands to a closure that looks something like (coffee: Coffee, water: Water) -> CoffeeMachine. Registering a dependency with SwiftInject might require you to call its resolve function many times for every required dependency.

Although feature rich, Cleanse also aims to remain as lightweight and simple as possible. It doesn't use any Swift reflection or swizzling and we think carefully about adding new core features. Remaining small was a motivating factor behind creating a plugin interface (RFC #118) that developers can use to add their own features and validations to the object graph that don't exist in the core library.

Lastly, we have a focus on keeping Cleanse highly performant. We've been using Cleanse here at Square for over 3 years with a huge object graph and haven't had any slowness fetching or resolving dependencies and intend to keep it that way. At the end of the day, I think both frameworks do a great job of providing a dependency injection library, and the differences lie in the implementation details and feature set.

kaushalyap commented 4 years ago

@sebastianv1 Thanks for detailed response. It's better to add a comparison to the readme.

In Android dev world Square is highly recognized as an company creating high quality libraries for android dev ex: Retrofit, LeakCanary, SQLDelight But I have not heard much about Square's iOS dev ecosystem. May be you guys are more focused on Kotlin Multiplatform. So you guys do not have to use separate libraries for iOS side.

sebastianv1 commented 4 years ago

@kaushalyap We haven't seriously adopted Kotlin Multiplatform aside from isolated experiments, but I'd encourage you to poke around some of the other iOS open source projects we've published!