Comments (5)
Yeah at that point I think it's better to let people do their own static func instantiate (deps: Dependencies)
on their own class, calling instantiate()
+ injecting the bars in there, than making that into Reusable but opening too much properties as public in the pod.
from reusable.
Hi Andrew
In fact that's exactly how I do this too so far.
I haven't found a better way as the library can't guess in advance what properties you'll likely want to inject.
Now, thinking about it, I think there could be an alternative, for example creating a protocol with associated type like this:
protocol InjectionReusable: Reusable {
associatedtype Dependencies
var dependencies: Dependencies { get set }
init(dependencies: Dependencies)
}
extension InjectionReusable {
init(dependencies: Dependencies) {
self.dependencies = dependencies
}
}
extension InjectionReusable where Self: UIViewController {
static func instantiate(dependencies: Dependencies) -> Self {
…
}
}
And then you could do:
class MyVC: UIViewController, InjectionReusable {
typealias Dependencies = UserService & WebService & PersistentManagerService
}
let vc = MyVC.instantiate(dependencies: myDeps)
If you have multiple dependencies you'd need to group them in a struct conforming to the 3 XXXService
protocols but that's a practice that is common if you follow Krzysztof's suggestion in http://merowing.info/2017/04/using-protocol-compositon-for-dependency-injection/
Only thing I don't like in this solution is that the dependencies
property has to be declared in the protocol, so made public even though it would be more logical to make it private…
from reusable.
Thats a pretty good way to do it, then if you also made the .instantiate
function private to the class, they only way to create the object would be through the InjectionReusable. instantiate(dependencies:)
I'm probably going to use this, would you like me to make a PR targeting a new branch on your repo?
from reusable.
The problem is that I'm not sure you can make .instantiate()
private, as it's declared and provided by the protocol and its default implementation, and protocols can't declare things private.
That's the exact same reason why we can't make var dependencies: Dependencies { get set }
private or at least read-only, because in the protocol's default implementation we need to set its value so the protocol has to declare it publicly visible and settable…
I'd love to have a PR to have InjectionReusable
, but I'm curious if it's really doable in a way that doesn't exposes too much private stuff publicly…
from reusable.
Ahh, you're right. It isn't really possible to hide the implementation of Reusable
. I can still submit the PR if you'd like, but the benefit seems iffy at best.
Comment here if you'd like me to add it, closing for now though
from reusable.
Related Issues (20)
- getting error on using cellType(for indexPath:)
- Support for new API introduced on iOS 13
- MKMapView support
- Feature request - allow to use UITableViewCell with custom style
- Swift Package Manager: reusable deployment target warning HOT 3
- SPM xibs (Bundle.module) HOT 1
- Xcode 12.5 warnings HOT 2
- Swift tools version update too high, lower version build failed
- UITableViewHeaderFooterView - Warning with NibReusable
- Sure, go ahead!
- ## Summary
- Extension for UICollectionViewCell / UITableViewCell with contentView HOT 6
- Problem while compiling with Carthage HOT 3
- NibReusable should have bundle as an optional parameter HOT 5
- Swift 5 Support? HOT 2
- Undefined symbols when compiling for unit tests HOT 1
- Xcode 11 beta 4 compiler error HOT 1
- loadNibContent always loads nib named after base class HOT 14
- is there a way to override sceneStoryboard for StoryboardSceneBased HOT 2
- Swift Package Manager HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reusable.