IOS - dependency injection in UIViewController

I am looking for an elegant way to implement DI controllers in view mode when working with segues. If I have controller A that ultimately loads controller B, I would like unit test to have this behavior. What would be a good way to implement it?

+4
source share
2 answers

It would be very helpful if we had a simple use of the โ€œconstructorโ€ injection when using storyboards, but unfortunately not, because the infrastructure handles VC initialization for us.

Depending on the size and context of the project as a whole, you can either go to a full-fledged framework like Typhoon (which is very good in my experience), or try something simpler, like using a mediator to process all segues between VCs.

I wrote about the last method here: http://cocoapatterns.com/passing-data-between-view-controllers/ and here http://cocoapatterns.com/ios-view-controller-transitions-mediator-pattern/

Especially in combination with the State Design template, the pick provides a central way to handle injections during segues, and also helps in maintaining and expanding the code base.

+3
source

Typhoon. , Objective-C. "" :

  • TyphoonDefinition - .
  • , .

: ApplicationAssembly.swift

public class ApplicationAssembly: TyphoonAssembly {

    /*
     * These are modules - assemblies collaborate to provie components to this one.  
     * At runtime you can instantiate Typhoon with any assembly that satisfies the 
     * module interface.
     */
    var coreComponents : CoreComponents!
    var themeAssembly : ThemeAssembly!


    public dynamic func weatherReportController() -> AnyObject {

        return TyphoonDefinition.withClass(WeatherReportViewController.self) {
            (definition) in

            definition.useInitializer("initWithWeatherClient:weatherReportDao:cityDao:assembly:") {
                (initializer) in

                initializer.injectParameterWith(self.coreComponents.weatherClient())
                initializer.injectParameterWith(self.coreComponents.weatherReportDao())
                initializer.injectParameterWith(self.coreComponents.cityDao())
                initializer.injectParameterWith(self)

            }
        };
    }

    public dynamic func addCityViewController() -> AnyObject {

        return TyphoonDefinition.withClass(AddCityViewController.self) {
            (definition) in

            definition.useInitializer("initWithNibName:bundle:") {
                (initializer) in

                initializer.injectParameterWith("AddCity")
                initializer.injectParameterWith(NSBundle.mainBundle())
            }
            definition.injectProperty("cityDao", with:self.coreComponents.cityDao())
            definition.injectProperty("weatherClient", with:self.coreComponents.weatherClient())
            definition.injectProperty("theme", with:self.themeAssembly.currentTheme())
            definition.injectProperty("rootViewController", with:self.rootViewController())
        }                
    }
}

Injection Dependency ( DI ), IDE. " " โ€‹โ€‹(, , Swift, ).


:

, WeatherReportController CitiesListController, assembly.citiesListController. , Typhoon , , factory .

:

Typhoon .

enter image description here

Swift Objective-C ,

:

, .

unit test UIStoryboard "" ( setValue: forKey, . viewController UIStoryboard - "", ) .storyboard "instantiateViewControllerForStoryboardIdentifier" , .

+1

All Articles