-
-
Save krzysztofzablocki/6a1b6afab974f442f5ff to your computer and use it in GitHub Desktop.
class ApplicationController { | |
let coreDataStack: CoreDataStack | |
let mfpService = MFPService(username: x, password: y) | |
init(coreDataStack stack: CoreDataStack) { | |
coreDataStack = stack | |
let _ = integrator | |
let _ = rootFlowController | |
let _ = window | |
} | |
lazy var rootFlowController: FlowController = { | |
return SessionFlowController(coreDataStack: self.coreDataStack) | |
}() | |
lazy var window: UIWindow = { | |
let window = UIWindow(frame: UIScreen.mainScreen().bounds) | |
window.backgroundColor = UIColor.whiteColor() | |
window.rootViewController = self.rootFlowController.rootViewController | |
window.makeKeyAndVisible() | |
return window | |
}() | |
lazy var integrator: IntegrationService = { | |
let coreDataProcessor = CoreDataReportProcessor(stack: self.coreDataStack) | |
let integrator = IntegrationService(provider: self.mfpService) { | |
coreDataProcessor.processReports($0) | |
} | |
integrator.fetchEvery(interval: 5.hours) | |
integrator.fetch() | |
return integrator | |
}() | |
} |
@scott I use composition and init injection almost everywhere, but there are situations where it doesn't make much sense, trying to find best way to deal with them :)
Private functions sounds like the most straight forward approach I would think. Who cares if it’s a static method or anything. Object oriented is only really about public interfaces. So just do something functional like Jessy’s https://gist.github.com/krzysztofzablocki/6a1b6afab974f442f5ff#gistcomment-1623209
Except I would call the functions initIntegrator
or createIntegrator
instead of using pascal case, as they look like their own initialisers and types instead of simple functions.
Nice work with this all by the way!
How about Option 2 - lazy var’s with side-effects
+ builders
?
Got the link to this, thought I'd leave a question.
What about the option of not creating these objects inside this class, and injecting them in the init?
I've often found when I ran into issues like this, how the objects I was trying to create in the
init
were created didn't matter to the class, the class just needed a reference to them and it seemed like a convenient place to put the code.I'm not really sure what your circumstances are for this, though, and perhaps you have a good reason for not liking that option.