//
//  Container.swift
//  Swinject
//
//  Created by Yoichi Tagaya on 7/23/15.
//  Copyright © 2015 DevSwinject. All rights reserved.
//

import Foundation

public final class Container {
    private var factories = [Any]()
    
    // The first argument is for a workaround to the problem that cannot explicitly specialize a generic function in Swift.
    // http://stackoverflow.com/questions/28726937/how-do-i-call-a-generic-swift-function-when-none-of-the-arguments-provides-the-g
    public func register<Service>(_: Service.Type, factory: Container -> Service) {
        factories += [factory as Any]
    }
    
    public func resolve<Service>(_: Service.Type) -> Service? {
        for factory in factories {
            if let factory = factory as? Container -> Service {
                return factory(self)
            }
        }
        return nil
    }
}

//
//  Container.swift
//  Swinject
//
//  Created by Yoichi Tagaya on 7/23/15.
//  Copyright © 2015 DevSwinject. All rights reserved.
//

import Foundation

public final class Container {
    private var factories = [String: Any]()
    
    public func register<Service>(serviceType: Service.Type, factory: Container -> Service) {
        factories[String(serviceType)] = factory as Any
    }
    
    public func resolve<Service>(serviceType: Service.Type) -> Service? {
        if let factory = factories[String(serviceType)] as? Container -> Service {
            return factory(self)
        }
        return nil
    }
}

Use a dictionary to store the factories as Funq does.
Use a dictionary to store the factories as Funq does.
