Injection factory with Macwire
1 answer
Injection factories are supported, but this is not really a MacWire feature, but in the spirit of MacWire, you can "just use Scala".
In this case, you can use function types. Following the Guice examples, let's say you want to create a parameter Payment
parameterized startDate: Date
and amount: Money
. You can define a dependency:
val paymentFactory = (startDate: Date, amount: Money) => wire[Payment]
// or create the payment in any other way
and then use it as a normal dependency:
class ServiceUsingPayment(paymentFactory: (Date, Money) => Payment)
val serviceUsingPayment = wire[ServiceUsingPayment]
You can also use a type alias to avoid duplicating the function signature and use this alias when declaring other service dependencies (as in the ServiceUsingPayment
above):
type PaymentFactory = (Date, Money) => Payment
+2
source to share