hmlongco / Resolver

Swift Ultralight Dependency Injection / Service Locator framework
MIT License
2.15k stars 190 forks source link

Allow passing arguments via @Injected and @LazyInjected #45

Closed smamczak closed 3 years ago

smamczak commented 4 years ago

Any chances to make this possible?

hmlongco commented 4 years ago

The develop branch has been updated with changes to the argument passing mechanism in Resolver. (Make sure you switch to the develop branch.)

That said, I probably will not make any changes to the property wrappers. (EDITED)

Property wrappers are instantiated immediately along with the rest of the object's properties prior to init() being called, so self isn't available and as such the only thing you could pass would be fixed constants and not variables.

As such, I'm not sure how valuable that would be.

smamczak commented 4 years ago

Hi, you are right. It makes sense only for @LazyInjected. Looking forward to the update.

I checked your code. Why don't you use variadic parameters fo passing multiple args? Less code and work in my opinion

hmlongco commented 4 years ago

See edited comment above. Doesn't make sense for Lazy either.

hmlongco commented 4 years ago

Considered variadic parameters, but I wanted to reinforce the 1-to-1 relationship between the args passed and the args consumed in the factory. (arg0 = arg[0], arg1 = arg[1]

register { (_ args) in ABC(name: args[0]!) }

let abc: ABC = Resolver.resolve(arg0: "fred")

With variadics getting the order wrong would be even easier.

Still wondering if I should switch to a dictionary instead.

register { (_ args) in ABC(name: args["name"]!) }

let abc: ABC = Resolver.resolve(args: ["name": "fred"])
smamczak commented 4 years ago

Dictionary is more deterministic in my opinion.

In current stage of @propertyWrappers I see there is no way to call self and access class instance. Maybe they will change it in the future, but for now it looks like there is nothing we can do.

hmlongco commented 4 years ago

Check out the develop branch for 1.2. There's actually a way to pass arguments thru @LazyInjected.

https://github.com/hmlongco/Resolver/blob/develop/Documentation/Arguments.md

smamczak commented 4 years ago

Great! Thanks a lot!

doozMen commented 4 years ago

Hei I really love the good and considered way this project is build and how you discussed arguments here. I hope to contribute to the discussion. I will make an MR and reference it here for the arguments. I made it from master before I read this and now see that my solution is not optimal and I might need to reconsider. But one thing I noticed while playing around is that when you register for a Type 2 ways to inject it with property wrappers you would, one with and one without arguments you would expect them to resolve differently, but they do not. I tried to clarify that in an MR. Please let me know what you think. Thanks

hmlongco commented 3 years ago

As mentioned above, argument passing was updated for 1.2, but not for property wrappers.