Injectable factory with Macwire

筅森魡賤 提交于 2019-12-12 14:30:38

问题


I'm teasing out MacWire for dependency injection.

One thing that i found useful with Guice is the assisted inject, to autowire a factory that would help you to create some service that needs run parameters.

Is there something similar with Macwire ?


回答1:


Injectable factories are supported, but are not really a feature of MacWire, rather in MacWire's spirit, 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 Payment parametrized by a startDate: Date and amount: Money. You could 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 could also use a type alias to avoid repeating the function signature, and use that alias when declaring another service's dependencies (as in ServiceUsingPayment above):

type PaymentFactory = (Date, Money) => Payment


来源:https://stackoverflow.com/questions/25457400/injectable-factory-with-macwire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!