When the only thing that will work is gacutil – level 200

I try to avoid the GAC at all costs.  I prefer xcopy deployments
of applications.  However, if you use some projects that all
reference log4net, then you know the pain of trying to resolve log4net
versions.  More often than not, it’s not feasible to recompile
every project you depend on.  If you need to reference two
libraries that each then reference different versions of log4net, you
have a tough problem because only one file named “log4net.dll” can be
copied to the /bin directory.

log4net is a strong-named assembly, so version information is a part of
finding the assembly.  In this case, I had to resort to putting
two versions in the GAC.  As much as I hated doing so, it seemed
like the only way out.

gacutil /i log4net.dll (x2)

Resharper 2.0 now released – level 000

Resharper 2.0 is finally released.  I’ve been using the beta of 2.0 for a while, and I used v1.5 on VS 2003, and it enhances productivity a lot.  One of the simplest things that I couldn’t live without is rename.  If I rename a class property that is used throughout my solution, it’s done instantly.  Without an autorename, I’m left with search and replace, a time-consuming affair.

Check it out!!!  They have a 30-day trial.  Read the docs and get familiar with the keyboard shortcuts.

You can see the list of new features at:
http://www.jetbrains.com/resharper/releaseNotes20.html

To download this latest version of ReSharper 2.0:
http://www.jetbrains.com/resharper/download

StructureMap v1.1 (for .Net 2.0) released on sourceforge – level 000

Not long ago, Jeremy Miller released version 1.0 his excellent dependency injection tool, StructureMap.  I’ve ported it to .Net 2.0, and I’ve added generics to the bread and butter class, ObjectFactory.  Download the .Net 2.0 release on sourceforge.net.  StructureMap supports creating very loosely coupled applications by service location and dependency injection.


Coding to interfaces is fine and dandy, but at some point, you need an instance of the concrete class that implements the interface.  Typing “new SomeClass” tightly couples your code, so what do you do?


Slap an attribute on the interface, the concrete class, and then all you have to type is:


IMyInterface instance = ObjectFactory.GetInstance<IMyInterface>( );


That’s all it takes, and you have an instance of the interface you are binding against.  Using this pattern, you can harvest reuse in your applications and test any component in isolation.

VS 2005 Web Application Projects installed! *breaths a breath of fresh air* – level 100

Upon hearing the announcement that Web Application Projects has been released, I wanted to get EZWeb converted
over as soon as possible (I’m close to making another release). 
I’d been limping along with the default web site option in Whidbey, and
I didn’t like it at all. 

First, you can download the VS 2005 add-in here.  There isn’t an automatic port of a VS 2005 website, but it was easy enough to do.  Here were my steps:

  1. Add new web application project to my solution. 
  2. In Win Explorer, copy the entire contents of my web site project to the web application folder.
  3. Delete my web site folder.
  4. Remove the website from my solution.
  5. Show all files in the web application.
  6. Explicitly include everything except bin and obj
  7. Add any assembly references necessary to the new project.
  8. Set any post-build events that you’ve been jerry-rigging up to this point.
  9. Run a build.  You’ll notice it fails on control declarations in code-behind files.
  10. Right click on the web project and run “Convert to Web Application”.  This adds an explicit partial class to  your code-behinds that hold your control declarations from the markup file.
  11. Run the build again.  It passed for me at this point.  I ran my application, and all was well.
  12. Run all unit tests and integration tests.  They all passed for me.

This project model is so much easier to use for real web applications
(that aren’t just web _sites_).  Kudos to the ASP.NET team for
getting this patch out.

If you are using Resharper 2.0 (beta), you’ll notice a slight
difference in navigating to files.  CTRL+N will locate the ascx.cs
and ascx.designer.cs files since they are C# code files.  To get
to the ascx files, you’ll need to use CTRL+SHIFT+N.

Integration testing demonstrated – level 200

In this
post, I’ll talk about and demonstrate integration testing.  If you are just starting out with integration
testing, you want to test small before you test big.  Full-system tests are good, but if they fail,
they don’t give much of a hint as to where the failure is.  Smaller integration tests will help narrow
the area where the failure lies.

 

A few
rules to live by

·                    
An
integration test must be isolated in setup and teardown.  If it requires some data to be in a database,
it must put it there.  Environmental
variables should not cause the test to fail randomly. 

·                    
It
must also run fast.  If it is slow, build
time will suffer, and you will run fewer builds – leading to other
problems. 

·                    
Integration
tests should be order-independent.  It
should not matter the order you run them. 
They should all pass.

·                    
Feel
free to make up rules that objectively result in fewer bugs.

 

Testing
a custom SiteMapProvider

In my
example, I have a custom SiteMapProvider (PageInfoSiteMapProvider).  This site map provider gets it’s data from
inside my EZWeb application, specifically, the IPageConfigProvider interface.  I use StructureMap for service location, so
one of the things that an integration test will validate is that my interface
implementations can be resolved correctly. 
I’m going to focus on an integration test for one method on the site map
provider, FindSiteMapNode(url). 

 

Here
is the constructor and method on my custom site map provider:

        public
PageInfoSiteMapProvider()

        {

            _provider = (IPageConfigProvider) ObjectFactory.GetInstance(typeof (IPageConfigProvider));

            ICurrentContext
context = (ICurrentContext)ObjectFactory.GetInstance(typeof(ICurrentContext));

            IConfigurationSource
config = (IConfigurationSource)ObjectFactory.GetInstance(typeof(IConfigurationSource));

 

            _applicationPath =
context.ApplicationPath;

            _defaultPage = config.DefaultPage;

        }

 

        public override SiteMapNode
FindSiteMapNode(string rawUrl)

        {

            VirtualPath
path = new VirtualPath(rawUrl,
_applicationPath, _defaultPage);

            PageInfo
config = _provider.GetPageConfig(path);

            SiteMapNode
node = MakeNodeFromPageInfo(config);

            return
node;

        }

 

This is
simple enough.  The site map provider is
just a wrapper around the interface call. 
Notice in the constructor, that I’m using a call to StructureMap’s
ObjectFactory class to resolve the interfaces that I need.  I need the current HttpContext and some stuff
in the web.config file.  Obvious in my
integration test I don’t have the ASP.NET runtime, and I don’t have the
web.config file, so I’ll need to simulate thing (mock, stub, fake, whatever you
want to call it).  In my integration
test, I’m going to have to use fake implementations of these interfaces.

 

Here
is the integration test fixture that tests this provider all the way down to
the point where it reads the data from the xml file on the disk. 
I’ve chosen this scope because it’s not too large, and it’s
not too small.

    [TestFixture]

    public class PageInfoSiteMapProviderTester

    {

        [SetUp]

        public void Setup()

        {

            IConfigurationSource
source = new TestingConfigurationSource();

            ICurrentContext
context = new TestingCurrentContext(source);

 

            ObjectFactory.InjectStub(typeof(IConfigurationSource),
source);

            ObjectFactory.InjectStub(typeof(ICurrentContext),
context);

        }

       

        [Test]

        public void ShouldGetRootSiteMapNode()

        {

            string
xmlFileContext = @”<?xml
version=””1.0″”
encoding=””utf-16″”?>

                <PageInfo
xmlns:xsi=””http://www.w3.org/2001/XMLSchema-instance”&#8221;
xmlns:xsd=””http://www.w3.org/2001/XMLSchema””&gt;

                 
<VirtualPath>/</VirtualPath>

                  <Title>My home
page</Title>

                  <Template />

                  <Theme />

                  <Plugin />

                 
<HasParent>false</HasParent>

                  <Children />

                  <Links />

                  <Editors />

                </PageInfo>”;

 

            string
fileName = FilePageConfigProvider._fileName;

            string
fileSubDir = FilePageConfigProvider._filesDir;

 

            string
fileDirectory = Environment.CurrentDirectory + “\” + fileSubDir;

            string
path = fileDirectory + “\” +
fileName;

 

            FileHelper
helper = new FileHelper();

            helper.SaveFileContents(path,
xmlFileContext);

           

            PageInfoSiteMapProvider
provider = new PageInfoSiteMapProvider();

            SiteMapNode
node = provider.FindSiteMapNode(“/mywebapplication/default.aspx”);

           

            Assert.AreEqual(“My home page”, node.Title);

            Assert.AreEqual(“/myWebApplication/Default.aspx”,
node.Url);

            Assert.AreEqual(“/”, node.Key);

        }

    }

 

This is
my entire integration test to make sure that the SiteMapNode object is put
together correctly. Notice that I have data setup with the xml file.  Then I call the provider and assert on the
results.

 

Faking
the “cruise missile” with StructureMap

If my
code was in charge of launching a cruise missile correctly, then I would have
to fake out the cruise missile when testing my code.  In fact, my code might not ever run in its
true environment until the world plunged into war again.  On a small scale, I have to fake the two
interfaces that are not reproduceable in my test context.  Direct your attention to the Setup
method.  You’ll notice that I’m creating
two fake instances and instructing StructureMap to “InjectStub”.  Because of this, when my code asks for an
instance of one of those interfaces, the class I created in Setup will be
returned.

 

Here
are the stub classes

    public class TestingCurrentContext
: CurrentContext

    {

        public
TestingCurrentContext(IConfigurationSource
source) : base(source) { }

 

        public override string
MapPath(string path)

        {

            string
executingDir = Environment.CurrentDirectory;

            string
combinedPath = executingDir + “\”
+ path;

            return
combinedPath;

        }

 

        public override string
ApplicationPath

        {

            get

            {

                return
“/myWebApplication”;

            }

        }

    }

 

    public class TestingConfigurationSource
: ConfigurationSource

    {

        public override string
ClientFilesPath

        {

            get

            {

                return
@””;

            }

        }

 

        public override string
DefaultPage

        {

            get

            {

                return
“Default.aspx”;

            }

        }

    }

 

These
classes are simple enough.  They merely
return expected environmental settings so that I can test my code.

 

Integration
testing is hard at first

If your
style of coding isn’t loosely coupled, then it will be difficult to do
automated integration testing.  Seams
must exist in the code where fake implementation can be inserted.  In my case, I had a web.config file and the
HttpContext of the web application that get in the way.  I stub these out for my test.  My tools list for integration testing is:

·                    
NUnit
for organizing and running the tests.

·                    
StructureMap
for resolving interfaces and for stubbing interfaces at hard boundaries.

 

My way
or the highway

Just kidding.  I’m not saying that this
is the “golden” way of doing integration testing.  There are many ways, and that’s why software
engineering is engineering and not assembly-line work.  The above approach has worked well for me,
and I put interfaces where they make sense (everywhere) in my code to maintain
flexibility.  I’m open for critique or
questions.

VS 2003 web projects model now available in VS 2005 – level 200

One of the biggest criticizms of VS 2005 was the radical change in the way web applications had to be set up.  From the folks doing more simple websites, this change was welcome, but the folks doing complex web applications, this change caused a bit of trouble.  It’s clear that some folks like the new way, and some folks like the way VS 2003 handled it (minus the mandatory control declarations).

Scott Guthrie has just announced the release of the Web Application Projects add-in for VS 2005 that brings the VS 2003 model to Whidbey.  It’s a simple install for VS 2005, and the project type is immediately available.   I’ll be installing and using it shortly, so I’ll be back with a personal review.

The word is that this will be included in VS 2005 SP1 as well.

StructureMap 1.0 released – Don’t create a loosely coupled system without it – level 200

StructureMap is very easy to use, but it makes creating loosely coupled OO systems a breeze (ok, that’s exaggerating.  it still requires engineering).  I use it in every project I work on, and I like it better than Spring.Net.  Spring needs Xml configuration for every mapping between an interface and a class.  This causes the Xml file to continue to grow and grow and grow.  StructureMap uses Attributes to tag the interface and class definition.  At runtime, it hooks them up.  This type of metadata is much easier to manage.

Jeremy Miller recently released version 1.0 of StructureMap.  I was please he included a feature I explicitly asked for.  If you need a port to .Net 2.0, let me know.  I’ve done it.

How Microsoft Virtual Server 2005 helps software development – level 200

Since Virtual Server 2005 and VMWare server are now both free to the masses, many more developers can take advantage of virtualization.  In short, with a virtual computer, you can do more things with one physical machine.  You can run multiple operating systems on one machine.  You can test your server farm on one machine.  You can test maintaining session state over a web farm on one machine.  You can configure a domain of computers on one machine.  You can test out the latest beta software without hosing your machine!

In my development shop, we only have one server that we use.  It’s a 1U 4-proc server with 4GB of RAM.  Obviously, you need some decent hardware to be productive with virtual machines running inside because each virtual machine still needs resources just as it does when installed as the base operating system.  We have CruiseControl.Net and FitNesse installed on the host machine, and we run 6 or 7 development servers all the time as virtual machines on Virtual Server 2005.  We need all the development servers, and it’s a lot easier having one physical server than it is to maintain a whole rack.  When a situation arises where we need another dev server, it’s no problem.  We can copy the .vhd file from an existing one, and we instantly have another server up and running.  We obviously have to change the network name, but that’s no problem.

My workstation has 2GB of RAM, so I’m able to run virtualization locally with Virtual PC 2004 (I have run Virtual Server locally but found it simpler to use VPC for local use).  I can test out a pre-release of software, and I’ve even been able to play around with a Longhorn (Windows Vista) beta (by the way, Virtual Server 2005 R2 has virtual machine additions for Longhorn – it’s dog-slow without them).

In short, virtualization is a boon for developers, and I’m glad I it as a tool in my toolbelt.

I haven’t installed R2 yet, but R1’s manager web site only works well with IE.  Here’s to hoping that’s fixed with R2.

Virtual Server 2005 is now free – level 000


Christoph De Baene
was early with the news on the blogosphere that Virtual Server 2005 R2 is now a free download.  The word is getting out very quickly, and I’m sure a lot of people will now download it for the first time.

I’ve used Virtual PC 2004 and Virtual Server 2005 since they debuted, and I was recently impressed that VMWare had decided to offer the server edition for free.  This competitive move has undoubtedly influenced Microsoft to compete in this space.

Congratulations to both players for competing, which is always better for the customer and industry advancements.

You can download Virtual Server 2005 RS enterprise edition here.