0

Consider the following class:

class MongoDumpBuilder (recordingId: String, parquetService: ParquetService) {
...
}

I want to inject the parquetService, the problem is that I want to create MongoDumpBuilder as follows:

NOTE: I want to pass recordingId.stringify to the class

new MongoDumpBuilder(recordingId.stringify)
        .withPacificEvents(pacificEvents)
        .withAssets(assets)
        .withTransactions(transactions)
        .withReports(reports)
        .withExpenses(expenses)
        .build()

But this will work only with:

new MongoDumpBuilder(recordingId.stringify, parquetService)

If i will do MongoDumpBuilder with Inject i cannot create it

class MongoDumpBuilder @Inject()(recordingId: String, parquetService: ParquetService) {
???
}

Any ideas how to solve it?

0

1 Answer 1

1

You can use naming in order to bind recordingId. Add this to your Module:

bind(classOf[String]).annotatedWith(Names.named("recordingId")).toInstance(recordingId.stringify)

Then define your class:

class MongoDumpBuilder @Inject()(@Named("recordingId") val recordingId: String, parquetService: ParquetService) {
...
}
Sign up to request clarification or add additional context in comments.

5 Comments

Oh, thanks but thats will not work for me, since there mongoDumpBuilder for a lot of different recordingIds.
@ZviMints do you have a single ParquetService?
Its singleton but its not an object, its inject other dependenceis
@ZviMints Then I don't think you are doing a good usage of guice. Since you have a lot of recordingIds, it shouldn't be a class parameter, but a method. You should create one instance of MongoDumpBuilder, and pass inside the recordingId.
I got your advise, I changed the ParquetService to be object without dependences, Thanks as always :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.