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

Proposition for function registration #458

Open
twittemb opened this issue Aug 1, 2020 · 5 comments
Open

Proposition for function registration #458

twittemb opened this issue Aug 1, 2020 · 5 comments

Comments

@twittemb
Copy link

twittemb commented Aug 1, 2020

Hi everyone,

I've been using Swinject for 2 years now (very great project). Gradually, I have adopted a functional programming style and I need to register/resolve functions thanks to Swinject.

As there can be many functions having the same signature, the only way to discriminate them is using a name (from my understanding).

I have implemented a helper to avoid explicitly using names. It is called a NamedService. It is a protocol that provides a default name for you. All you have to do is declare the conformance to this protocol like that:

enum MyFunctionNamedService: NamedService {
    typealias Service = (String, Int) -> Int
}

enum MyOtherFunctionNamedService: NamedService {
    typealias Service = (String, Int) -> Int
}

And then you can register/resolve this namedService:

container.register(namedService: MyFunctionNamedService.self, factory: { ... })
container.register(namedService: MyOtherFunctionNamedService.self, factory: { ... })

...

let myFunction = resolver.resolve(namedService: MyFunctionNamedService.self)!
let myOtherFunction = resolver.resolve(namedService: MyOtherFunctionNamedService.self)!

Would this be interesting to upstream this feature to the Swinject repo ? @jakubvano @yoichitgy

Thanks.

@davidthorn
Copy link
Member

Nice idea, but the first issue we need to resolve, is which one of the contributors is going to continue to maintain the project.

@twittemb
Copy link
Author

Thanks for the response

I did not realize the project was not maintained anymore :-(

@davidthorn
Copy link
Member

Hi, @twittemb I would be very interested in using your idea in my forked version of Swinject.
Create a PR on my version and it will be merged: https://github.com/davidthorn/Swinject

@twittemb
Copy link
Author

twittemb commented Aug 17, 2020

Hi @davidthorn

ok no pb. Will do.

@twittemb
Copy link
Author

@davidthorn just opened a PR in your repo 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants