Swinject Service Class Without Parameters Failing to Register in .Container Scope with Swift 3.0: Why

Swinject service class without parameters failing to register in .container scope with Swift 3.0: why?

As Jakub has commented, the issue lies with your capitalization of .Container. Update the registration to the following:

defaultContainer.register(IndependentProtocol.self, factory: { _ in 
IndependentService()
}).inObjectScope(.container)

Swinject - Ambiguous reference to member

I faced the same issue and i think compiler could be a bit more verbose in this case.

Anyway, my problem was on my side, not in Swinject

Check the following:

  1. NetworkModeling and Network are visible in scope of your registration (they are public, or internal in the same module. remember, that swift3 introduced fileprivate and many other specifiers, so make sure your identifiers are visible to registeting code

  2. Make sure that Network conforms to NetworkModeling. Being unable to see inheritance, swift compiler raises error about ambigous types for Swinject factory

Hope, this helps

Dependency injection inconsistency in differing ViewControllers in Swinject, post Swift 3.0 update: why?

It turns out this is a bug, detailed here: https://github.com/Swinject/Swinject/issues/177. A fix is currently being worked on. I will report back once I know more.

UPDATE

This is apparently fixed.

Why is Swinject model class registered without .inObjectScope( .Container ) producing a singleton?

Default scope for services is .Graph. From documentation:

With ObjectScope.Graph, an instance is always created, as in ObjectScope.None, if you directly call resolve method of a container, but instances resolved in factory closures are shared during the resolution of the root instance to construct the object graph.

If you want a unique instance to be created for each reference even during object graph resolution, you should use object scope .None, i.e.

defaultContainer.register(Stopwatch.self) { resolver in 
Stopwatch(signals: resolver.resolve(SignalsService.self)!)
}.inObjectScope(.None)


Related Topics



Leave a reply



Submit