Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I abstract the domain layer from the persistence layer in Scala

Tags:

UPDATE: I've edited the title and added this text to better explain what I'm trying to achieve: I'm trying to create a new application from the ground up, but don't want the business layer to know about the persistence layer, in the same way one would not want the business layer to know about a REST API layer. Below is an example of a persistence layer that I would like to use. I'm looking for good advice on integrating with this i.e. I need help with the design/architecture to cleanly split the responsibilities between business logic and persistence logic. Maybe a concept along the line of marshalling and unmarshalling of persistence objects to domain objects.

From a SLICK (a.k.a. ScalaQuery) test example, this is how you create a many-to-many database relationship. This will create 3 tables: a, b and a_to_b, where a_to_b keeps links of rows in table a and b.

object A extends Table[(Int, String)]("a") {   def id = column[Int]("id", O.PrimaryKey)   def s = column[String]("s")   def * = id ~ s   def bs = AToB.filter(_.aId === id).flatMap(_.bFK) }  object B extends Table[(Int, String)]("b") {   def id = column[Int]("id", O.PrimaryKey)   def s = column[String]("s")   def * = id ~ s   def as = AToB.filter(_.bId === id).flatMap(_.aFK) }  object AToB extends Table[(Int, Int)]("a_to_b") {   def aId = column[Int]("a")   def bId = column[Int]("b")   def * = aId ~ bId   def aFK = foreignKey("a_fk", aId, A)(a => a.id)   def bFK = foreignKey("b_fk", bId, B)(b => b.id) }  (A.ddl ++ B.ddl ++ AToB.ddl).create A.insertAll(1 -> "a", 2 -> "b", 3 -> "c") B.insertAll(1 -> "x", 2 -> "y", 3 -> "z") AToB.insertAll(1 -> 1, 1 -> 2, 2 -> 2, 2 -> 3)  val q1 = for {   a <- A if a.id >= 2   b <- a.bs } yield (a.s, b.s) q1.foreach(x => println(" "+x)) assertEquals(Set(("b","y"), ("b","z")), q1.list.toSet) 

As my next step, I would like to take this up one level (I still want to use SLICK but wrap it nicely), to working with objects. So in pseudo code it would be great to do something like:

objectOfTypeA.save() objectOfTypeB.save() linkAtoB.save(ojectOfTypeA, objectOfTypeB) 

Or, something like that. I have my ideas on how I might approach this in Java, but I'm starting to realize that some of my object-oriented ideas from pure OO languages are starting to fail me. Can anyone please give me some pointers as to how approach this problem in Scala.

For example: Do I create simple objects that just wrap or extend the table objects, and then include these (composition) into another class that manages them?

Any ideas, guidance, example (please), that will help me better approach this problem as a designer and coder will be greatly appreciated.

like image 404
Jack Avatar asked Jul 18 '12 13:07

Jack


People also ask

What is Dao in Scala?

DAO | Abstraction of the Application/Business layer from the persistence layer.

What does persistence layer do?

In Java servers, persistence layer is also known as the repository layer. This layer is responsible for data persistence and is used by the business layer to access the cache and database. In terms of functionality, it is fine to write the persistence layer code in the service class.


2 Answers

The best idea would be to implement something like data mapper pattern. Which, in contrast to active record, will not violate SRP.

Since I am not a Scala developer, I will not show any code.

The idea is following:

  • create domain object instance
  • set conditions on the element (for example setId(42), if you are looking for element by ID)
  • create data mapper instance
  • execute fetch() method on the mapper by passing in domain object as parameter

The mapper would look up current parameters of provided domain object and, based on those parameters, retrieve information from storage (which might be SQL database, or JSON file or maybe a remote REST API). If information is retrieved, it assigns the values to the domain object.

Also, I must note, that data mappers are created for work with specific domain object's interface, but the information, which they pass from domain object to storage and back, can be mapped to multiple SQL tables or multiple REST resources.

This way you can easily replace the mapper, when you switch to different storage medium, or even unit-test the logic in domain objects without touching the real storage. Also, if you decide to add caching at some point, that would be just another mapper, which tried to fetch information from cache, and, if it fails, the mapper for persistent storage kicks in.

Domain object (or, in some cases, a collection of domain objects) would be completely unaware of whether it is stored or retrieved. That would be the responsibility of the data mappers.

If this is all in MVC context, then, to fully implement this, you would need another group of structures in the model layer. I call them "services" (please share, of you come up with better name). They are responsible for containing the interaction between data mappers and domain objects. This way you can prevent the business logic from leaking in the presentation layer (controllers, to be exact), and these services create a natural interface for interaction between business (also know as model) layer and the presentation layer.

P.S. Once again, sorry that I cannot provide any code examples, because I am a PHP developer and have no idea how to write code in Scala.

P.P.S. If you are using data mapper pattern, the best option is to write mappers manually and not use any 3rd party ORM, which claims to implement it. It would give you more control over codebase and avoid pointless technical debt [1] [2].

like image 109
tereško Avatar answered Sep 28 '22 05:09

tereško


A good solution for simple persistence requirements is the ActiveRecord pattern: http://en.wikipedia.org/wiki/Active_record_pattern . This is implemented in Ruby and in Play! framework 1.2, and you can easily implement it in Scala in a stand-alone application

The only requirement is to have a singleton DB or a singleton service to get a reference to the DB you require. I personally would go for an implementation based on the following:

  • A generic trait ActiveRecord
  • A generic typeclass ActiveRecordHandler

Exploiting the power of implicits, you could obtain an amazing syntax:

trait ActiveRecordHandler[T]{    def save(t:T):T    def delete[A<:Serializable](primaryKey:A):Option[T]    def find(query:String):Traversable[T] }  object ActiveRecordHandler {   // Note that an implicit val inside an object with the same name as the trait    // is  one of the way to have the implicit in scope.   implicit val myClassHandler = new ActiveRecordHandler[MyClass] {      def save(myClass:MyClass) = myClass      def delete[A <: Serializable](primaryKey: A) = None      def find(query: String) = List(MyClass("hello"),MyClass("goodbye"))   } }  trait ActiveRecord[RecordType] {   self:RecordType=>     def save(implicit activeRecordHandler:ActiveRecordHandler[RecordType]):RecordType = activeRecordHandler.save(this)    def delete[A<:Serializable](primaryKey:A)(implicit activeRecordHandler:ActiveRecordHandler[RecordType]):Option[RecordType] = activeRecordHandler.delete(primaryKey) }  case class MyClass(name:String) extends ActiveRecord[MyClass]   object MyClass {   def main(args:Array[String]) = {     MyClass("10").save   } } 

With such a solution, you only need your class to extends ActiveRecord[T] and have an implicit ActiveRecordHandler[T] to handle this.

There is actually also an implementation: https://github.com/aselab/scala-activerecord which is based on similar idea, but instead of making the ActiveRecord having an abstract type, it declares a generic companion object.


A general but very important comment on the ActiveRecord pattern is that it helps meet simple requirements in terms of persistence, but cannot deal with more complex requirements: for example is when you want to persist multiple objects under the same transaction.

If your application requires more complex persistence logic, the best approach is to introduce a persistence service which exposes only a limited set of functions to the client classes, for example

def persist(objectsofTypeA:Traversable[A],objectsOfTypeB:Traversable[B])

Please also note that according to your application complexity, you might want to expose this logic in different fashions:

  • as a singleton object in the case your application is simple, and you do not want your persistence logic to be pluggable
  • through a singleton object which acts as a sort as a "application context", so that in your application at startup you can decide which persistence logic you want to use.
  • with some sort of lookup service pattern, if your application is distributed.
like image 42
Edmondo1984 Avatar answered Sep 28 '22 05:09

Edmondo1984