This file contains hidden or 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 ProductsRepositoryInjectable { | |
var products : ProductsRepository {get} | |
} | |
extension ProductsRepositoryInjectable { | |
var products : ProductsRepository { | |
return ProductsRepositoryImplementation() | |
} | |
This file contains hidden or 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 ProductViewModel: ProductsRepositoryInjectable { | |
init() { | |
self.products.fetchProducts().forEach { | |
print("This \($0.name) costs R\($0.price)") | |
} | |
} | |
} | |
ProductViewModel() |
This file contains hidden or 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 ProductsRepositoryImplementation : ProductsRepository { | |
func fetchProducts() -> [Product] { | |
return [Product(name: "Adidas Sneakers", price: 2030.0), Product(name: "Nike Sneakers", price: 1000.0)] | |
} | |
} |
This file contains hidden or 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
//: Playground - noun: a place where people can play | |
import UIKit | |
//ARC (Weak and Strong reference) | |
class User : NSObject { | |
let name : String = "" | |
weak var surname : NSString? = "" | |
} |
This file contains hidden or 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
public protocol Registrable { | |
func register<T>(depedency: T.Type, implemenation: @escaping () -> T, objectScope: ObjectScope) | |
} |
This file contains hidden or 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
public protocol Resolvable { | |
func resolve<T>(_ dependency: T.Type) -> T | |
func reset() | |
} |
This file contains hidden or 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 DepedencyContainer { | |
private let container = Container() | |
public static let instance = DepedencyContainer() | |
} |
This file contains hidden or 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
extension DepedencyContainer : Registrable { | |
public func register<T>(depedency: T.Type, implemenation: @escaping () -> T, objectScope: ObjectScope = .graph) { | |
container.register(depedency, factory: { _ in implemenation() }).inObjectScope(objectScope) | |
} | |
} |
This file contains hidden or 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
extension DepedencyContainer : Resolvable { | |
public func resolve<T>(_ dependency: T.Type) -> T { | |
guard let implementation = container.resolve(dependency) else { | |
fatalError("Nothing to Resolve") | |
} | |
return implementation | |
} | |
public func reset() { |
This file contains hidden or 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 Resolver { | |
private static var container: Resolvable = DepedencyContainer.instance | |
} |