Giter Club home page Giter Club logo

persistence-api-experiments's Introduction

Persistence API Proposal

This repo is a sandbox to experiment with a new Persistence API for Lagom. At the moment, it only has a very minimal, Scala only, implementation based on also experimental Akka Typed Persistence API. The purpose is to work on the shape of this API and get the types on the right place. The same can be achieved in Java, but much less concise.

The original motivation for reviewing the current API comes from feedback we got in Gitter and Mailing list. Part of the discussion is documented at Lagom issue #919.

This current proposal has a few new features and/or advantages over the current API.

  1. Behavior is a function from Option[State] => Actions
  2. No explicit initial value needed. Initial value of a model is always None. We don't need to force users to come up with a definition of empty. For legacy reasons, provide the means for defining an initial state.
  3. Command handlers have only one argument, the Command. No Context or State passed around. Context become obsolete and State is available in scope by other means (see bellow).
  4. Effect is the new black. A command handler is a function from Command => Effect and we provide a EffectBuilder DSL for different types. At the moment we have support for Effects emitting: Event, Seq[Event] and Option[Event].
  5. No need for onReadOnlyCommand. A read-only directive is a general Effect that does not emit events.

Please, feel free to open issues to make suggestions and discuss any topic in detail.

Behavior as Option[State] => Actions

This is a important change that alleviates the API in many places and removes the need to come up with an artificial initial state.

The main observation backing this change is that at the beginning there isn't an Entity and like any other event that may take place, there is an initial event that creates the Entity, much like we are used to create an object by calling its constructor. At some moment an Entity is created and the creation is expressed as an Event.

The Behavior can be decomposed in two main functions.

  • None => Actions when the Entity doesn't exists yet. At API level this is a () => Actions
  • Some[State] => Actions when the Entity was already created. At API level this is a PartialFunction[State, Actions]

There is an alternative Behavior builder where we first must defined an initial value followed by a function State => Actions.

At API level, the developers doesn't need to deal with Option. They only need to provide the Actions before and after creation. This will also allow the usage of ADTs to express model transition.

Simplified Command Handlers

The current API (Lagom 1.3.19) defines a command handler as a PartialFunction[(Command, CommandContext[Reply], State), Persist]. In this new API it is simplified to PartialFunction[Command, Effect].

The Context becomes obsolete because of a new Fluent / Intention Driven API.

Code examples

There are three examples in the test folder to demonstrate the available API and its look-and-feel.

persistence-api-experiments's People

Contributors

octonato avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

ignasi35

persistence-api-experiments's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.