Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better query supports: projection #7

Open
gtrefs opened this issue Jul 2, 2017 · 0 comments
Open

Add better query supports: projection #7

gtrefs opened this issue Jul 2, 2017 · 0 comments

Comments

@gtrefs
Copy link
Owner

gtrefs commented Jul 2, 2017

Problem

Currently a query is a fold over all domain events in an eventstore which may result into some boiler plate code. For example, two queries for different events result into two folds which only differ in the actual type of the event.

val registeredUsers: (List<DomainEvent>) -> List<UserRegistered> = { it.fold(emptyList(),
         { s,d -> when(d){ is UserRegistered -> s + d else -> s }}
)} 
val loggedInUsers: (List<DomainEvent>) -> List<UserLoggedIn> = { it.fold(emptyList(),
        {s, d -> when(d){ is UserLoggedIn -> s + d else -> s}} 
)}

Another problem occurs when we want to compose queries. For example, if we want to get all users which are currently viewing a specific page, we need to first query for all logged in users and then filter those which are on the page. However, composing those queries is quite hard with fold.

A little projection algebra

Let's think about query again. Instead using the fold model we can see it as a function.

  • A projection is a function from a list of events to a structure.
typealias Projection<T> = (List<DomainEvent>) -> T
  • An empty projection does not project anything
val empty:Projection<List<DomainEvent>> = { it } // identity function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant