<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Accumulative Construction</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/05/18/accumulative-construction.aspx</link><description>A while back someone asked for guidance on what order should polymorphic construction occur in C# classes. I guess I had never really put much thought into it before and have never seen other guidance on the topic; but, I can see where this can become</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Accumulative Construction</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/05/18/accumulative-construction.aspx#1690208</link><pubDate>Thu, 23 Apr 2009 16:09:48 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1690208</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Charles. &amp;nbsp;Also, multiple initialization is an oxymoron; it makes the interface and the resulting code confusing.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1690208" width="1" height="1"&gt;</description></item><item><title>re: Accumulative Construction</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/05/18/accumulative-construction.aspx#1690207</link><pubDate>Thu, 23 Apr 2009 16:08:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1690207</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Charles. &amp;nbsp;It looks like you&amp;#39;re trying to reuse object instances. &amp;nbsp;I don&amp;#39;t recommend that because now the type must support reuse in that way. &amp;nbsp; Unless Initialize can guarantee that the state is complete reset upon every call (and it can&amp;#39;t) your implementation of Person is dependent on it&amp;#39;s usage. &amp;nbsp;This limits reuse and makes for a tightly coupled design.&lt;/p&gt;
&lt;p&gt;Instance re-use like this is a premature optimization. &amp;nbsp;The GC will do much of that for you, making any perceived savings negligible at the expense of readability and maintainability.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1690207" width="1" height="1"&gt;</description></item><item><title>re: Accumulative Construction</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/05/18/accumulative-construction.aspx#1690202</link><pubDate>Thu, 23 Apr 2009 14:29:37 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1690202</guid><dc:creator>Charles Rex</dc:creator><description>&lt;p&gt;Hello&lt;/p&gt;
&lt;p&gt;When I have asked about initialization,&lt;/p&gt;
&lt;p&gt;I was thinking to do something useful afterwards:&lt;/p&gt;
&lt;p&gt;Thus, it makes sense to &amp;quot;initialize&amp;quot; several times.&lt;/p&gt;
&lt;p&gt;Person p = new Person(&amp;quot;f&amp;quot;,&amp;quot;m&amp;quot;,&amp;quot;l&amp;quot;,4);&lt;/p&gt;
&lt;p&gt;p.Initialize(&amp;quot;f1&amp;quot;,&amp;quot;m1&amp;quot;,&amp;quot;n1&amp;quot;,11);&lt;/p&gt;
&lt;p&gt;p.DoSomething();&lt;/p&gt;
&lt;p&gt;p.Initialize(&amp;quot;f2&amp;quot;,&amp;quot;m2&amp;quot;,&amp;quot;n2&amp;quot;,22);&lt;/p&gt;
&lt;p&gt;p.DoSomething();&lt;/p&gt;
&lt;p&gt;p.Initialize(&amp;quot;f3&amp;quot;,&amp;quot;m3&amp;quot;,&amp;quot;n3&amp;quot;,33);&lt;/p&gt;
&lt;p&gt;p.DoSomething();&lt;/p&gt;
&lt;p&gt;etc&lt;/p&gt;
&lt;p&gt;Many thanks.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1690202" width="1" height="1"&gt;</description></item><item><title>re: Accumulative Construction</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/05/18/accumulative-construction.aspx#1690201</link><pubDate>Thu, 23 Apr 2009 14:29:08 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1690201</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Peter. &amp;nbsp;It depends on the type. &amp;nbsp;Domain Model types aren&amp;#39;t necessarily going to have a property setter for each and every attribute of the type. &amp;nbsp;The recommended practice is to model behaviour with Domain Model types; they aren&amp;#39;t data containers. &amp;nbsp;In cases like this, object initializers can&amp;#39;t be used.&lt;/p&gt;
&lt;p&gt;Also, types used in multithreaded environments will tend to be immutable; meaning their value can&amp;#39;t change after construction. &amp;nbsp;In cases like this object initializers can&amp;#39;t be used either.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1690201" width="1" height="1"&gt;</description></item><item><title>re: Accumulative Construction</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/05/18/accumulative-construction.aspx#1690194</link><pubDate>Thu, 23 Apr 2009 13:20:58 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1690194</guid><dc:creator>Petar Repac</dc:creator><description>&lt;p&gt;With object initializers in C# 3.0 I think many of old constructors can/should be removed. &lt;/p&gt;
&lt;p&gt;Only constructors that demand essential information for object construction should exist. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1690194" width="1" height="1"&gt;</description></item><item><title>re: Accumulative Construction</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/05/18/accumulative-construction.aspx#1690188</link><pubDate>Thu, 23 Apr 2009 12:30:46 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1690188</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Charles. &amp;nbsp;I disagree. &amp;nbsp;&amp;quot;Construction&amp;quot; is tantamount to &amp;quot;initialization&amp;quot;. &amp;nbsp;In your example, it doesn&amp;#39;t make sense to &amp;quot;initialize&amp;quot; several times.&lt;/p&gt;
&lt;p&gt;In object-oriented design we want to model the behaviour of real-world object through the design of our classes. &amp;nbsp;With a person type I would have specific methods that model the behaviour of a person. &amp;nbsp;A person never &amp;quot;Initializes&amp;quot; itself. &amp;nbsp;It may:&lt;/p&gt;
&lt;p&gt;public ChangeName(IEnumerable&amp;lt;String&amp;gt; givenNames, String surname)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;/.../&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public Age(int yearsToAddToAge)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;//...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;You wouldn&amp;#39;t arbitrarily re-initialize a person in response to some action. &amp;nbsp;Given names and age properties of a person, you&amp;#39;re either going to change the name of a person or age them. &amp;nbsp;Having ChangeName and Age behaviour makes it explicit how to use Person and as a result makes it explicit what you are doing to a Person object.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1690188" width="1" height="1"&gt;</description></item><item><title>re: Accumulative Construction</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/05/18/accumulative-construction.aspx#1690185</link><pubDate>Thu, 23 Apr 2009 12:03:35 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1690185</guid><dc:creator>Charles Rex</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;- Once a Person object is created, &amp;nbsp;I feel there&amp;#39;s a need for a method like this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; Initialize(firstName, middleName, lastName, aAge)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Person p = new Person(&amp;quot;f&amp;quot;,&amp;quot;m&amp;quot;,&amp;quot;l&amp;quot;,4);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;p.Initialize(&amp;quot;f1&amp;quot;,&amp;quot;m1&amp;quot;,&amp;quot;n1&amp;quot;,11);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;p.Initialize(&amp;quot;f2&amp;quot;,&amp;quot;m2&amp;quot;,&amp;quot;n2&amp;quot;,22);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;p.Initialize(&amp;quot;f3&amp;quot;,&amp;quot;m3&amp;quot;,&amp;quot;n3&amp;quot;,33);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;//etc&lt;/p&gt;
&lt;p&gt;How would you write the Initialize method in order to avoid copy paste from the &lt;/p&gt;
&lt;p&gt;constructors ??&lt;/p&gt;
&lt;p&gt; public Initialize( String firstName, String middleName, String lastName, int age) &lt;/p&gt;
&lt;p&gt; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//assignments&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.firstName = firstName;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.middleName = middleName;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.lastName = lastName;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.age = age;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;If I do this, all the benefit of having the initialization code in one place, vanishes.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1690185" width="1" height="1"&gt;</description></item></channel></rss>