Coder Social home page Coder Social logo

Comments (6)

paulz avatar paulz commented on May 25, 2024 3

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.

tkohout avatar tkohout commented on May 25, 2024 1

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.

paulz avatar paulz commented on May 25, 2024 1

And thank you for such an awesome pod, I've been using it on the last 3 project with the great success!

from swinjectautoregistration.

tkohout avatar tkohout commented on May 25, 2024

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.

paulz avatar paulz commented on May 25, 2024

Maybe as an optional module like
SwinjectResolveOptionals

from swinjectautoregistration.

jakubvano avatar jakubvano commented on May 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.