Last active
November 20, 2017 13:23
-
-
Save xxKRASHxx/5b8019ba50f211b1b626b2f4ebb68158 to your computer and use it in GitHub Desktop.
SwiftInject DI Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnyInteractor: DBAccessable { | |
/* | |
easy access to self.dbService | |
*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppDelegate { | |
//... | |
fileprivate let container = Container.defaultContainer() { container in | |
container.register(DBServiceProtocol.self) { _ in CoreDataService() } .inObjectScope(.container) | |
} | |
//... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CoreDataService: DBServiceProtocol { | |
/* | |
protocol implementation | |
*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol DBServiceProtocol { | |
func eat() | |
func pray() | |
func love() | |
} | |
protocol DBAccessable { | |
var dbService : DBServiceProtocol { get } | |
} | |
extension DBAccessable { | |
var dbService : DBServiceProtocol { get { return Container.defaultContainer().resolve(DBServiceProtocol.self)! } } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Swinject | |
fileprivate static let _defaultContainer = Container() | |
extension Container { | |
public static func defaultContainer() -> Container { return _defaultContainer } | |
public static func defaultContainer( _ registeringClosure: (Container) -> Void) -> Container { | |
registeringClosure(_defaultContainer) | |
return _defaultContainer | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment