Last active
July 22, 2016 06:42
-
-
Save ukitaka/da07582f59576d4be54a68459eed1d5f to your computer and use it in GitHub Desktop.
SwinjectとProtocol Extensionを用いたDIパターン
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
@_exported import class Swinject.Container | |
public struct DIContainer { | |
public static let container = Container() | |
private init() { } | |
public static func regist(@noescape block: Container -> Void) { | |
container.removeAll() | |
block(container) | |
} | |
} | |
protocol UsesCoreComponents { | |
var userRepository: UserRepository { get } | |
} | |
extension UsesCoreComponents { | |
var userRepository: UserRepository { | |
return DIContainer.container.resolve(UserRepository.self)! // TODO: Handle errors and logging. | |
} | |
} |
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
DIContainer { container in | |
container.register(UserRepository.self) { _ in UserRepositoryImpl(...) } | |
.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
struct HogeUseCase: UsesCoreComponents { | |
func users() -> Future<[User], Error>{ | |
return userRepository.findAll().map { ... } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment