Tuesday, December 30, 2008

NHibernate training materials

If you ever wondered how do some of the basic/more advanced stuff with NHibernate but didn't have a clue whatta heck is NHibernate at all look at www.summerofnhibernate.com site. It's awesome!!!

Creating a blog web application - part 1

Hi,

Today I'm starting a series of posts that will describe the process of creating a blog web application.

First of all we'll have to gather some high-level requirements and define the technologies that we'll use to write this web application.

Let's start with the requirements. As you'll see they are pretty simple as the application we're going to write is not a very fancy thing.

Blog Web Application Requirements.
  1. Everyone must be able to view the list of posts along with comments.

  2. Owner of this blog will have the possibility to add new posts.

  3. Everyone must be able to post comments to posts added by blog owner.

  4. Retrieve the list of blog entries as RSS feed.

  5. Add posts using a WebService interface.

That's it. Pretty simple, right? Now since this is going to be a full-blown application we're going to need to make some decisions about the technology used to persist the data, manipulate them and finally to visualize them in a web browser.

For the purpose of this tutorial I've selected the following parts:
  • Visual Web Developer 2008 SP1 Express Edition IDE - free, powerful, ergonomic, awesome tool

  • For the main framework we're going to use I've selected ASP.NET MVC framework because of its vast control over every single part of the application. It gives a clean separation of concerns and enables testability as well so it's a really nice thing.

  • As a presentation engine we're going to use the standard ASPX engine as it comes for free with the ASP.NET MVC.

  • We'll follow the View-Controller-Service-DAO-Database execution scheme. That way every single component of the application will have its clean task to perform and testing it will be a breeze.

  • For database access we'll use NHibernate because of its independence of the actual RDBMS. I realize that most of you will use Microsoft SQL Server and thus the LINQ-to-SQL framework but this one option is already well covered in a number of tutorials and I wanted to do something else.

  • For the actual RDBMS we'll use Firebird. The reason behind this choice is quite simple: first Firebird has an option to run as an embedded database as well as a full-blown database engine. Secondly I've used Firebird for a number of projects already and I'm very pleased with the performance of this database. And third, it's licensing model allows one to use it in any kind of application (commercial and open source)

  • Obviously we're going to need some IoC engine. As you'll see we'll have the luxury of selecting any IoC framework we want, ranging from Spring.NET, Castle, StructureMap, NInject, Autofac to Microsoft's Unity Application Block. I personally prefer Unity for its attribute-driven control over injected dependencies but Spring.NET is going to be an option we'll discuss as well.

  • And last but definitely not least we're going to follow the Test-Driven Development style. For that we'll use NUnit as it contains all of the features we're going to need ranging from a command-line xslt-driven output runner to simple mock framework. It has been around for a long time now and despite of some shortcomings and differences with other unit-testing frameworks (like JUnit or DUnit to name a few) it is still my favorite one.

Let me make a note on that last point: I'm definitely not an expert on TDD. You might find that throughout the project I'm not exactly following some practices or you might know some better option to perform some steps. Finally you might skip the testing part all along if you want to - whatever makes you happy.

That's it for now. In the next part we'll start off with creating the necessary projects and writing some code that will make up our controllers.

Stay tuned!

Sunday, December 28, 2008

Running unit tests during builds in Visual Studio .NET - part 2

Hi all,

as I've mentioned in my previous post it's quite easy to implement running tests during build process of VisualStudio. Now not everyone is comfortable with using a set of 3rd party tasks to do simple things. This time we're going to simplify things a little bit and use the Exec task of MSBuild that already comes with the standard installation.

We're going to start as previously with the XML style sheet that will convert the XML output to a format that's understandable by VisualStudio. Save the following content into your project under VSTestFormat.xslt:


<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='text'/>

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="test-results">
<xsl:apply-templates select="//test-case[failure]"/>
</xsl:template>

<xsl:template match="test-case">
<xsl:value-of select="substring-before(substring-after(failure/stack-trace, ' in '), ':line')"/>
<xsl:text>(</xsl:text>
<xsl:value-of select="normalize-space(substring-after(substring-after(failure/stack-trace, ' in '), ':line '))"/>
<xsl:text>)</xsl:text>
<xsl:text> : warning NU001: </xsl:text>
<xsl:value-of select="@name"/><xsl:text>: </xsl:text>
<xsl:value-of select="normalize-space(child::node()/message)" />
<xsl:text disable-output-escaping='yes'>&#xD;&#xA;</xsl:text>
</xsl:template>
</xsl:stylesheet>


Next we're going to add the task after building the project (that goes into the [projectname].csproj file):


<Target Name="AfterBuild">
<Exec Command='"$(ProgramFiles)\Nunit 2.4.8\bin\nunit-console.exe" $(OutputPath)$(AssemblyName).dll /xml=$(OutputPath)nunit-results.xml /transform=VSTestFormat.xslt /noshadow /nologo /nodots' />
</Target>


That's it! Now when you'll build your project it is going to run tests before returning to the editor and all failing tests are converted to a list of entries in the "Error List" window of VisualStudio.

Let me make one note here: This solution works perfectly with all IDEs - the full-blown one as well as with Express editions.

For those that don't like to experiment and just want to start building tests here's a VisualStudio template ready to use. Please note that it's provided "AS-IS" without any warranty so use it at your own risk.

NUnitTests.zip

Happy building!

Tuesday, December 23, 2008

Designing .NET Class Libraries

Hi,

I've stumbled upon a very interesting seminar provided by Microsoft about designing .NET class libraries. It gives a unique overview of a number of factors that one should consider when developing code for use by other developers.

Designing .NET Class Libraries


Matthias.

Running unit tests during builds in Visual Studio .NET

Hi there,

Some time ago I've attended a refactoring training provided by my employer. One of the interesting things there was that the project we worked on had unit tests hooked up to the build process. Now that I'm learning the .NET platform and the tools that come with it I've decided to give it a try and to integrate NUnit into the build process of a class library.

Visual Studio creates its projects in MSBuild format. That means that we can customize/extend the build process in a standard fashion as MSBuild is the standard tool (aside from NAnt) used to build .NET projects.

Here are the steps I took to enable such integration:

  1. Install NUnit (this procedure assumes the version 2.4.8 - note the version in ToolPath parameter of NUnit task).
  2. Install MSBuild community tasks (http://msbuildtasks.tigris.org/)
  3. Add the following lines to your class library project (.csproj) with tests (output file must be named "something.Tests.dll")

    <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
    <ItemGroup>
    <TestAssemblies Include="$(OutputPath)\$(AssemblyName).dll" />
    </ItemGroup>
    <Target Name="AfterBuild">
    <NUnit Assemblies="@(TestAssemblies)" ToolPath="$(ProgramFiles)\Nunit 2.4.8\bin\" XsltTransformFile="vsfmt.xslt" OutputXmlFile="$(OutputPath)\nunit-results.xml"/>
    </Target>

  4. Copy the following content into vsfmt.xslt in your test project path or somewhere else (remember to modify XsltTransformFile parameter if you store it somewhere else):

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method='text'/>

    <xsl:template match="/">
    <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="test-results">
    <xsl:apply-templates select="//test-case[failure]"/>
    </xsl:template>

    <xsl:template match="test-case">
    <xsl:value-of select="substring-before(substring-after(failure/stack-trace, ' in '), ':line')"/>
    <xsl:text>(</xsl:text>
    <xsl:value-of select="normalize-space(substring-after(substring-after(failure/stack-trace, ' in '), ':line '))"/>
    <xsl:text>)</xsl:text>
    <xsl:text> : warning NU001: </xsl:text>
    <xsl:value-of select="@name"/><xsl:text>: </xsl:text>
    <xsl:value-of select="normalize-space(child::node()/message)" />
    <xsl:text disable-output-escaping='yes'>&#xD;&#xA;</xsl:text>
    </xsl:template>
    </xsl:stylesheet>

  5. Switch to VS - a dialog should appear that the project file has changed. Click "Reload".
  6. Rebuild solution - if there are any test cases they'll get executed as part of the build process.
  7. (optional) Add the following parameter to NUnit task if you don't want your unit tests to fail the build but rather to issue warnings:
    ContinueOnError="true"

    For example:

    <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
    <ItemGroup>
    <TestAssemblies Include="$(OutputPath)\$(AssemblyName).dll" />
    </ItemGroup>
    <Target Name="AfterBuild">
    <NUnit ContinueOnError="true" Assemblies="@(TestAssemblies)" ToolPath="$(ProgramFiles)\Nunit 2.4.8\bin\" XsltTransformFile="vsfmt.xslt" OutputXmlFile="$(OutputPath)\nunit-results.xml"/>
    </Target>

Remember that using this kind of integration with unit testing framework forces developers to create unit tests that are really fast. Nobody likes to wait for the build process to complete.

While working on this integration I've used the following pages as references:

http://blogs.msdn.com/msbuild/archive/2006/11/03/msbuild-visual-studio-aware-error-messages-and-message-formats.aspx
http://www.w3.org/TR/xpath#section-String-Functions
http://blog.maartenballiauw.be/category/NUnit.aspx
http://www.nunit.org
http://msbuildtasks.tigris.org


Happy building!