Single Value Objects as C# structs Domain-Driven Design bottom-up Corniel Nobel, 2016-06-08, A’dam
About me Corniel Nobel Energetic, optimistic, creative, out of the box thinking, experienced software engineer Exact (2014-current) Senior software engineer Qowaiv community (2013-current) Founding member TJIP (2004-2014) OGD ICT-diensten (2003-2004) IT-Professional 2B Insite (2002-2003) Software engineer
Program Structs in C# DDD in short Single Value Objects Design choices Qowaiv Demo Questions Program 3 - Single Value Objects as C# structs
Structs in C# 4 - Single Value Objects as C# structs
Reference Types & Value Types Pointer to data elsewhere in memory Mutable by default Inheritable by default Examples Object String* Type[] Value Type Stores own data (or point to data) Immutable by design* Sealed by design Examples Single, Double, Decimal Byte, SByte Int16, Int32, Int64 UInt16, UInt32, UInt64 Boolean, Char, DateTime, Guid 5 - Single Value Objects as C# structs
DDD in short 6 - Single Value Objects as C# structs
DDD in short Domain Subject area of the software Model Repositories Aggregate roots Entities (Single) Value Objects Ubiquitous Language Language structured around the domain (Bounded) Context 7 - Single Value Objects as C# structs
Single Value Objects 8 - Single Value Objects as C# structs
Single Value Objects in DDD Subset underlying primitive type Self-descriptive Immutable Equal by value 9 - Single Value Objects as C# structs
Example SVO’s Percentage Date Week date Year Month Country Currency Stream size UUID Seed SQL Timestamp Initials Gender House number Email address Postal Code BMI IBAN BIC BSN License plate Elo 10 - Single Value Objects as C# structs
Example Stream Size var size = StreamSize.FromMegaBytes(12.4); var size = StreamSize.Parse("13 kb"); StreamSize size = 1400; Console.WriteLine("The file was {0:0.0 F}.", size); // The file was 13.0 Kilobyte 11 - Single Value Objects as C# structs
Example Postal Code var code = PostalCode.Parse("1234ab"); if (code.IsValid(Country.BE)){ /* ... */ } Console.WriteLine(“Your postal code: {0:NL}", code); // Your postal code: 1234 AB 12 - Single Value Objects as C# structs
Example IBAN var iban = IBAN.Parse("nl20ingb0001234567"); Console.WriteLine(“Your IBAN: {0:F}", iban); // Your IBAN: NL20 INGB 0001 2345 67 13 - Single Value Objects as C# structs
Design Choises 14 - Single Value Objects as C# structs
Creation & conversion No (public) constructor Parse(string) & TryParse(string) Create(underlying) & TryCreate(underlying) TypeConverter Casting String (explicit) Underlying type (implicit and/or explicit) 15 - Single Value Objects as C# structs
Validation Potential valid False positive/negative IsValid() Context independent IsValid(context) 16 - Single Value Objects as C# structs
Serialization RunTime Serializable Attribute System.Iserializable Ctor(SerializationInfo, StreamingContext) XML System.Xml.IXmlSerializable JSON void FromJson() void FromJson(String;Int64;Double;DateTime) Object ToJson() 17 - Single Value Objects as C# structs
Formatting & debugging IFormattable ToString() ToString(string) ToString(IFormatProvider) ToString(string, IFormatProvider) [DebuggerDisplay("{DebuggerDisplay }")] private string DebuggerDisplay { get; } Unified internal string representation* 18 - Single Value Objects as C# structs
Qowaiv 19 - Single Value Objects as C# structs
Qowaiv community Open Source .NET library MIT licence Code generator GitHub github.com/Corniel/qowaiv NuGet packages Qowaiv Qowaiv.Web Qowaiv.Json.Newtonsoft 20 - Single Value Objects as C# structs
21 - Single Value Objects as C# structs
Questions? 22 - 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.