DIKit
DIKit copied to clipboard
Defining dependencies with arguments
This PR is a draft to have a discussion if this is a thing we want to add at all and how.
This PR adds support for dependencies with arguments. I am looking forward for any feedback and thoughts about this.
Usage
// Defining a resolvable
factory { LocalStorage(name: "default") as LocalStorageProtocol }
factory { name in LocalStorage(name: name) as LocalStorageProtocol }
factory { (args: (name: String, other: Int)) in LocalStorage(name: args.name) as LocalStorageProtocol }
// Resolving a depdendency
@Inject var defaultLocalStorage: LocalStorageProtocol
@Inject("With just one parameter") var localStorageWithJustOneParameter: LocalStorageProtocol
@Inject((name: "Local Storage with multiple parameters", other: 1)) var localStorageWithMultipleParameters: LocalStorageProtocol
let localStorage: LocalStorageProtocol = resolve("The name of the storage")
func doSomething(_ storage: LocalStorageProtocol = resolve("storage name")) { }
Implementation details
- Only factories can have an argument
- One cannot define a resolvable with a tag and arguments
- It is possible to define multiple resolvables of the same type with different arguments
- resolvables can only have a single argument. Multiple arguments can be resolved using tuples
- The type of the resolvable at definition time must match exactly the one at resolution time. E.g.
String
!=String?
or(String, Int)
!=(name: String, age: Int)
.
Todos
- [ ] Add documentation in code
- [ ] Add documentation in the Readme
Hey @benjohnde, is this something interesting for DIKit?