[2.4.1 Java] Implement repository pattern with Ebean

486 views
Skip to first unread message

Adis

unread,
Jul 2, 2015, 5:25:34 AM7/2/15
to play-fr...@googlegroups.com
Hi,

I want to seperate the Ebean stuff out of my models to be able to test my Business rules inside my models. I know I can test with fakeApplication but I rather not want to.
In PHP I have used repository pattern, but with Ebean I just do now know how to implement the find method in a generic way.

My proposal:

All business models will implement the BaseModelInterface. This is just an empty interface.
The I want to have the following user repository interface which can have their additional queries defined

  1. Interface BaseRepositoryInterface
    1. findById(Long id)
    2. save()
    3. update()
    4. delete()
    5. findAllPaged(int pageNumber)
    6. etc...
  2. Interface UserRepositoryInterface
    1. findUserByEmail(String email)
    2. findUserByUserName()

Abstract class BaseRepository implements BaseRepositoryInterface

Then I would have a UserRepository that extends the BaseRepository but implements the UserRepositoryInterface so that inside UserRepository I would have to implement the specific User queries.

Does anyone had to do something specific? In the documentation the repository is referenced but no implementation details with Ebean: Link to docs
Please advise me how to achieve this?

Regards,

Ian Rae

unread,
Jul 2, 2015, 8:47:56 AM7/2/15
to play-fr...@googlegroups.com
Check out this sample project:  https://github.com/ianrae/mcomputer

It splits the functionality of Play models into entities (POJOs) and DAOs (which have save(), update(), etc).  Then you create mock DAOs for unit tests that have no EBean dependency.

This recent article may also be helpful (althouth it's not about EBean specifically) http://java.dzone.com/articles/persistence-and-dao-testing-0

Adis

unread,
Jul 2, 2015, 2:30:31 PM7/2/15
to play-fr...@googlegroups.com
Thanx for taking time to help me!

I think I have a good solution that is working fine for me. I will update my this post when I tweak it a little bit!
Reply all
Reply to author
Forward
0 new messages