<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;CU4DQ308eCp7ImA9WhRbFU4.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329</id><updated>2012-02-06T05:12:52.370-08:00</updated><category term="C#" /><category term="Pragmattic Programmer" /><category term="PHP" /><category term="Code Generator" /><category term="n-tier" /><category term="Wrapper" /><category term="Data Access Layer" /><category term="Event Handlers" /><category term="System.Data" /><category term="User Experience" /><category term="Loose Coupling" /><category term=".Net" /><category term="NPOI" /><category term="ID3" /><category term="oop" /><category term="DataTable" /><category term="business logic" /><category term="Inheritance and Composition CodeProject" /><category term="Layers in Programming" /><category term="Decision Tree" /><category term="Excel" /><title>Struggles In Object Oriented Programming</title><subtitle type="html">My struggles in understanding and learning about Object Oriented design, and the tools and knowledge I've taken from them.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>36</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/StrugglesInObjectOrientedProgramming" /><feedburner:info uri="strugglesinobjectorientedprogramming" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><entry gd:etag="W/&quot;C0UGSH04eip7ImA9WhRQEko.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-3378764294795412094</id><published>2011-12-02T07:27:00.000-08:00</published><updated>2011-12-07T07:20:29.332-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-07T07:20:29.332-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Wrapper" /><category scheme="http://www.blogger.com/atom/ns#" term="Excel" /><category scheme="http://www.blogger.com/atom/ns#" term="NPOI" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>NPOI Wrapper</title><content type="html">There's a utility that I find quite useful, and probably use it as much as any other 3rd party library.  It's called &lt;a href="http://npoi.codeplex.com"&gt;NPOI&lt;/a&gt;, which is a .Net version of a Java project called POI.  NPOI offers the ability to read and write Excel 97-2003 files.  NPOI does not offer Excel 2007 and later manipulation (as far as I know), but that's for a different post.&lt;br /&gt;
&lt;br /&gt;
Prior to stumbling onto &lt;a href="http://npoi.codeplex.com"&gt;NPOI&lt;/a&gt;, I used to use Interop.  I would occassionally use ADO, as well; however, both Interop and ADO were clunky, and had some pretty major drawbacks, including slowness, and awkward implementation.  I'm also not a fan of using COM directly, because I'm not very good at it.  So, eventually I found NPOI.&lt;br /&gt;
&lt;br /&gt;
The problem with &lt;a href="http://npoi.codeplex.com"&gt;NPOI&lt;/a&gt;, (at least, my problem with NPOI), is that even though it is a fantastic tool, I had a tough time finding documentation all in one place.  On top of that, I didn't find its external interfaces particularly intuitive.&lt;br /&gt;
&lt;br /&gt;
So, what does a programmer do when he finds a tool useful, yet lacking in documentation and intuitiveness?  He (or she) starts building a wrapper!  And that's exactly what I did.&lt;br /&gt;
&lt;br /&gt;
You can &lt;a href="http://www.box.com/s/kbea3pvti84a31i8q389"&gt;download the wrapper here&lt;/a&gt;.  &lt;br /&gt;
&lt;br /&gt;
The wrapper, as it is, covers the bulk of what I need to do with Excel (save for the lack of Excel 2007 support - this is because Microsoft changed the file format of Excel files in 2007 to a XML/binary format).  However, there's probably things that some people feel inclined to do with Excel that this wrapper does not cover.  If you're so inclined to use this wrapper, and add functionality to cover your needs, feel free to comment on this post.&lt;br /&gt;
&lt;br /&gt;
I'll spare you some of the epiphanies and stories about how I had to track down various functionality of NPOI, and just get to the meat of how to use it, in a variety of examples.&lt;br /&gt;
&lt;br /&gt;
Example 1 - Creating a new file&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ExcelWrapper excel = new ExcelWrapper();
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.CreateFile(@"c:\ExcelFile.xls");
&lt;/pre&gt;&lt;br /&gt;
Example 2 - Opening an existing file, then saving it&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ExcelWrapper excel = new ExcelWrapper(@"c:\ExcelFile.xls");
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.SaveFile();
&lt;/pre&gt;&lt;br /&gt;
Example 3 - Getting cell values (note, as opposed to Interop, NPOI is 0-based, not 1-based)&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ExcelWrapper excel = new ExcelWrapper(@"c:\ExcelFile.xls");
&amp;nbsp;&amp;nbsp;&amp;nbsp;string a1 = excel.GetCellValue(0, 0);
&amp;nbsp;&amp;nbsp;&amp;nbsp;string b1 = excel.GetCellValue(0, 1);
&amp;nbsp;&amp;nbsp;&amp;nbsp;string b2 = excel.GetCellValue(1, 1);
&amp;nbsp;&amp;nbsp;&amp;nbsp;string d4 = excel.GetCellValue(3, 3);
&lt;/pre&gt;&lt;br /&gt;
Example 4 - Getting a sheet as a System.Data.DataTable&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ExcelWrapper excel = new ExcelWrapper(@"c:\ExcelFile.xls");
&amp;nbsp;&amp;nbsp;&amp;nbsp;DataTable dt = excel.ToDataTable();
&lt;/pre&gt;&lt;br /&gt;
Example 5 - Working with different sheets&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ExcelWrapper excel = new ExcelWrapper(@"c:\ExcelFile.xls");
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.SetActiveSheet("Sheet2");
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.SetActiveSheet(1); /// The sheet collection in a workbook is 0-based
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.DeleteSheet("Sheet3");
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.CreateSheet("Sheet99");
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.SaveFile();
&lt;/pre&gt;&lt;br /&gt;
Example 6 - Manipulating Cells&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ExcelWrapper excel = new ExcelWrapper(@"c:\ExcelFile.xls");
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.WriteCellValue(0, 0, "This is A1");
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.WriteCellValue(1, 1, "This is B2 - bolded and italicized", true, true);
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.HighlightCell(1, 1, ExcelColors.Yellow);
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.SetCellFontColor(1, 1, ExcelColors.Blue);
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.SaveFile();
&lt;/pre&gt;&lt;br /&gt;
Example 7 - Converting Excel Cell Names to 0-based Row and Column&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ExcelCell cell = new ExcelCell("B23");
&amp;nbsp;&amp;nbsp;&amp;nbsp;ExcelWrapper excel = new ExcelWrapper(@"c:\ExcelFile.xls");
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.WriteCellValue(cell.Row, cell.Column, "Cell B23 is set!");
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.SaveFileAs(@"c:\ExcelFile2.xls");
&lt;/pre&gt;&lt;br /&gt;
Example 8 - Working with formatting and styles&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ExcelWrapper excel= new ExcelWrapper("h:\\wrapperTest.xls");            
&amp;nbsp;&amp;nbsp;&amp;nbsp;ExcelStyle style = new ExcelStyle();
&amp;nbsp;&amp;nbsp;&amp;nbsp;style.BackColor = ExcelColors.Blue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;style.ForeColor = ExcelColors.Yellow;
&amp;nbsp;&amp;nbsp;&amp;nbsp;style.IsBold = true;
&amp;nbsp;&amp;nbsp;&amp;nbsp;style.IsItalics = false;
&amp;nbsp;&amp;nbsp;&amp;nbsp;style.BorderType = BorderTypes.Dashed;
&amp;nbsp;&amp;nbsp;&amp;nbsp;style.BorderTop = true;
&amp;nbsp;&amp;nbsp;&amp;nbsp;style.BorderBottom = true;
&amp;nbsp;&amp;nbsp;&amp;nbsp;style.FontFace = CommonFonts.Calibri;
&amp;nbsp;&amp;nbsp;&amp;nbsp;style.FontSize = 15;
&amp;nbsp;&amp;nbsp;&amp;nbsp;excel.WriteCellValue(6, 2, "6, 2 coordinates", style);
&lt;/pre&gt;So, there you have it.  This is my NPOI wrapper, along with some examples.  I'll probably add more to this post as it grows, but for now, this is what I've got.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-3378764294795412094?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/3378764294795412094/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=3378764294795412094" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/3378764294795412094?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/3378764294795412094?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/12/npoi-wrapper.html" title="NPOI Wrapper" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CE8GRX46fip7ImA9WhZTE0Q.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-6024753832781379079</id><published>2011-03-17T12:30:00.000-07:00</published><updated>2011-03-17T13:13:44.016-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-17T13:13:44.016-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Event Handlers" /><category scheme="http://www.blogger.com/atom/ns#" term="Loose Coupling" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="oop" /><title>The Vendor, The Consumer, and Loose Coupling</title><content type="html">I recently had an epiphany that has made me a better programmer.  Seeing as the intent of this blog is to share my experiences in my journey with object oriented programming, and considering this discovery relates to object oriented programming, I thought I would share.&lt;br /&gt;&lt;br /&gt;We all know that code should be loosely coupled.  That is, objects should avoid knowing too much about unrelated objects.  If an object shouldn't need another object to exist and function, then why should that object know about it at all?&lt;br /&gt;&lt;br /&gt;There's a lot of nuance in this concept, but part of it goes back to &lt;a href="http://oopstruggles.blogspot.com/2011/01/another-layers-of-programming-post.html"&gt;programming in layers&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So what happens when this nuance comes into play?&lt;br /&gt;&lt;br /&gt;For a few years now, I've had a solution that worked well enough, but violated loose coupling principles.&lt;br /&gt;&lt;br /&gt;I've dealt with this lack of eloquence for a time, partly because I didn't really have a better solution, and partly because it worked well enough.&lt;br /&gt;&lt;br /&gt;But I've got a solution now, and it was something that I knew about all along:  Event Handlers.&lt;br /&gt;&lt;br /&gt;An Event Handler is the perfect solution for loosening up the coupling of your objects, because it puts the flow in the proper perspective.  In code, there are vendors, consumers, and states.  The Vendor in code is the code that has methods, attributes, etc.  The Consumer is the code that uses another object's methods, attributes, etc.  A state is sort of an abstract concept that describes when something happens - for example, when an object is out of work to do, or when a button gets clicked, or when an item is added to a collection.&lt;br /&gt;&lt;br /&gt;A Vendor should generally have to know as little as possible about the consumer.  This is a principle that my code has often failed to follow religiously.  And the effect is tightly coupled code.&lt;br /&gt;&lt;br /&gt;Consider a System.Windows.Forms.Button object.  The Button object knows very little about other controls on a form, and it certainly doesn't know anything about your custom controls that do all sorts of funky stuff.  And yet, you can make it so that a click of a button has an impact on all the other controls on the form.&lt;br /&gt;&lt;br /&gt;How can this be?&lt;br /&gt;&lt;br /&gt;Well, the short answer is by hooking an event handler up to the Clicked event of the button.&lt;br /&gt;&lt;br /&gt;This is something a programmer learns within about 10 hours of programming in WinForms or WPF or whatever UI tool he (or she) has.  But there's a bigger principle at play that a 10 hour old programmer probably doesn't know, and that is this loose coupling principle.&lt;br /&gt;&lt;br /&gt;The Button has a Clicked event that some consumer (in this case, the Form that the Button is on) can subscribe to, and pass a delegate function to.  So when the event fires (the button gets clicked), the consumer delegate that is hooked up to the vendor's (Button's) event gets called, and any behavior defined in the consumer's custom delegate gets executed.&lt;br /&gt;&lt;br /&gt;This model is a beautiful one, and the underlying principle opens up a whole world of more eloquent solutions.&lt;br /&gt;&lt;br /&gt;Consider (some version) of my real life example.&lt;br /&gt;&lt;br /&gt;In an application I built about 8 months ago, I have an object called ActivityMonitor.  The ActivityMonitor object keeps track of what's going on in the application, and exposes that information when a TCP/IP client requests it.&lt;br /&gt;&lt;br /&gt;So, the ActivityMonitor looks something like this:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;class ActivityMonitor&lt;br /&gt;{&lt;br /&gt;   ActivityCollection _activities;&lt;br /&gt;&lt;br /&gt;   public void AddActivity(string activity)&lt;br /&gt;   {&lt;br /&gt;      Activity a = new Activity(activity);&lt;br /&gt;      _activities.Add(a);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public string GetActivityString()&lt;br /&gt;   {&lt;br /&gt;     return _activities.ToString();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, before considering Event Handlers as a solution for this problem, when non-related objects did something, and I wanted the ActivityMonitor to know about it, I would send the ActivityMonitor to the consumer object as a parameter.  This is a tightly coupled pattern, and as I see it now, something I'll avoid in the future.&lt;br /&gt;&lt;br /&gt;For example:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;public class BigSpecialObject&lt;br /&gt;{&lt;br /&gt;   ActivityMonitor _monitor;&lt;br /&gt;&lt;br /&gt;   public BigSpecialObject(ActivityMonitor monitor)&lt;br /&gt;   {&lt;br /&gt;     _monitor = monitor;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void DoStuff()&lt;br /&gt;   {&lt;br /&gt;     _monitor.AddActivity("About to do a bunch of stuff");&lt;br /&gt;     ///Do a bunch of stuff&lt;br /&gt;     _monitor.AddActivity("Done doing a bunch of stuff");&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;When I look at BigSpecialObject, I can infer a few things:&lt;br /&gt;&lt;br /&gt;1.  The consumer of BigSpecialObject knows about ActivityMonitor&lt;br /&gt;2.  BigSpecialObject is tightly coupled with ActivityMonitor&lt;br /&gt;3.  It didn't have to be this way&lt;br /&gt;&lt;br /&gt;The issue of state comes into play.  Presumably, at instantiation time, BigSpecialObject is not done doing stuff.  After BigSpecialObject.DoStuff() is called, at some point, BigSpecialObject is done doing stuff.  This is a great example of where an EventHandler is not only appropriate, but it is the optimal solution.&lt;br /&gt;&lt;br /&gt;So, with a couple extra lines of code, I can really "bust some heads" (to use a Ghostbusters reference):&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;public class BigSpecialObject&lt;br /&gt;{&lt;br /&gt;   public event EventHandler WorkRequested;&lt;br /&gt;   public event EventHandler WorkCompleted;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   public BigSpecialObject()&lt;br /&gt;   {&lt;br /&gt;     /// Notice that BigSpecialObject&lt;br /&gt;     /// knows jack-squat about ActivityMonitor&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void DoStuff()&lt;br /&gt;   { &lt;br /&gt;     this.alertWorkRequest();&lt;br /&gt;     ///Do a bunch of stuff&lt;br /&gt;     this.alertWorkDone();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private void alertWorkRequest()&lt;br /&gt;   {&lt;br /&gt;     if (this.WorkRequested == null)&lt;br /&gt;       return;&lt;br /&gt;&lt;br /&gt;     this.WorkRequested(this, new EventArgs());&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private void alertWorkDone()&lt;br /&gt;   {&lt;br /&gt;     if (this.WorkCompleted == null)&lt;br /&gt;       return;&lt;br /&gt;&lt;br /&gt;     this.WorkCompleted(this, new EventArgs());&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, the consumer of BigSpecialObject would take advantage of this loose coupling by doing the following:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;public class Consumer&lt;br /&gt;{&lt;br /&gt;   ActivityMonitor _monitor;&lt;br /&gt;&lt;br /&gt;   BigSpecialObject _bso;&lt;br /&gt;&lt;br /&gt;   public Consumer()&lt;br /&gt;   {&lt;br /&gt;     this.initialize();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private void initialize()&lt;br /&gt;   {&lt;br /&gt;     _monitor = new ActivityMonitor();&lt;br /&gt;     _bso = new BigSpecialObject();&lt;br /&gt;&lt;br /&gt;     _bso.WorkRequested += new EventHandler(onBSOWorkRequested);&lt;br /&gt;     _bso.WorkCompleted += new EventHandler(onBSOWorkCompleted);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private void onBSOWorkRequested(object sender, EventArgs e)&lt;br /&gt;   {&lt;br /&gt;     _monitor.AddActivity("About to do a bunch of stuff");&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private void onBSOWorkCompleted(object sender, EventArgs e)&lt;br /&gt;   {&lt;br /&gt;     _monitor.AddActivity("Done doing a bunch of stuff");&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void DoSomeConsumption()&lt;br /&gt;   {&lt;br /&gt;     _bso.DoStuff();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, there you have it.  Pattern versus Antipattern.  Having the Consumer class alert the ActivityMonitor when the work is done is better, because that means that BigSpecialObject is more reusable.  And even though we added more lines of code (believe it or not, I try to find solutions to problems that minimize lines of code), this is a better solution, because of the looser coupling.&lt;br /&gt;&lt;br /&gt;In looking at code I've written in the past, it's completely littered with examples of tight coupling.  Well, I guess it's time to get to work!&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-6024753832781379079?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/6024753832781379079/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=6024753832781379079" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/6024753832781379079?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/6024753832781379079?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/03/vendor-consumer-and-loose-coupling.html" title="The Vendor, The Consumer, and Loose Coupling" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;D0IARng_cSp7ImA9Wx9aEE0.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-5528729362100097961</id><published>2011-03-01T10:00:00.000-08:00</published><updated>2011-03-01T10:52:27.649-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-01T10:52:27.649-08:00</app:edited><title>Stock Predictor C# Utility</title><content type="html">This blog references an application and source code.&lt;br /&gt;&lt;br /&gt;To download this application, &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2MjM2ZDE0ODEtMjQ0Yy00Nzc2LTkzN2EtNzIyMTBjNmU3YjEy&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To download the source, &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2ZDFmM2ZlNTktOGE5MS00Y2U2LTk2ZjctNmZlNzg0ZDRmYmI1&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Evidently, some people buy and sell securities on markets in the attempt to make money.  These folks are called traders, and everyone is looking for the next big strategy to estimate what a particular security (also called Stock) is going to do in the future.&lt;br /&gt;&lt;br /&gt;I'm not particularly interested in this, but a friend of mine brought up a method many traders use to predict when it is time to buy or sell a stock.  The strategy involves looking at the current stock price, and comparing it to its 200-day moving average.  When the stock price is below the 200 day moving average, has a history of being above its 200 day moving average, and is on an upward trend, that is a good indication that it might be time to buy.&lt;br /&gt;&lt;br /&gt;When some version of the converse is true (stock is above the 200 day average, has a history of being below the 200 day moving average, and is on a downward trend), then it's time to sell.&lt;br /&gt;&lt;br /&gt;Below is a screenshot of the utility I built&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/-lFB1fMilzKk/TW004rG-b3I/AAAAAAAAABE/7qb1hPXzGEg/s1600/SearchResults.png"&gt;&lt;img width: 320px; height: 224px;" src="http://4.bp.blogspot.com/-lFB1fMilzKk/TW004rG-b3I/AAAAAAAAABE/7qb1hPXzGEg/s320/SearchResults.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5579173661631213426" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I'm not an active trader, and I know very little about how this whole capitalism thing works, but I know how to build an application that can represent this paradigm.  I built this in C# Winforms.  So, that's what I did.&lt;br /&gt;&lt;br /&gt;I used the ZedGraph library to build a graph for me, and I built some logic around some of the concepts.&lt;br /&gt;&lt;br /&gt;A few warnings:&lt;br /&gt;1.  I use Yahoo's stock history to grab prices.  Usage of this tool may or may not violate Yahoo's terms of use agreement.&lt;br /&gt;2.  I make no guarantees about the correctness of the data derived from this tool, and this tool in no way constitutes a recommendation of a security transaction.&lt;br /&gt;3.  The code quality of this tool is not as high as it could be.  It was sort of a rush job, because I didn't have much time to work on it before I had to start working on my next project.&lt;br /&gt;&lt;br /&gt;I hope someone can use this little tool and get some value out of it.  Or, if you decide to improve on it, let me know, and I'll post any fixes you make.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-5528729362100097961?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/5528729362100097961/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=5528729362100097961" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/5528729362100097961?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/5528729362100097961?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/03/stock-predictor-c-utility.html" title="Stock Predictor C# Utility" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-lFB1fMilzKk/TW004rG-b3I/AAAAAAAAABE/7qb1hPXzGEg/s72-c/SearchResults.png" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DEAER3k9fSp7ImA9Wx9UGEo.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-1808948803216742480</id><published>2011-02-16T09:15:00.000-08:00</published><updated>2011-02-16T09:18:26.765-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-16T09:18:26.765-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="User Experience" /><title>User Experience</title><content type="html">&lt;b&gt;Tim, Hero Extraordinaire&lt;/b&gt;&lt;br /&gt;I just completed a massive (well massive for the company I work for, anyway) process of importing some 6 million records into the CRM software we use.  It was an arduous process, but the work of one person (me, of course), over the course of about a week, saved a half a dozen people several weeks, or even months of data entry work.  It was a major win for both the company and the department I work in.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;The Shocker&lt;/b&gt;&lt;br /&gt;Because I'm a bit of a narcissist (or maybe even a diligent worker), I took some time to get a pulse check on how some of the end users were feeling about the software transition - of course, I was expecting to get pats on the back, offers of free beer, first-born children, and a lifetime of gratitude.  What I got instead was feedback from several people that it had been challenging and frustrating experience for them, even some of those people who were saved all that tedious time of data entry!&lt;br /&gt;&lt;br /&gt;Wait a minute.  What?  What are you talking about?  I saved you people zillions of keypresses, and that's your response?  That this has been a frustrating process?  Don't you people remember the last transition like this that you had to deal with (before I joined the company, of course)?  Printouts of data entry items had to be put in garbage bags for queuing because you ran out of boxes!&lt;br /&gt;&lt;br /&gt;So, the narcissist (or diligent worker...however you wish to label me) in me felt quite inclined to probe into why this was a frustrating experience.&lt;br /&gt;&lt;br /&gt;I got responses like:&lt;br /&gt;&lt;br /&gt;...The screen layout isn't quite right&lt;br /&gt;&lt;br /&gt;...When I press the tab key, it takes me to a different field than I want it to.&lt;br /&gt;&lt;br /&gt;...This screen's background color gives me a headache.&lt;br /&gt;&lt;br /&gt;If you've ever built any sizeable pieces of software, you know that these issues raised by the end-users are so mundane, that it's hard to give them priority over the bigger pieces of functionality that need to be built.  Background color, control placement, tab index...all of these things often end up being afterthoughts.  Finding the right &lt;a href="http://oopstruggles.blogspot.com/2011/02/id3-decision-tree-in-c.html"&gt;decision tree algorithm&lt;/a&gt;, or appropriate layering of your &lt;a href="http://oopstruggles.blogspot.com/2011/01/another-layers-of-programming-post.html"&gt;application's architecture&lt;/a&gt; are much sexier problems to solve.&lt;br /&gt;&lt;br /&gt;But to the end user, they make up a huge element of their experience.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Lessons Learned&lt;/b&gt;&lt;br /&gt;I don't really have any pearls of wisdom to provide around this.  After all, I'm certainly not an artist, and user interface elements are not particularly a strength of mine.  But I did take something important from this process.&lt;br /&gt;&lt;br /&gt;Firstly, when you take work away from someone who never knows they were going to have to do that work in the first place, the work you take away from them is invisible to them.  They don't care, and neither would you if you were in their position.  But that's the burden you bear as a person whose role it is to create and optimize processes that benefit end users.&lt;br /&gt;&lt;br /&gt;Secondly, pay attention to the user experience.&lt;br /&gt;&lt;br /&gt;I didn't build the CRM the company uses.  The things the users mentioned about their experience were all configurations of the software, and after paying attention to what the end-users were saying and changing the settings to what were optimal for them, we ended up getting a lot fewer complaints about this process.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Aftermath&lt;/b&gt;&lt;br /&gt;Since the CRM transition, I've pushed out a couple new features in an internal application that the marketing department uses, and for those features, before they got rolled out to production, I spent some time looking over the shoulder of an end-user who was testing it for me.  The 20 minutes I spent doing that led me to make a few minor tweaks to the user experience, and ended up saving the user several mouse clicks and about 30 seconds every time they used the features I built.  This 30 seconds, multiplied by a few hundred times per year, will be paying dividends for years.  And the user will enjoy using the software because it doesn't subconsciously make them want to start the building on fire because of all the darn mouseclicks their main software requires.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br /&gt;There are lots of themes in software development.  If we're not busy creating solutions that look for problems, we're spending a lot of time solving problems for ourselves, rather than for the consumers of our products.  User experience matters more than we think, and minor tweaks to the user interface help us to "put a bow" on our masterpieces.  As software developers, it's important to make sure that we pay attention to the user experience, and find opportunities to decrease mouse clicks, make the user interface more aesthetically pleasing, and create a user interface flow that is intuitive.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-1808948803216742480?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/1808948803216742480/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=1808948803216742480" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1808948803216742480?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1808948803216742480?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/02/user-experience.html" title="User Experience" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkcMRXc6cCp7ImA9Wx9UEUQ.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-8161922505057582747</id><published>2011-02-08T10:16:00.000-08:00</published><updated>2011-02-08T10:34:44.918-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-08T10:34:44.918-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="DataTable" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term=".Net" /><category scheme="http://www.blogger.com/atom/ns#" term="Data Access Layer" /><title>A PHP Data Access Layer that uses Custom DataTable</title><content type="html">In my last post about &lt;a href="http://oopstruggles.blogspot.com/2011/02/php-code-that-looks-like-net-code.html"&gt;PHP Code that looks like .Net Code&lt;/a&gt;, I demonstrated a DataTable object that I built in PHP.&lt;br /&gt;&lt;br /&gt;I'm really digging the potential that has bought me, so I created a DataAccessLayer object that has a method that can return query results as a DataTable.&lt;br /&gt;&lt;br /&gt;To download the updated "library" I'm building, &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2MmE3ZDlmODEtMzZjZS00MTdhLTg1ZjAtZTQ1Yzk5Yzg5MWMy&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The method I built is in the class.DataAccessLayer.php file.  The contents of the method are below:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;///Gets the query results as a datatable&lt;br /&gt;public function QueryResultsAsDataTable($query)&lt;br /&gt;{&lt;br /&gt; $this-&gt;connectToDatabase();  &lt;br /&gt; &lt;br /&gt; if( substr($query, 0, 6) == "SELECT" )&lt;br /&gt; {&lt;br /&gt;  $currentRow = 0;&lt;br /&gt;  if(!strpos($query,"'"))&lt;br /&gt;  {&lt;br /&gt;   $query = mysql_real_escape_string($query);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  $this-&gt;_dataSet   = mysql_query($query, $this-&gt;_connection);&lt;br /&gt;&lt;br /&gt;  $returnDataTable = new DataTable();&lt;br /&gt;&lt;br /&gt;  if($this-&gt;_dataSet)&lt;br /&gt;  {  &lt;br /&gt; &lt;br /&gt;   if (mysql_num_rows($this-&gt;_dataSet) &gt; 0 &amp;&amp; $currentRow == 0) &lt;br /&gt;   {&lt;br /&gt;&lt;br /&gt;    for ($i=0; $i &lt; mysql_num_fields($this-&gt;_dataSet); $i++) &lt;br /&gt;     $dt-&gt;AddColumn(mysql_field_name($this-&gt;_dataSet, $i));&lt;br /&gt;&lt;br /&gt;   }    &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   while($this-&gt;row = mysql_fetch_array($this-&gt;_dataSet))   &lt;br /&gt;   {&lt;br /&gt;    $dr = $returnDataTable-&gt;NewRow();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    for( $this-&gt;columnCount = 0; $this-&gt;columnCount &lt; mysql_num_fields($this-&gt;_dataSet); $this-&gt;columnCount++ )&lt;br /&gt;    {&lt;br /&gt;     $columnName = mysql_field_name($this-&gt;_dataSet, $this-&gt;columnCount);&lt;br /&gt;     $dr-&gt;AddValue($columnName, $this-&gt;row[$this-&gt;columnCount]);&lt;br /&gt;    }  &lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;    $returnDataTable-&gt;AddRow($dr);  &lt;br /&gt;   &lt;br /&gt;    $currentRow++;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;br /&gt;   return $returnDataTable;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  throw new Exception('Error getting data:  ' .mysql_errno($this-&gt;_connection) . ': ' . mysql_error($this-&gt;_connection));   &lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So, I could issue the following code to my data access layer, and I would be able to iterate through the results:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;$dt = $_dataAccessObject-&gt;QueryResultsAsDataTable("SELECT firstname, lastname FROM people WHERE id=3");&lt;br /&gt;&lt;br /&gt;$firstName = "";&lt;br /&gt;$lastName = "";&lt;br /&gt;&lt;br /&gt;for($i = 0; $i &lt; $dt-&gt;Rows-&gt;GetCount(); $i++)&lt;br /&gt;{&lt;br /&gt;   $firstName = $dt-&gt;Rows-&gt;GetValue("firstname");&lt;br /&gt;   $lastName  = $dt-&gt;Rows-&gt;GetValue("lastname");&lt;br /&gt;   print("FirstName is " . $firstName . " - LastName is " . $lastName);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So there you have it, a simple data access layer that returns a (somewhat) sophisticated object in a quick, non-obscure way.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-8161922505057582747?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/8161922505057582747/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=8161922505057582747" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/8161922505057582747?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/8161922505057582747?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/02/php-data-access-layer-that-uses-custom.html" title="A PHP Data Access Layer that uses Custom DataTable" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkcHR3s5fCp7ImA9Wx9UEU0.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-1538737601011317204</id><published>2011-02-07T08:33:00.000-08:00</published><updated>2011-02-07T09:33:56.524-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-07T09:33:56.524-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="DataTable" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="System.Data" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>PHP Code that Looks like .Net code</title><content type="html">This blog references PHP files available for download here:  &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2OWRlYjBkYjgtMzczYi00YzhiLWIyY2QtM2JiYWU4NmQxYmFi&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;In programming, I think it's important to learn.  Learning can involve new aspects of your chosen language, or new languages.  I've been thinking about my PHP days lately.  I really know a lot more about programming now then I did when I was actively programming in PHP.&lt;br /&gt;&lt;br /&gt;When I go back and look at some of the code I was producing in PHP, I cringe a bit at all of the principles I see myself violating.  Most of the PHP code I wrote were big balls of mud.&lt;br /&gt;&lt;br /&gt;So, as an academic experiment, as well as to try to re-sharpen some of my PHP skills, I decided to write some base libraries that I may (or may not) eventually use someday.&lt;br /&gt;&lt;br /&gt;As with any language (programming or verbal), or with any foreign concept, the first thing to do is to find metaphors that allow us to relate the themes of the thing we're learning to the things we already know.  The classic example in programming is the &lt;a href="http://en.wikipedia.org/wiki/Hello_world"&gt;Hello World&lt;/a&gt; application.&lt;br /&gt;&lt;br /&gt;Well, I'm not too interested in creating a "Hello World" for PHP, because I'm already familiar with a little bit of how PHP works.  But what I wanted to do was to build some PHP code that looks (at least a little bit) like .Net code.&lt;br /&gt;&lt;br /&gt;So, after thinking a little bit about what libraries I use most in .Net, I centered around System.Data, in particular DataTable (and DataRow, DataColumn, DataRowCollection, and DataColumnCollection).  I haven't created DataSet yet, because there's only so much time in the day.&lt;br /&gt;&lt;br /&gt;After thinking a bit more about how I would build some of this PHP code that looks like (has a similar interface) the System.Data .Net library, I concluded that I wanted to have a base Collection object.  So I created it, and put it into a file called System.Collections.php.  Below is a diagram of the code I created:&lt;br /&gt;&lt;br /&gt;&lt;img style="cursor:pointer; cursor:hand;width: 577px; height: 406px;" src="http://1.bp.blogspot.com/_Faiut5EqR8c/TVAohAly-FI/AAAAAAAAAA8/2LLguoTy-1M/s320/Net_PHP_Library.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5570997286616627282" /&gt;&lt;br /&gt;&lt;br /&gt;As you can see, in my PHP code, a DataTable has a DataRowCollection and a DataColumnCollection, as well as a couple of the external interface behaviors of .Net System.Data.DataTable (NewRow() and AddRow()); however, because of core differences between C# (or I guess .Net languages) and PHP, there are a few changes.  Below are the issues that I encountered in this mini-project:&lt;br /&gt;&lt;br /&gt;1.  I couldn't figure out if I could use an indexer type property to have square brackets ([]) to represent an indexed item in a collection.  Therefore, I left that for later.&lt;br /&gt;&lt;br /&gt;2.  There are some pretty serious differences between C# and PHP in terms of how they type variables.  In C#, generally you generally define a variable as a type on the left side of the variable name, and you initialize it on the right side (eg int a = 3;  OR Person p = new Person()).  You don't have to do that in PHP, so you can initialize a variable as any type, which throws a bit of a wrench into the model of how I see the world.&lt;br /&gt;&lt;br /&gt;3.  PHP (at least 5.0) supports a lot classic OOP concepts such as inheritance, interfaces, polymorphism, Exception handling, etc.  Obviously, the syntax of how to do this is different than it is in C#, so I dealt with some of the pains of the languages' differences.&lt;br /&gt;&lt;br /&gt;4.  PHP (from my understanding) does not support generic variables.  So I can't use List variables the way I like to in C#.  So, that's why DataRow and DataRowCollection both inherit from my Collection class.  &lt;br /&gt;&lt;br /&gt;But, I think my finished product gives me a basis to build on for later.  So, if I wanted to initialize a DataTable in PHP code, I would include a reference to System.Data.php, and do something like below:&lt;br /&gt;&lt;pre name="code" class="php"&gt; &lt;br /&gt;&lt;?&lt;br /&gt;include 'System.Data.php';&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;$dt = new DataTable();&lt;br /&gt;$dt-&gt;AddColumn("FirstName");&lt;br /&gt;$dt-&gt;AddColumn("LastName");&lt;br /&gt;$dt-&gt;AddColumn("Age");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;$dr = $dt-&gt;NewRow();&lt;br /&gt;$dr-&gt;AddValue("FirstName", "Tim");&lt;br /&gt;$dr-&gt;AddValue("LastName", "Claason");&lt;br /&gt;$dr-&gt;AddValue("Age", 29);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;$dt-&gt;AddRow($dr);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Notice in the above that, unlike c#, I can't do $dr["FirstName"].  Maybe there's a way to do this in PHP, but in my (short) research time, I didn't find it.&lt;br /&gt;&lt;br /&gt;Obviously, this interface isn't exactly the same as .Net's System.Data, but it's close enough to give me a decent metaphor between PHP and C#.&lt;br /&gt;&lt;br /&gt;Please note that I haven't tested this code yet, so there are no guarantees as to whether or not it works.  If it does work, expect more blog entries on this topic.&lt;br /&gt;&lt;br /&gt;To download the PHP Code I've got so far, &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2OWRlYjBkYjgtMzczYi00YzhiLWIyY2QtM2JiYWU4NmQxYmFi&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-1538737601011317204?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/1538737601011317204/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=1538737601011317204" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1538737601011317204?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1538737601011317204?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/02/php-code-that-looks-like-net-code.html" title="PHP Code that Looks like .Net code" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/_Faiut5EqR8c/TVAohAly-FI/AAAAAAAAAA8/2LLguoTy-1M/s72-c/Net_PHP_Library.jpg" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CUIAQH44eSp7ImA9Wx9VFko.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-3980503615617236119</id><published>2011-02-02T11:04:00.000-08:00</published><updated>2011-02-02T11:05:41.031-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-02T11:05:41.031-08:00</app:edited><title>Consistency in Coding (And Databases)</title><content type="html">I've been thinking about consistency in coding, and coding standards.  The thoughts I've been having on this concept have been sparked by my basic human desire to have consistency in my life, along with work I've been doing with the CRM that my company uses.&lt;br /&gt;&lt;br /&gt;I've been doing a whole bunch of work on the database of this CRM, basically copying records across a couple dozen tables.  I've noticed lots of inconsistencies and design problems in the database, and I thought I would enumerate what those problems were, and try to find the lessons that I can take from those design problems:&lt;br /&gt;&lt;br /&gt;1.  The database employs the use of natural keys, as opposed to surrogate keys.  An example of a natural key is the combination of a first name, middle name, last name, and social security number to identify the uniqueness of a record.  An example of a surrogate key is giving a unique number to each record, and targeting that unique number for database selects, updates, etc.  I believe that employing natural keys is a poor design decision, but not everyone agrees with me on that; however, I will never ever ever build a database that relies on natural keys.&lt;br /&gt;&lt;br /&gt;2.  There are a few tables in the database that rely on surrogate keys - an unnecessary inconsistency because the table has fields in it that allow it to link to its parent table via the natural key&lt;br /&gt;&lt;br /&gt;3.  There are major inconsistencies in column naming.  Some date columns are suffixed with _ISO, some abbreviate Date with "DT" and some spell "DATE" out in the column name.  There are other columns that have similar naming inconsistencies, too, such as "Account" and "Acct" or "Code" versus "CD" or "Event" versus "EVT".  I am always mixing these up, and all that confusion (which I'm sure occurs internally in the company that makes this CRM, as well) could have been avoided by simply making naming conventions more consistent.&lt;br /&gt;&lt;br /&gt;4.  Data Duplication.  The database has datetime stamps to indicate when the record was created, and it also has an integer representation of the time it was created.  I'm sure this integer representation is a carryover from legacy code and technology, but the column is still there, taking up space, and wasting resources.&lt;br /&gt;&lt;br /&gt;5.  Lack of normalization.  The database has a UDF recorded value table that has about 200 columns, and at least 2/3 of those columns are always null.  That table should be refactored/pivoted, which would reduce design complexity, and the amount of code required to represent that table -- not to mention memory required to store that data.&lt;br /&gt;&lt;br /&gt;6.  Too many tables with too many columns.  The average table seems to have at least 75 columns (with several having more than 200)...that's a database design smell, in my mind (and nose).&lt;br /&gt;&lt;br /&gt;There are other things too, but you get the picture.&lt;br /&gt;&lt;br /&gt;So, why do these problems happen?  In my experience working with software design, there's a number of reasons:&lt;br /&gt;&lt;br /&gt;1.  Legacy technical debt.  Carryover from older technologies that didn't have some of the bells and whistles we have now are a major reason why inconsistencies occur in the software - it's usually easier to write a wrapper to interact with old technologies than it is to completely re-write them.&lt;br /&gt;&lt;br /&gt;2.  Too many people.  Different people working in different areas of the product (along with no naming conventions/standards/whatever).  If there isn't a set standard on how datetime columns, bit columns, recurring-theme columns are named in the database, then there's going to be inconsistencies.&lt;br /&gt;&lt;br /&gt;3.  Learning.  No one knows everything, and I know less than most people; however, I'm learning all the time, and I am constantly finding things I did months or years ago that I would do different now.  This happens all the time in software, and I see plenty of examples in the database where it appears this happened.&lt;br /&gt;&lt;br /&gt;4.  Urgency to the market.  Let's face it, software is meaningless/worthless if it never gets out of design, construction, or testing phases.  Not to mention that companies have payrolls to meet.  There are always trade-offs between design excellence and need to get a product to the market.  And sometimes those who control the money decide that they'd rather deal with higher support costs 3-6 months from now than higher development costs today.&lt;br /&gt;&lt;br /&gt;5.  Dysfunctional development process.  When the development process doesn't support what the software is trying to do, and the team isn't "optimized" to use developers' strengths in the right way, it can lead to "cowboy programming".&lt;br /&gt;&lt;br /&gt;What to do about it?&lt;br /&gt;&lt;br /&gt;There's all kinds of things to do.  In fact, there's people and companies who make their living off of solutions to these problems.  I don't have all the answers, but here's what I do to avoid some of these problems:&lt;br /&gt;&lt;br /&gt;1.  Define a clear coding standard.  You can borrow from Microsoft, online forums, or convene with other developers to make decisions on how members, attributes, methods, interfaces, classes, etc should be named.  There are also plenty of online and book resources on style guidelines.&lt;br /&gt;&lt;br /&gt;2.  Keep learning.  Learning things, in the short term, leads to inconsistencies, but helps push your code, database, or whatever, to be as good as it can be&lt;br /&gt;&lt;br /&gt;3.  Incremental refactoring.  Rome wasn't built in a day, and neither is a 100,000 line application.  Baby steps is the way to go.  Slowly, but deliberately, refactor to achieve consistency.&lt;br /&gt;&lt;br /&gt;4.  Choose the right software development method.  Waterfall, SCRUM, TDD, XP, some combination of all of these...Team buy-in, and a methodology that suits the skill set of your team (including your business analysts and QA people, along with developers) is important to produce quality products.&lt;br /&gt;&lt;br /&gt;5.  Pragmattic Programming.  I love the book "The Pragmattic Programmer," because it outlines the type of developer we should all strive to be.  I wrote a blog on "&lt;a href="http://oopstruggles.blogspot.com/2011/01/writing-code-that-writes-code.html"&gt;Writing Code That Writes Code&lt;/a&gt;".  In it, I demonstrated an application that helps me produce very consistent code.&lt;br /&gt;&lt;br /&gt;Like everything else in life, whether it be good health, a good family life, a good career, etc, there's no silver bullet.  Practicing fundamentals, self-improvement, humility, and understanding that you will never be perfect is the way forward.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-3980503615617236119?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/3980503615617236119/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=3980503615617236119" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/3980503615617236119?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/3980503615617236119?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/02/consistency-in-coding-and-databases.html" title="Consistency in Coding (And Databases)" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;C0cDQ3syfSp7ImA9Wx9aFk8.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-3206862124264197660</id><published>2011-02-01T10:17:00.000-08:00</published><updated>2011-03-08T13:51:12.595-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-08T13:51:12.595-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Decision Tree" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="ID3" /><title>ID3 Decision Tree in C#</title><content type="html">This blog references an executable and source code available for download. To download the referenced executable, &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2MDcxZjVlMzEtMDk1MC00MmNhLTkwMGItZTM0MTc1MjI1YzRh&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To download source, &lt;a href="https://docs.google.com/leaf?id=0ByVjBO2MVhg2YTJlZTMxYTgtMDc3NC00NGJkLThkMzEtOWFkM2ExYmEzMTZk&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;It seems like just about anything "new and shiny" can distract me from the last "new and shiny" thing that I decide to devote all my energy to learning about and/or building.  Case in point:  AI (artificial intelligence).  There are all kinds of ways to use AI, and there's all kinds of subtechnologies that make up the field of Artificial Intelligence.&lt;br /&gt;&lt;br /&gt;Having said that, the first technology that has been easy enough for me to get my head around is "Decision Tree Learning."  A decision tree is basically an algorithm that takes a set of collected data, the outcomes of each collection of inputs, and builds a tree to demonstrate the best input variable for a particular output.  From my college days, I had lots of statistics classes that were very similar to this concept - particularly, regression analysis.&lt;br /&gt;&lt;br /&gt;A decision tree ends up looking a bit like the image below:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://upload.wikimedia.org/wikipedia/commons/9/91/BDD.png"&gt;&lt;br /&gt;&lt;br /&gt;An example of a decision tree implementation is, if you want to gather a bunch of information about what inputs affect whether or not it's going to rain, a decision tree can build a graphical representation on whether it will rain or not based on the inputs you provided.  Inputs for a decision tree may include cloudiness, temperature, relative humidity, what the weather report says, etc.  The decision tree algorithm should calculate the input that best guesses whether or not it's going to rain, and then finds the next best variable, etc, until a tree has been built that demonstrates a decision tree for determining whether or not it's going to rain.&lt;br /&gt;&lt;br /&gt;To demonstrate with words:  If the weather report says it will rain, and if the relative humidity is high, and it's very cloudy, then the output will be rain.&lt;br /&gt;&lt;br /&gt;Well, you get the idea...I don't completely have my head around the various ways to build a decision tree.  In fact, I'm quite a novice when it comes to demonstrating decision tree algorithms.&lt;br /&gt;&lt;br /&gt;The reason that I'm writing this blog entry at all is because I found a pretty decent implementation of an ID3 decision tree in C# at &lt;a href="http://www.codeproject.com/KB/recipes/id3.aspx"&gt;codeproject.com&lt;/a&gt;.  But when trying to use it to suit my needs, I wasn't able to make the original code suit my needs.  There were a few problems with the code that I felt compelled to fix.&lt;br /&gt;&lt;br /&gt;I don't know much about fellow who wrote this particular C# ID3 algorithm except that his screen name is "Roosevelt" and he's from Brazil - and the source code is commented in Portugese.  And he wrote it a long time ago.&lt;br /&gt;&lt;br /&gt;The funny thing is that this was really the only C# code I could find on the subject, and this code was written 7 1/2 years ago.  ID3 is not the most recent technology in Decision trees (evidently, C4.5 is a more recent iteration of decision tree learning).  Sure, Java code exists, but I'm not a Java developer, so some of the base libraries referenced made it difficult for me to translate the Java code to C#.&lt;br /&gt;&lt;br /&gt;So, I decided to see if I could take Roosevelt's code and make it less rigid (you see, all of the source data and attributes are statically defined in the code.  There's no way to configure the data without recompiling, and that just won't do).  In my iteration, the decision tree can be built dynamically based on the source data - it does not rely on statically defined concepts within the code, anymore (and output is not in Portugese, either).&lt;br /&gt;&lt;br /&gt;I did some other refactoring of the code as well, and made it a bit better - probably still not quite right, but I think it's quite a bit better.&lt;br /&gt;&lt;br /&gt;To download the executable I built, &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2MDcxZjVlMzEtMDk1MC00MmNhLTkwMGItZTM0MTc1MjI1YzRh&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To download source, &lt;a href="https://docs.google.com/leaf?id=0ByVjBO2MVhg2YTJlZTMxYTgtMDc3NC00NGJkLThkMzEtOWFkM2ExYmEzMTZk&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now that I've gotten my fill of this "new and shiny" thing, I can get back to the last "new and shiny" thing I was working on, and maybe some day (hopefully sooner than 7 1/2 years from now), someone who is zealous enough to improve my code can do so, and share it with the world.  For now, this is my contribution.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-3206862124264197660?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/3206862124264197660/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=3206862124264197660" title="8 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/3206862124264197660?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/3206862124264197660?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/02/id3-decision-tree-in-c.html" title="ID3 Decision Tree in C#" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>8</thr:total></entry><entry gd:etag="W/&quot;Ak8EQXYzeyp7ImA9Wx9VE00.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-2268345752904112654</id><published>2011-01-28T06:14:00.000-08:00</published><updated>2011-01-29T05:46:40.883-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-29T05:46:40.883-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Code Generator" /><category scheme="http://www.blogger.com/atom/ns#" term="Pragmattic Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>Writing Code That Writes Code</title><content type="html">This blog references an executable and source code available for download.  To download the referenced executable, &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2MDMxMmJlYjctOGFlNy00MGZjLWFhZGYtOTI4OGQxOWJjMzA3&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To download source, &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2MjJjYmUxZmItYmM1OS00YzBkLThjZWItZDk1MmQxYjk5ZmMx&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The Pragmatic Programmer, which is a really nifty book that outlines principles for how a programmer should conduct himself (or herself), tells us to "write code that writes code."  &lt;br /&gt;&lt;br /&gt;There are several reasons one would want to have code that writes code:&lt;br /&gt;&lt;br /&gt;1.  We tend to write code in a consistent way.&lt;br /&gt;2.  If we don't write code in a consistent way, we should&lt;br /&gt;3.  Because of 1 &amp; 2, reducing the amount of lines of code we have to physically write can increase our productivity, because it takes less physical work to create more functionality.&lt;br /&gt;&lt;br /&gt;When I'm building a database application in C#, my business logic classes often look very similar.  When considering how I would build an application that would write code for me, I started by looking at the consistencies in my code.  I found the following:&lt;br /&gt;&lt;br /&gt;1.  I mostly write database applications, and business classes that represent (more or less) table definitions in my database.  For example, for a CRM application that manages marketing campaigns, I might have a table called "Person" that represents people in my database; I may also have a table called "MarketingCampaign" that allows me to keep track of my marketing campaigns.  I may have a link table called "MarketingRecipient" that links "Person" to "MarketingCampaign" (in other words, the MarketingRecipient table has a record for each person a marketing campaign targeted).  To represent these business concepts in code, I would probably build *at least* 3 classes (probably more because I'm careful to avoid violating the Open-Closed principle).  But these classes would probably be:  Person, MarketingCampaign, and MarketingHistory (which is a collection of MarketingCampaign objects).  I don't use ORM (object-relational mapping), because I don't like to surrender as much as it seems ORM asks us to surrender for the sake of convenience.&lt;br /&gt;&lt;br /&gt;2.  Most of the business layer classes I build have a load() method and a Save() method, and they often look similar; however, not similar enough to over-rely on inheritance.&lt;br /&gt;&lt;br /&gt;3.  I often have a need to represent business layer classes as a collection.  These collections often look very similar - again, not similar enough to use inheritence (IMHO).&lt;br /&gt;&lt;br /&gt;4.  I make an effort to document (comment) every private member, public attribute, constructor, and method.&lt;br /&gt;&lt;br /&gt;5.  I often have a need to have a corresponding public attribute for every private member I have.  Obviously this is not always the case.&lt;br /&gt;&lt;br /&gt;Armed with these bits of information, I embarked on building a rinky-dink code generator that suits my needs.  &lt;br /&gt;&lt;br /&gt;So, this little application looks as follows:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_Faiut5EqR8c/TULSP9mcJMI/AAAAAAAAAAQ/o_YrXyXREAk/s1600/CC1.jpg"&gt;&lt;img src="http://4.bp.blogspot.com/_Faiut5EqR8c/TULSP9mcJMI/AAAAAAAAAAQ/o_YrXyXREAk/s320/CC1.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5567243261059015874" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;As you can see, this app is quite simple, and the top input control is for "Class Name".  This, as you might guess, asks you to enter a class name.&lt;br /&gt;&lt;br /&gt;In the below example, I create a class name of "Person".  Once I click the "Create" button, the panel below is activated.&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_Faiut5EqR8c/TULSzfe7V_I/AAAAAAAAAAY/Wbnkjusdfe8/s1600/CC2.jpg"&gt;&lt;img src="http://1.bp.blogspot.com/_Faiut5EqR8c/TULSzfe7V_I/AAAAAAAAAAY/Wbnkjusdfe8/s320/CC2.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5567243871449733106" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;At this point, I can start adding attributes to the class.  I can manually type in the data type I want, or there is a set of primitive types in the dropdown.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_Faiut5EqR8c/TULTLRlkbdI/AAAAAAAAAAg/4K3UsgBTCOw/s1600/CC3.jpg"&gt;&lt;img src="http://2.bp.blogspot.com/_Faiut5EqR8c/TULTLRlkbdI/AAAAAAAAAAg/4K3UsgBTCOw/s320/CC3.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5567244280036355538" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Once I'm done adding attributes to the class, I click the "Generate Code" button in the lower right corner:&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_Faiut5EqR8c/TULTazCk0jI/AAAAAAAAAAo/KeDsoiZhHvg/s1600/CC4.jpg"&gt;&lt;img src="http://4.bp.blogspot.com/_Faiut5EqR8c/TULTazCk0jI/AAAAAAAAAAo/KeDsoiZhHvg/s320/CC4.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5567244546714423858" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;And voila - we have code.  Granted, there's not a ton of functionality or code smarts this buys me, but it does do the following:&lt;br /&gt;&lt;br /&gt;1.  It does quite a bit of typing of members and attributes - this can be a big time saver when talking about 10-50 classes being created&lt;br /&gt;2.  It comments for me.  Maybe not the best comments, but I like to comment all my members, attributes, and methods&lt;br /&gt;3.  It saves me from having to rewrite the same things over and over again, which helps me to abide by the "Don't repeat yourself" principle.&lt;br /&gt;&lt;br /&gt;Granted, I often have to change things that are producted from this little app, but as mentioned above, this little app has turned out to be a big time saver.  I'm sure there are better code generators out there, but for now, this works for me.&lt;br /&gt;&lt;br /&gt;To download the referenced executable, &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2MDMxMmJlYjctOGFlNy00MGZjLWFhZGYtOTI4OGQxOWJjMzA3&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To download source, &lt;a href="https://docs.google.com/uc?id=0ByVjBO2MVhg2MjJjYmUxZmItYmM1OS00YzBkLThjZWItZDk1MmQxYjk5ZmMx&amp;export=download&amp;hl=en"&gt;Click Here&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-2268345752904112654?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/2268345752904112654/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=2268345752904112654" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/2268345752904112654?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/2268345752904112654?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/01/writing-code-that-writes-code.html" title="Writing Code That Writes Code" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_Faiut5EqR8c/TULSP9mcJMI/AAAAAAAAAAQ/o_YrXyXREAk/s72-c/CC1.jpg" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DEAMRn48eyp7ImA9Wx9WGE4.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-4537104791595071938</id><published>2011-01-23T11:35:00.000-08:00</published><updated>2011-01-23T18:39:47.073-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-23T18:39:47.073-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="n-tier" /><category scheme="http://www.blogger.com/atom/ns#" term="Layers in Programming" /><category scheme="http://www.blogger.com/atom/ns#" term="business logic" /><title>Another Layers of Programming Post</title><content type="html">The first blog I wrote on the layers of programming, I feel like I really faltered on describing what "Business Logic" is. &lt;br /&gt;&lt;br /&gt;Part of the reason for this is because the concrete definition of it wasn't as clear in my mind as it is now, and part of it was that I had more of an affinity towards the data access layer at the time, and I didn't find explaining business logic to be as exciting.&lt;br /&gt;&lt;br /&gt;Times have changed, and I find business logic to be much more exciting than I did then.  I also find this concept of "n-tier programming" to be one of the most challenging concepts to get right in object oriented programming.&lt;br /&gt;&lt;br /&gt;So, I'll do a quick recap of the three main tiers in n-tier programming:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Data Access Layer:&lt;/b&gt;  The code responsible for getting data from the database and delivering it to the business logic layer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;User Interface Layer:&lt;/b&gt;  The code responsible for rendering output to the end-user.  The user interface layer is mostly composed of user interface controls that render a user interface -- these controls can be the parent form that other controls are attached to, textboxes, comboboxes, radio buttons, and checkboxes for user input, buttons for user submission, list boxes and data grids for outputting collections of data...you get the idea.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Business Logic Layer:&lt;/b&gt;  The code responsible for solving the business problems that software is being built to solve.&lt;br /&gt;&lt;br /&gt;There are all kinds of implications for what the business logic layer is.  Because object oriented programming helps us represent reality so well, I usually have my business logic layer define the concepts of the system I'm building.&lt;br /&gt;&lt;br /&gt;For instance, suppose we're building a customer relationship management software (CRM). &lt;br /&gt;&lt;br /&gt;Suppose this CRM is supposed to:&lt;br /&gt; 1.   Keep track of our customers&lt;br /&gt; 2.   Help us manage our inventory&lt;br /&gt; 3.   Keep sales history for our customers&lt;br /&gt;&lt;br /&gt;Obviously we could add more features to this CRM, but I think the above is adequate for describing what Business Logic does. &lt;br /&gt;&lt;br /&gt;There's a lot of ways (methodologies) to build software - usually software is built after requirements have been written. &lt;br /&gt;&lt;br /&gt;So, I'll preface these definitions with the fact that I'm building this business logic without having a requirements document.&lt;br /&gt;&lt;br /&gt;But in this CRM, we need concepts defined, such as:  what is a customer?  what is a product?  What is a sale?   How do these concepts interact with one another?&lt;br /&gt;&lt;br /&gt;The answers to the above questions really define what business logic is.  For instance, one of the classes in my business logic layer may be Customer.  The customer class would look like this:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;public class Customer&lt;br /&gt;{&lt;br /&gt;   private string _firstName;&lt;br /&gt;&lt;br /&gt;   private string _lastName;&lt;br /&gt;&lt;br /&gt;   private string _emailAddress;&lt;br /&gt;&lt;br /&gt;   private string _address;&lt;br /&gt;&lt;br /&gt;   public string FirstName&lt;br /&gt;   {&lt;br /&gt;     get { return _firstName; }&lt;br /&gt;     set { _lastName = value; }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public string LastName&lt;br /&gt;   {&lt;br /&gt;     get { return _lastName; }&lt;br /&gt;     set { _lastName = value; }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public string EmailAddress&lt;br /&gt;   {&lt;br /&gt;     get { return _emailAddress; }&lt;br /&gt;     set { _emailAddress = value; }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public string Address&lt;br /&gt;   {&lt;br /&gt;     get { return _address; }&lt;br /&gt;     set { _address = value; }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The above is a truncated version of what the Customer class would look like, but the above would also be the first lines of code that would come in the business logic layer, because all of the features of the CRM focuses around the concept of "Customer."&lt;br /&gt;&lt;br /&gt;The next concept to define in our Business Logic is the concept of an Inventory Product.  I would imagine that an inventory product would look a bit like the code below:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;public class InventoryProduct&lt;br /&gt;{&lt;br /&gt;   private string _partNumber;&lt;br /&gt;&lt;br /&gt;   private string _productName;&lt;br /&gt;&lt;br /&gt;   private bool _isActive;&lt;br /&gt;&lt;br /&gt;   public string PartNumber&lt;br /&gt;   {&lt;br /&gt;     get { return _partNumber; }&lt;br /&gt;     set { _partNumber = value; }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public string ProductName&lt;br /&gt;   {&lt;br /&gt;     get { return _productName; }&lt;br /&gt;     set { _productName = value; }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public bool IsActive&lt;br /&gt;   {&lt;br /&gt;     get { return _isActive; }&lt;br /&gt;     set { _isActive = value; }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, the next question to answer is how does a Customer interact with an InventoryProduct?  I would argue that a class called "ItemPurchase" would need to get created.  An ItemPurchase would basically be a link between a Customer and an InventoryProduct, with a couple other attributes, such as a PurchaseDate and a Quantity, and maybe a couple other things, depending on what kind of information is getting collected about it.&lt;br /&gt;&lt;br /&gt;Purchase would look as follows:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;public class ItemPurchase&lt;br /&gt;{&lt;br /&gt;   private Customer _customer;&lt;br /&gt;   private InventoryProduct _product;&lt;br /&gt;   private int _quantity;&lt;br /&gt;   private DateTime _date;&lt;br /&gt;&lt;br /&gt;   public ItemPurchase(Customer customer, InventoryProduct product, int quantity)&lt;br /&gt;   {&lt;br /&gt;      _customer = customer;&lt;br /&gt;      _product = product;&lt;br /&gt;      _quantity = quantity;&lt;br /&gt;      _date = DateTime.Now;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   ///Other methods to save the purchase&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This link between Customer and InventoryProduct is great, but we've got another problem:  the problem is that the ItemPurchase object only covers one item.  Obviously, a customer can purchase multiple things at one time, so we need another collection class.  Maybe it could be called "ShoppingCart."  I'll exclude the code for this because this is getting to be kind of a long blog entry, but I think you get the idea.&lt;br /&gt;&lt;br /&gt;The shopping cart would serve to link an entire purchase to a customer...which leads us to another collection - PurchaseHistory.  A PurchaseHistory object would be a list of ShoppingCart objects, and this PurchaseHistory object would get linked to a Customer object, and the Customer class would HAVE-A PurchaseHistory object in it.  This relationship in the business logic also lines up very well with how the database would be designed (not covering database design in this post).&lt;br /&gt;&lt;br /&gt;So, since I'm creating an PurchaseHistory object, which is essentially a List of ShopingCart objects, AND since I don't want to violate Open-Closed principle, I would start by creating a InventoryProductCollection object, and have PurchaseHistory inherit from that:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;public class ShoppingCartCollection : List &amp;lt;ShoppingCart&amp;gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class PurchaseHistory : ShoppingCartCollection &lt;br /&gt;{&lt;br /&gt;   Customer _customer;&lt;br /&gt;&lt;br /&gt;   public PurchaseHistory(Customer customer)&lt;br /&gt;   {&lt;br /&gt;     _customer = customer;&lt;br /&gt;     this.populate();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private void populate()&lt;br /&gt;   {&lt;br /&gt;     ///Go to the data access layer to grab information about this customer's purchase history&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Notice in PurchaseHistory, the default constructor has a parameter of Customer.  This makes sense to me because a PurchaseHistory, as we've defined it so far, does not seem to exist outside the context of a Customer.  There's a lot of conversation that can be had around this, and how a PurchaseHistory will exist, but for this example, we'll just assume this will always be the case.&lt;br /&gt;&lt;br /&gt;Because a customer will HAVE-A PurchaseHistory, I would modify the Customer class as follows:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;public class Customer&lt;br /&gt;{&lt;br /&gt;     ///All of the code above&lt;br /&gt;&lt;br /&gt;   private PurchaseHistory _purchases;&lt;br /&gt;&lt;br /&gt;   public PurchaseHistory Purchases&lt;br /&gt;   {&lt;br /&gt;     get&lt;br /&gt;     {&lt;br /&gt;          if(_purchases == null)&lt;br /&gt;          {&lt;br /&gt;             _purchases = new PurchaseHistory(this);&lt;br /&gt;          }&lt;br /&gt;          return _purchases;&lt;br /&gt;     }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Notice in the getter in Customer, I check to see if the _purchases member is null - this is a trick called "Lazy initialization," which helps me avoid instantiating a PurchaseHistory object until I need it.  This may be the right thing to do or the wrong thing to do, depending on all kinds of other factors (size and optimization of database indexes, how often a Customer's PurchaseHistory will be accessed, how often a Customer's PurchaseHistory will be changed, etc, etc, etc).&lt;br /&gt;&lt;br /&gt;So, the business logic we've defined will:&lt;br /&gt;&lt;br /&gt;1.  Get its data from the database&lt;br /&gt;2.  Deliver it to the user interface&lt;br /&gt;3.  The data will (presumably) be changed in the user interface&lt;br /&gt;4.  The request to save to the database will come from the user interface&lt;br /&gt;5.  Prompting our business logic to tell the data access layer to save the changed data to the database.&lt;br /&gt;&lt;br /&gt;That's business logic as I see it, and this is where a programmer generally earns his (or her) wage.  The ability to see the business concepts as relationships, and then represent them in code is a real skill that takes years of tweaking to get right.  There's all kinds of pitfalls and obstacles that come with almost any strategy one uses when building this stuff, ranging from scalability issues, extensibility issues, code management issues because of poor design or overly complicated design, performance problems because of use (or lack thereof) of lazy initialization, and a host of other things.&lt;br /&gt;&lt;br /&gt;But, this is the world as I see it, and I hope it helps.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-4537104791595071938?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/4537104791595071938/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=4537104791595071938" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/4537104791595071938?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/4537104791595071938?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/01/another-layers-of-programming-post.html" title="Another Layers of Programming Post" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;A0QCQ3c9fyp7ImA9Wx9XEEU.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-8549558802046502879</id><published>2011-01-03T13:14:00.000-08:00</published><updated>2011-01-03T13:16:02.967-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-03T13:16:02.967-08:00</app:edited><title>The Liskov Substitution Principle</title><content type="html">Where the Open-Closed princple advocates inheritance to build good code, the Liskov Substitution principle says "Not so fast!"  Inheritance is a great way to create hierarchies in programming, but with great power comes great responsibility.  Officially, the Liskov Substitution Principle states:  Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.  There are 2 main risks that one assumes by using inheritance: &lt;br /&gt;&lt;br /&gt;1.  Inappropriate behavior when casting occurs&lt;br /&gt;2.  Overly broad generalizations about child class behavior&lt;br /&gt;&lt;br /&gt;Again, Martin relies on shapes to demonstrate how inheritance can create problems, this time using a square and a rectangle as the demonstration points.&lt;br /&gt;&lt;br /&gt;In his example, he describes a Rectangle as a parent class, and a Square as a child class of Rectangle.  Below is C# code that emulates behavior Martin describes:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;    public class Rectangle&lt;br /&gt;    {&lt;br /&gt;        protected double _width;&lt;br /&gt;        protected double _height;&lt;br /&gt;&lt;br /&gt;        /// &lt;summary&gt;&lt;br /&gt;        /// Just to keep track of the object&lt;br /&gt;        /// &lt;/summary&gt;&lt;br /&gt;        protected string _objectName;&lt;br /&gt;&lt;br /&gt;        public Rectangle()&lt;br /&gt;        {&lt;br /&gt;            _objectName = "Rectangle";&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public virtual void SetWidth(double w)&lt;br /&gt;        {&lt;br /&gt;            _width = w;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public virtual void SetHeight(double h)&lt;br /&gt;        {&lt;br /&gt;            _height = h;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public double GetWidth()&lt;br /&gt;        {&lt;br /&gt;            return _width;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public double GetHeight()&lt;br /&gt;        {&lt;br /&gt;            return _height;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void Output()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(_objectName + " attributes: Height=" + _height + " | Width=" + _width);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public class Square : Rectangle&lt;br /&gt;    {&lt;br /&gt;        public Square()&lt;br /&gt;        {&lt;br /&gt;            _objectName = "Square";&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public override void SetWidth(double w)&lt;br /&gt;        {&lt;br /&gt;            _width = w;&lt;br /&gt;            _height = w;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public override void SetHeight(double h)&lt;br /&gt;        {&lt;br /&gt;            _height = h;&lt;br /&gt;            _width = h;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The above code looks clean, elegant, etc, but there's a problem.  And this problem comes when, later on in the development process, someone builds code that takes the parent class as a parameter, expecting &lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;class SomeImplementation&lt;br /&gt;    {&lt;br /&gt;        public void DoStuff()&lt;br /&gt;        {&lt;br /&gt;&lt;br /&gt;            Square square = new Square();&lt;br /&gt;            manipulateShape(square);&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private void manipulateShape(Rectangle rectangle)&lt;br /&gt;        {&lt;br /&gt;            rectangle.SetHeight(2);&lt;br /&gt;            rectangle.SetWidth(4);&lt;br /&gt;&lt;br /&gt;            if (rectangle.GetWidth() * rectangle.GetHeight() == 8)&lt;br /&gt;                return;&lt;br /&gt;&lt;br /&gt;            throw new Exception("Error with rectangle dimensions");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In the above example, if a Rectangle object were passed to manipulateShape(), everything would be fine; however, in this case, when a Square object (which also IS-A rectangle) was passed to manipulateShape(), it throws an exception, because the Width * Height would equal 16, and not 8.  The real problem here is that we over-rely on the parent's behavior, and as the body of code grows, managability is a major concern, because other users of code ought to rely on expected behavior, without having to always factor the interaction that occurs in the inheritance chain.&lt;br /&gt;&lt;br /&gt;Ultimately, the developer who wrote the manipulateShape() method is expecting a Rectangle, but not one of its children (which is a reasonable thing to do); however, the real issue in this design is the overreliance on virtual/overridden methods:  a square is a rectangle based on its public interface, but not on its behavior.&lt;br /&gt;&lt;br /&gt;Consider a modification to the example, where a Rectangle is generated from some outside method:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;Rectangle someUnknownShape = getShape();&lt;br /&gt;manipulateShape(someUnknownShape);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If the getShape() method returns anything other than a top-level Rectangle, the exception gets thrown.&lt;br /&gt;&lt;br /&gt;Because the behavior of Square is not consistent with Rectangle, the ultimate solution is to not have Square inherit from Rectangle.  The safer alternative,  in C#, as I see it, is to create an interface that both Square and Rectangle implement:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;public interface IBoxShape&lt;br /&gt;{&lt;br /&gt; void SetWidth(double width);&lt;br /&gt; void SetHeight(double height);&lt;br /&gt; void GetWidth();&lt;br /&gt; void GetHeight();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If both Square and Rectangle implemented the IBoxShape interface, then the above manipulateShape() method could keep its parameter as Rectangle, and the behavior of the application would perform as expected, and the developer(s) would not be tempted to send inappropriate objects to manipulateShape().&lt;br /&gt;&lt;br /&gt;Going back to the Liskov Substitution Principle:  Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.&lt;br /&gt;&lt;br /&gt;The manipulateShape() method in its original state ought to be able to expect that the area of the rectangle, after manipulating it, will be 8 - that behavior ought to be transparent, and changing behavior in child classes by use of virtual and overridden methods should be avoided.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-8549558802046502879?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/8549558802046502879/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=8549558802046502879" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/8549558802046502879?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/8549558802046502879?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/01/liskov-substitution-principle.html" title="The Liskov Substitution Principle" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;A0UHRHk8fyp7ImA9Wx9XEEU.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-1595861405665156905</id><published>2011-01-03T13:09:00.000-08:00</published><updated>2011-01-03T13:13:55.777-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-03T13:13:55.777-08:00</app:edited><title>The Open-Closed Principle</title><content type="html">The Open-Closed Principle states, quite concisely, that classes should be "Open for extension, but closed for modification."  In other words, the attributes and behaviors of a class (ie the building blocks of a class) should be thoughtfully placed where they belong in the inheritance chain.  I don't think I could really overstate how many times I've violated this principle.  In fact, in looking back at code I've written in the past, I see all kinds of examples where I violate it.  But this is definitely one of the principles that has strongly impacted how I write code.&lt;br /&gt;&lt;br /&gt;The example Uncle Bob used to describe this principle was a Shape example.  The implementation of the Open-Closed is via inheritance, and eventually, polymorphism.&lt;br /&gt;&lt;br /&gt;Martin starts with an antipattern to describe Open-Closed.  Below is an example in C# that is similar to Martin's:&lt;br /&gt;&lt;br /&gt;We start with some base citizens in our code base:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt; &lt;br /&gt;public class Shape&lt;br /&gt;{&lt;br /&gt; double Area;&lt;br /&gt; ///...Other shape code&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Square : Shape&lt;br /&gt;{&lt;br /&gt; ///...Square code&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Circle : Shape&lt;br /&gt;{&lt;br /&gt; ///....Circle code&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class SomeImplementation&lt;br /&gt;{&lt;br /&gt; public void DrawCircle()&lt;br /&gt; {&lt;br /&gt;  ///...Draw a circle&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void DrawSquare()&lt;br /&gt; {&lt;br /&gt;  ///...Draw a square&lt;br /&gt; }&lt;br /&gt; ///...Some code in Some Implementation&lt;br /&gt;&lt;br /&gt; public void DrawAllShapes(List&lt;Shape&gt; shapeList)&lt;br /&gt; {&lt;br /&gt;  for(int i = 0; i &lt; shapeList.Count; i++)&lt;br /&gt;  {&lt;br /&gt;   Shape currentShape = shapeList[i];&lt;br /&gt;   if(currentShape is Square)&lt;br /&gt;    DrawSquare();&lt;br /&gt;   else if(currentShape is Circle)&lt;br /&gt;    DrawCircle();&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The above code violates the Open-Closed Principle because it could not accomodate new shapes.  So, adding new shapes would entail making a change to DrawAllShapes() every time a new shape got added to the program's vernacular.  In the original article, Shape, Square, and Circle were Structs, not Classes, which made the antipattern much more ugly-looking.  I didn't use a struct in my example, because overusing Structs was never a real problem in my learning how to program.&lt;br /&gt;&lt;br /&gt;The better solution, which does not violate the Open-Closed Principle is shown below:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt; &lt;br /&gt;public abstract class Shape&lt;br /&gt;{&lt;br /&gt; double Area;&lt;br /&gt;&lt;br /&gt;  public abstract void Draw();&lt;br /&gt;&lt;br /&gt; ///...Other shape code&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Square : Shape&lt;br /&gt;{&lt;br /&gt; public override void Draw()&lt;br /&gt; {&lt;br /&gt;  ///...Do Square drawing&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Circle : Shape&lt;br /&gt;{&lt;br /&gt; public override void Draw()&lt;br /&gt; {&lt;br /&gt;  ///...Do Circle drawing&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class SomeImplementation&lt;br /&gt;{&lt;br /&gt; ///...Some code in Some Implementation&lt;br /&gt;&lt;br /&gt; public void DrawAllShapes(List&lt;Shape&gt; shapeList)&lt;br /&gt; {&lt;br /&gt;  foreach(Shape currentShape in shapeList)&lt;br /&gt;   currentShape.Draw();&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In the above code, the SomeImplementation class uses polymorphism to draw any shape in the passed shape list.  So, as more shapes are added to the application, the DrawAllShapes() method does not need to change (the Closed portion of the principle), and the new shape can simply be a new class, inherited from Shape, or one of its children (the Open portion of the principle).&lt;br /&gt;&lt;br /&gt;As a personal aside, I used to violate this principle all the time, because I would create "Collection classes" that would inherit from a List&lt;Whatever&gt; object, and every time a need for a new type of the same collection occurred, I would add a new constructor to represent that type of collection; the better solution would have been to inherit from the base collection.  So, the below code is what I used to do:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt; &lt;br /&gt;public class SomeClass&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class SomeClassCollection : List&lt;SomeClass&gt;&lt;br /&gt;{&lt;br /&gt; Category _category;&lt;br /&gt; Person _person;&lt;br /&gt;&lt;br /&gt; public class SomeClassCollection()&lt;br /&gt; {&lt;br /&gt;  _category = null;&lt;br /&gt;  _person = null;&lt;br /&gt;  populate();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public class SomeClassCollection(Category category)&lt;br /&gt; {&lt;br /&gt;  _category = category;&lt;br /&gt;  _person = null;&lt;br /&gt;  populate();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public class SomeClassCollection(Person person)&lt;br /&gt; {&lt;br /&gt;  _person = person;&lt;br /&gt;  _category = null;&lt;br /&gt;  populate();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private void populate()&lt;br /&gt; {&lt;br /&gt;  if(_category == null &amp;&amp; _person == null)&lt;br /&gt;  {&lt;br /&gt;   ///Do type 1 collection population&lt;br /&gt;  }&lt;br /&gt;  else if(_category == null &amp;&amp; _person != null)&lt;br /&gt;  {&lt;br /&gt;   ///Do type 2 collection population&lt;br /&gt;  }&lt;br /&gt;  else if(_category != null &amp;&amp; _person == null)&lt;br /&gt;  {&lt;br /&gt;   ///Do type 3 collection population&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The better way to build the above code to avoid violating the Open-Closed Principle would have been to extend SomeClassCollection:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt; &lt;br /&gt;public class SomeClassCollection : List&lt;SomeClass&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt; protected virtual void populate()&lt;br /&gt; {&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public CategoryCollection : SomeClassCollection&lt;br /&gt;{&lt;br /&gt; public CategoryCollection(Category category)&lt;br /&gt; {&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt; override void populate()&lt;br /&gt; {&lt;br /&gt;  ///Populate based on category&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public CategoryCollection : SomeClassCollection&lt;br /&gt;{&lt;br /&gt; public CategoryCollection(Person person)&lt;br /&gt; {&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt; override void populate()&lt;br /&gt; {&lt;br /&gt;  ///Populate based on person&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-1595861405665156905?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/1595861405665156905/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=1595861405665156905" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1595861405665156905?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1595861405665156905?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/01/open-closed-principle.html" title="The Open-Closed Principle" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;Ak4DSX88cCp7ImA9Wx9XEEU.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-4709884364586818192</id><published>2011-01-03T13:08:00.000-08:00</published><updated>2011-01-03T13:09:38.178-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-03T13:09:38.178-08:00</app:edited><title>SOLID Principles</title><content type="html">I recently stumbled onto a collection of articles written by an author named Robert Martin way back in 1996 for a now defunct magazine called "The C++ Report" (The C++ Report was evidently followed by the "Journal of Object-Oriented Programming", which doesn't seem to exist anymore either).  These articles address core principles of object oriented design, and seem as true today as they were all those years ago.  Of course, a few of the references Martin used in his articles may be a bit outdated, but the spirit and intent behind the articles is clear, and unlike a lot of articles I read that were written much more recently than Martin's articles, these articles are unmuddied by the complexities and interactions between all of the various technologies, subtechnologies, wrappers, APIs, etc that exist these days.&lt;br /&gt;&lt;br /&gt;Martin (affectionately known as "Uncle Bob") wasn't really inventing any new concepts with his articles; rather, he was simply synthesizing information created by others into a paradigm that was appropriate for the time and available technology.  But Robert Martin's articles were so successful and widely adopted, that they are still at the core of most object oriented design that is in use today, including later concepts, such as design patterns.  Evidently, Martin is still at it, owning a company called "Object Mentor" which does company/enterprise-level coaching of these, and other object oriented design principles.  He's also written a number of books on the topic, as well.  Today, Martin is considered a legend in the programming world.  He even has a blog, which can be found at &lt;a href="http://blog.objectmentor.com/articles/category/uncle-bobs-blatherings"&gt;http://blog.objectmentor.com/articles/category/uncle-bobs-blatherings&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I'm about half way through reading this series of articles Martin wrote, and the information I've gleened from them have drastically (or at least, significantly) impacted how I approach design and refactoring (in fact, some of the principles demonstrated in these articles have sparked several refactoring sessions).  Martin's SOLID principles are made up of:  Single Responsibility Principle, The Open-Closed Principle, The Liskov Substitution Principle, the Interface Segregation Principle, and the Dependency Inversion Principle.&lt;br /&gt;&lt;br /&gt;If nothing else my next few blogs will be a way for me to take the information I gathered from the core OOP principles Uncle Bob put forward (hereafter referred to as SOLID principles).  And perhaps, someone out there will read them, and be compelled to apply them in their own programming.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-4709884364586818192?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/4709884364586818192/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=4709884364586818192" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/4709884364586818192?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/4709884364586818192?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2011/01/solid-principles.html" title="SOLID Principles" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DEINRH08cCp7ImA9Wx5bEE4.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-5708702534182318196</id><published>2010-10-25T09:53:00.000-07:00</published><updated>2010-10-25T12:56:35.378-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-10-25T12:56:35.378-07:00</app:edited><title>Resizable Controls on a .Net desktop form</title><content type="html">I've never really found a great way to have controls on a form grow and shrink with a form when a C# Desktop app&amp;nbsp;form gets resized.&amp;nbsp; I'm sure that there are other solutions out there besides the one I created, but sometimes sometimes I feel the need to just write code.&amp;nbsp; So, a little background on what I did:&lt;br&gt;&lt;br&gt;1.&amp;nbsp; I created a form object called ResizeableForm, which inherits from System.Windows.Forms.&lt;br&gt;2.&amp;nbsp; I created an AbstractControl object, which Has-A System.Windows.Forms.Control on it&lt;br&gt;3.&amp;nbsp; I created an AbstractControlCollection object, which inherits from List&amp;lt;AbstractControl&amp;gt;.&amp;nbsp; This simply provides a quicker means to do bulk operations on my collection of AbstractControls.&lt;br&gt;&lt;br&gt;When the ResizeableForm gets painted, I initialize my AbstractControlCollection (if it is null):&lt;br&gt;&lt;br&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&lt;br&gt;private&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;void&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; onFormPaint(&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;object&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; sender, &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;PaintEventArgs&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; e)&lt;br&gt;{&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; if&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; (_controls == &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;null&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _controls = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AbstractControlCollection&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;(&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;this&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;);&lt;br&gt;}&lt;br&gt;&lt;br&gt;I also added a Resize event handler to make a call to my AbstractControlCollection.Resize() method:&lt;br&gt;&lt;br&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&lt;br&gt;private&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;void&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; onFormResize(&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;object&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; sender, &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;EventArgs&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; e)&lt;br&gt;{&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; if&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; (_controls == &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;null&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp; _controls.Resize();&lt;br&gt;}&lt;br&gt;&lt;br&gt;Obviously, the above code requires that you link up these delegates to the form's event handlers with the following lines:&lt;br&gt;&lt;br&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br&gt;this&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;.Paint += &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;PaintEventHandler&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;(onFormPaint);&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;this&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;.Resize += &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;EventHandler&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;(onFormResize);&lt;/font&gt;&lt;br&gt;The abstractcontrol object keeps track of ratios that the original control had, in terms of width and height, as well as distance from the top and left of the form.&amp;nbsp; Below is the class:&lt;br&gt;&lt;br&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&lt;br&gt;///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Stores information about a form, and provides a quick means &lt;br&gt;/// to resize a control when a form is resized&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;class&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AbstractControl&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;{&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; The real control associated with this control&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;&amp;nbsp; Control&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; _associatedControl;&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; The form associated with the control&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;&amp;nbsp; ResizableForm&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; _parentForm;&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Original Height ratio&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; _originalHeightRatio;&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Original width ratio&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; _originalWidthRatio;&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Original X Location of the control&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; _originalXRatio;&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Original Y Location of the form&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; _originalYRatio;&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; The handle associated with the associated control&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;&amp;nbsp; IntPtr&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; _handle; &lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;p style="margin-right: 0px;" dir="ltr"&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; public&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; AbstractControl(&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;Control&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; c, &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;ResizableForm&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; originalForm)&lt;br&gt;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; _parentForm = originalForm;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _associatedControl = c;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _handle = c.Handle;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _originalHeightRatio = (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)c.Height / (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)originalForm.Height;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _originalWidthRatio = (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)c.Width / (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)originalForm.Width;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _originalXRatio = (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)c.Location.X / (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)originalForm.Width;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _originalYRatio = (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)c.Location.Y / (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)originalForm.Height;&lt;br&gt;&amp;nbsp; }&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Public access to this control's handle&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; public&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;IntPtr&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; Handle&lt;br&gt;&amp;nbsp; {&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;get&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; { &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;return&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; _handle; }&lt;br&gt;&amp;nbsp; }&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Provides a means to resize the control based on the new form size&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; public&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;void&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; Resize()&lt;br&gt;&amp;nbsp; {&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;int&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; newWidth = (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;int&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)((&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)(_parentForm.Width * _originalWidthRatio));&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; newHeight = (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;int&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)((&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)(_parentForm.Height * _originalHeightRatio));&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; newX = (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;int&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)((&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)(_parentForm.Width * _originalXRatio));&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; newY = (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;int&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)((&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;double&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)(_parentForm.Height * _originalYRatio)); &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _associatedControl.Width = newWidth;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _associatedControl.Height = newHeight;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _associatedControl.Location = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; System.Drawing.&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;Point&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;(newX, newY);&lt;br&gt;&amp;nbsp; }&lt;br&gt;&lt;br&gt;So, the remaining piece of the code is the Abstract Control Collection:&lt;/font&gt;&lt;br&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&lt;br&gt;///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Represents a collection of abstract controls&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;class&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AbstractControlCollection&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; : &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;List&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AbstractControl&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&amp;gt;&lt;br&gt;{&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Form associated with the control collection&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;&amp;nbsp; ResizableForm&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; _originalForm;&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Default constructor - adds controls to the collection based on the controls on the form&lt;br&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;param name="originalForm"&amp;gt;Form associated with the collection&amp;lt;/param&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; public&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; AbstractControlCollection(&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;ResizableForm&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; originalForm)&lt;br&gt;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;_originalForm = originalForm;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; addControls(originalForm);&lt;br&gt;&amp;nbsp; }&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Method to add all controls and children controls to the collection&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;param name="baseControl"&amp;gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt;Control to add child controls from&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/param&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; private&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;void&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; addControls(&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;Control&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; baseControl)&lt;br&gt;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;foreach&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; (&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;Control&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; c &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;in&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; baseControl.Controls)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; AbstractControl&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; abstractControl = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AbstractControl&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;(c, _originalForm);&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&lt;br&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; (!&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;this&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;.Contains(abstractControl, &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;true&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;))&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;his&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;.Add(abstractControl);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; addControls(c);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp; }&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Quick way to resize all the controls on the form at the same time&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; public&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;void&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; Resize()&lt;br&gt;&amp;nbsp; {&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; foreach&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; (&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AbstractControl&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; c &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;in&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;this&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; c.Resize();&lt;br&gt;&amp;nbsp; }&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; Separate contains function to check to see if the collection contains a control&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;param name="control"&amp;gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt;Control to search for&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/param&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;param name="overrideIndicator"&amp;gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt;Overload var&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/param&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;nbsp; ///&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;returns&amp;gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt;True if the collection contains the searched control&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;/returns&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp; public&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;bool&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; Contains(&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AbstractControl&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; control, &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;bool&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; overrideIndicator)&lt;br&gt;&amp;nbsp; {&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;foreach&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; (&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;AbstractControl&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; c &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;in&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;this&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; if&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; (control.Handle == c.Handle)&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;true&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;false&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;;&lt;br&gt;&amp;nbsp; }&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;To use this code, simply add the files to&amp;nbsp;your solution, and when creating a form, simply have it inhert from ResizeableForm, instead of System.Windows.Forms.Form.&amp;nbsp; Everything else takes care of itself&lt;/font&gt;&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-5708702534182318196?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/5708702534182318196/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=5708702534182318196" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/5708702534182318196?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/5708702534182318196?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2010/10/resizable-controls-on-net-desktop-form.html" title="Resizable Controls on a .Net desktop form" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;A04FQ3o9eSp7ImA9WxFaEU4.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-8974441663546145704</id><published>2010-07-14T13:33:00.000-07:00</published><updated>2010-07-14T13:38:32.461-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-07-14T13:38:32.461-07:00</app:edited><title>A Thought On Collections</title><content type="html">I love using generic collections in .Net. The main reason for this is because, like most features of .Net, they significantly reduce the work I have to do in architecting an application. Since collection classes always seem to be a theme in my programming, I thought I would jot (or type) down a few thoughts I have on collection classes.&lt;br /&gt;One of the things I often do when creating a collection class is to simply inherit from the List&lt;&gt; class of the type I want to collection-ize.&lt;br /&gt;For instance, suppose I'm writing an application that needs a representation of a library (a book library, not a programmatic library). In the library, there would probably be a class called "Book," that would look something like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;class Book&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public string Title&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Getter and setter for the title&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Author BookAuthor&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Getter and setter for the book author&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void CheckoutBook()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Perform behavior that checks out a book&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void CheckInBook()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Perform behavior that checks in a book&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This library would obviously need a representation for a collection of books. This collection could be manifested in a number of ways. The lazy way would be to simply create member variables within other classes that are of type List&amp;lt;&amp;gt;&lt;book&gt;. I've written enough code that concepts as important as this should be represented by it's own class, so I would simply create a new class that inherits from List&lt;book&gt; like the following:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;class BookCollection : List &amp;lt;Book&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Custom methods and attributes that&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The reason I would do the above is because, with its own class, I have more flexibility moving forward with the use of a collection of books. One of the flexibility features the above offers is the context a collection of books can exist within; for instance, a collection of books could be any of the following:&lt;br /&gt;1. All of the books in the library&lt;br /&gt;2. All of the books a library visitor has checked out&lt;br /&gt;3. All of the overdue books in the library&lt;br /&gt;4. All of the overdue books a library visitor has checked out&lt;br /&gt;5. All of the books a particular author has written&lt;br /&gt;6. All of the books written in a particular month, year, decade, or century&lt;br /&gt;7. All of the books in a particular genre&lt;br /&gt;8. etc&lt;br /&gt;&lt;br /&gt;With the BookCollection class, I have the option of representing any of the above collections as a simple book collection, or by using more inheritance to represent each. For instance:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;class LibraryOverdueBooks : BookCollection&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///particulars of all the library's overdue books&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span size="2" face="courier new"&gt;class LibraryVisitorOverdueBooks : BookCollection&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///particulars of a particular visitor's overdue books&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Because this sort of collection hierarchy is so common in programming, I felt inclined to write about it. Of course, there are lots of ways to represent a collection, especially with the System.Data namespace, or with Linq and its collection of IEnumerable flexibility. As the size of the collection grows, it often makes sense to reevaluate how you are implementing the collection, but in theory, the above is a simple, but effective way to represent collections, and it's pretty common to find code like this in the code I write.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-8974441663546145704?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/8974441663546145704/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=8974441663546145704" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/8974441663546145704?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/8974441663546145704?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2010/07/thought-on-collections.html" title="A Thought On Collections" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DEACQH48eCp7ImA9WxFUGUw.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-1802683361309648278</id><published>2010-06-30T09:49:00.000-07:00</published><updated>2010-06-30T09:52:41.070-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-30T09:52:41.070-07:00</app:edited><title>A thought on Interfaces</title><content type="html">When I first started learning about Interfaces in object oriented programming, I struggled with the name of it.  The word "interface" didn't seem to fit what an Interface actually did.  My interpretation of an interface is what a user interface does; that is, an interface is how an outside entity interacts with something.  It seemed a stretch to describe what Interfaces did as an "interface."  Phrases like "Public Contract" or "Shopping List" seemed more descriptive of the actual intent of an Interface. &lt;br /&gt;&lt;br /&gt;Consider the following code:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;interface IPerson&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;void Talk(); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;void Walk();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Person : IPerson&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void Talk()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Do talk behavior&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;} &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void Walk()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Do walk behavior&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Intuitively, to me, the above code doesn't seem to describe my personal definition of an interface at all. &lt;br /&gt;But, as time went on, I just accepted the terminology, and disregarded my preconceived notion of what an interface was, and ended up finding Interfaces to be quite useful in my programming endeavors. &lt;br /&gt;The reason that I'm revisiting that part in my journey to learn more about object oriented design is because I find myself constantly relearning things, or looking at things in different ways, because the code I'm writing requires I do so. &lt;br /&gt;&lt;br /&gt;I've recently been making an effort to plan my code before I actually write it, to make a clean break from "cowboy" programming.  My planning includes a high-level design, where I first write down what classes will be required to perform the functionality of the features I'm trying to build.  After I've written down the class names, I figure out what public methods I will need to have in each of the classes. &lt;br /&gt;In one of my recent high-level design sessions, I was struggling to build the right incantation of classes/methods, etc, and when I was talking about it with a colleague, I inadvertently referred to my collection of public methods in one of my classes as its "public interface."  And in that moment, the terminology that once seemed like such a misnomer to me became a spot-on description of what an interface does. &lt;br /&gt;&lt;br /&gt;Even though the class I was referencing did not actually implement an interface, its public methods represent its interface that other classes have to it.  And so, looking at what a user interface does, which is provide a means for an end-user to interact with a system, it is quite analagous to what an interface does, and that is, provide a means for outside classes to interact with a class that implements the interface.  Yay interfaces!&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-1802683361309648278?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/1802683361309648278/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=1802683361309648278" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1802683361309648278?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1802683361309648278?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2010/06/thought-on-interfaces.html" title="A thought on Interfaces" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;AkEHSX49fSp7ImA9WxBWGUw.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-7219988146691017298</id><published>2010-01-23T13:58:00.000-08:00</published><updated>2010-02-11T12:03:58.065-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-11T12:03:58.065-08:00</app:edited><title>Creating Deadlock Resistent Apps</title><content type="html">I wrote this for SQLServerCentral.com, and since they do not ask to own the content of their authors, I figured I'd cross-post it on my blog:&lt;br /&gt;&lt;br /&gt;In my experience, one of the biggest problems in building applications and databses is dealing with deadlocks, and other resource contention issues. A dilemma in the process of building a database application is the lack of load that may exist in the development environment; in the real world, there's plenty of built-in load that comes with multiple users using the database. It's not as easy in a simulated environment.&lt;br /&gt;&lt;br /&gt;Sure, there are applications you can build to simulate the sort of activity that may exist in a customer's environment, but since everyone seems to be cash-strapped right now, I thought I would share with you one way I've come up with to test how deadlock and timeout resistent an application is.&lt;br /&gt;&lt;br /&gt;The first step, in this method, is to find out which tables in the database are most commonly used. Assuming that the way the application has been used in your simulated environment is close to the way it is used in production environments, the below query (&gt;SQL 2005) should suffice as a way to determine this:&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DECLARE @YourDatabaseName varchar(125)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SET @YourDatabaseName='LC_OTORL'&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;exec('USE ' + @YourDatabaseName)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SELECT&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DB_NAME(ius.database_id) AS DBName,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;OBJECT_NAME(ius.object_id) AS TableName,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SUM(ius.user_seeks + ius.user_scans + ius.user_lookups) AS TimesAccessed&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;FROM sys.indexes i&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;INNER JOIN sys.dm_db_index_usage_stats ius&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ON ius.object_id = i.object_id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND ius.index_id = i.index_id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ius.database_id = DB_ID()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND DB_NAME(ius.database_id)=@YourDatabaseName&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;GROUP BY&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DB_NAME(ius.database_id),&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;OBJECT_NAME(ius.object_id)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ORDER BY SUM(ius.user_seeks + ius.user_scans + ius.user_lookups) DESC&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If your simulated environment does not provide accurate data about the most-used tables in a production environment, this method may include running the above query against one of your customers' databases.&lt;br /&gt;&lt;br /&gt;In either case, assuming you've got accurate data from the above query about what your most frequently accessed tables are, you can build a simple TSQL-based job that leverages this information to create simulated contention in your database. This job can run every 30 seconds, minute, or several minutes. It depends on what makes sense in your test environment.&lt;br /&gt;&lt;br /&gt;For a methodology one could use to create contention, the easiest way to do so is to disregard the long-held convention that one should abstain from holding a transaction open for too long. In other words, creating an unnecessarily long and ineffective transaction, and running them over-and-over again will wreak havoc on how your application interacts with the database, thus giving you a clear answer as to whether or not your application is deadlock resistent, or at the very least, can recover from a query timeout.&lt;br /&gt;&lt;br /&gt;To create such an evil-looking script, I'll use a cursor to build it. Below is the concoction I've created:&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DECLARE @TableName varchar(125)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DECLARE @NumberOfTimesAccessed int&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DECLARE @YourDatabaseName varchar(125)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DECLARE @ThreshholdNumberOfTimesAccessed int&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DECLARE @UpdateQueryText varchar(255)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DECLARE @TargetedColumnName varchar(255)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/*&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;*&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;* Use this variable to prevent locking on&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;* a table if it hasn't been accessed enough&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;* In this example, locking will not be&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;* simulated if the table has not been&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;* accessed at least 5 times&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;* This is a good candidate variable for parameterization,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;* If you choose to make this script into a stored procedure&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;*&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;*/&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SET @ThreshholdNumberOfTimesAccessed=5&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SET @YourDatabaseName='ADT'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;exec('USE ' + @YourDatabaseName)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;BEGIN TRANSACTION&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DECLARE MostUsedTables_cur CURSOR&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;FOR&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/*&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* This query gets the most recently used tables in the database.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* As time goes on, this script becomes more targeted, as the&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* most frequently used tables in the database get used more and more&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*/&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SELECT&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OBJECT_NAME(ius.object_id) AS TableName,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SUM(ius.user_seeks + ius.user_scans + ius.user_lookups) AS TimesAccessed&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FROM sys.indexes i&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;INNER JOIN sys.dm_db_index_usage_stats ius&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ON ius.object_id = i.object_id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND ius.index_id = i.index_id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ius.database_id = DB_ID()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND DB_NAME(ius.database_id)=@YourDatabaseName&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;GROUP BY&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DB_NAME(ius.database_id),&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OBJECT_NAME(ius.object_id)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ORDER BY SUM(ius.user_seeks + ius.user_scans + ius.user_lookups) DESC&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Open MostUsedTables_cur&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Fetch NEXT FROM MostUsedTables_cur INTO @TableName, @NumberOfTimesAccessed&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;While (@@FETCH_STATUS = 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BEGIN&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IF @NumberOfTimesAccessed &gt;= @ThreshholdNumberOfTimesAccessed&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BEGIN&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/*&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* The below query grabs an arbitrary, non-primary key column&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* in the current table, and assembles an update statement&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* to set the column value equal to itself, so this behavior&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* should be relatively low-risk&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*/&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SET @TargetedColumnName = ( select top 1 co.[name] as ColumnName&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FROM sys.indexes i&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JOIN sys.objects o on i.object_id = o.object_id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JOIN sys.index_columns ic on ic.object_id = i.object_id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND ic.index_id = i.index_id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JOIN sys.columns co on co.object_id = i.object_id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND co.column_id = ic.column_id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE i.is_primary_key = 0&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND o.[type] = 'U'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND o.name = @TableName )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SET @UpdateQueryText = 'UPDATE ' + @TableName + ' SET ' + @TargetedColumnName + ' = ' + @TargetedColumnName&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/*&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* Commenting the below Print line out, but if you wish to debug&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* this script, simply Uncomment the Print line, and&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;*comment the exec() line&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*/&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-- PRINT @UpdateQueryText&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;exec(@UpdateQueryText)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/*&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* The below command waits 5 seconds. You can parameterize this to&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* make it more configurable on-the-fly&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* The effect is that the transaction stays open for&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* a long time&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*/&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WAITFOR DELAY '00:00:05'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;END&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Fetch NEXT FROM MostUsedTables_cur INTO @TableName, @NumberOfTimesAccessed&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;END&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;CLOSE MostUsedTables_cur&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DEALLOCATE MostUsedTables_cur&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;--Commit the transaction here, thus ending the long-held open transaction&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;COMMIT&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above script, when run as a job, and possibly parameterized into a stored procedure, can really wreak havoc on a database, particularly if your database is large. The considerations to keep in mind will revolve around the following:&lt;br /&gt;1. Setting the @ThreshholdNumberOfTimesAccessed value correctly. If your database is highly used, the value of this variable should be high&lt;br /&gt;2. Setting the WAITFOR DELAY value appropriately. Setting it too long will create a very long script, and if this script is being run in the context of a SQL Agent job, the script may not be done running before the next iteration begins&lt;br /&gt;3. Frequency of running the script. If running as a SQL Agent job, make sure to couple the frequency of the job with how the system is being tested.&lt;br /&gt;&lt;br /&gt;If used correctly, the above script can become another tool for appropriately testing your system, and could perhaps be a shell of your stress test methodology. Obviously, this script is not the be-all-end-all for all testing, but I believe that it is a high value tool that can definitely add value in diagnosing how well your application can recover from a bad database environment.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-7219988146691017298?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/7219988146691017298/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=7219988146691017298" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/7219988146691017298?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/7219988146691017298?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2010/01/creating-deadlock-resistent-apps.html" title="Creating Deadlock Resistent Apps" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DEAFQnczcSp7ImA9WxBXEUQ.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-7903054154128847547</id><published>2010-01-22T13:36:00.000-08:00</published><updated>2010-01-22T13:45:13.989-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-01-22T13:45:13.989-08:00</app:edited><title>The Observer Design Pattern</title><content type="html">I've mentioned before that I don't get too wrapped up in design patterns because of the tendency to overuse them, or to use the wrong one, resulting in overly-complicated code.  There are, however, times when a design pattern is the exact solution for a given problem.&lt;br /&gt;&lt;br /&gt;As an academic experiment, I recently looked into the "Observer" design pattern, which is probably one of the coolest of the design patterns (IMHO).  The Observer design pattern is also called a "Publish-Subscribe" design pattern, because it creates a platform by which some entity can maintain a list of publishers and subscribers, and map them together.  Then publishers and subscribers can send and receive messages, and have some level of confidence that the messages get to where they need to go.&lt;br /&gt;&lt;br /&gt;The implementation of the Observer pattern I used was such that there was a managing "Engine" that kept track of all the subscriptions, and when it was notified of an event, it would notify all subscribers of that particular event.  It was then the responsibility of the subscriber to either accept or disregard a message, based on the subject of the message.&lt;br /&gt;&lt;br /&gt;The Observer pattern is not needed by all conceivable applications; in fact, most applications would not need it at all.  So, one of the things I wanted to do was to think of a fun example of how one could use the Observer pattern.  My solution:  a Television analogy.  A television has commercials.  A television has people who watch it.  Some of those people are shopping for the products that the commercials contain.  For instance, some people are in the market to purchase furniture.  Some people are in the market to purchase a car; therefore, the television is a medium for makers of those products to put commercials in them for watchers to watch.  If a viewer of the television is not currently shopping for it, then they simply disregard the commercial.&lt;br /&gt;&lt;br /&gt;One of the downfalls of the Observer pattern is that, because it is the role of the subscriber to either pay attention to or disregard a message, excess traffic gets generated.  In my implementation of the Observer pattern, it is scalable to deal with this issue fairly easily, by switching this responsibility from the potential subscriber to the engine that manages the list.&lt;br /&gt;&lt;br /&gt;Below are the list of classes I created, along with their definition:&lt;br /&gt;&lt;br /&gt;Subject - An abstract class that other classes will implement.  A subject, in this example, is the TV commercial subject.  For instance, a Car Commercial would be of type CarCommercial, which inherits from Subject.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public abstract class Subject&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;protected List&amp;#60;Observer&amp;#62; _listOfObservers;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public string SubjectName;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;protected SubscriptionEngine _engine;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Subject(SubscriptionEngine connectingEngine)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine = connectingEngine;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public abstract void Attach(Observer o);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public abstract void Detach(Observer o);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public virtual void Notify()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach (Observer obs in _listOfObservers)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine.Notify(this);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public abstract string State&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;get;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;set;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;CarCommercial - Concrete class that inherits from Subject, and represents one of the Subjects that the engine will know about.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class CarCommercial : Subject&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private string _state;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public CarCommercial()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;: base(null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.SubjectName = "Car Commercial";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public CarCommercial(SubscriptionEngine engine) : base(engine)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.SubjectName = "Car Commercial";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public override string State&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;get { return _state; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;set { _state = "Hello"; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public override void Attach(Observer o)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine.Register(this, o);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public override void Detach(Observer o)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine.Unregister(this, o);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;FurnitureCommercial - Concrete class that inherits from Subject, and represents one of the Subjects that the engine will know about.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class FurnitureCommercial : Subject&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private string _state;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public FurnitureCommercial()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;: base(null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.SubjectName = "Furniture Commercial";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public FurnitureCommercial(SubscriptionEngine engine)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;: base(engine)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.SubjectName = "Furniture Commercial";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public override string State&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;get { return _state; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;set { _state = value; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public override void Attach(Observer o)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine.Register(this, o);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public override void Detach(Observer o)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine.Unregister(this, o);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Observer - An abstract class that represents a television viewer, in this example.  An observer, in the code created, can be either a CarShopper or a FurnitureShopper&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public abstract class Observer&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;protected SubscriptionEngine _engine;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public string Name;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public List&amp;#60;Subject&amp;#62; ListOfSubjectsSubscribedTo;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Observer(SubscriptionEngine engine)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ListOfSubjectsSubscribedTo = new List&amp;#60;Subject&amp;#62;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine = engine;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public abstract bool Update(Subject s);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;CarShopper - Concrete class that implements Observer.  Represents a TV Viewer who cares about CarCommercials&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class CarShopper : Observer&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public CarShopper(SubscriptionEngine e) : base(e)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ListOfSubjectsSubscribedTo.Add(new CarCommercial());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.Name = "Car Shopper";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine.Register(ListOfSubjectsSubscribedTo[0], this);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public override bool Update(Subject s)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach (Subject subject in this.ListOfSubjectsSubscribedTo)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (subject.SubjectName == s.SubjectName)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;FurnitureShopper - Concrete class that implements Observer.  Represents a TV Viewer who cares about FurnitureCommercials.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class FurnitureShopper : Observer&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public FurnitureShopper(SubscriptionEngine e) : base(e)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.Name = "Furniture Shopper";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ListOfSubjectsSubscribedTo.Add(new FurnitureCommercial());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine.Register(ListOfSubjectsSubscribedTo[0], this);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public override bool Update(Subject s)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach (Subject subject in this.ListOfSubjectsSubscribedTo)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (subject.SubjectName == s.SubjectName)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Subscription - Class (could have easily been a Struct) that represents a mapping between an Observer (TV Watcher) and Subject (TV Commercial)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Subscription&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private Subject _subject;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private Observer _observer;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Subscription(Subject s, Observer o)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_subject = s;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_observer = o;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Subject Subject_&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;get { return _subject; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Observer Observer_&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;get { return _observer; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;SubscriptionEngine - The engine that maintains a list of Subscriptions, and handles the actual notification, in the event that a Subject is sent.  The way this is implemented is in it's public method Notify().  The parameter in Notify is a Subject.  So therefore, the SubscriptionEngine would go through its list of subscribers, and send to all Observers.  It would be the Observer's responsibility of whether or not they care.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class SubscriptionEngine&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;#60;Subscription&amp;#62; _listOfSubscriptions;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public SubscriptionEngine()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_listOfSubscriptions = new List&amp;#60;Subscription&amp;#62;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public bool Contains(Subscription item)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach (Subscription subscription in _listOfSubscriptions)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (subscription.Observer_.Name == item.Observer_.Name&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;&amp; subscription.Subject_.SubjectName == item.Subject_.SubjectName)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void Register(Subject s, Observer o)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Subscription item = new Subscription(s, o);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (!this.Contains(item))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_listOfSubscriptions.Add(new Subscription(s, o));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void Unregister(Subject s, Observer o)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (int i = 0; i &amp;#60; _listOfSubscriptions.Count; i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (_listOfSubscriptions[i].Observer_ == o &amp;&amp; _listOfSubscriptions[i].Subject_ == s)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_listOfSubscriptions.RemoveAt(i);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void Notify(Subject s)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach (Subscription subscription in _listOfSubscriptions)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (subscription.Observer_.Update(s))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MessageBox.Show(s.SubjectName + " has sent a message to " + subscription.Observer_.Name);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Television - A simple Form, which acts as an intermediary between the SubscriptionEngine and the Subjects and Observers.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public partial class Television : Form&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SubscriptionEngine _engine;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Television()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine = new SubscriptionEngine();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;InitializeComponent();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private void onTelevisionLoad(object sender, EventArgs e)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.showCommercials();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private void showCommercials()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FurnitureCommercial furnitureCommercial = new FurnitureCommercial(_engine);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CarCommercial carCommercial = new CarCommercial(_engine);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FurnitureShopper furnitureShopper = new FurnitureShopper(_engine);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CarShopper carShopper = new CarShopper(_engine);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;furnitureCommercial.Attach(furnitureShopper);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;carCommercial.Attach(carShopper);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine.Notify(furnitureCommercial);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_engine.Notify(carCommercial);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The way I see the Observer pattern working is a little bit different that it was laid out by the Gang Of Four, or other authors.  In the GOF's implementation, the Subject has more "smarts" than I think it needs.  The way I see this pattern, the Subject ought to be rather "dumb," and the responsibility for doing stuff should lie with the Observer and the SubscriptionEngine.&lt;br /&gt;&lt;br /&gt;For more info about the Observer pattern, check out http://dofactory.com/Patterns/PatternObserver.aspx&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-7903054154128847547?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/7903054154128847547/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=7903054154128847547" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/7903054154128847547?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/7903054154128847547?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2010/01/observer-design-pattern.html" title="The Observer Design Pattern" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DEYFQXcyeip7ImA9WxBXEUQ.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-1193602074480551684</id><published>2010-01-22T13:30:00.000-08:00</published><updated>2010-01-22T13:35:10.992-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-01-22T13:35:10.992-08:00</app:edited><title>Inheriting from .Net base controls</title><content type="html">.Net Controls (and their Win32 ancestors), such as TextBox, ComboBox, DataGridView, etc, were built to be decoupled from any one application; they have to be.&amp;nbsp;&amp;nbsp;These controls need to be used by tens or hundreds of thousands of developers building any number of types of applications.&lt;br /&gt;&lt;br /&gt;The trouble with generic, decoupled code (and APIs) is that it is so loosely coupled from the rest of the application, that it takes finangling to get the loosely coupled code to work with the rest of your tightly coupled code.&lt;br /&gt;&lt;br /&gt;This is a case study in inheritance, and my guess is that most software engineers use inheritance to build custom controls (or build their own controls, or purchase them from external vendors, such as SyncFusion).&amp;nbsp;&amp;nbsp;For the context of this blog, I'll simply address using inheritance to extend base .Net classes for your own purposes.&lt;br /&gt;&lt;br /&gt;When building a class (in this case, a Control), there are 3 strategies one can use to look at the class:&lt;br /&gt;1.&amp;nbsp;&amp;nbsp;Set all attributes, and interact with class behavior from outside of the class&lt;br /&gt;2.&amp;nbsp;&amp;nbsp;Set all attributes, and interact with class behavior from inside of the class&lt;br /&gt;3.&amp;nbsp;&amp;nbsp;Do some combination of 1 and 2.&lt;br /&gt;&lt;br /&gt;Most code falls into category #3, as is the appropriate thing to do.&amp;nbsp;&amp;nbsp;In the case of Controls (and many other code constructs, as well), the difference between tightly coupled code and loosely coupled code is what percentage of behavior is set from outside of the class, versus what percentage is set from within the class.&amp;nbsp;&amp;nbsp;Another component of the tightly versus loosely coupled code is how much the code knows about the data structures and code that you've built into your application.&lt;br /&gt;&lt;br /&gt;In the case of .Net controls, the controls don't, by default, know anything at all about the classes in your application; that is the trade-off you make with extending base control behavior:&amp;nbsp;&amp;nbsp;the custom controls know about your code, but those controls are probably going to be meaningless outside of your code base because they have become so tightly coupled to your architecture.&lt;br /&gt;&lt;br /&gt;So, on to an example.&amp;nbsp;&amp;nbsp;Consider a DataGridView control.&amp;nbsp;&amp;nbsp;Typically, if using the base .Net DataGridView, you instantiated it, put it on a control, and set the data source.&amp;nbsp;&amp;nbsp;The setting of the data source is the "doing work from outside of the class."&amp;nbsp;&amp;nbsp;Suppose you want to transfer that responsibility to the class.&amp;nbsp;&amp;nbsp;You could extend the class as follows:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class CustomDataGridView : System.Windows.Forms.DataGridView&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; public CustomDataGridView(DataTable dataSource)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ///Set the base data source to the passed&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //DataTable parameter&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.DataSource = dataSource;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The above is a simple example, but suppose you wanted multiple controls to behave in a similar way.&amp;nbsp;&amp;nbsp;Since all controls have different behavior, and since a class (in .Net) cannot inherit from more than one class (outside of an inheritance chain), the solution is to create an Interface.&amp;nbsp;&amp;nbsp;Consider the following example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Interface ISaveableControl&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;void Save();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Then I could have a couple controls implement it:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class CustomTextBox : System.Windows.Forms.TextBox, ISaveableControl&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;public CustomTextBox(string defaultValue)&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.Text = defaultValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;public void Save()&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ///Go to the database and update the associated record with the associated value&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class CustomComboBox : System.Windows.Forms.ComboBox, ISaveableControl&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;public CustomTextBox(string defaultValue)&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.Items.Add(defaultValue);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.Text = defaultValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;public void Save()&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ///Go to the database and update the associated record with the associated value&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If I were to add the above controls to a basic System.Windows.Form, I could do something after a button was clicked (or some other event was triggered):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;foreach(Control ctrl in this.Controls)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;if(ctrl is ISaveableControl)&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((ISaveableControl)ctrl).Save();&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And Voila, extending base controls to be more tightly coupled with your app.&amp;nbsp;&amp;nbsp;There are decisions that need to be made to determine if this is a strategy your software should contain, because even though there is a lot of value in tightly coupled code (especially controls), there is also some value in keeping things loosely coupled.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-1193602074480551684?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/1193602074480551684/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=1193602074480551684" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1193602074480551684?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1193602074480551684?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2010/01/inheriting-from-net-base-controls.html" title="Inheriting from .Net base controls" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkUHSHg7eCp7ImA9WxBQFEU.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-4335811258055696215</id><published>2010-01-14T06:42:00.001-08:00</published><updated>2010-01-14T06:43:59.600-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-01-14T06:43:59.600-08:00</app:edited><title>Collection Classes</title><content type="html">In his book Prefactoring, Ken Pugh talks about collections of objects.  Pugh's assertion is that:  "If it has collection operations, make it a collection."&lt;br /&gt;&lt;br /&gt;The above statement has been one of the most helpful axioms I have found in building object oriented applications.  Before I was armed with the tool of the "Collection" class, I used some bastardization of arrays or lists, and did all their operations within the parent class.  This made for pretty sloppy code.  For instance, consider the following:&lt;br /&gt;&lt;br /&gt;class ParentClass&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private List&lt;string&gt; _childClassList;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public ParentClass()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Create the new list object in constructor&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_childClassList = new List&lt;string&gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/*******************************&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; * Here are all the behaviors &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; * of the ParentClass object&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *******************************/&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public int GetChildClassCount()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return _childClassList.Count;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void AddToChildList(string val)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_childClassList.Add(val);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public string GetLongestStringInChildClassList()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string longestString = String.Empty;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach(string member in _childClassList)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(member.Length &gt; longestString.Length)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;longestString = member;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return longestString.ToUpper();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;The above code works, but the methods GetChildClassCount(), AddToChildList(), and GetLongestStringInChildClassList() don't belong in ParentClass.  They belong in a separate class, and deserve their own separate implementations.&lt;br /&gt;&lt;br /&gt;Before going further, bear in mind that as .Net progresses, many of these functionalities are becoming available in the "primitive types" that .Net offers.  For instance, as of .Net 3.5, the List&lt;&gt; variable has a GetAverage() function that does this for you...it also has a lot of those other features;  however, I believe that adding a layer of abstraction can be a powerful tool for both consistency of code in your application, and for coding standards, if you're working with multiple developers.&lt;br /&gt;&lt;br /&gt;So, refactoring the above code, I would probably do something like this:&lt;br /&gt;&lt;br /&gt;class ParentClass&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Notice here I substitute the List for a&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///ChildClassCollection&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private ChildClassCollection _childClassList;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public ParentClass()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Create the new collection object in constructor&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_childClassList = new ChildClassCollection ();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/*******************************&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; * Here are all the behaviors &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; * of the ParentClass object&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *******************************/&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void DoStuff(string input)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(  input.ToUpper() == _childClassList.GetLongestStringInChildClassList() )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ///Do stuff here&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class ChildClassCollection&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private List&lt;string&gt; _childClassList;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public int GetChildClassCount()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return _childClassList.Count;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void AddToChildList(string val)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_childClassList.Add(val);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public string GetLongestStringInChildClassList()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string longestString = String.Empty;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach(string member in _childClassList)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(member.Length &gt; longestString.Length)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;longestString = member;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return longestString.ToUpper();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;In my mind, the above example of factoring out the first code example's behavior into 2 classes really looks better, and gets at something that I think is really important in building object oriented systems:  class relationships.&lt;br /&gt;&lt;br /&gt;Understanding relationships between different "things" in your code, and knowing how to represent them in a solid class relationship is key to building high quality code.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-4335811258055696215?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/4335811258055696215/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=4335811258055696215" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/4335811258055696215?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/4335811258055696215?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2010/01/collection-classes.html" title="Collection Classes" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CEUMSHkycCp7ImA9WxBREUw.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-4207680034960515389</id><published>2009-12-29T10:39:00.000-08:00</published><updated>2009-12-29T10:44:49.798-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-29T10:44:49.798-08:00</app:edited><title>Factory Design Pattern</title><content type="html">I try not to get too wrapped up in design patterns, because one can really overuse them, or use them in inappropriate places, if one is not careful; that's not to say that design patterns don't have their place, because I think design patterns are common ways programmers have found to "best practice" object oriented programming.&lt;br /&gt;&lt;br /&gt;But even though I try not to over-emphasize design patterns, there are several design patterns that I find myself using over-and-over again.  One such pattern is the factory pattern.&lt;br /&gt;&lt;br /&gt;Factory patterns are often used with inheritance or interfaces, because one of the best uses of a factory design pattern is for figuring out what the best version of object is to instantiate, based on some input.&lt;br /&gt;&lt;br /&gt;A factory pattern is basically an object that has some method that is a big if-statement (or switch statement, if one prefers).  For instance, suppose your configuration file had a configuration for what type of data access you want to use (MSSQL, XML, Oracle, mySQL, etc).&lt;br /&gt;&lt;br /&gt;Here's a simple example.  The classes (along with accompanying interface) would look something like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;interface IAccessLayer&lt;br /&gt;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; string GetQueryResultsAsString(string query);&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;DataTable GetQueryResultsAsDataTable(string query);&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;void ExecuteModificationQuery(string query);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Implementing classes might look something like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class MSSQLAccessLayer : IAccessLayer&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Implement public contract from IAccessLayer&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class mySQLAccessLayer : IAccessLayer&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Implement public contract from IAccessLayer&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class XMLAccessLayer : IAccessLayer&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Implement public contract from IAccessLayer&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class OracleAccessLayer : IAccessLayer&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Implement public contract from IAccessLayer&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;If the configuration file had a finite set of allowable strings for the access layer that were as follows:&lt;br /&gt;&lt;br /&gt;MSSQL&lt;br /&gt;ORACLE&lt;br /&gt;XML&lt;br /&gt;MYSQL&lt;br /&gt;&lt;br /&gt;Then, the factory method would look something like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public IAccessLayer GetAccessLayer(string dataAccessMedium)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;dataAccessMedium = dataAccessMedium.ToUpper();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(dataAccessMedium == "MSSQL")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return new MSSQLAccessLayer();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else if(dataAccessMedium == "ORACLE")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return new OracleAccessLayer();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else if(dataAccessMedium == "MYSQL")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return new mySQLAccessLayer();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else if(dataAccessMedium == "XML")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return new XMLAccessLayer();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw new Exception("Error.  " + dataAccessMedium + " is not a valid data access method");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The above is one application of the factory pattern.  There are lots of variants, and lots of applications, which is why I felt so inclined to write about them.&lt;br /&gt;&lt;br /&gt;By the way, check out my new project at SourceForge: TestAutomation. It helps to automate software testing in Windows environments. https://sourceforge.net/projects/testautomation/ .  I'm always looking for people to join the project.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-4207680034960515389?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/4207680034960515389/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=4207680034960515389" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/4207680034960515389?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/4207680034960515389?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2009/12/factory-design-pattern.html" title="Factory Design Pattern" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;Ck4DQHwzfyp7ImA9WxBSFE8.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-1509039656410688206</id><published>2009-12-21T10:42:00.001-08:00</published><updated>2009-12-21T10:42:51.287-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-21T10:42:51.287-08:00</app:edited><title>When to use Inheritance versus Interfaces</title><content type="html">I tend to lean towards using interfaces, as opposed to inheritance.  The reason for this is multi-fold:&lt;br /&gt;&lt;br /&gt;1.  I can implement multiple interfaces on a single class in C#.  The only way I can implement multiple classes in C# is to have a big inheritance chain, which tends to get confusing, when going back to review code weeks or months later.&lt;br /&gt;2.  With interfaces, there's no ambiguity in who implements what behavior.  There may be methods one overrides in C#, and that can get confusing when implementing a child class (ie Does the behavior this class uses match the parent class, or does it have different behavior?).  With interfaces, I must define the behavior in each implementation.&lt;br /&gt;3.  In my world, and the types of applications I build, I don't see behavior being so expandable as to support a lot of inheritance; obviously, there are times when it makes complete sense to use inheritance, but for the most part, objects aren't that related.&lt;br /&gt;&lt;br /&gt;That being said, there are times when I choose to use an interface, and find that it was an inappropriate choice for the solution I was trying to build.  The reason it becomes inappropriate is because, when I first chose to use an interface, I didn't consider some of the granularity of the classes, and I start realizing that I'm re-doing a lot of work that I wouldn't have to do if I had chosen inheritance.&lt;br /&gt;&lt;br /&gt;There are canned solutions that make solving these problems easier, such as abstract classes; however, in this example, I'll refrain from considering the abstract class as a possible solution.&lt;br /&gt;&lt;br /&gt;Instead, I'll try to iterate some reasons to choose an interface, as opposed to inheritance, and vice-versa:&lt;br /&gt;&lt;br /&gt;Reasons to choose an interface&lt;br /&gt;1.  Uncommon behavior, but a common "fingerprint":  If you envision a set of classes having similar public methods, but uncommon private members, constructors, and behaviors, then an interface is probably the right choice&lt;br /&gt;2.  Defining multiple ways to arrive at a solution:  The data access layer is a good example of this.  An interface is a great way to have multiple options for database storage, and having that be a configurable piece of the software&lt;br /&gt;3.  Multiple developers working on common tasks:  Interfaces were designed to solve problems that arise when multiple people are working independently of one another.  Defining an interface (often referred to as a public contract) is a way to keep people on the same page.&lt;br /&gt;&lt;br /&gt;Reasons to choose inheritance&lt;br /&gt;1.  Similar-looking classes:  If classes begin to look very similar, especially at the private-member level, refactoring to make those private members protected, and implementing inheritance is probably a good decision&lt;br /&gt;2.  Common Behavior:  Let's face it:  interfaces exist because common behavior does occur between classes.  "Inventing" common behavior, and forcing inheritance onto multiple classes that do not have a real relationship is not a good thing, though.&lt;br /&gt;&lt;br /&gt;A lot of the decision to choose inheritance or interfaces comes down to style and preference; however, these are a couple "fun facts" I've gleamed in the process of writing code.&lt;br /&gt;&lt;br /&gt;By the way, check out my new project at SourceForge:  TestAutomation.  It helps to automate software testing in Windows environments.  https://sourceforge.net/projects/testautomation/&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-1509039656410688206?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/1509039656410688206/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=1509039656410688206" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1509039656410688206?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/1509039656410688206?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2009/12/when-to-use-inheritance-versus.html" title="When to use Inheritance versus Interfaces" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DUUGRXc-eyp7ImA9WxNSGEw.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-7074528845340877229</id><published>2009-09-01T08:00:00.001-07:00</published><updated>2009-09-01T08:00:24.953-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-01T08:00:24.953-07:00</app:edited><title>Decoupling in Relational Databases</title><content type="html">Before I understood relational databases, the tables I put in my databases, and the resulting architectural output was bad.  And sloppy.  I would put columns into tables that had 1-to-1 relationships with their foreign key counterpart, and I would duplicate those columns, to generate the effect of a bridge table.&lt;br /&gt;&lt;br /&gt;For instance, if I had a database where I had a table called USER, and another table called GROUP, and the relationship between USER and GROUP was such that a USER could belong to 1 or more groups, I may have created the USER table like the one below&lt;br /&gt;&lt;br /&gt;USER&lt;br /&gt;-----&lt;br /&gt;id - int (auto increment)&lt;br /&gt;firstname - varchar(50)&lt;br /&gt;lastname  - varchar(50)&lt;br /&gt;fk_group1 - int (relates to GROUP table)&lt;br /&gt;fk_group2 - int (relates to GROUP table)&lt;br /&gt;fk_group3 - int (relates to GROUP table)&lt;br /&gt;&lt;br /&gt;As you can see in the above table, I may have made a bunch of columns that have a foreign key relationship with the GROUP table.&lt;br /&gt;&lt;br /&gt;That's all fine and dandy, except in the event that a USER belongs to 1000 GROUPS.  Every time a USER exceeded the number of groups available in the USER table, I would have to go back and add more columns.&lt;br /&gt;&lt;br /&gt;The other implication of not understanding some basic tenets of relational databases is that the application code to represent the relationship between USER and GROUP was just as sloppy (or even worse!) than the database design.&lt;br /&gt;&lt;br /&gt;It was not until I started exploring concepts in decoupling that I realized the error of my ways.  One of the best ways to implement decoupling in relational databases is with bridge tables.  A bridge table is a table whose relationship with other tables creates a Many-to-Many relationship.  In a many-to-many relationship paradigm, the two tables are decoupled, because they don't have a direct relationship with one another.&lt;br /&gt;&lt;br /&gt;For instance, to implement a bridge table that acts as a go-between for USER and GROUP, I would modify the USER table to look as follows:&lt;br /&gt;&lt;br /&gt;USER&lt;br /&gt;-----&lt;br /&gt;id - int (auto increment)&lt;br /&gt;firstname - varchar(50)&lt;br /&gt;lastname  - varchar(50)&lt;br /&gt;&lt;br /&gt;And, just for completeness sake, I'll show what the GROUP table looks like&lt;br /&gt;&lt;br /&gt;GROUP&lt;br /&gt;-----&lt;br /&gt;id - int (auto increment)&lt;br /&gt;groupname - varchar(50)&lt;br /&gt;&lt;br /&gt;The bridge table to allow for as many user memberships as I wanted, I would create a table called USER_GROUP (you can name bridge tables whatever you want, I just like to have the name format be something like TABLEa_TABLEb).&lt;br /&gt;&lt;br /&gt;The USER_GROUP table would look as follows:&lt;br /&gt;&lt;br /&gt;USER_GROUP&lt;br /&gt;-----------&lt;br /&gt;id - int (auto increment)&lt;br /&gt;fk_user - int (relationship with USER)&lt;br /&gt;fk_group - int (relationship with GROUP)&lt;br /&gt;&lt;br /&gt;Then, if I wanted to build a SQL query to show all groups, and their associated users, it would look something like this:&lt;br /&gt;&lt;br /&gt;SELECT GROUP.groupname, USER.firstname, USER.lastname&lt;br /&gt;FROM GROUP&lt;br /&gt;INNER JOIN USER_GROUP ON GROUP.id=USER_GROUP.fk_group&lt;br /&gt;INNER JOIN USER ON USER_GROUP.fk_user=USER.id&lt;br /&gt;ORDER BY GROUP.groupname, USER.lastname, USER.firstname&lt;br /&gt;&lt;br /&gt;This decoupling also makes it easier to write code for, as well, because each  of the concepts (USER and GROUP) can exist independently of one another, yet the logical relationship still exists (GROUP "has a" user).  In the composition relationship between GROUP and USER, I can demonstrate that with either a LIST variable, or by implementing an IEnumerable interface.&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-7074528845340877229?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/7074528845340877229/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=7074528845340877229" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/7074528845340877229?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/7074528845340877229?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2009/09/decoupling-in-relational-databases.html" title="Decoupling in Relational Databases" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DUQERnk_eSp7ImA9WxNSFUs.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-3794433975807138630</id><published>2009-08-29T10:33:00.000-07:00</published><updated>2009-08-29T10:35:07.741-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-29T10:35:07.741-07:00</app:edited><title>Many-To-One Composition Relationships</title><content type="html">Many-to-one relationships are very common, especially in applications involving databases.  The reason for this is that the many-to-one paradigm is a common scenario in the real world, and programming languages, databases, and other technical constructs attempt to represent the real world.&lt;br /&gt;&lt;br /&gt;Consider the relationship between a car and its tires.  Composition solves the problem of how a car "has" tires.  It would go something like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;///Here is a generic implementation of a Car class&lt;br /&gt;public class Car&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private List&lt;Tire&gt; _allFourTires;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Car()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_allFourTires = new List&lt;Tire&gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;///And here is the class "Tire", which composes Car&lt;br /&gt;public class Tire&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private string _model;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private string _type;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private int _width;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private double _aspectRatio;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///You get the idea&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The List&lt;&gt; type is a great way to solve the problem of many-to-one composition, but there is another way in C#, and it is called "Collections."&lt;br /&gt;&lt;br /&gt;Collections are implemented in a lot of the base .Net objects, including DataSets and DataTables.&lt;br /&gt;&lt;br /&gt;For instance, consider the below code:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;///Assume I have a DataSet called myDataSet, and it has&lt;br /&gt;///a bunch of datatables in it&lt;br /&gt;foreach(DataTable table in myDataSet.Tables)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach(DataRow row in table.Rows)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Do some stuff with the table&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The reason DataSet has a .Tables and a DataTable has .Rows is because they implement the IEnumerable .Net interface.&lt;br /&gt;&lt;br /&gt;As mentioned above, an alternative to implementing the IEnumerable is to have a DataSet have a List&lt;&gt; variable with DataTables, and to have a DataTable have a List&lt;&gt; variable of DataRows, but over time, the IEnumerable is easier to manage.&lt;br /&gt;&lt;br /&gt;Interfaces are a public contract between classes.  They're basically a grocery list, and if you plan on using them, you must have everything on the grocery list.  Luckily, IEnumerable has only one "item" on the list.  That item is a public method called GetEnumerator().&lt;br /&gt;&lt;br /&gt;When implementing a paradigm such as this, the effect you're really having is to create a bridge between the container class (Car), and the containee class (Tire).  So, in between Car and Tire goes the IEnumerable class.  In the below example, we'll call it TireCollection&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/// The container class&lt;br /&gt;public class Car&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///A global variable of the bridge (IEnumerable) class&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private TireCollection _tireCollection;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// Constructor - makes a tire collection based&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// on the model&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Car(string model)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; makeTireCollection(model);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// This is just sort of a factory method - I could&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// get more abstract, and create a TireCreatorFactory&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// class, but I think this is sufficient for this example&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private void makeTireCollection(string model)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (model == "Ford")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  makeFordTireCollection();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// Makes the tire collection for Ford cars&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private void makeFordTireCollection()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; List&lt;Tire&gt; tireList = new List&lt;Tire&gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tireList.Add(new Tire("Bridgestone", "P", 215, 75));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tireList.Add(new Tire("Bridgestone", "P", 215, 75));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tireList.Add(new Tire("Bridgestone", "P", 215, 75));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tireList.Add(new Tire("Bridgestone", "P", 215, 75));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _tireCollection = new TireCollection(tireList);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// This is an unnecessary attribute, but if I didn't have this&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// I would have to do:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// foreach(Tire in car)...&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// With this attribute, I can do&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// foreach(Tire in car.Tires)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// I like that better&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public TireCollection Tires&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get { return _tireCollection; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Notice in the above, I have an attribute called Tires.  As mentioned in the comments, this is an unnecessary attribute, but I think it looks cleaner when we go to implement the Car class.&lt;br /&gt;&lt;br /&gt;Below is the IEnumerable class TireCollection.  Note that it has a method called GetEnumerator():&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/// This essentially acts as a bridge between &lt;br /&gt;/// the Car class and the tire class&lt;br /&gt;public class TireCollection  : IEnumerable&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///We do have a List&lt;&gt; variable in the TireCollection.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///So, in effect, we don't get away from having a List&lt;&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///variable...the collection simply buys us a "decoupling"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///effect&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&lt;Tire&gt; _tireList;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public TireCollection(List&lt;Tire&gt; tireList)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _tireList = tireList;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public IEnumerator GetEnumerator()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (_tireList as IEnumerable).GetEnumerator();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then, the Tire class looks pretty typical&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/// This is the Tire class - there are many tires on a car&lt;br /&gt;/// Well...four, anyway&lt;br /&gt;public class Tire&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private string _model;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private string _type;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private int _width;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private double _aspectRatio;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// Constructor&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Tire(string model, string type, int width, double aspectRatio)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _model = model;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _type = type;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _width = width;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _aspectRatio = aspectRatio;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/// This is simply a public method &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public string GetTireProperties()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return "Tire is of model " + _model + " with type " + _type;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now comes the question of how to implement this code.  This part is the simple part:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Car car = new Car("Ford");&lt;br /&gt;foreach (Tire tire in car.Tires)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Output tire.GetTireProperties()&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;It is a few more steps to implement an IEnumerable interface, but it's worth it for those solutions that wish to have looser coupling between the composition relationship.&lt;br /&gt;&lt;br /&gt;In the comments in the makeFordTireCollection() method in Car, I mention that I could have just as easily implemented a TireCreatorFactory class that assembles a Tire collection for me.  The effect of doing so would have provided a higher degree of decoupling between Car and Tire, which is probably a more optimal situation, since loosely coupled class interactions are highly valued in object oriented design.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://oopstruggles.blogspot.com/" rel="tag"&gt;CodeProject&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-3794433975807138630?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/3794433975807138630/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=3794433975807138630" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/3794433975807138630?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/3794433975807138630?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2009/08/many-to-one-composition-relationships.html" title="Many-To-One Composition Relationships" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DUUNRHczcCp7ImA9WxNSFUs.&quot;"><id>tag:blogger.com,1999:blog-7458647044495088329.post-7413510492096090159</id><published>2009-08-21T12:38:00.001-07:00</published><updated>2009-08-29T10:34:55.988-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-29T10:34:55.988-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Inheritance and Composition CodeProject" /><title>Is-A Versus Has-A</title><content type="html">Is-A versus Has-A&lt;br /&gt;&lt;br /&gt;Believe it or not, the concept of whether a class "Is a [something]" or "Has a [something]" comes up quite a bit in object oriented design.  For instance, consider the questions "Is a transmission a car?" and "Does a car have a transmission?"&lt;br /&gt;&lt;br /&gt;Clearly, the second question is the only of the two that one would answer in the affirmative.  A transmission is not a car, but a car does have a transmission.  This is a classic "Is-A/Has-A" example.  And sometimes the answer to the questions are not so clear cut, particularly if the relationship is more obscure, like the relationship between a wall and sheetrock.  For example, sheetrock, if hanged, could perform the duties of a wall, and a wall can contain sheetrock.  The implementation of the solution to that question could probably be solved with either implementing a "Is-A" paradigm, or a "Has-A" paradigm.&lt;br /&gt;&lt;br /&gt;So, I haven't described how "Is-A" and "Has-A" are implemented in object oriented design.  Well the answer to that is really quite simple:&lt;br /&gt;&lt;br /&gt;"Is-A" is implemented via INHERITANCE&lt;br /&gt;"Has-A" is implemented via COMPOSITION&lt;br /&gt;&lt;br /&gt;INHERITANCE&lt;br /&gt;&lt;br /&gt;Inheritance is a good solution when some component would be logically defined, more abstractly, as another object.&lt;br /&gt;&lt;br /&gt;For instance, think of fruit.  Fruit is really more of an abstract concept than it is an implementation.  Fruit can either have seeds (raspberries), or not have seeds (bananas).  It can also have rind (bananas), or not have rind (raspberries).  These attributes (seeds, rinds) are values that the more abstract "fruit" has.&lt;br /&gt;&lt;br /&gt;The implementation of this code could be as follows:&lt;br /&gt;&lt;br /&gt;///Below is the "Fruit" class&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Fruit&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private bool _hasSeeds;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private bool _hasRind;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Public attribute for whether or not the fruit has seeds&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public bool HasSeeds&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;get { return _hasSeeds; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;set { _hasSeeds = value; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Public attribute for whether the fruit has a rind&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public bool HasRind&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;get { return _hasRind; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;set { _hasRind = value; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;///Public implementation of Banana&lt;br /&gt;///Inherits from Fruit&lt;br /&gt;public class Banana : Fruit&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Because the nature of a banana lends itself&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///to knowing whether or not it has seeds and rind&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///I will set those attributes in the constructor&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///There is no reason that the class who calls &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Banana should have to know that a banana does not&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///have seeds and does have a rind.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Banana()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.HasSeeds = false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.HasRind = true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void Peel()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Do some kind of action to peel the banana&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Since Peel() is not common among all fruits&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Peel() does not have to be in the Fruit class&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///However, there may be reasons to put it in there&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///anyway.  It depends on how similar the behavior&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///is for all fruits' Peel() behavior&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;///Public implementation of Banana&lt;br /&gt;///Inherits from Fruit&lt;br /&gt;public class Raspberry : Fruit&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Like in Banana, raspberries HasSeeds and HasRind&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///attributes are set&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Raspberry()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.HasSeeds = true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.HasRind = false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;As you can see, this example lends itself very well to the "Is-A" paradigm, because clearly, Bananas and Raspberries are "Fruit."  Also note that in the above, I did not put the Peel() method in the Fruit class, because it didn't seem to belong there.  If I have an issue where many fruits need to have the Peel() behavior defined in them, I could do one of the following:&lt;br /&gt;&lt;br /&gt;1.  Create an interface, and have any peelable fruit invoke it:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public interface IPeelableFruit&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Notice how there is no implementation in the interface's Peel()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///This is because an interface has no implementation&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///It is simply a public contract between anyone implementing it&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;void Peel();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Making an interface like the above would make a lot of sense if each fruit's implementation of Peel() were significantly different from one another.&lt;br /&gt;&lt;br /&gt;With an interface, when I declare Banana, or any other peelable fruit, it would look like:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Banana : Fruit, IPeelableFruit&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void Peel()...&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;2.  I could create a PeelableFruit class, and a NonPeelableFruit class, and have subsequent classes inherit:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class PeelableFruit : Fruit&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public PeelableFruit()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.HasRind = true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then declare Banana as follows:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Banana : PeelableFruit&lt;br /&gt;{&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;What would happen in the above example is:  PeelableFruit would be a fruit.  Banana would be ("is-a") a PeelableFruit, and also a Fruit, because it would inherit from both.  The technical term is "inheritance chain".&lt;br /&gt;&lt;br /&gt;COMPOSITION&lt;br /&gt;&lt;br /&gt;"Has-A" lends itself to object relationships where something contains something else, like the above example of a Car and a Transmission.  But, suppose we wanted to really define, in a fruit, what a "Rind" is.  This would give us a pretty good example of "Has-A", because a PeelableFruit "Has-A" Rind.&lt;br /&gt;&lt;br /&gt;So, if I wanted to build a "Rind" class, I would do something like the following:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Rind&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private string _color;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Let's say this is how difficult the rind is &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;//to peel&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;//A banana would be something like 2, and an orange would&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;//be 5&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private int _difficultyToPeel;  &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;private int _thicknessInMillimeters;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Each of these private members would also have&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///corresponding public members.  I'll omit those&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Rind(string color, int difficultyToPeel, int thicknessInMillimeters)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_color = color;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_difficultyToPeel = difficultyToPeel;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_thicknessInMillimeters = thicknessInMillimeters;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;So, now that the Rind class is defined, we can now put a Rind object into the PeelableFruit Class:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class PeelableFruit : Fruit&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///In the constructor, I instantiate the Rind object&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Otherwise, later on in the execution, I may forget&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///and exceptions will get thrown&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Note the constructor had to be changed to facilitate&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///the necessity to instantiate the &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///Rind object (the color, difficulty, and thickness)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///parameters&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public PeelableFruit(string rindColor, int difficultyToPeel, int thicknessInMillimeters)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.HasRind = true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.PeelableFruitRind = new Rind(rindColor, difficultyToPeel, thicknessInMillimeters);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;protected Rind PeelableFruitRind;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then, when it comes time to instantiate a banana, doing so gives the banana much more robustness:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Banana : PeelableFruit&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///I don't really know if this is "best practice" to make a class so knowing of how&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///it implements a parent class, but I don't care.  This is how I will continue&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;///to invoke this sort of inheritance&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Banana() : base("YELLOW", 2, 7)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note that, when Banana gets instantiated, it will inherit the PeelableFruit's protected variable PeelableFruitRind, and that fruit rind will have a color of "YELLOW", a peeling difficulty of 2, and a thickness of 7 millimeters.  Now, Banana "Has-A" rind, and that means it's using COMPOSITION.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://oopstruggles.blogspot.com/" rel="tag"&gt;CodeProject&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;CodeProject&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7458647044495088329-7413510492096090159?l=oopstruggles.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://oopstruggles.blogspot.com/feeds/7413510492096090159/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7458647044495088329&amp;postID=7413510492096090159" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/7413510492096090159?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7458647044495088329/posts/default/7413510492096090159?v=2" /><link rel="alternate" type="text/html" href="http://oopstruggles.blogspot.com/2009/08/is-versus-has.html" title="Is-A Versus Has-A" /><author><name>Tim Claason</name><uri>http://www.blogger.com/profile/09507565535710674694</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry></feed>

