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

Support parameters in wire[] to override dependencies #7

Open
adamw opened this issue Jun 25, 2014 · 6 comments
Open

Support parameters in wire[] to override dependencies #7

adamw opened this issue Jun 25, 2014 · 6 comments

Comments

@adamw
Copy link
Member

adamw commented Jun 25, 2014

(One of the ideas from ScalaDays 2014 - by @matthewfarwell)

The wire[] method could accept parameters, which would be used to wire an instance of the given type; the parameters would take precedence over (override) vals/defs in wider scopes.

Example usage:

class Service(s2: Service2, s3: Service3, config: Map[String, String])

lazy val s2 = wire[Service2]
lazy val s3 = wire[Service3]
lazy val defaultConfig = Map("username" -> "default")

// here the given map would be used instead of the default
lazy val sA = wire[Service](Map("username" -> "admin"))

// here the default config map would be used and an alternative Service2 impl
lazy val sB = wire[Service](new Service2(...))

A parameter which doesn't match any of the constructor parameters of the wired type would result in a compile-time error.

This can be useful for:

  • providing alternative implementations only for a single usage
  • providing configuration values on a per-instance basis
@adamw adamw mentioned this issue Jun 25, 2014
@ktoso
Copy link

ktoso commented Jun 25, 2014

+1
Make the error messages extra good on this one; could potentially be quite confusing sometimes (when ambiguous).

@ewiner
Copy link

ewiner commented Feb 6, 2015

As a workaround, you can kind of trick Macwire to do this by making a factory then using it just once:

lazy val sA = {
  def make(config: Map[String, String]): Service = wire[Service]
  make(Map("username" -> "admin"))
}

@adamw
Copy link
Member Author

adamw commented Feb 7, 2015

That must be the first MacWire hack - nice ;)

@backuitist
Copy link
Contributor

Since #11 is implemented you can also do this:

lazy val sA = {
   val config = Map("username" -> "admin")
   wire[Service]
}

The advantage over wire[Service](Map("username" -> "admin")) is that you get to name your params.

@phkorn
Copy link

phkorn commented Feb 2, 2023

Since #11 is implemented you can also do this:

lazy val sA = {
   val config = Map("username" -> "admin")
   wire[Service]
}

The advantage over wire[Service](Map("username" -> "admin")) is that you get to name your params.

Can we add this method of solving ambiguity to the readme? In some cases it seems like a more practical approach than using Qualifiers

@adamw
Copy link
Member Author

adamw commented Feb 3, 2023

@k0rn1 please do, PR will be great :)

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

5 participants