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

Defining dependencies with arguments #49

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

brototyp
Copy link
Contributor

@brototyp brototyp commented Dec 29, 2022

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

@brototyp
Copy link
Contributor Author

brototyp commented Mar 2, 2023

Hey @benjohnde, is this something interesting for DIKit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant