The way setters work with ImmutableBase<TImmutable> works is pretty simple. Every time a value is to be changed for a given property, the code takes that value along with all of the other current values for all of the other properties, passes it to the class's constructor and returns the new object. However, we currently assume there is only one constructor. If you want to have several constructors, ImmutableBase<TImmutable> will need a hint as to which constructor is supposed to be called to create the new version of the caller's data. This can be especially useful if you want consumers of your immutable object to only be able to set specific values when initialing hydrating objects of this type.
[x] Update the static constructor such that it looks for the [ImmutableSetterConstructor] attribute on all available constructors. If found, it uses that constructor for setting. If not, it looks to see if there is a single constructor. If there is more than one constructor, it throws an InvalidOperationException() stating the need for a single constructor or an explicit [ImmutableSetterConstructor]-tagged constructor. This exception could be thrown in the static constructor or when SetPropertyValueImpl is called for the first time.
Background
The way setters work with
ImmutableBase<TImmutable>
works is pretty simple. Every time a value is to be changed for a given property, the code takes that value along with all of the other current values for all of the other properties, passes it to the class's constructor and returns the new object. However, we currently assume there is only one constructor. If you want to have several constructors,ImmutableBase<TImmutable>
will need a hint as to which constructor is supposed to be called to create the new version of the caller's data. This can be especially useful if you want consumers of your immutable object to only be able to set specific values when initialing hydrating objects of this type.Tasks
[ImmutableSetterConstructor]
attribute that is applied to a constructor that is intended for use with calls toImmutableBase<TImmutable>.SetPropertyValueImpl()
.[ImmutableSetterConstructor]
attribute on all available constructors. If found, it uses that constructor for setting. If not, it looks to see if there is a single constructor. If there is more than one constructor, it throws anInvalidOperationException()
stating the need for a single constructor or an explicit[ImmutableSetterConstructor]
-tagged constructor. This exception could be thrown in the static constructor or whenSetPropertyValueImpl
is called for the first time.