… how to generate classes from XSDs that include

•07/30/2009 • Leave a Comment

If you have ever consumed a POX web service, you’ve probably been given an XSD.  A typical pattern is to run XSD.EXE against the XSD file to generate C# classes.  You can then use XmlSerializer to deserialize the XML response.  All is good… unless the XSD relies on other XSD files and imports via <xsd:import/> statement.  When you attempt to generate C# classes, the XSD util will complain of missing elements or datatypes.

Previously, I would edit the main XSD and paste in the imported XSD.  Really annoying.  Couldn’t figure out how to the XSD util to follow the directive.  Until now… 🙂

XSD util lets you supply multiple .xsd files. Extemely simple solution.

xsd.exe /c Main.xsd External1.xsd

… how to get a generic Type object at runtime

•07/02/2009 • Leave a Comment

Today I needed to query StructureMap for instances that implemented a type with a generic parameter.  The problem is that I didn’t know the type parameter until runtime.  After a bit of looking through members of Type I came across MakeGenericType(Type[] typeArguments).   Acting on a generic type, MakeGenericType will substitute typeArguments for the type parameters.  Not so sure about performance.

Also worth noting that the instances returned by GetAllInstances are of type object.  In my case, AbstractService<T> inherits from AbstractService which contains the method to execute.  An explicit cast was in order.

An alternative approach would consist of using named instances in StructureMap and querying the container based on the type’s name.  If you fat-finger the instance name during the registration process, you can’t know at compile time.

IController controller;
Type type = typeof (AbstractService<>)
    .MakeGenericType(new Type[] {controller.GetType()});
var instances = ObjectFactory.GetAllInstances(type);
foreach (var instance in instances) {
   ((AbstractService) instance).DoStuff();

… how to execute setup code before every TestFixture without using inheritance

•06/16/2009 • Leave a Comment

Not sure how I missed this, but it came in very handy during a recent IoC refactor.

I’m sure everyone knows that about the SetUp and TearDown method attributes.  Most people also know about the TestFixtureSetUp and TestFixtureTearDown method attributes.  Let’s say you needed to run some setup code once before any tests execute.  In addition, you have ton of existing tests that don’t inherit from a base test.  Enter the SetupFixture attribute.  It provides one-time setup and teardown for all fixtures in a namespace or assembly.

Unlike the previous attributes, SetupFixture is a class attribute.  Inside a class with this attribute, you can have methods with the standard SetUp and TearDown attributes.  Asssuming the class is in a namespace, the SetUp method will run once before ALL  fixtures in the namespace.  The TearDown method will run once after ALL fixtures in the namespace.  If the SetupFixture class is not in namespace, it will affect every TestFixture in the entire assembly.  All of this happens without a base class.

Some SetupFixture class requirements:

  • Must be a publicly exported type
  • Must have a default constructor
  • Must have at most one method decorated with [SetUp] attribute
  • Must have at most one method decorated with [TearDown] attribute
  • Only one [SetupFixture] should exist per namespace.  (Haven’t tried this so not sure what happens)

Sample SetupFixture class below

namespace NUnit.Tests {
 using System;
 using NUnit.Framework;

 public class MySetUpClass  {

    RunBeforeAnyTests()  {
       // ...

    RunAfterAnyTests()  {
       // ...

… about some new (and old) ReSharper features

•06/11/2009 • Leave a Comment

Here are some new ReSharper features that I picked up yesterday.  Shortcuts below are for IntelliJ IDEA mappings.

Highlight all usages of a symbol within the current file (more)

Position the cursor with any symbol (usage or declaration) and press Ctrl+Shift+F7. By default, declaration is highlighted in pink and usage in blue.  Highlights are also displayed in “code status indicator bar”  on the right-hand side of code window.

Locate in Solution Explorer (more)

Baked into Visual Studio is the ability to track the active item in Solution Explorer.  You can find the option in Tools – Options – Projects and Solutions.  Personally, I don’t like the Solution Explorer scrolling and expanding constantly when I change code windows so I turn off the feature.  There are sometimes when I want to know where in the solution I am. Press Shift+Alt+L to highlight the current file in the Solution Explorer.

I also stumbled across a ReSharper 4.0 Feature Map.  In addition to listing features, it also includes a description of where the feature can be found in the menu as well as keystrokes for VS and IntelliJ IDEA layouts.

… how to perform geolocation with JavaScript only (and a HTML 5 browser)

•06/07/2009 • Leave a Comment

While  HTML 5 may not reach recommendation status for a few years, support for some feature will be available in browsers very soon.  I was recently asked to review some geolocation alternatives and here’s what I learned.

First, a basic sample.


   if (navigator.geolocation) {
   } else {
      alert('not available');

   function showPosition(position) {
       alert(position.coords.latitude + " " + position.coords.longitude);

   function showError(error) {
      alert("Error: " + error.code + " " + error.message);


void getCurrentPosition(in PositionCallback successCallback);
void getCurrentPosition(in PositionCallback successCallback, in PositionErrorCallback errorCallback);
void getCurrentPosition(in PositionCallback successCallback, in PositionErrorCallback errorCallback, in PositionOptions options);

As you can see the getCurrentPosition()  function can accept 1, 2 or 3 parameters.  The first 2 are callbacks for success and error respectively.  The 3rd parameter can be used to adjust the required accuracy.  In addition there’s another function named watchPosition.  In that case, the success callback is called multiple times as the position changes.

The most important members of the Position object are latitude, longitude and accuracy.   These properties should be non-null for all implementations.  Other properties which may or may not be supplied based on the browers implementation include: altitude, altitudeAccuracy, heading and speed.

To see this work today, try the sample in Firefox 3.5 Beta 4.  For more detailed information, try to get through the current specification.

… what AutoMapper can do for you

•06/04/2009 • Leave a Comment

Writing code to map one type object to another has to be the worse code to write.   It’s a necessary part of many applications though and I highly value the outcome.

To alleviate the pain of writing the mapping code, Jimmy Bogard has been working on AutoMapper.  As he explains in his introduction post, AutoMapper will automatically match properties based on property name, nested property names and a host of other rules.  Of course, it supports custom mapping for the (hopefully) rare cases.  It also includes validation functionality to ensure that every property of the destination type can be mapped to a property on the source type (think StructureMap’s AssertConfigurationIsValid function).

I intend to dig much deeper into AutoMapper soon.   Stay tuned.

… which ReSharper keyboard commands can be mapped for running unit tests

•06/03/2009 • Leave a Comment

During a recent workshop given by Scott Bellware, we mapped keyboard shortcuts to TestDriven.NET commands to run and rerun a set of tests.  I don’t use TestDriven.NET so I found these ReSharper test runner commands.    My personal keyboard mappings can be found in parenthesis.

Resharper.Resharper_UnitTest_ContextRun (Ctrl-T, Ctrl-T)

Executes the test or fixture given the current text editor context.

Resharper.Resharper_UnitTest_RunCurrentSession (Ctrl-T, Ctrl-R)

Executes all tests in the current test session.

Resharper.Resharper_UnitTest_RunSolution (Ctrl-T, Ctrl-A)

Executes all tests in the currently loaded solution.