Giter Club home page Giter Club logo

cqrs-bank's Introduction

CQRS Bank

CQRS is an architectural pattern that separates commands (which mutate state) from queries (which return values). With CQRS the “read” data store and the “write” data store can be on different severs, can use different storage engines, and can be scaled independently. CQRS is often linked with the Event Sourcing pattern which models state as a series of events (past tense verbs) rather than a single “latest” value. What works for an accountant’s ledger and for Git can work for our “write” store too. Given a series of events we can deal with concurrency and collisions more intelligently than “last guy wins”. We can also define varied service level agreements for commands and queries.

Materials on CQRS

alt text

The challlenge

  • We are modeling a simple bank that has checking account customers.
  • We need to define a task-based UI to capture user intent and issue commands.
  • The data for our UIs will come from queries to read-side projections.
  • Command handlers will receive a command and call the corresponding function on an aggregate.
  • Aggregates are loaded and saved via a repository.
  • The aggregate will contain the business logic to determine if a command will be processed or rejected.
  • If a command is accepted an event will be emitted and applied (state change) to the aggregate.
  • One or more event handlers will respond to an event and update read-side projections.
  • The task-based UI will reload to show changes to the projections.

Bonus: Event sourcing In typical CRUD application only the latest state of an aggregate is stored. With event sourcing, we store each individual event that was applied to the aggregate. With event sourcing load an aggregate by playing back the events that were previously applied to the aggregate.

Commands

  • create_bank_account(accountId:int, accountHolder:string, pin:int) -> ack
  • deposit_money_into_bank_account(accountId:int, amount:int) -> ack
  • withdraw_money_from_bank_account(accountId:int, amount:int, pin:int) -> ack

Queries

  • check_balance(accountId:int) -> int
  • get_account_list() -> list<{accountId:int, accountHolder:string}>
  • get_total_deposits() -> int

Events

  • bank_account_created{accountId:int, accountHolder:string}
  • money_deposited_into_bank_account{accountId:int, amount:int}
  • money_withdrawn_from_bank_account{accountId:int, amount:int}
  • invalid_pin{accountId:int, amount_requested:int}
  • account_overdrawn{accountId:int, balance:int, amount_requested:int}

Write your own

Join the fun! Add a directory to this repo containing your code.

If you don't already have the right permissions to push to this repo, file an issue! We'll hook you up.

By convention, we use directory names that tell who wrote the code and what language it's in, separated by a +. For example: bryan_hunter+elixir.

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.