0
votes

I have an domain object:

case class User(val id: Long, username: String)

I don't want to follow service-repository approach (like typical spring applications, where entities are just data holders), I want to put functionality related to user into User object.

But I don't want to tie User object to concrete infrastructure implementations, so I want to pass interfaces (traits) instead.

I'm trying do this by following way:

case class User(val id: Long, val username: String, implicit val userRepository: IUserRepository)

And somewhere in application (controller, or somewhere else), I want to inject (with @Inject()) concrete implementation of IUserRepsoitory and want it to implicitly passed to constructor of User.

Question 1: case class User(val id: Long, val username: String, implicit val userRepository: IUserRepository) - this doesn't work, it is not compiled

Question 2: Is it correct approach to decouple infrastructure implementation from object domain in play? are they some best practices?

Thanks.

1
That's subjective, but for Q2 I would generally say "no", the domain object should not know how the application is managing it, according the context.cchantep
What do you mean? Is it bad practice if User (domain object) interacts with repository interface? (not implementation)Teimuraz
For me yes that's a badcchantep
Why? Do you prefer entities to be just data holder? Just I'd like follow ddd approach (not 100%), more OO approach.. Why is it bad?Teimuraz
For me, case class are designed to represent data, not processcchantep

1 Answers

1
votes

An entire parameter list is either implicit or not. You're looking for

case class User(id: Long, username: String)(implicit userRepository: IUserRepository)

Architecturally, it sounds like a bad idea to have a user class know it's own repository.