ImmutableEntityHelper 0.1.0
dotnet add package ImmutableEntityHelper --version 0.1.0
NuGet\Install-Package ImmutableEntityHelper -Version 0.1.0
<PackageReference Include="ImmutableEntityHelper" Version="0.1.0" />
paket add ImmutableEntityHelper --version 0.1.0
#r "nuget: ImmutableEntityHelper, 0.1.0"
// Install ImmutableEntityHelper as a Cake Addin #addin nuget:?package=ImmutableEntityHelper&version=0.1.0 // Install ImmutableEntityHelper as a Cake Tool #tool nuget:?package=ImmutableEntityHelper&version=0.1.0
ImmutableEntityHelper
A library to create and maniputate immutable objects.
The composition method allows you to use existing mutable classes as immutable classes, safe to use. They are also easy to clone and "modify" using this library.
The inheritance method allows you to create flexible fluent interfaces with far less mess.
Example for the Inheritance Method
If a field is intended to be modified then a new instance of the object is returned with the assigned field value.
This allows the developer to create fluent interfaces with safe immutable entities.
// The immutable Person class extends ImmutableEntity<Person>
// class and provides the SetName a SetAge methods,
// which return new instances of Person, as invoked.
var person1 = new Person();
var person2 = person1
.SetName("Joseph")
.SetAge(26);
// In this case,
// person1 and person2 are totally
// different instances !!!
// person2 came by copying person1
// and "modifying" the Name and Age fields
// using those methods.
Following the previous example, the class Person
extends our class ImmutableEntity<T>
and provides the SetName
and SetAge
methods in a fluent interface. These methods returns new
immutable instances of the Person
class, as used.
class Person : ImmutableEntity<Person>
{
// Read only properties!
public string Name { get; }
public int Age { get; }
public Person SetName(string name)
{
// These fluent interface methods
// use the SetField method from ImmutableEntity<T>
// to create and return new immutable instances.
return SetField((x) => x.Name, name);
}
public Person SetAge(int age)
{
return SetField((x) => x.Age, age);
}
}
Example using the Composition Method
Some entities cannot extend ImmutableEntity<T>
, so this class can also be instantiated
and consumed as an instance in another class.
In this example, we decorate the Student
class in the class ImmutableStudent
by adding
some fluent methods like SetName
or SetAge
, which return a new class of the the immutable class.
class ImmutableStudent : Student, IStudent
{
private ImmutableEntity<ImmutableStudent> _immutableEntity;
public ImmutableStudent()
{
_immutableEntity = new ImmutableEntity<ImmutableStudent>(this);
}
public ImmutableStudent SetName(string name)
{
return _immutableEntity.SetField((x) => x.Name, name);
}
public ImmutableStudent SetAge(int age)
{
return _immutableEntity.SetField((x) => x.Age, age);
}
}
To use the previous, just instantiate the ImmutableStudent
class and use
its instantiation methods for immutable entities. You can also cast ImmutableStudent
to Student
, if required.
This method allows the developer to use mutable classes as immutable objects, preventing side effects.
var person1 = new ImmutableStudent();
var person2 = person1
.SetName("Joseph")
.SetAge(26);
// Different immutable instances,
// but as easy to manipulate
// as strings, which are also immutable.
person1.Should().NotBe(person2);
person2.Name.Should().Be("Joseph");
person2.Age.Should().Be(26);
The class used for this example is:
class Student : IStudent
{
public string Name { get; }
public int Age { get; }
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.1.0 | 751 | 10/26/2018 |
Initial version.