{"id":3563,"date":"2013-08-08T07:00:00","date_gmt":"2013-08-08T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/08\/08\/on-partially-constructed-objects-additional-remarks-not-as-interesting\/"},"modified":"2013-08-08T07:00:00","modified_gmt":"2013-08-08T07:00:00","slug":"on-partially-constructed-objects-additional-remarks-not-as-interesting","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130808-00\/?p=3563","title":{"rendered":"On partially-constructed objects, additional remarks, not as interesting"},"content":{"rendered":"<p>\nDon&#8217;t worry.\nOur\n<a HREF=\"https:\/\/www.youtube.com\/watch?v=HLyX4DbE6Hc\">\nlong national nightmare<\/a>\nof CLR week is almost over.\n<\/p>\n<p>\nI had originally planned to write an article about partially-constructed\nobjects, but in the time since I queued up the topic (back in November 2005)\nto the time I got around to writing it up, I found that\nJoe Duffy had already written it for me!\n<\/p>\n<p>\n<a HREF=\"http:\/\/www.bluebytesoftware.com\/blog\/2010\/06\/27\/OnPartiallyconstructedObjects.aspx\">\n<b>On partially-constructed objects<\/b>\n<\/a>\n<\/p>\n<p>\nRead it.\n<\/p>\n<p>\nOkay, here are some follow-up remarks.\n<\/p>\n<p>\nOne place where people get caught out by partially-constructed objects\nis when they try to maintain a cache of objects\n(perhaps with a little bit of\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2013\/08\/07\/10439879.aspx\">\n<code>Weak&shy;Reference<\/code> action<\/a>)\nand stash the objects into the cache before they are fully constructed:\n<\/p>\n<pre>\nclass SomeClass {\n public SomeClass(...) {\n  cache.Add(this);\n  AdditionalConstructionWork();\n }\n}\n<\/pre>\n<p>\nIf the <code>Additional&shy;Construction&shy;Work<\/code> takes\nan exception,\nthen you end up with a partially-constructed object in your cache.\n(Mind you, you had one all along, but now it&#8217;s a persistent condition\nas opposed to a transient one.)\n<\/p>\n<p>\nYou might think to fix the problem by reordering the operations:\n<\/p>\n<pre>\nclass SomeClass {\n public SomeClass(...) {\n  AdditionalConstructionWork();\n  \/\/ add to cache only after construction ran to completion\n  cache.Add(this);\n }\n}\n<\/pre>\n<p>\nBut that still doesn&#8217;t work once you have derived classes:\n<\/p>\n<pre>\nclass Derived : SomeClass {\n public Derived(...) : base(...) {\n  AdditionalConstruction(); \/\/ oops, what if this throws?\n }\n}\n<\/pre>\n<p>\nThe base constructor runs first,\nit successfully constructs the base object,\nand then puts it in the cache.\nAnd then the derived constructor runs and encounters an exception.\nYou&#8217;re back in the same boat with a partially-constructed object\nin the cache.\n<\/p>\n<p>\nYou want to wait until the object is fully constructed\nbecause you add it to your cache.\n<\/p>\n<pre>\nclass SomeClass {\n static public SomeClass Create(...) {\n  SomeClass c = new SomeClass(...);\n  Register(c);\n  return c;\n }\n protected static void Register(SomeClass c) { cache.Add(c); }\n protected SomeClass(...) { ... }\n}\nclass Derived : SomeClass {\n static public Derived Create(...) {\n  Derived d = new Derived(...);\n  Register(d);\n  return d;\n }\n public Derived(...) : base(...) { ... }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Don&#8217;t worry. Our long national nightmare of CLR week is almost over. I had originally planned to write an article about partially-constructed objects, but in the time since I queued up the topic (back in November 2005) to the time I got around to writing it up, I found that Joe Duffy had already written [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-3563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Don&#8217;t worry. Our long national nightmare of CLR week is almost over. I had originally planned to write an article about partially-constructed objects, but in the time since I queued up the topic (back in November 2005) to the time I got around to writing it up, I found that Joe Duffy had already written [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=3563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3563\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=3563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=3563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=3563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}