cake-pattern

Scala Slick Cake Pattern: over 9000 classes?

守給你的承諾、 提交于 2021-02-07 05:27:09
问题 I'm developing a Play! 2.2 application in Scala with Slick 2.0 and I'm now tackling the data access aspect, trying to use the Cake Pattern. It seems promising but I really feel like I need to write a huge bunch of classes/traits/objects just to achieve something really simple. So I could use some light on this. Taking a very simple example with a User concept, the way I understand it is we should have: case class User(...) //model class Users extends Table[User]... //Slick Table object users

Cake pattern with overriding abstract type don't work with Upper Type Bounds

流过昼夜 提交于 2020-01-09 02:29:04
问题 I want to override abstract type in trait with <: and not with = (like answer here Scala Upper Bounds : value is not a member of type parameter). I want to use cake pattern, but this doesn't work, i don't understand why ? trait A { def ping = println("ping") } trait Cake { type T } trait S { this: Cake => type T = A def t: T t.ping } OK, this example run, but in my real use case i want to override type with <: and not with = .It seems impossible to access the t function, why ? trait S { this:

How to use mocks with the Cake Pattern

你离开我真会死。 提交于 2019-12-21 10:15:28
问题 I have the following class: class LinkUserService() { //** cake pattern ** oauthProvider: OAuthProvider => //****************** def isUserLinked(userId: String, service: String) = { val cred = oauthProvider.loadCredential(userId) cred != null } def linkUserAccount(userId: String, service: String): (String, Option[String]) = { if (isUserLinked(userId, service)) { ("SERVICE_LINKED", None) } else { val authUrl = oauthProvider.newAuthorizationUrl ("SERVICE_NOT_LINKED", Some(authUrl)) } } def

Cake pattern with Java8 possible?

你说的曾经没有我的故事 提交于 2019-12-20 08:57:02
问题 I just wonder: with Java 8, and the possibility to add implementation in interfaces (a bit like Scala traits), will it be possible to implement the cake pattern, like we can do in Scala? If it is, can someone provide a code snippet? 回答1: With inspiration from other answers I came up with the following (rough) class hierarchy that is similar to the cake pattern in Scala: interface UserRepository { String authenticate(String username, String password); } interface UserRepositoryComponent {

Scala cake pattern with Existential Types: compile error

久未见 提交于 2019-12-13 04:30:05
问题 Through this question, I found this article on the 'config' pattern from Precog. I tried this with two modules: case class Pet(val name: String) trait ConfigComponent { type Config def config: Config } trait Vet { def vaccinate(pet: Pet) = { println("Vaccinate:" + pet) } } trait AnotherModule extends ConfigComponent { type Config <: AnotherConfig def getLastName(): String trait AnotherConfig { val lastName: String } } trait AnotherModuleImpl extends AnotherModule { override def getLastName():

Cake pattern: how to get all objects of type UserService provided by components

寵の児 提交于 2019-12-07 18:08:39
问题 This question may help you understand my needs. Cake pattern: one component per implementation, or one component per trait? I have a Scala application using multiple UserService implementations which will be provided by component(s?). I wonder if there is a way in another component to "scan" the application so that I can retrieve a set of all components providing an object which implement the trait UserService? So that I can iterate over all the UserService interfaces provided by my cake

importance of cake pattern in scala

余生颓废 提交于 2019-12-07 03:54:19
问题 I have started learning scala for a while now and now looking at cake pattern. I got the example from here trait UserRepositoryComponent { def userLocator: UserLocator trait UserLocator { def findAll: List[User] } } trait UserRepositoryJPAComponent extends UserRepositoryComponent { val em: EntityManager def userLocator = new UserLocatorJPA(em) class UserLocatorJPA(val em: EntityManager) extends UserLocator { def findAll = { println("Executing a JPA query") List(new User, new User) } } } trait

Cake pattern: how to get all objects of type UserService provided by components

独自空忆成欢 提交于 2019-12-06 01:57:28
This question may help you understand my needs. Cake pattern: one component per implementation, or one component per trait? I have a Scala application using multiple UserService implementations which will be provided by component(s?). I wonder if there is a way in another component to "scan" the application so that I can retrieve a set of all components providing an object which implement the trait UserService? So that I can iterate over all the UserService interfaces provided by my cake built application? I guess I can have a component which build a list of UserService according to its

Scala Cake Pattern and Dependency Collisions

拟墨画扇 提交于 2019-12-05 16:17:19
问题 I'm trying to implement dependency injection in Scala with the Cake Pattern, but am running into dependency collisions. Since I could not find a detailed example with such dependencies, here's my problem: Suppose we have the following trait (with 2 implementations): trait HttpClient { def get(url: String) } class DefaultHttpClient1 extends HttpClient { def get(url: String) = ??? } class DefaultHttpClient2 extends HttpClient { def get(url: String) = ??? } And the following two cake pattern

importance of cake pattern in scala

空扰寡人 提交于 2019-12-05 06:29:10
I have started learning scala for a while now and now looking at cake pattern. I got the example from here trait UserRepositoryComponent { def userLocator: UserLocator trait UserLocator { def findAll: List[User] } } trait UserRepositoryJPAComponent extends UserRepositoryComponent { val em: EntityManager def userLocator = new UserLocatorJPA(em) class UserLocatorJPA(val em: EntityManager) extends UserLocator { def findAll = { println("Executing a JPA query") List(new User, new User) } } } trait UserServiceComponent { def userService: UserService trait UserService { def findAll: List[User] } }