Open VS2005 Project Files in VS2008

Here’s an interesting tip from a co worker. If you need to work on a 2005 project in both 2005 and 2008 you can add the following to the Project element: ToolsVersion=”3.5” Resulting in:


Thanks to My Digital Life!

I now have Live Writer installed on my Windows Server 2003 machine. Thanks My Digital Life and Resource Hacker!

Windows Live team, please change the Live Writer install.


Making Sure Your Container Contains Your Basic Services

I’m working on creating some of the base code for our systems. One of the things we’re doing is moving to using IoC containers for our projects. These projects are all internal and as such they will be sharing some base functionality, like logging and configuration. To that end I wanted to create an implementation of WindsorContainer which has these services and facilities already setup. What I found was the IWindsorInstaller class. Using that in the constructor of my NewWindsorContainer I can call Install with any number of these installers, like:

public class MyContainer : WindsorContainer
{</p>



<span class="kwrd">public</span> MyContainer(<span class="kwrd">string</span> settingName)
{
    Install(<span class="kwrd">new</span> InstrumentationInstaller());
}

}

The Installer interface is just one method Install which takes a container and a configuration store. This makes these installer classes easy to implement and test. Below we just make sure the logging facility is registered.

public class InstrumentationInstaller :IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<MyLoggingFacility>();
    }
}

And the test code:     

[Test]
public void Install_Expect_Facility_Installed_Can_Resolve_Logger()
{
    var installer = new InstrumentationInstaller();
    var c = new WindsorContainer();
    installer.Install(c, null);
    var logger = c.Resolve<IMyLogger>();
    Assert.IsNotNull(logger);
}

I was initially going to create a facility to load our base services, but I like this approach more because it's simpler and the installer classes follow Single Responsibility Principle.


What Tools I&rsquo;m Currently Using

I setup a new machine this week, the machine was imaged with some of the obvious MS stuff, VS, Office, etc. I thought I’d note some of the apps I install right away. Things I know I’ll need, most development related, some system related, some others.

System:
FireFox: web browser and associated plug-ins
Download Them All
Smart Bookmarks
No Script
SysInternals Suite: a ton of tools for windows to help see what’s going on in the OS.
7Zip: open source zip utility.
Powershell: upgraded command line for windows.

Development:
QueryExPlus: Light weight query analyzer.
Subversion: SCM used by a lot of projects.
Tortoise SVN: Explorer integration with Subversion.
NUnit: Unit testing framework.
MbUnit: A nother unit testing framework.
Fiddler: HTTP inspector.
ReSharper: Coding optimizer I guess, not sure what it’s called but I can’t really code without it.
Notepad++: Tabbed notepad with syntax highlighting and a bunch of other text tools.
Nant: Build tool.
RhinoMocks: My mocking framework of choice.
Patch for RhinoMocks: Need this if your using .net 3.5.
Reflector: .Net decompiler.

Other:
Pidgin: Multi-service instant messaging application.
Launchy: Application launcher. Apparently has problems on some x64 machines, including my new machine. I’m really missing this.
FARR: Find and Run Robot, trying this as a Launchy replacement.
Windows Live Writer, but it doesn’t install on Server 2003, WTF?

I’m sure I’ve missed some, but these are at least the essentials that I use almost daily.

Any others I should checkout?


More Effective C# Notes: Generics

I’ve been reading the More Effective C# book by Bill Wagner. I’m going to blog some of my notes and thoughts on the book. If you find some of this stuff useful you will want to pick up a copy of the book.

Use the least amount of constraints on type parameters as possible. I’ve run into this, and besides limiting the usefulness of your class, in many cases you need to implement an interface or add a default constructor to get classes to compile. And ultimately your generic class ends up not being very generic.

In order to help achieve the above, you can use methods or delegates to enforce a specific needed operation. Whatever constraint you were going to have you could create a delegate that needs to be passed in and remove the constraint.

If you need to create new instances of the type parameter, you need to make sure you dispose of it correctly if it implements IDisposable. If it’s created as an instance member your generic classes needs to implement IDisposable. This simple pattern can be used for method scoped instances.

T instance = new T();
using (instance as IDisposable)</p>



instance.MethodCall();<style type="text/css">.csharpcode, .csharpcode pre

{

font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/

}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{

background-color: #f4f4f4;
width: 100%;
margin: 0em;

}
.csharpcode .lnum { color: #606060; }
</style>

Remember that the type parameter is always going to be the best match in an overloaded method search. As, such you can use run-time type checking to optimize for performance. I would not bother doing this until you know that it’s a performance issue. Or unless your making a closed source framework.

Use generic methods unless the type parameter is going to be an instance field. This makes the generic class easier to use as you don’t need a new class for each different type parameter being used. Also these types of methods lend themselves to becoming extension methods.

Again these are just some of my notes, the book, like the previous one, is worth a full read.


Intro To Unit Testing: 2 of n

Where do tests fit into my project and process?

There are a few ways to structure unit tests. Two of the most common seem to be one to one assemblies and same assembly. One to one assemblies meaning one test assembly for each assembly being tested. This approach has the benefit of easily removing the test assemblies before releasing code to production. It also allows testing release built code with out an extra build cycle like the following method requires. One drawback is that internal classes are harder to test.

The second structure, same assembly, has the TestFixtures in the assembly being tested. The main challenge with this is creating builds without the tests. I’ve seen this handled two way, first by the build script excluding all files under a Tests directory. And secondly, using compiler directives like #if TEST around the test code. Both have draws backs but the main one being two complies one to test and one to release. The main advantages are access to internal classes and fewer assemblies in a solution.

Which is right? As with many things, it depends.

Unit tests generally fit into the development in two key places, three for TDD. The first is before any code check-in the unit tests should be run and pass. Secondly, the continuous integration system should run the tests and fail the build if the tests fail. Third, if developing using TDD you would run the tests almost constantly while developing and refactoring.


Intro To Unit Testing: 1 of n

What is UnitTesting? What are UnitTests?

UnitTesting is testing a unit of functionality with other code.

UnitTests, in the context of c#, are simply classes with attributes. Usually they are called test fixtures which contain test cases. Test cases are just methods with a Test attribute on them. UnitTests are mostly concerned with testing interaction between classes or logical outcomes. Interaction tests test for the correct interaction between classes, usually with Stub or Mock objects. An interaction test might test for a web service call with a specific message. Logical tests test that the given business logic is working was expected. For example making sure a filter on insurance applications actually filters out applications. Both of these test types will use Assertions to make sure what was expected to happen did happen. An Assertion is really just a logical check that throws an exception if it’s false. If a test has Assert.AreEqual(expected, actual) and expected does not equal actual it will throw an exception, with details, to fail the test.
Here is a super simple test to make sure the test framework is working correctly.

using MbUnit.Framework;
namespace IntroToUnitTests.Tests
{</p>



[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> TestFixture
{
    [Test]
    <span class="kwrd">public</span> <span class="kwrd">void</span> Is_The_Test_Framework_Working()
    {
        Assert.IsTrue(<span class="kwrd">true</span>);
    }
}

}

Why would I want to write tests? What are the advantages?

  • Unit tests allow code to be changed and refactored without worrying about breaking the expected functionality.
  • Unit tests promote better design and cleaner, simpler code.
  • Unit tests help document the intent of code.
  • Unit tests help make sure bugs do not come back to life.
  • Unit tests help reduce time in the debugger.

Why wouldn't I want to write tests? What are the challenges?

Unit testing the UI is typically a very challenging. WebForms usually make unit testing really hard or impossible. This is partly because recreating the ASP.Net life cycle is not possible. And partly because it's really easy to mix business concerns with UI concerns.

Unit testing databases is challenging on two fronts. First having a way to create a clean DB for testing. Secondly, keeping the test time reasonable.

Legacy Code, which is often considered code without tests, is challenging because before you change legacy code it needs tests, but often you need to change the code to be more testable.

Who writes and maintains these tests?

Developers are responsible for writing, maintaining, and running unit tests. The build/release process should also be responsible for running and failing if any of the unit tests fail.

I hope this provides basic high level overview of unit testing, and idea of what value they might add and what challenges they might present. More detailed posts will be coming soon.


Brutalist Framework