Skip to content

Injecting objects #1

Open
Open

Description

@AdieOlami

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

Parameters like string, bool, int etc.

Activity

  1. FloWritesCode commented on Oct 26, 2022

    @FloWritesCode
    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?

  2. AdieOlami commented on Oct 30, 2022

    @AdieOlami
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions