Skip to content

Improve Optional Dependencies #457

Description

@dbagwell

It would be nice if the framework allowed for lower scope to have an optional property provided by an ancestor scope with a non-optional of the same type. Like was once mentioned in #66 .

In addition it would be great if optional dependencies didn't have to be satisfied by an ancestor scope. Where if an optional dependency isn't provided by an ancestor it just returns nil.

This would allow for something like this:

final class LoggedOutComponent: BootstrapComponent {
    
    func loggedInComponent(userName: String) -> LoggedInComponent {
        return LoggedInComponent(userName: userName, parent: self)
    }
    
    var gameComponent: GameComponent {
        return GameComponent(parent: self)
    }
    
}

final class LoggedInComponent: Component<EmptyDependency> {
    
    let userName: String
    
    init(userName: String, parent: Scope) {
        self.userName = userName
        super.init(parent: parent)
    }
    
    var gameComponent: GameComponent {
        return GameComponent(parent: self)
    }
    
}

protocol GameDependency: Dependency {
    var userName: String? { get }
}

final class GameComponent: Component<GameDependency> {}

In this example, GameComponent has an optional dependency of userName that would be provided by LoggedInComponent when created from that scope, but would be nil when created from the LoggedOutComponent scope since there is no provider.

Activity

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions