invio / Invio.Immutable

C# Library used to ease immutable class creation and data management
MIT License
2 stars 0 forks source link

Add `SetPropertyValuesImpl` implementation on `ImmutableBase` #41

Closed carusology closed 6 years ago

carusology commented 6 years ago

Background

As of right now, when someone wants to create a setter that updates two properties simultaneously on ImmutableBase<TImmutable>, they have to create their own setter that manually invokes the constructor or invokes SetPropertyValueImpl twice. This is because SetPropertyValueImpl only allows one property to be set at a time.

This is problematic for two reasons:

  1. It is an unnecessary performance hit because you have to create two instances even though you knew the value of each property before the operation began.
  2. If two properties are coupled and have argument validation perform in the constructor, you cannot update them both simultaneously so they avoid a conflicting state.

The latter is particularly frustrating. Consider that you have, for example, aMinimum and Maximum property on the immutable class. In your constructor, you throw an ArgumentException if Minimum is greater than Maximum. This means if you want to change the range of Minimum and Maximum from 1 to 5, to 6 to 10, or -5 to -1, you have to know which order to set the values in, adding complexity to what should be a simple setter.

Adding the ability to set both of these properties at the same time removes this complexity.

Task

Add a SetPropertyValuesImpl implementation that takes in a IDictionary<string, object> that maps property names to property values so they can be assigned simultaneously, like so:

protected SetPropertyValuesImpl(IDictionary<string, object> propertyValues);

Caveats: