Artur Soler
Artur Soler

Reputation: 3024

Models as Scala case classes interacting with DAO?

I was starting a design involving Scala case classes as models, and I was wondering about a design decision.

Let's imagine we have two models, a User model and an Order model. The Order model references the User model as a foreign key.

case class User(id: UserId, [Other fields...], password: String)
case class Order(id: OrderId, [Other fields...], userId: UserId)

Then, given this design, we would have an Orders DAO with a method findByUser method.

My question is: would it be good design to have an orders method in User that calls this DAO method, and thus making the system more OO, or is it better to keep layers isolated and not include this method?

Thanks!

Upvotes: 4

Views: 524

Answers (1)

Haris Osmanagić
Haris Osmanagić

Reputation: 1285

If you understand you correctly, you're asking about the Active Record Pattern. As any pattern, it has its pros and cons, you can find more about it online. Here are some of them:

http://www.mehdi-khalili.com/orm-anti-patterns-part-1-active-record

https://softwareengineering.stackexchange.com/questions/70291/what-are-the-drawbacks-to-the-activerecord-pattern

Active Record Design Pattern?

In a Play2 project, I firstly used the pattern, mainly because of Ebean support. However, since I needed a bit more logic for persisting some models, extension of it turned cumbersome. In the end, I separated everything: separate service, separate model, separate DAO. That also helped me easily switch between repository layer implementations (in the end I could experiment with Spring Data JPA, Hibernate etc. more freely)

Upvotes: 1

Related Questions