Comments (6)
Here is how we solved injecting optionals on our project:
import Swinject
import SwinjectAutoregistration
protocol WrappingProtocol {
associatedtype Unwrapped
init(_ some: Unwrapped)
}
extension ImplicitlyUnwrappedOptional : WrappingProtocol {
typealias Unwrapped = Wrapped
}
extension Optional : WrappingProtocol {
typealias Unwrapped = Wrapped
}
postfix func ~> <Service: WrappingProtocol>(r: Resolver) -> Service {
return Service(r ~> Service.Unwrapped.self)
}
It allow us to injection optionals, for example:
container.storyboardInitCompleted(AccountViewController.self) { r, c in
c.account = r~>
c.accountManager = r~>
c.accountUpdater = r~>
}
from swinjectautoregistration.
Hello @paulz , thanks for taking an interest in the library.
Unfortunately, it is not possible to achieve registering optional or unwrapped dependencies technically. If you look at the source you can see there are generated autoregister functions for up to 20 dependencies.
For your example with PersonOwner following function is used:
public func autoregister<Service, A, B>(_ service: Service.Type, name: String? = nil, initializer: @escaping (A, B) -> Service)
To be able to use it with optional service and unwrap it, you would also have to have
initializer: (A, B?) -> Service
But also
initializer: (A?, B) -> Service
and
initializer: (A?, B?) -> Service
This leads to large number of permutations for 20 dependencies (20!) both for Optional and ImplicitlyUnwrapped dependencies.
I also tried to cast and unwrap the dependencies in the existing functions but because we are working with types and not instances I think it is not currently possible.
from swinjectautoregistration.
And thank you for such an awesome pod, I've been using it on the last 3 project with the great success!
from swinjectautoregistration.
I am not a big fan of adding new conformance to the native optional type. I would rather prefer solution in the Swinject itself. You can upvote this issue if you would like to have this functionality.
from swinjectautoregistration.
Maybe as an optional module like
SwinjectResolveOptionals
from swinjectautoregistration.
As of Swinject 2.3.0 resolution of the optional dependencies shoud work as described here, i.e
container.autoregister(PersonOwner.self, initializer: PersonOwner.init)
// or
container.register(PersonOwner.self) { PersonOwner(name: $0~>, pet: $0~> }
Will resolve pet
to an instance if there is a registration for Animal.self
, or to nil
if there isn't one.
This should work transparently with current vesion of SwinjectAutoregistration (provided you update the Swinject version)
from swinjectautoregistration.
Related Issues (20)
- Framework size HOT 1
- Xcode 10.2 Support HOT 2
- Release Swift5 version HOT 1
- Possible Limitation of SwinjectAutoregistration Involving Initializers HOT 2
- Mix dependencies with and without arguments HOT 1
- More auto extensions HOT 3
- Autoregistration for property injection HOT 9
- Auto-register on resolve HOT 1
- SwinjectAutoregistration crashes on Xcode 12.0 (12A7209) HOT 5
- Swinject crash on iOS 14 HOT 4
- Showing Ambiguous use of 'init' when opening the project on Xcode version 12.0 + HOT 3
- Reason for container.autoregister with max of 3 arguments only HOT 1
- arm64 is missing from ARCHS HOT 4
- How to auto register when init has default arguments? HOT 1
- Allow dynamic in Package.swift HOT 2
- Support for Xcode13 HOT 5
- Wrong version of Swinject on podspec HOT 7
- Build fails on Carthage without xcframeworks HOT 1
- the path does not point to a valid framework HOT 2
- Publish new dot release HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swinjectautoregistration.