Skip to content

Instantly share code, notes, and snippets.

@ukitaka
Last active July 22, 2016 06:42
Show Gist options
  • Save ukitaka/da07582f59576d4be54a68459eed1d5f to your computer and use it in GitHub Desktop.
Save ukitaka/da07582f59576d4be54a68459eed1d5f to your computer and use it in GitHub Desktop.
SwinjectとProtocol Extensionを用いたDIパターン
@_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.
}
}
DIContainer { container in
container.register(UserRepository.self) { _ in UserRepositoryImpl(...) }
.inObjectScope(.Container)
}
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