Giter VIP home page Giter VIP logo

Comments (2)

octonato avatar octonato commented on June 7, 2024

Implementation work is being done here:
https://github.com/strongtyped/fun-cqrs/tree/feature/binding-dsl

from fun-cqrs.

octonato avatar octonato commented on June 7, 2024

After discussing with some DDD/CQRS experts, we decided to change the naming of method to come closer to the common CQRS vocabulary.

The current implementation looks like:

describe[Lottery]
    .whenCreating {
      // creational command and event
      handler { cmd: CreateLottery => LotteryCreated(cmd.name, metadata(id, cmd)) }
        .listener { evt => Lottery(name = evt.name, id = id) }

      // updates
    } whenUpdating { lottery =>

      // Select a winner when run!
      tryHandler { cmd: Run.type =>
        lottery.selectParticipant().map { winner => WinnerSelected(winner, metadata(id, cmd)) }
      } listener { evt =>
        lottery.copy(winner = Option(evt.winner))
      }

    } whenUpdating { lottery =>

      tryHandler { cmd: AddParticipant =>
        if (lottery.hasParticipant(cmd.name))
          Failure(new IllegalArgumentException(s"Participant ${cmd.name} already added!"))
        else
          Success(ParticipantAdded(cmd.name, Lottery.metadata(id, cmd)))
      } listener { evt =>
        lottery.addParticipant(evt.name)
      }
    } whenUpdating { lottery =>
          // handler produces more than one event
      handler.manyEvents { cmd: Reset.type =>
        lottery.participants.map { name => ParticipantRemoved(name, metadata(id, cmd)) }
      } listener {
            // listener must be a partial function
        case evt: ParticipantRemoved => lottery.removeParticipant(evt.name)
      }
    }

Basically, we have the following methods:

Single events

  • handler - defines a command handler that returns a raw Event (not wrapped)
  • tryHandler - defines a command handler that returns a Try[Event]
  • asyncHandler - defines a command handler that returns a Future[Event]

Whenever declaring a handler we must declare an event listener that can respond to the Event being generated.
In case of single events we must define a function from Event => Aggregate

Many Events

In some situations we may generate more than one event (also at construction time).

For commands generating many events we have the following operations.

  • handler.manyEvents - defines a command handler that returns a raw Seq[Event] (not wrapped)
  • tryHandler.manyEvents - defines a command handler that returns a Try[Seq[Event]]
  • asyncHandler.manyEvents - defines a command handler that returns a Future[Seq[Event]]

Whenever a handler returns a Seq of Events we must declare an event listener using a PartialFunction. The reason for that is that we need a function able to handle all possible events in the list.

from fun-cqrs.

Related Issues (20)

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.