unicorn
unicorn copied to clipboard
Create trait for BaseIdRepository
Hi,
would it be possible to make it such that the BaseIdRepository
class implements some kind of trait which includes all the methods not in BaseIdQueries
(e.g. findAll
, deleteAll
)? I am looking to use a cake pattern to allow easy mocking of the repositories for testing and it would be significantly easier if I could refactor them to be used as follows:
trait UserRepositoryComponent {
def userRepository: UserRepository
trait UserRepository extends BaseIdRepository[User,...]
}
trait PostgresUserRepositoryComponent {
val userRepository = new PostgresUserRepository
class PostgresUserRepository extends BaseIdRepositoryImpl[User,...]
}
trait TestUserRepositoryComponent {
val userRepository = mock[UserRepository]
}
It seems cleaner to mock the trait than having to mock the final class instead.
What do you think? I'm happy to work on a PR for this if you think it acceptable.
Cheers, Hugh
It's nice idea. The simple reason why we hadn't got that interface is that unicorn wasn't initially built using cake pattern.
As i assume you want to extract abstract interface for BaseIdRepository
? I don't like suffix Impl
as is in your example, but it's only naming.
I'll try to find some time to make the changes in the next couple of weeks. Do you have any preference on the name other than Impl
?
Also related - as it stands the underlying TableQuery
is protected in the base repository. I found this slightly awkward when for example doing joins across tables which each had a separate repository. Do you have any objection to making this field public?
In scala standard library they use Like
suffix for abstract implementation.
We are aware problem with corss-repository joins. I this should be escalate as different issue where we would discuss abut the best solution for this.
Abut making TableQuery
public. It think it is potential breaking Demeter rule and could easily increase coupling or leaking abstraction.