Friday, September 14, 2007

Singleton Pattern

The singleton pattern in C# is very simple to implement and very useful. This pattern ensures that there is only one instance of its self providing a global point of access. There are different ways of doing this but I like to use the example below  because it is thread safe and clean.

public class User
{ private int _userId = 1; private static User _instance = new User(); static User() { } private User() { } public int UserId { get { return _userId ; } set { _userId = value; } } public static User Instance { get { return _instance; } }
}
Here is a quick test I wrote in NUnit that shows how it is used and comfirms there is only one instance.
[Test]
 public void TestSingleton()
 {
     User sngle1 = User.Instance;
     sngle1.UserId = 1;
     User sngle2 = User.Instance;
     sngle2.UserId = 2;
     Assert.AreEqual(sngle2.UserId, sngle1.UserId);
 }

More Info:

Everything you need to know about C# singleton.

More Patterns

#     Comments [4]  
kick it on DotNetKicks.com
 Thursday, September 13, 2007

Unable to find the report in the manifest resources. please build the project, and try again.

I was reorganizing a project and I moved a crystal report after that I received this error at runtime "Unable to find the report in the manifest resources. please build the project, and try again." No amount of building could fix this problem. I eventually had to open up the .csproj file in note pad and edit the items entries in the xml/MSBUILD.

Here is what a Crystal Report should look like in your MSBUILD script.

    <Compile Include="CrystalReport1.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>CrystalReport1.rpt</DependentUpon>
      <SubType>Component</SubType>
    </Compile>

    <EmbeddedResource Include="CrystalReport1.rpt">
      <Generator>CrystalDecisions.VSDesigner.CodeGen.ReportCodeGenerator</Generator>
      <LastGenOutput>CrystalReport1.cs</LastGenOutput>
    </EmbeddedResource>

 After you correct the entry Rename the file Build then rename it again to the old name and build again. It might work if you just delete the code behind and then rebuild, but I didnt't try this.

 

#     Comments [2]  
kick it on DotNetKicks.com
 Wednesday, September 12, 2007

DataTable Select Method

The Select method is really handy for filtering and sorting the rows you need from a strongly typed dataTable. It has 3 overloads Select() for returning all the rows as an array select(stringFilter) for returning the rows with out sorting and Select(stringFilter,stringSort) for returning rows with sorting. Here is quick way to fire everyone with the first name of bob in your employee management application.
EmployeeDataSet.PeopleRow[] people = peopleDataTable.Select("FirstName = bob AND Fired = 0", "LastName") as EmployeeDataSet.PeopleRow[];

 foreach (EmployeeDataSet.PeopleRow person in people)
 {
     person.Fired = true;
 }

more Info:
MSDN
even better example

#     Comments [3]  
kick it on DotNetKicks.com
 Thursday, August 23, 2007

Code Search Engine

Found a cool code search engine called Koders.com that crawls open source projects by language for code examples. It has a great syntax highlighting and plugs into your IDE.
 
#     Comments [0]  
kick it on DotNetKicks.com
 Wednesday, August 22, 2007

Eponymous Laws Of Software Development

Do you want to sound smart, impress your friends, increase your chances of scoring next time you go out on a night on the town? Take a look at Phil Haack's blog post on 19 Eponymous Laws Of Software Development.

My Favorites are Sturgeon's Revelation Ninety percent of everything is crud (it seems to me to have a duel meaning crud = Create Update and delete or crud = crap) and Hofstadter's Law A task always takes longer than you expect, even when you take into account Hofstadter’s Law.

#     Comments [2]  
kick it on DotNetKicks.com
 Tuesday, August 21, 2007

Measuring Time Elapsed in Code

Some times you need to time an algorithm or method when trying to increase performance of an application. There is a quick and easy way to do this using the StopWatch class in System.Diagnostics.

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
TestMethod();
sw.Stop();
Debug.WriteLine(sw.ElapsedMilliseconds.ToString());


#     Comments [1]  
kick it on DotNetKicks.com
 Wednesday, June 13, 2007

Mission Complete: Continuous Integration .Net

I have completed or at least gotten a good start with Continuous Integration. What I have done may not be doctrine but it is what I could find to be the simplest and cleanest way. There are in my opinion 4 main parts you must have in order to achieve CI. First you must have a good repository, second you need to have a development tree setup for your project, then each project needs a build script, and finally you need to setup an automated build server. For more on continuous integration check out Martin Fowlers article and Jay Flowers article.

  1. Repository

The repository/version control/source code management, what ever you like to call it is the first thing that needs to be established. There needs to be a common place where your entire source is kept so that your build server can know where to get the latest version (head). I choose Subversion (svn) with TortoiseSVN. I was using visual source safe and I was happy about this, but like my grandpa is happy with dial-up, I had no idea what I was missing. If you are using source safe switch now to Subversion its open source and its free you will not regret it.

If you would like to know just about everything a user would need to know about modern repositories go to Eric Sink’s online book: Source Control HOWTO.

  1. Creating Development Tree

Basically the Development tree is the folder structure/organization of your project. A good development tree is defined by Mike Roberts as easily inheritable on new environments, requires little maintenance, easily maintained, supports productivity, and is consistent. I tried to follow his guide lines in his article: How to Setup a .NET Development Tree. I am using a refactored patterns project I did earlier for this example. I define the trunk/root folder name to be the same as my visual studio solution. In this folder I have 3 other folders src, lib, and tools.

The src contains all the developer created content need to build the solution. The lib contains all references dlls that are needed for the project and are not built with the solution. The tools folder will house things like NAnt, NUnit, FXCop, and whatever else you need to help build quality software. You can also include a docs folder to contain the documentation. Another good source on development trees is an article on

  1. Build Script
To automate my builds I choose NAnt  + NAntContrib. I save the build script in the root of the project with the file name of [projectName].Build and a go.bat that contains one line that calls NAnt with the build script.

@tools\nant\Nant.exe -buildfile:[projectName].build %* > buildOut.txt

 The > BuildOut.txt saves the out put of the build result to a text file I found this convenient to view how the build went. My build script should be made in a way that is easily implemented, easy to maintain, and need as little maintenance as possible. I include this as an existing solution item that way it can be edited along with the src.

<?xml version="1.0" encoding="utf-8"?>
<project name ="nant" default ="all">

  <!-- The Build Directory -->   
  <property name="build.dir" value ="build"/>

  <!-- Define Default targets and the order they run -->
  <target name="all" depends="clean, compile, run-unit-tests" description="Compile and Run Tests"/>

  <!-- delete build folder, everything in this folder should be created on the build,
    so it should be safe to delete at any time. -->
  <target name="clean" description="Delete automated build artifacts">
    <delete dir="${build.dir}" if="${directory::exists(property::get-value('build.dir'))}"/>
  </target>

  <!-- Compile solution with msbuild task(Only Available with NAntCOntrib)
    if it is 2003 use solution task all build properties should be
    set up in a new VS Automated Build configuration. -->
  <target name="compile">
  <!-- point msbuild to the solution file with should be at the root of your src folder-->   
    <msbuild project="src\Patterns.sln">
      <property name="Configuration" value="Automated"/>
    </msbuild>
  </target>

  <!-- RunUnit tests -->
  <target name ="run-unit-tests">
    <!-- Creates Nunit Reprot Directory-->
    <mkdir dir="${build.dir}\test-reports"/>
    <exec program="nunit-console.exe" basedir="tools\nunit"
          workingdir="${build.dir}">
      <!-- The dll that contains your test that
    should now be in the build folder -->
      <arg value="Strategy.Test.dll"/>
      <!-- Location and name of the NUnit test report -->
      <arg value="/xml:test-reports\UnitTests.xml"/>
    </exec>
  </target>

</project>

In side VS I set up a new visual studio build configuration called Automated. Automated is basically a copy of the default configuration Release but I changed the folder it builds to be ..\..\build\.

  1. Build Server
The final step is implementing your build server. I have heard of people just using scheduled tasks for this but that doesn’t sound very useful. I wanted the build done when some one commits a change to the repository. For this I used CruiseControl.NET. CruiseControl.NET is an Automated Continuous Integration server, implemented using the Microsoft .NET Framework. It works with majority of the source code management (SCM) tools out there and more importantly it works with subversion. The installation was simple just download and run the MSI but the configuration can be a little difficult because of the flexibility available. To get it running you need to edit the ccnet.config.

<cruisecontrol>
    <!-- Each Project needs the following setup -->
<project name="Patterns">
<!-- The Working folder/checked out
folder for the project on the server -->
<workingDirectory>H:\MyApps\Patterns</workingDirectory>
<!-- sourcecontrol setup -->
<sourcecontrol type="svn">
  <!-- URL to the project in the repository -->
    <trunkUrl>http://www.myrepos.com/myapps/Patterns/</trunkUrl>
    <executable>D:\Program Files\Subversion\bin\svn.exe</executable>
    <!-- The Working folder/checked out
    folder for the project on the server -->
    <workingDirectory>H:\MyApps\Patterns</workingDirectory>
</sourcecontrol>
<!--Build task-->
<tasks>
    <nant>
     <!-- Nant EXE for project -->
        <executable>H:\MyApps\Patterns\tools\Nant\nant.exe</executable>
    <!-- where the build file lives -->
        <baseDirectory>H:\MyApps\Patterns</baseDirectory>
        <buildArgs></buildArgs>
        <nologo>false</nologo>
        <buildFile>Patterns.build</buildFile>
        <buildTimeoutSeconds>1200</buildTimeoutSeconds>
    </nant>
</tasks>
<!-- reporting information -->
<publishers>            
    <merge>
        <files>        
            <!-- Location of Unit test reports -->
            <file>H:\MyApps\Patterns\Build\test-reports\UnitTests*.xml</file>
        </files>
    </merge>
  <!-- create the log files used by the CruiseControl.NET Web Dashboard -->
    <xmllogger logDir="D:\Program Files\CruiseControl.NET\server\Patterns\BuildLogs" />
  <!--default statistics for capturing during the build process.-->
  <statistics />
</publishers>
</project>
</cruisecontrol>

After that just edit the project, commit the changes, and watch the dashboard build report.


#     Comments [5]  
kick it on DotNetKicks.com
 Friday, June 08, 2007

Getting Started with .Net Development

  

 I am really impressed with the amount of developer support being put out by Microsoft; everything you need to get started is pretty much free. You can download the Express version of Visual Studios and it gives you a majority of its big brothers features which is 300$ IDE, the most notable difference is that the express version is not pluggable. There are different flavors of Visual Studio express, one for windows, web with AJAX, hardware/robotics and game /XNA development.  Although the free IDE is nice and I use visual studio as my primary development tool I am totaly blown away with the amount of resources available for learning. I am not talking about the MSDN Library, most of the time the MSDN documentation just confuses me, but webcast live and On Demand, podcasts and virtual labs are fantastic. There is tons of time worthy information put up here for beginning and advance programmers. It’s the first place I go to when I am trying to learn a new technology. I hear a lot of trash talked about Microsoft everywhere I go but I feel they have the best development support out there.

#     Comments [4]  
kick it on DotNetKicks.com