Design Patterns - Writing better software

Writing applications that simply work is for the most part pretty easy. However writing software that is well designed, scalable and also readable takes a bit more work. Using and understanding design patterns can help you write better software. Here is a few links I found related to the topic. I was going to write all about this but found someone that did almost exactly what I would have done so just going to post a link to the topic.

http://wiki.asp.net/page.aspx/276/design-patterns/

 

Object Oriented Design Patterns

While I have been programming now for about 3 years using the Object Oriented programming environment of C# I have not really spent any time looking at patterns. I am guessing in my natural progression of learning programming this seems to be the next logical step.

I am on vacation currently and this gave me the perfect opportunity to spend some time reading which I don't usually do much of. In fact I can say that I have never read a technical publication fully. Well as of today that is no longer the case. I read "Design Patterns Explained" by Alan Shallow and James Trott entirely. I have to say that this is the very first publication I have read that explained things the way I learn. In fact the author explains the same process he went through in understanding patterns and his natural progression in learning software. I highly recommend this book if you are just learning OO Design patterns.

Next I went and did some searches on the Design patterns that I had been reading about but related to my programming language and came across a great page on codeproject that shows an entire program and how each aspect relates to various patterns.

 

Design Patterns Implementation in a Storage Explorer Application

By Breman Sinaga

URL: http://www.codeproject.com/KB/architecture/sinagastorageexplorer.aspx

Breman does an incredible job of showing how some of the software patterns might be implemented in a real world application and really this is bringing together a much better understanding of how to apply some of these patterns. I am starting to think about how I am possibly already using some of these patterns without knowing it before and how some of the things solved in our applications might have been better structured had we implemented some of the various patterns.

I read some more today and found a link to a page that gives an outline at how to study patterns and information on creating a study group. I think it might be cool to see if Seattle has a local study group related to patterns so putting this as a reminder.

http://www.industriallogic.com/papers/learning.html

http://www.bennorthrop.com/Home/Blog/2006_11_01_design_patterns.php

http://www.onjava.com/pub/a/onjava/2005/04/06/designpatterns.html

Good reference resource Wikipedia: http://en.wikipedia.org/wiki/Design_Patterns

Spawning multiple .NET delegates really slow

I recently worked on a project that had a listener service running that took TCP requests and made external calls to a remote WebService. The listener service could have up to 300 concurrent requests.

In testing the service it was showing a significant delay in executing requests above 3-4 concurrent clients. The first bottleneck had to do with the number of allowed HTTP connections to a remote machine from our service. It turns out that by default only 2 concurrent HTTP connections are allowed by default.

Here is an MSDN article that explains the settings that need to be set to allow more connections to be made.

http://msdn.microsoft.com/en-us/library/fb6y0fyc.aspx

So setting something like this what required.

configuration>
  <system.net>
    <connectionManagement>
      <add address = "http://www.contoso.com" maxconnection = "10" />
      <add address = "*" maxconnection = "2" />
    </connectionManagement>
  </system.net>
</configuration>

While this showed a little improvement the problems were not over. It turns out that the ThreadPool by default only starts up with 4 idle threads. As you exceed that limit the ThreadPool checks every 500ms to see if more threads are needed and only spawns up one thread every 500ms. So you can see that it will take a long time to get spawned a large number of concurrent requests.

Solution to this is to set the minimum threads in the threadpool.

ThreadPool.SetMinThreads - http://msdn.microsoft.com/en-us/library/system.threading.threadpool.setminthreads.aspx

So we set the Min to the minimum number of threads we wanted sitting idle. Ok now performance was very good up to the number of threads we set as our minimum. But wait things are not over. We moved the code to another machine and the performance degraged. What could be wrong? It worked on my machine just fine!!

So we looked at what was different about the machines. Turned out my machine was running .NET 2.0 and the other machine was running .NET 2.0 SP1. I guess in .NET 2.0 SP1 a new bug was introduced that if threads are requested to quickly it will revert back to slowly invoking new threads.

Solution is to put a delay after each new call to a delegate for a few milliseconds using something like Thread.Sleep(50);. I had to put about 50ms delay on the machine in question. However I found that this bug was to be fixed in .NET 2.0 SP2. A bit of searching I found that .NET 2.0 SP2 had actually been released but not by itself. You have to download .NET 3.5 SP1 which also includes .NET 2.0 SP2. After installing this on the machine no delay was required any longer and performance was very good.

Hope this helps someone