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

Possible Limitation of SwinjectAutoregistration Involving Initializers #51

Closed
vermont42 opened this issue Jul 6, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@vermont42
Copy link

commented Jul 6, 2019

Am I correct that, when I use SwinjectAutoregistration, no component initializer may be private?

In my app, I have a service/protocol, GameCenterable, with two components that implement that protocol, RealGameCenter and TestGameCenter. RealGameCenter has a private initializer, which takes no arguments. TestGameCenter has an internal initializer which takes a Bool argument, isAuthenticated. For context, I show in the snippets below registration and use of TestGameCenter and RealGameCenter. To make the latter work, I had to change the initializer of RealGameCenter from private to internal.

Vanilla Swinject does not have this limitation, but I would prefer to use SwinjectAutoregistration so I don't get Optionals from the container.

// registration and use of TestGameCenter component
container.autoregister(
  GameCenterable.self,
  argument: Bool.self,
  initializer: TestGameCenter.init(isAuthenticated:)
)

let gameCenter = container ~> (GameCenterable.self, argument: false)

// registration and use of RealGameCenter component
container.autoregister(
  GameCenterable.self,
  initializer: RealGameCenter.init
)
                           
let gameCenter = container ~> (GameCenterable.self)
@jakubvano

This comment has been minimized.

Copy link
Member

commented Jul 8, 2019

AFAIK there is nothing special about SwinjectAutoregistration with regard to the access modifiers. If .init is private, you cannot reference it from other source file - shouldn't matter in what context.
What puzzles me though, is that vanilla register works 🤔
Is your injection code in the same sourcefile as the definition of RealGameCenter.init? Could it be that RealGameCenter has more that one init? Can you reproduce this behavior in isolated example?

@vermont42

This comment has been minimized.

Copy link
Author

commented Jul 9, 2019

Thanks, @jakubvano.

Looking at the code now, I see that I had to comment out the line making RealGameCenter's initializer private, even for vanilla register. My observation was incorrect.

Makes sense that a private initializer would be problematic for Swinject.

In my conference talk and blog post, in the section on Swinject, I plan to mention, as a positive of Swinject, the help that you and other Swinject contributors provided.

@vermont42 vermont42 closed this Jul 9, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.