Presentation is loading. Please wait.

Presentation is loading. Please wait.

Single Value Objects as C# structs

Similar presentations


Presentation on theme: "Single Value Objects as C# structs"— Presentation transcript:

1 Single Value Objects as C# structs
Domain-Driven Design bottom-up Corniel Nobel, , Kiev

2 About me Corniel Nobel Senior Software engineer Exact mail@corniel.nl
corniel.nl/blog github.com/Corniel linkedin.com - Single Value Objects as C# structs

3 Take away What you should know after this talk
What a Single Value Object is Benefits of using Single Value Objects Difference between value types and reference types Benefits of modeling Single Value Objects as structs How to code a Single Value Object yourself 3 - Single Value Objects as C# structs

4 Single Value Object A Value Object
Single Value Object A Value Object* that can be represented by a single scalar 4 - Single Value Objects as C# structs

5 Value Object Equal by value By definition (Domain-Driven Design)
Immutable 5 - Single Value Objects as C# structs

6 Single Value Object Equal by value
By definition (Domain-Driven Design) Immutable Subset of primitive type (scalar) Per definition Self-descriptive Not dependent on other data Ubiquitous naming (language) 6 - Single Value Objects as C# structs

7 The benefits of Single Value Objects
Richer contracts/interfaces More descriptive Potential valid by default Centralized (business) logic Serialization Binding Parsing & Formatting Domain specific logic/transformations 7 - Single Value Objects as C# structs

8 But you already knew that…
8 - Single Value Objects as C# structs

9 DateTime TimeSpan Guid
9 - Single Value Objects as C# structs

10 Example: Rich Contract
public class President { public string Name { get; set; } public string { get; set; } public DateTime DateOfBirth { get; set; } public string CountryOfBirth { get; set; } public int YearOfElection { get; set; } } 10 - Single Value Objects as C# structs

11 Example: Rich Contract
public class President { public string Name { get; set; } public Address { get; set; } public Date DateOfBirth { get; set; } public Country CountryOfBirth { get; set; } public Year YearOfElection { get; set; } } 11 - Single Value Objects as C# structs

12 Why To use structS To model Single value objects? Bottom-up Approach
12 - Single Value Objects as C# structs

13 Reference Type (Class) vs. Value Type (Struct)
Pointer to data elsewhere in memory Mutable by default Inheritable by default Equal by reference Examples Object String* Type[] Value Type Stores own data (or point to data) Immutable by design* Sealed by design Equal by value Examples Floating points Integers Boolean, Char 13 - Single Value Objects as C# structs

14 Reference Type (Class) vs. Value Type (Struct)
Pointer to data elsewhere in memory Mutable by default Inheritable by default Equal by reference Examples Object String* Type[] Value Type Stores own data (or point to data) Immutable by design* Sealed by design Equal by value Examples Floating Points Integers Boolean, Char 14 - Single Value Objects as C# structs

15 What do Single Value Objects look like in practice
Examples What do Single Value Objects look like in practice 15 - Single Value Objects as C# structs

16 Example: International Bank Account Number (IBAN)
var iban = IBAN.Parse("nl20ingb "); Console.WriteLine(“Your IBAN: {0:F}", iban); // Your IBAN: NL20 INGB 16 - Single Value Objects as C# structs

17 Example: Postal Code var code = PostalCode.Parse(“H0H0H0"); if (code.IsValid(Country.CA)){ /* ... */ } Console.WriteLine(“Formatted postal code: {0:CA}", code); // Formatted postal code: H0H 0H0 17 - Single Value Objects as C# structs

18 Example: Stream Size if (file.Length > size){ .. } // Comparable var size = StreamSize.FromMegaBytes(12.4); // Factory method var size = StreamSize.Parse("13 kb"); // Factory method var size = StreamSize.GB; // Constant StreamSize size = 1400; // (Implicit) cast Console.WriteLine("The file was {0:0.0 F}.", size); // The file was 13.0 Kilobyte 18 - Single Value Objects as C# structs

19 Structs and how they work in .NET
var number = default(int); // What is the value of number? var text = default(string); // What is the value of text? public virtual bool Equals(obj other) { // if type and all underlying values are equal return true } public virtual string ToString() { return GetType().ToString(); public virtual int GetHashCode() { return RuntimeHelpers.GetHashCode(this); 19 - Single Value Objects as C# structs

20 Some design decisions How to (de)serialize (XML, JSON, Memory)
Implement IComparable or not Implement IFormattable or not To cast (and which types) or not The scalar to use for the unified internal representation Debug experience 20 - Single Value Objects as C# structs

21 21 - Single Value Objects as C# structs

22 “Qowaiv is a (Single) Value Object library
“Qowaiv is a (Single) Value Object library. It aims to model reusable (Single) Value Objects that can be used a wide variety of modeling scenarios, both inside and outside a Domain-driven context.” 22 - Single Value Objects as C# structs

23 github.com/Corniel/Qowaiv
23 - Single Value Objects as C# structs

24 nuget.org/packages/Qowaiv
24 - Single Value Objects as C# structs

25 Take away What you should know after this talk
What a Single Value Object is Benefits of using Single Value Objects Difference between value types and reference types Benefits of modeling Single Value Objects as structs How to code a Single Value Object yourself 25 - Single Value Objects as C# structs

26 Questions? 26 - Single Value Objects as C# structs

27 Disclaimer This document contains certain statements and expectations that are forward looking, and which are based on information and plans that are currently available. By their nature, such forward-looking statements and expectations generate risk and uncertainty because they concern factors and events in the future and depend on circumstances that may not occur. Unforeseen factors that could influence the statements and expectations may, for instance, be changes in expenditures by companies in the markets we operate in; economic, political and foreign exchange fluctuations; possible statutory changes; changes in salary levels of employees; or future takeovers and divestitures. Exact can therefore not guarantee the accuracy and completeness of such statements and expectations, nor that such statements and expectations will be realized. Actual results may differ materially. Exact refuses to accept any obligation to update statements made in this document.

28 Exact. Cloud business software.


Download ppt "Single Value Objects as C# structs"

Similar presentations


Ads by Google