Skip to content

Instantly share code, notes, and snippets.

@ronstar
ronstar / Configurableresolve.swift
Last active December 8, 2020 07:12
Configurable resolve
func resolve<Service: Configurable>(type: Service.Type, configuration: Service.Configuration) -> Service? {
let service = resolve(type: type) // 1
service?.configure(configuration: configuration) // 2
return service
}
@ronstar
ronstar / Configurableexample.swift
Created December 8, 2020 06:58
Configurable example
dic.register(type: ServiceThreeConfigurable.self) { (dic) -> AnyObject in // 1
return ServiceThreeConfigurable(serviceOne: dic.resolve(type: ServiceOneProtocol.self)!)
}
let configuration = ServiceThreeConfigurable.ServiceThreeConfiguration(paramOne: true, paramTwo: Data()) // 2
let serviceThree = dic.resolve(type: ServiceThreeConfigurable.self, configuration: configuration) // 3
@ronstar
ronstar / Servicethreeconfigurable.swift
Last active December 8, 2020 06:40
Service three configurable
class ServiceThreeConfigurable: ServiceThreeProtocol, Configurable { // 1
struct ServiceThreeConfiguration { // 2
var paramOne: Bool
var paramTwo: Data
}
let serviceOne: ServiceOneProtocol
var paramOne: Bool! // 3
var paramTwo: Data!
@ronstar
ronstar / Configurableprotocol.swift
Created December 7, 2020 18:44
Configurable protocol
protocol Configurable {
associatedtype Configuration
func configure(configuration: Configuration)
}
@ronstar
ronstar / DICinterfaceconfigurable.swift
Created December 7, 2020 18:27
DIC interface configurable
protocol DICProtocol {
func register<Service>(type: Service.Type, factoryClosure: @escaping FactoryClosure)
func resolve<Service>(type: Service.Type) -> Service?
func resolve<Service: Configurable>(type: Service.Type, configuration: Service.Configuration) -> Service?
}
@ronstar
ronstar / DICinterface.swift
Created December 7, 2020 18:19
DIC interface
protocol DICProtocol {
func register<Service>(type: Service.Type, factoryClosure: @escaping FactoryClosure)
func resolve<Service>(type: Service.Type) -> Service?
}
@ronstar
ronstar / Registerclasswithparams.swift
Last active December 7, 2020 16:04
Register class with params
dic.register(type: ServiceThreeProtocol.self) { (dic) -> AnyObject in
return ServiceThree(serviceOne: dic.resolve(type: ServiceOneProtocol.self)!,
paramOne: true,
paramTwo: Data())
}
@ronstar
ronstar / Classwithparameters.swift
Created December 7, 2020 15:28
Class with parameters
class ServiceThree: ServiceThreeProtocol {
let serviceOne: ServiceOneProtocol
let paramOne: Bool
let paramTwo: Data
init(serviceOne: ServiceOneProtocol, paramOne: Bool, paramTwo: Data) {
self.serviceOne = serviceOne
self.paramOne = paramOne
self.paramTwo = paramTwo