Injection factory with Macwire

I am encouraging MacWire for dependency injection.

One thing I found helpful with Guice is the helper injection to autwire factory, which will help you create some service that needs startup parameters.

Is there something similar with Macwire?

+3


source to share


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







All Articles