0

In common-project I have this:

trait DBProvider trait DBTableNamesProvider trait DefaultDBProvider extends DBProvider trait DefaultTableNames extends DBTableNamesProvider trait MyService extends DBProvider with DBTableNamesProvider object MyService { def apply() = new MyService with DefaultDBProvider with DefaultTableNames {} } 

In projectA which has a reference to common-project as a jar I wish to construct MyService

projectA (has dependency on common-project):

object MyOtherApp { trait MyOtherTableName extends DBTableNamesProvider val MyCustomService = MyService() with MyOtherTableName // will not compile how to reuse the module's MyService() with another implementation of one of the traits? } 

The above will not compile I cannot just call MyService() construction and override some of the dependencies.

The above is what I wish to do, I wish to override from a different project the factory construction of MyService() apply with my own implementation of MyProjectATableNames is that possible in scala? if not what is the recommended way without code repetition?

2 Answers 2

1
 val MyCustomService = new MyService() with MyOtherTableName 

should work

If you want to also inherit from the DefaultDBProvider and DefaultTableNames, you would have to either list them explicitly as well:

val MyCustomService = new MyService() with MyOtherTableName with DefaultDBProvider with DefaultTableNames 

or create an intermediate trait in the common library:

trait DefaultService extends MyService with DefaultDBProvider with DefaultTableNames 
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot override constructed type like that because MyService() is an object not a type anymore. So to reuse your code you might create a class like

class ConfiguredMyService extends DefaultDBProvider with DefaultTableNames 

in the first app you can declare

object MyService { def apply() = new ConfiguredMyService } 

and in the second

val MyCustomService = new ConfiguredMyService with MyOtherTableName 

Note: Nowadays, Cake pattern is considered an anti-pattern, so I recommend you checkout Dependency Injection.

1 Comment

MyService is a trait , and you certainly CAN extend it anonymously like that. Also, there is nothing wrong with the cake pattern, except, maybe, for the opinions of a couple of bloggers who happen to like Play.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.