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

Injecting objects #1

Open
AdieOlami opened this issue Oct 26, 2022 · 2 comments
Open

Injecting objects #1

AdieOlami opened this issue Oct 26, 2022 · 2 comments

Comments

@AdieOlami
Copy link

Hi, I'm just wondering how you would inject parameters into a service.

Parameters like string, bool, int etc.

@chFlorian
Copy link
Owner

When you're setting the dependency, you can just pass your arguments via the initialiser, e.g.

DependencyValues[\.userService] = MockUserService(myParameter1: 0, myParameter2: true)

Does this help?

@AdieOlami
Copy link
Author

AdieOlami commented Oct 30, 2022

This is you passing static values. Example

if the parameters change based on scenario probably it is a Codable or Equatable type rather than just String or Bool.

do you just create your key like this or how do you handle that

private struct UserServiceKey: DependencyKey {
static var currentValue: UserService = MyUserService(model: SomeModel(name: "abc"))
}

extension DependencyValues {
var userService: UserService {
get { Self[UserServiceKey.self] }
set { Self[UserServiceKey.self] = newValue }
}
}

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

No branches or pull requests

2 participants