Recently I've adopted a handy way to make sure tree-like structure members are aware of their parent node:
private metaCollection<metaPage> _servicePages;
/// <summary>
/// Registry of service pages used by this document
/// </summary>
[Category("metaDocument")]
[DisplayName("servicePages")]
[Description("Registry of service pages used by this document")]
public metaCollection<metaPage> servicePages
{
get
{
if (_servicePages == null) {
_servicePages = new metaCollection<metaPage>();
_servicePages.parent = this;
}
return _servicePages;
}
}
(the concept is to create instance for private field within property get method)
I would love to know if this pattern has some common-known name? and even more: are there known issues / bad implications on such practice?
Thanks!
Yes, it's called Lazy Initialization. From the example on the Wikipedia Lazy Loading page:
Lazy initialization
Main article: Lazy initialization
With lazy initialization, the object to be lazily loaded is originally set to null, and every request for the object checks for null and creates it "on the fly" before returning it first, as in this C# example:
private int myWidgetID;
private Widget myWidget = null;
public Widget MyWidget
{
get
{
if (myWidget == null)
{
myWidget = Widget.Load(myWidgetID);
}
return myWidget;
}
}