Teimuraz
Teimuraz

Reputation: 9335

Scala, play framework: How to properly inject dependency in domain (entity) class

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.

Upvotes: 0

Views: 497

Answers (1)

Martijn
Martijn

Reputation: 12112

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.

Upvotes: 1

Related Questions