Single Value Objects as C# structs

Slides:



Advertisements
Similar presentations
Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.
Advertisements

Pointers Prasun Dewan Comp 114.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
CS 211 Inheritance AAA.
The Type System1. 2.NET Type System The type system is the part of the CLR that defines all the types that programmers can use, and allows developers.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Review of C++ Programming Part II Sheng-Fang Huang.
CIS Computer Programming Logic
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
EXACT ONLINE APP CENTER THE ROADMAP Dimitri Braakman (Product Marketing Manager) Ruben Mijwaart (Product Manager)
BPJ444: Business Programming using Java Java basics Tim McKenna
EXist Indexing Using the right index for you data Date: 9/29/2008 Dan McCreary President Dan McCreary & Associates (952) M.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Certain statements in this presentation concerning our future growth prospects are forward-looking statements, which are subject to a number of risks,
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Introduction to Java Java Translation Program Structure
Introduction to Programming
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Text Files and String Processing
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
AJAX. Ajax  $.get  $.post  $.getJSON  $.ajax  json and xml  Looping over data results, success and error callbacks.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
A Method for Improving Code Reuse System Prasanthi.S.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
1 XML and XML in DLESE Katy Ginger November 2003.
Single Value Objects as C# structs
Creating Database Objects
Chapter 9 Domain Models.
JAVA MULTIPLE CHOICE QUESTION.
Introduction to Visual Basic 2008 Programming
Java Primer 1: Types, Classes and Operators
Module 5: Common Type System
Semantic Analysis with Emphasis on Name Analysis
Test Review Computer Science History
Review: Two Programming Paradigms
C++, OBJECT ORIENTED PROGRAMMING
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Satisfying Open/Closed Principle
Creating Objects & String Class
Single Value Objects as C# structs
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Object Based Programming
Chapter 4: Writing classes
SDMX: A brief introduction
DISCLAIMER and COPYRIGHT RESERVATION
Introduction to Primitive Data types
Morgan Stanley Technology Media & Telecoms Conference
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Building Java Programs
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Focus of the Course Object-Oriented Software Development
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Java’s Central Casting
Classes, Objects and Methods
Chap 2. Identifiers, Keywords, and Types
design OO words Debug Variables Data types
Creating Database Objects
Forward Looking Statements
Introduction to Primitive Data types
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 - Single Value Objects as C# structs

“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

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

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

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

Questions? 26 - Single Value Objects as C# structs

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.

Exact. Cloud business software.