Skip to content

x789/ValueArrays

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ValueArrays

This package contains the implemetation of a ValueArray, which is an immutable (read-only) container whose equality is determined based on its content. That means, two ValueArrays with the same items in the same order are equal.

ValueArrays are the perfect fit if your DDD value-objects need to contain a collection of objects and you do not want to care about equality and immutability. This makes modeling ValueObjects with C# records a breeze.

Installation

  1. Get a compiled package from Nuget at: https://www.nuget.org/packages/ValueArrays/.
  2. Import the namespace ValueArrays and you are ready to go!

Usage

var array = new ValueArray<string>(new[] { "one", "two", "three" });
var copy = new ValueArray<string>(array.ToArray());
var other = new ValueArray<string>(new[] { "two", "one", "three" });

// Equality is determined based on the items...
array.Equals(copy); // true

// ...and the order.
array.Equals(other); // false

// Like equality, the hash-code of a value-array is determined based on its content.
array.GetHashCode() == copy.GetHashCode(); // true
array.GetHashCode() == other.GetHashCode(); // false

// The equality-operators are overloaded and work as expected.
array == copy; // true
array != other; // true

// Value-arrays are enumerable...
foreach (var item in array)
    Console.WriteLine(item); // prints "one", "two", "three"

// ... and therefore support LINQ.
array.Where(x => x == "two").Any(); // true

// They have an indexer...
Console.WriteLine(array[2]); // prints "three"

// ...and a 'Length' property.
for (var i = 0; i < array.Length; i++)
    Console.WriteLine(array[i]); // prints "one", "two", "three"

// Value-arrays are immutable. The indexer is read-only...
array[1] = "foo"; // triggers CS0200 "indexer is read only"

// ...and changing the original data has no side-effects.
var data = new[] { 1, 2, 3 };
var va = new ValueArray<int>(data);
data[1] = 4;
va[1] == 2; // true

// They can safely be used to model value-objects as records, since they fit into the built-in Equals() and GetHashCode().
// Assume that 'Line' is defined as: 'record Line(ValueArray<string> points) {}'
var line1 = new Line(array);
var line2 = new Line(copy);
line1.Equals(line2); // true
line1 == line2; // true
line1.GetHashCode() == line2.GetHashCode(); // true

(c) 2021 TillW -- Visit on Nuget -- Visit on GitHub

About

Source code of Nuget package 'ValueArrays'

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages