Coder Social home page Coder Social logo

Comments (5)

yoichitgy avatar yoichitgy commented on July 30, 2024

You can do that if I caught your question correctly.

Here is the document:
https://github.com/Swinject/Swinject/blob/master/Documentation/DIContainer.md#registration-with-arguments-to-di-container

from swinject.

nayzak avatar nayzak commented on July 30, 2024

In documentation nothing mentioned about parameters and initCompleted callback. In my case I have a view model, that depends on some parameters and services. So I registering it like this:

container.register(MenuPageModeller.self) {  r, menuId in 
  MenuPageModel(menuId: menuId, apiService: r.resolve(ApiService.self)!) 
}

And I have a view controller, that have public property viewModel: MenuPageModeller. I'd like to inject this property automatically. Instead of doing this:

let viewModel = container.resolve(MenuPageModeller.self, argument: 123)
let page = MenuPage()
page.viewModel = viewModel

I'd like to do this:

let page = container.resolve(MenuPage.self, argument: 123)

Is that possible? Or I'm trying to do something weird? I'm new to DI :)

from swinject.

yoichitgy avatar yoichitgy commented on July 30, 2024

You can do that something like this without using initCompleted if your types do not have circular dependencies.

container.register(MenuPageModeller.self) {  r, menuId in MenuPageModel(menuId: menuId) }

container.register(MenuPage.self) { r, menuId in
         let page = MenuPage()
         page.viewModel = r.resolve(MenuPageModeller.self, argument: menuId)
         return page
     }

then

let page = container.resolve(MenuPage.self, argument: 123)

(initCompleted doesn't support taking arguments.)

from swinject.

nayzak avatar nayzak commented on July 30, 2024

Thanks! I have already tried this example before asking my question, but compiler didn't understand this code:

container.register(MenuPage.self) { r, menuId in
         let page = MenuPage()
         page.viewModel = r.resolve(MenuPageModeller.self, argument: menuId)
         return page
     }

After your answer I tried it again and figured out, that we need to specify closure arguments types:

container.register(MenuPage.self) { (r: ResolverType, menuId: Int) in
      let page = MenuPage()
      page.viewModel = r.resolve(MenuPageModeller.self, argument: menuId)!
      return page
    }

And this works as expected.

Thank you again! ๐Ÿ‘

from swinject.

yoichitgy avatar yoichitgy commented on July 30, 2024

@nayzak that's good to hear you got the solutionโ—๏ธI forgot mentioning the explicit type specification in case the Swift compiler cannot infer the types.

from swinject.

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.