unicorn icon indicating copy to clipboard operation
unicorn copied to clipboard

Create trait for BaseIdRepository

Open nagirrab opened this issue 10 years ago • 3 comments

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

nagirrab avatar Sep 03 '14 01:09 nagirrab

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.

bambuchaAdm avatar Sep 08 '14 05:09 bambuchaAdm

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?

nagirrab avatar Sep 15 '14 14:09 nagirrab

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.

bambuchaAdm avatar Sep 23 '14 08:09 bambuchaAdm