We have a java system that is implemented in several layers. On the lowest level, there are services which abstract a range of infrastructure components such as database, blob-storage, processing-queues etc... On top of that, we have the domain layer that implements workflows that use one or more of these services (using command and compound-command patterns). Finally there is a jax-rs (jersey) based layer on top of that, which provides a REST interface/protocol implemented in terms of the workflows from the domain layer.
My experience with Scala is limited, but I suspect that it might be a good tool to write a more expressive version of our domain layer because the Java code is quite verbose and hard to debug at times. Partly because the control of command composition and execution is handled by base-classes that are extended by concrete commands. In hindsight, this might not have been an ideal design choice.
I hope that this very high level description serves to illustrate what I'm trying to achieve. Let me know if I need to get more specific. I'm mostly interested in how the domain layer could be build in with Scala while keeping the Java based service layer. Furthermore, suggestions on how to implement the http/REST protocol on top of the domain layer so that it can be deployed in a java web application container would also be very interesting.
Tactically an obvious starting point is to take advantage of Scala's structural typing and look at representing your commands as traits:
trait CommandType1 { def operation() }
It is then possible to build your command compositions out of these traits:
case class CompositeCommand(commandOperand: CommandType1*) extends CommandType1 {
def operation() = commandOperand foreach { _.operation() }
}
Another advantage Scala has over Java for your workflow layer is that you can define your commands as passable functions. This opens up the possibility of creating general high level commands representing abstract operations, and then creating and storing partial functions of these commands for your commonly used/accessed workflows.
The front facing Scala protocol layer could look at spinning up actors to deal with incoming requests. Additionally it is probably worth looking at something like Scala's Cake Pattern to provide DI to control your business logic, and into multimethods for dynamic dispatch in your front-end code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With