Questions tagged [cake-pattern]

The cake pattern is a Scala dependency injection solution that uses only native language features, without any framework support.

The cake pattern is a Scala dependency injection solution that uses only native language features, without any framework support.

References:

73 questions
1
vote
1 answer

Can I say this scala code is dependency injected?

I've read some articles about Scala's cake pattern, basically understood it. Following is some sample code I copied from this article: Components: case class User(username:String, password: String) trait UserRepositoryComponent { val…
Freewind
  • 193,756
  • 157
  • 432
  • 708
1
vote
1 answer

How to setup my UserService using the cake pattern inside of play?

Currently I am using guice to wireup my UserService inside of a controller like: @Singleton class UserController @Inject()(userService: UserService) extends Controller { def show(userId: Int) { val user = userService.get(userId) …
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
1
vote
1 answer

Scala - Cake Pattern + Typeclasses + Implementations needing constructor parameter

Here is a bit of code I've distilled down as much as I can: trait CakeLayer[A] extends { // typeclass hack to make it play nice with traits implicit def requireTypeclass: MyTypeclass[A] val typeclassInVal = requireTypeclass /* other stuff…
adelbertc
  • 7,270
  • 11
  • 47
  • 70
1
vote
2 answers

Problems compiling routes after migrating to Play 2.1

After migrating to Play-2.1 I stuck into problem that routes compiler stopped working for my routes file. It's been completely fine with Play-2.0.4, but now I'm getting the build error and can't find any workaround for it. In my project I'm using…
Michael Korbakov
  • 2,147
  • 1
  • 18
  • 20
1
vote
1 answer

Scala: Lazy baking and runtime compilation of cake pattern

One of the great limitations of the cake pattern is that its static. I would like to be able to mix-in traits potentially written by different coders completely independently. However the traits would not need to be mixed-in frequently. The user…
Rich Oliver
  • 6,001
  • 4
  • 34
  • 57
0
votes
0 answers

Scala, cake pattern and mixins. How to avoid many parameters in "injected" class

While building a new Playframework app I am trying to use the cake pattern. My first understanding was to mix custom traits into the controllers and pass those provided by Play as parameter: trait MyComponents { def actorSystem:…
gervais.b
  • 2,294
  • 2
  • 22
  • 46
0
votes
0 answers

IntelliJ warning : "abstract value used in trait may cause errors during initialization" - what's the problem with LAZY abstract val-s?

IntelliJ gives me the warning that "abstract value in trait may cause errors during initialization" - as shown in the picture below. What does this mean ? Should I take this warning seriously ? Should I use def-s instead of lazy val-s ? Are they…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
0
votes
1 answer

Understanding real cake pattern code with self and this references

I have recently learnt about cake pattern and the differences between the uses of self => and self:T => (see here). The difference between these technicalities and real Scala code as remarked here continue to create me problems. For instance, see…
user1868607
  • 2,558
  • 1
  • 17
  • 38
0
votes
0 answers

Does Cake Pattern always need to be implemented as inner class?

I just read Jonas' well-known article about Cake Pattern, for a class like: class UserRepository { def authenticate(user: User): User = { println("authenticating user: " + user) user } def create(user: User) = println("creating user:…
Lifu Huang
  • 11,930
  • 14
  • 55
  • 77
0
votes
1 answer

How to avoid duplications of mixing of an implementation with the cake pattern

In all articles related to Cake patter that I found on the Internet I see a single level dependencies and it's clear to me. But when I started using it I faced an issue that I can not use a service only in a high level class and I need to mix it in…
Ivan
  • 462
  • 3
  • 13
0
votes
1 answer

Expose protected member as public with self tyes

I have a trait that representing some module that exposes some public method (think of a service): trait X { def exposeMe: AService = ... def keepMeHidden: BService = ... } Then, I have a Y module that requires services from X. Clients of Y…
Łukasz
  • 8,555
  • 2
  • 28
  • 51
0
votes
0 answers

Cake pattern for deep function call chain ( threading dependency along as extra parameter)

Question on simple example: Assume we have : 1) 3 functions: f(d:Dependency), g(d:Dependency), h(d:Dependency) and 2) the following function call graph : f calls g, g calls h QUESTION: In h I would like to use the d passed to f, is there a way to…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
0
votes
2 answers

How reuse a dependency object in another composition in cake pattern

I have two service classes like below... User Service: class UserService { dao: UserGroupDao => ... def read(userId: String): Future[Option[User]] = dao.readUser(userId) ... } Group Service: class GroupService {dao: UserGroupDao => def…
ArunavaS
  • 197
  • 1
  • 12
0
votes
0 answers

Scala - Accessible member up to directly inherited class

Using cake pattern we can set a member only accessible to directly inherited trait trait A { val a = 1 } trait B { this: A => val b = a + 1 } // can access a trait C { this: B => val c = a + 1 } // will throw error because C cannot access a I …
Wonpyo Park
  • 301
  • 3
  • 11
0
votes
2 answers

Spring framework and java like Object collectors In Scala

In Spring framework and Java world, there is an interesting object collector pattern that I use. For example consider below - public interface Calculator { SomeOutput calculate(SomeInput input); } @Component public class CalImpl1 implements…
Mohammad Adnan
  • 6,527
  • 6
  • 29
  • 47