techdaysnl 2015 - ddd with f#

31

Upload: ronald-harmsen

Post on 11-Aug-2015

178 views

Category:

Software


1 download

TRANSCRIPT

Page 1: TechDaysNL 2015 - DDD with F#
Page 2: TechDaysNL 2015 - DDD with F#

DDD with F#Ronald Harmsen

Page 3: TechDaysNL 2015 - DDD with F#

"Focus on the domain and domain logic rather

than technology” Eric Evans

Page 4: TechDaysNL 2015 - DDD with F#

DDD F#

Page 5: TechDaysNL 2015 - DDD with F#

Why?

Page 6: TechDaysNL 2015 - DDD with F#
Page 7: TechDaysNL 2015 - DDD with F#

“First, it’s almost certainly true that functional programming is the next big thing. “

Robert C. Martin

Page 8: TechDaysNL 2015 - DDD with F#

“Object oriented programming makes code understandable by encapsulating moving parts. Functional programming makes code understandable by minimizing moving parts.“

Michael Feathers

Page 9: TechDaysNL 2015 - DDD with F#

“F# is excellent at concisely expressing business and domain logic.Developers trying to achieve explicit business logic within an Application may opt to express their domain in F# with the majority of plumbing code in C#.”

Page 10: TechDaysNL 2015 - DDD with F#

Functional programming

• Declarative

• Clearity

• Composability

• Immutability

Page 11: TechDaysNL 2015 - DDD with F#

Characteristic Imperative approach Functional approach

Programmer focus How to perform tasks (algorithms) how to track changes in state.

What information is desired and what transformations are required.

State changes Important. Non-existent.

Order of execution Important. Low importance.

Primary flow control Loops, conditionals, and function (method) calls. Function calls, including recursion.

Primary manipulation unit Instances of structures or classes. Functions as first-class objects

data collections.

Page 12: TechDaysNL 2015 - DDD with F#

Characteristic C# F#

Programmer focus How to perform tasks (algorithms) how to track changes in state.

What information is desired and what transformations are required.

State changes Important. Non-existent.

Order of execution Important. Low importance.

Primary flow control Loops, conditionals, and function (method) calls. Function calls, including recursion.

Primary manipulation unit Instances of structures or classes. Functions as first-class objects

data collections.

Page 13: TechDaysNL 2015 - DDD with F#

Why F# ?

• Functional-first

• Very strong type inference

• Integrate with other .NET technologies

• Windows, Web, Xamarin iOS & Android, Linux

The functional language in .NET

Page 14: TechDaysNL 2015 - DDD with F#

DDDBut first a small introduction to functions in F#

Page 15: TechDaysNL 2015 - DDD with F#

FunctionsEverything is a function

Page 16: TechDaysNL 2015 - DDD with F#

let a = 10

int a () {return

10;}

Page 17: TechDaysNL 2015 - DDD with F#

let sum a b = a + b

int sum (int a, int b) {return a + b;

}

Page 18: TechDaysNL 2015 - DDD with F#

Composability

Page 19: TechDaysNL 2015 - DDD with F#

Chaining

Chaining functions makes a service,

Chaining services makes an use-case

Page 20: TechDaysNL 2015 - DDD with F#

public static class Calc {

public static decimal Square(decimal n){

return n * n;}

public static decimal MultiplyByPi(decimal n) {

return n*3.14m;}

public static decimal AreaOfCicle(decimal r){

return MultiplyByPi(Square(r));}

}

Page 21: TechDaysNL 2015 - DDD with F#

module Calclet square n = n ** 2.0

let multiplyByPi n = n * 3.14

let areaOfCircle r = multiplyByPi (square (r))

Page 22: TechDaysNL 2015 - DDD with F#

module Calclet square n = n ** 2.0

let multiplyByPi n = n * 3.14

let areaOfCircle r = r |> square |> multiplyByPi

Page 23: TechDaysNL 2015 - DDD with F#

DDDIt’s all about modelling types

Page 24: TechDaysNL 2015 - DDD with F#

Types

type Email = string

type SimplePerson = Name * Email

type AnotherPerson = { Name: string; Age: int }

Page 25: TechDaysNL 2015 - DDD with F#

public class AnotherPerson{

public AnotherPerson(string name, int age){

Name = name;Age = age;

}

public string Name { get; private set; }public int Age { get; private set; }

public override int GetHashCode(){

return Name.GetHashCode() + Age.GetHashCode();}

public override bool Equals(object obj){

return Equals(obj as AnotherPerson);}

public bool Equals(AnotherPerson other){

if (other == null)return false;

return Name == other.Name && Age == other.Age;}

}

Page 26: TechDaysNL 2015 - DDD with F#

module CardGameBoundedContext =

type Suit = Club | Diamond | Spade | Heart type Rank = Two | Three | Four | Five | Six

| Seven | Eight | Nine | Ten

| Jack | Queen | King | Ace

type Card = Suit * Rank type Hand = Card list type Deck = Card list type Player = {Name:string; Hand:Hand} type Game = {Deck:Deck; Players: Player list} type Deal = Deck -> (Deck * Card) type PickupCard = (Hand * Card)-> Hand

Page 27: TechDaysNL 2015 - DDD with F#

module MoreTypes

type IPrintable = abstract member Print : unit -> unit

type SomeClass () = interface IPrintable with member this.Print () = printfn “Hello”

type AnotherClass (i: int) = member this.Value = imember val this.Mutable: int with get, set

Page 28: TechDaysNL 2015 - DDD with F#

Demo

Page 29: TechDaysNL 2015 - DDD with F#

Ronald [email protected]@ronaldharmsen

Simple. Clear. Software.

Page 30: TechDaysNL 2015 - DDD with F#

Your feedback is important!Scan the QR Code and let us know via the TechDays App.

Laat ons weten wat u van de sessie vindt!Scan the QR Code via de TechDays App.

Bent u al lid van de Microsot Virtual Academy?! Op MVA kunt u altijd iets nieuws leren over de laatste technologie van Microsoft. Meld u vandaag aan op de MVA Stand. MVA biedt 7/24 gratis online training on-demand voor IT-Professionals en Ontwikkelaars.

Page 31: TechDaysNL 2015 - DDD with F#