I am building a large, complex data structure with many interconnected objects. As part of this, I have a situation where I need to create an object before I know the values for all the properties. This is so I can pass a reference to that object around inside of a larger data structure, then change it later to allow all the consumers of that object to get the "updated" values.
Tracking the consumers of each object so I can go update their references means I am A) re-implementing a reference counting garbage collector and B) would require an external consumer to modify a class. For obvious reasons, I don't want to implement a garbage collector (or something similar). and allowing external consumers to modify properties means the object is mutable. Immutability is important because I know that many objects in this data structure are going to wind up in Dictionaries after the data structure has been built, but I can safely modify properties until then. Since this is going into a library that will be provided to other users, I must block interactions that will break things like invariants for Dictionaries.
I am implementing this "deferred initialization" concept for properties like this:
class MyClass
{
private AnotherClass mReference = null;
public bool Reference
{
get
{
return this.mReference
}
internal set
{
if ( this.mReference != null )
{
throw new Exception( "This value has already been initialized!" );
}
else
{
this.mReference = value;
}
}
}
}
I am allowing anything in the internal
scope to assign a non-null value to this property exactly once. Once the property has been set, there's no going back.
While C# has a keyword readonly
that sort of already does this, readonly
constrains the member to only be assigned to via either a static initializer or a constructor in the same class. But at the time the constructor is called, I don't know what the value for this property is!
My problem is I am now using this exact pattern across multiple classes, and I would like to avoid this code duplication.
Does anyone have any suggestions for how I can reduce the code duplication? Maybe a C# keyword like readonly
, or some other language feature? Or is this just not possible? Unfortunately, it is not possible to refactor the algorithms such that I can delay instantiation of all objects until all values are known. Think of bidirectional relationships with immutable objects at each end if you need an example of this scenario.