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

vermont42 opened this issue Jul 6, 2019 · 2 comments


None yet
2 participants
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
  argument: Bool.self,
  initializer: TestGameCenter.init(isAuthenticated:)

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

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

This comment has been minimized.

Copy link

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?


This comment has been minimized.

Copy link

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.