Skip to content

HelloWorldProjectionFSharp

Yves Reynhout edited this page May 29, 2016 · 2 revisions

Introduction

If you're using F#, a lot of what Projac offers (to C#) is already present in your language. In fact, except when you're targeting a relational store, you don't really need Projac. Obviously, you will still want to take advantage of the techniques presented here.

Step 1 - Messages

Before you can write projections, you need to define the messages you are going to project. Record types are a very common way to define them in F#. For simplicity sake, we'll define our first message as follows.

[<CLIMutable>]
type PeopleWhoSaidHello = { People: string array };

This message captures a list of people, their names, that said hello. Pretty useless in and by itself, but useful enough for a Hello World.

Step 2 - Projection Declaration

How we define our projection depends on the data store we target. For simplicity sake, we're going to use the Console as our data store and print a Hello World line for each person who said hello.

let helloWorldProjection console message =
  Array.iter message.People
  |> console.WriteLine

Defining a projection in F# is just a matter of defining a function, as shown above. Whenever we invoke this function, we'll pass it a TextWriter and an instance of our message, causing the function to write Hello World from ... for each person in that message.

Step 3: Projection Execution