Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named first runtime argument in resolve #19

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#### New Features

* Added support for circular dependencies by adding `ObjectGraph` scope to reuse resolved instances.
* Added support for circular dependencies:
* Added `ObjectGraph` scope to reuse resolved instances
* Added `resolveDependencies` method on `DefinitionOf` class to resolve dependencies of resolved instance.
[#11](https://github.com/AliSoftware/Dip/pull/11), [@ilyapuchka](https://github.com/ilyapuchka)
* Added methods to register/remove individual definitions.
[#11](https://github.com/AliSoftware/Dip/pull/11), [@ilyapuchka](https://github.com/ilyapuchka)
Expand All @@ -14,6 +16,7 @@
* Removed container thread-safety to enable recursion calls to `resolve`.
**Access to container from multiple threads should be handled by clients** from now on.
* Deprecated `register(tag:instance:)` method in favor of `register(.Singleton, …)`.
* Added name for the first runtime argument in `resolve(tag:withArguments:)` methods to make more clear separation between tag and factory runtime arguments.

## 3.0.0

Expand Down
48 changes: 42 additions & 6 deletions Dip/Dip/RuntimeArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ extension DependencyContainer {

- seealso: `resolve(tag:builder:)`
*/
public func resolve<T, Arg1>(tag tag: Tag? = nil, withArguments arg1: Arg1) -> T {
return resolve(tag: tag) { (factory: (Arg1) -> T) in factory(arg1) }
}

///**Deprecated** Use `resolve(tag:withArguments:)` method instead.
@available(*, deprecated, message="Use `resolve(tag:withArguments:)` method instead.")
public func resolve<T, Arg1>(tag tag: Tag? = nil, _ arg1: Arg1) -> T {
return resolve(tag: tag) { (factory: (Arg1) -> T) in factory(arg1) }
}
Expand All @@ -68,6 +74,12 @@ extension DependencyContainer {
}

/// - seealso: `resolve(tag:_:)`
public func resolve<T, Arg1, Arg2>(tag tag: Tag? = nil, withArguments arg1: Arg1, _ arg2: Arg2) -> T {
return resolve(tag: tag) { (factory: (Arg1, Arg2) -> T) in factory(arg1, arg2) }
}

///**Deprecated** Use `resolve(tag:withArguments:_:)` method instead.
@available(*, deprecated, message="Use `resolve(tag:withArguments:_:)` method instead.")
public func resolve<T, Arg1, Arg2>(tag tag: Tag? = nil, _ arg1: Arg1, _ arg2: Arg2) -> T {
return resolve(tag: tag) { (factory: (Arg1, Arg2) -> T) in factory(arg1, arg2) }
}
Expand All @@ -78,7 +90,13 @@ extension DependencyContainer {
return registerFactory(tag: tag, scope: scope, factory: factory) as DefinitionOf<T, (Arg1, Arg2, Arg3) -> T>
}

/// - seealso: `resolve(tag:_:)`
/// - seealso: `resolve(tag:withArguments:)`
public func resolve<T, Arg1, Arg2, Arg3>(tag tag: Tag? = nil, withArguments arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3) -> T {
return resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3) -> T) in factory(arg1, arg2, arg3) }
}

///**Deprecated** Use `resolve(tag:withArguments:_:_:)` method instead.
@available(*, deprecated, message="Use `resolve(tag:withArguments:_:_:)` method instead.")
public func resolve<T, Arg1, Arg2, Arg3>(tag tag: Tag? = nil, _ arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3) -> T {
return resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3) -> T) in factory(arg1, arg2, arg3) }
}
Expand All @@ -89,29 +107,47 @@ extension DependencyContainer {
return registerFactory(tag: tag, scope: scope, factory: factory) as DefinitionOf<T, (Arg1, Arg2, Arg3, Arg4) -> T>
}

/// - seealso: `resolve(tag:_:)`
/// - seealso: `resolve(tag:withArguments:)`
public func resolve<T, Arg1, Arg2, Arg3, Arg4>(tag tag: Tag? = nil, withArguments arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4) -> T {
return resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, Arg4) -> T) in factory(arg1, arg2, arg3, arg4) }
}

///**Deprecated** Use `resolve(tag:withArguments:_:_:_:)` method instead.
@available(*, deprecated, message="Use `resolve(tag:withArguments:_:_:_:)` method instead.")
public func resolve<T, Arg1, Arg2, Arg3, Arg4>(tag tag: Tag? = nil, _ arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4) -> T {
return resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, Arg4) -> T) in factory(arg1, arg2, arg3, arg4) }
}

// MARK: 4 Runtime Arguments
// MARK: 5 Runtime Arguments

public func register<T, Arg1, Arg2, Arg3, Arg4, Arg5>(tag tag: Tag? = nil, _ scope: ComponentScope = .Prototype, factory: (Arg1, Arg2, Arg3, Arg4, Arg5) -> T) -> DefinitionOf<T, (Arg1, Arg2, Arg3, Arg4, Arg5) -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory) as DefinitionOf<T, (Arg1, Arg2, Arg3, Arg4, Arg5) -> T>
}

/// - seealso: `resolve(tag:_:)`
/// - seealso: `resolve(tag:withArguments:)`
public func resolve<T, Arg1, Arg2, Arg3, Arg4, Arg5>(tag tag: Tag? = nil, withArguments arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4, _ arg5: Arg5) -> T {
return resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, Arg4, Arg5) -> T) in factory(arg1, arg2, arg3, arg4, arg5) }
}

///**Deprecated** Use `resolve(tag:withArguments:_:_:_:_:)` method instead.
@available(*, deprecated, message="Use `resolve(tag:withArguments:_:_:_:_:)` method instead.")
public func resolve<T, Arg1, Arg2, Arg3, Arg4, Arg5>(tag tag: Tag? = nil, _ arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4, _ arg5: Arg5) -> T {
return resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, Arg4, Arg5) -> T) in factory(arg1, arg2, arg3, arg4, arg5) }
}

// MARK: 5 Runtime Arguments
// MARK: 6 Runtime Arguments

public func register<T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(tag tag: Tag? = nil, _ scope: ComponentScope = .Prototype, factory: (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) -> T) -> DefinitionOf<T, (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory) as DefinitionOf<T, (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) -> T>
}

/// - seealso: `resolve(tag:_:)`
/// - seealso: `resolve(tag:withArguments:)`
public func resolve<T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(tag tag: Tag? = nil, withArguments arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4, _ arg5: Arg5, _ arg6: Arg6) -> T {
return resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) -> T) in factory(arg1, arg2, arg3, arg4, arg5, arg6) }
}

///**Deprecated** Use `resolve(tag:withArguments:_:_:_:_:_:)` method instead.
@available(*, deprecated, message="Use `resolve(tag:withArguments:_:_:_:_:_:)` method instead.")
public func resolve<T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(tag tag: Tag? = nil, _ arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4, _ arg5: Arg5, _ arg6: Arg6) -> T {
return resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) -> T) in factory(arg1, arg2, arg3, arg4, arg5, arg6) }
}
Expand Down
36 changes: 18 additions & 18 deletions Dip/DipTests/RuntimeArgumentsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RuntimeArgumentsTests: XCTestCase {
})

//when
let service = container.resolve(arg1) as Service
let service = container.resolve(withArguments: arg1) as Service

//then
XCTAssertTrue(service is ServiceImp1)
Expand All @@ -72,7 +72,7 @@ class RuntimeArgumentsTests: XCTestCase {
}

//when
let service = container.resolve(arg1, arg2) as Service
let service = container.resolve(withArguments: arg1, arg2) as Service

//then
XCTAssertTrue(service is ServiceImp1)
Expand All @@ -88,7 +88,7 @@ class RuntimeArgumentsTests: XCTestCase {
}

//when
let service = container.resolve(arg1, arg2, arg3) as Service
let service = container.resolve(withArguments: arg1, arg2, arg3) as Service

//then
XCTAssertTrue(service is ServiceImp1)
Expand All @@ -105,7 +105,7 @@ class RuntimeArgumentsTests: XCTestCase {
}

//when
let service = container.resolve(arg1, arg2, arg3, arg4) as Service
let service = container.resolve(withArguments: arg1, arg2, arg3, arg4) as Service

//then
XCTAssertTrue(service is ServiceImp1)
Expand All @@ -123,7 +123,7 @@ class RuntimeArgumentsTests: XCTestCase {
}

//when
let service = container.resolve(arg1, arg2, arg3, arg4, arg5) as Service
let service = container.resolve(withArguments: arg1, arg2, arg3, arg4, arg5) as Service

//then
XCTAssertTrue(service is ServiceImp1)
Expand All @@ -142,7 +142,7 @@ class RuntimeArgumentsTests: XCTestCase {
}

//when
let service = container.resolve(arg1, arg2, arg3, arg4, arg5, arg6) as Service
let service = container.resolve(withArguments: arg1, arg2, arg3, arg4, arg5, arg6) as Service

//then
XCTAssertTrue(service is ServiceImp1)
Expand All @@ -155,8 +155,8 @@ class RuntimeArgumentsTests: XCTestCase {
container.register { (a1: Int, a2: Int) in ServiceImp2() as Service }

//when
let service1 = container.resolve(arg1) as Service
let service2 = container.resolve(arg1, arg2) as Service
let service1 = container.resolve(withArguments: arg1) as Service
let service2 = container.resolve(withArguments: arg1, arg2) as Service

//then
XCTAssertTrue(service1 is ServiceImp1)
Expand All @@ -170,8 +170,8 @@ class RuntimeArgumentsTests: XCTestCase {
container.register(factory: { (a1: String) in ServiceImp2() as Service })

//when
let service1 = container.resolve(arg1) as Service
let service2 = container.resolve(arg2) as Service
let service1 = container.resolve(withArguments: arg1) as Service
let service2 = container.resolve(withArguments: arg2) as Service

//then
XCTAssertTrue(service1 is ServiceImp1)
Expand All @@ -185,8 +185,8 @@ class RuntimeArgumentsTests: XCTestCase {
container.register { (a1: String, a2: Int) in ServiceImp2() as Service }

//when
let service1 = container.resolve(arg1, arg2) as Service
let service2 = container.resolve(arg2, arg1) as Service
let service1 = container.resolve(withArguments: arg1, arg2) as Service
let service2 = container.resolve(withArguments: arg2, arg1) as Service

//then
XCTAssertTrue(service1 is ServiceImp1)
Expand All @@ -197,11 +197,11 @@ class RuntimeArgumentsTests: XCTestCase {
//given
let arg1 = 1, arg2 = 2
container.register { (a1: Int, a2: Int) in ServiceImp1() as Service }
let service1 = container.resolve(arg1, arg2) as Service
let service1 = container.resolve(withArguments: arg1, arg2) as Service

//when
container.register { (a1: Int, a2: Int) in ServiceImp2() as Service }
let service2 = container.resolve(arg1, arg2) as Service
let service2 = container.resolve(withArguments: arg1, arg2) as Service

//then
XCTAssertTrue(service1 is ServiceImp1)
Expand All @@ -217,11 +217,11 @@ class RuntimeArgumentsTests: XCTestCase {

//when
let url: NSURL = NSURL(string: "http://example.com")!
let service1 = container.resolve(80, url) as Service
let service2 = container.resolve(80, NSURL(string: "http://example.com")) as Service
let service1 = container.resolve(withArguments: 80, url) as Service
let service2 = container.resolve(withArguments: 80, NSURL(string: "http://example.com")) as Service

let service3 = container.resolve(80, NSURL(string: "http://example.com")! as NSURL!) as Service
let service4 = container.resolve(80, NSURL(string: "http://example.com")!) as Service
let service3 = container.resolve(withArguments: 80, NSURL(string: "http://example.com")! as NSURL!) as Service
let service4 = container.resolve(withArguments: 80, NSURL(string: "http://example.com")!) as Service

//then
XCTAssertEqual(service1.getServiceName(), name1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ container.register { (port: Int, url: NSURL?) in ServiceImp4(name: "3", baseURL:
container.register { (port: Int, url: NSURL!) in ServiceImp4(name: "4", baseURL: url, port: port) as Service }

let url: NSURL = NSURL(string: "http://example.com")!
let service1 = container.resolve(url, 80) as Service
let service2 = container.resolve(80, url) as Service
let service3 = container.resolve(80, NSURL(string: "http://example.com")) as Service
let service4 = container.resolve(80, NSURL(string: "http://example.com")! as NSURL!) as Service
let service1 = container.resolve(withArguments: url, 80) as Service
let service2 = container.resolve(withArguments: 80, url) as Service
let service3 = container.resolve(withArguments: 80, NSURL(string: "http://example.com")) as Service
let service4 = container.resolve(withArguments: 80, NSURL(string: "http://example.com")! as NSURL!) as Service

(service1 as! ServiceImp4).name
(service2 as! ServiceImp4).name
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ let webServices = DependencyContainer() { webServices in
webServices.register { (port: Int, url: NSURL?) in WebServiceImp3(url!, port: port) as WebServiceAPI }
}

let service1 = webServices.resolve(NSURL(string: "http://example.url")!, 80) as WebServiceAPI // service1 is WebServiceImp1
let service2 = webServices.resolve(80, NSURL(string: "http://example.url")!) as WebServiceAPI // service2 is WebServiceImp2
let service3 = webServices.resolve(80, NSURL(string: "http://example.url")) as WebServiceAPI // service3 is WebServiceImp3
let service1 = webServices.resolve(withArguments: NSURL(string: "http://example.url")!, 80) as WebServiceAPI // service1 is WebServiceImp1
let service2 = webServices.resolve(withArguments: 80, NSURL(string: "http://example.url")!) as WebServiceAPI // service2 is WebServiceImp2
let service3 = webServices.resolve(withArguments: 80, NSURL(string: "http://example.url")) as WebServiceAPI // service3 is WebServiceImp3

```
Though Dip provides support for up to six runtime arguments out of the box you can extend this number using following code snippet for seven arguments:
Expand All @@ -156,7 +156,7 @@ func register<T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(tag: Tag? = nil, scop
return registerFactory(tag, scope: .Prototype, factory: factory) as DefinitionOf<T, (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7) -> T)>
}

func resolve<T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(tag tag: Tag? = nil, _ arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4, _ arg5: Arg5, _ arg6: Arg6, _ arg7: Arg7) -> T {
func resolve<T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(tag tag: Tag? = nil, withArguments arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4, _ arg5: Arg5, _ arg6: Arg6, _ arg7: Arg7) -> T {
return resolve(tag) { (factory: (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7) -> T) in factory(arg1, arg2, arg3, arg4, arg5, arg6, arg7) }
}

Expand Down