techdaysnl 2015 - ddd with f#
TRANSCRIPT
DDD with F#Ronald Harmsen
"Focus on the domain and domain logic rather
than technology” Eric Evans
DDD F#
Why?
“First, it’s almost certainly true that functional programming is the next big thing. “
Robert C. Martin
“Object oriented programming makes code understandable by encapsulating moving parts. Functional programming makes code understandable by minimizing moving parts.“
Michael Feathers
“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#.”
Functional programming
• Declarative
• Clearity
• Composability
• Immutability
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.
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.
Why F# ?
• Functional-first
• Very strong type inference
• Integrate with other .NET technologies
• Windows, Web, Xamarin iOS & Android, Linux
The functional language in .NET
DDDBut first a small introduction to functions in F#
FunctionsEverything is a function
let a = 10
int a () {return
10;}
let sum a b = a + b
int sum (int a, int b) {return a + b;
}
Composability
Chaining
Chaining functions makes a service,
Chaining services makes an use-case
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));}
}
module Calclet square n = n ** 2.0
let multiplyByPi n = n * 3.14
let areaOfCircle r = multiplyByPi (square (r))
module Calclet square n = n ** 2.0
let multiplyByPi n = n * 3.14
let areaOfCircle r = r |> square |> multiplyByPi
DDDIt’s all about modelling types
Types
type Email = string
type SimplePerson = Name * Email
type AnotherPerson = { Name: string; Age: int }
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;}
}
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
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
Demo
Ronald [email protected]@ronaldharmsen
Simple. Clear. Software.
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.