Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests

Shhh...don't tell anyone but we don't unit test all of our code.  We're striving to get all developers and managers on board with unit testing but we're not there yet.  Despite our delinquency in writing tests one thing we do try our very best to do is keep mistakes from happening again.  If we find a bug in the system, before fixing it, we go and write a unit test for that problem.  We verify the test fails, the go and write the code that fixes it.  This recognizes that bugs will appear in the system, but once fixed we'll insulate ourselves from that bug happening again.

We had one such case last week.  We were getting a yellow-screen error after a bunch of commits by developers.  The error stemmed from a component in Windsor that did not have all of it's dependencies met.  In other words, we registered a component which relied on another component which was not registered.  We already had written a test to compile and test our Windsor configuration, however that test was not mature enough and needed some tweaking. 

In our application we leverage the new ASP.Net MVC.  We also take advantage of the ability to leverage an IoC container, Windsor, to create controller instances when a web request comes in.  When the application starts, we register all of the controllers in the application into Windsor using reflection.  Therefore our Windsor configuration at runtime is made up of two things, the XML configuration and the controllers which are registered on startup.  The "bug" here was that one of our controllers had a dependency on it that was not registered in Windsor.  When you ran the test by itself, the test would pass, since it was only loading up the components from the Windsor configuration.

Wanting to protect us in the future, we acknowledged there was something missing from our test and set out to find it.  To create the failing test was relatively simple.  All that we needed to do replicate in our test what we were doing at runtime.  Therefore we needed to register all implementers of IController, just like we do on application startup.  Below I've

Before:

   1: IWindsorContainer container = new WindsorContainer("windsor.config.xml");
   2:  
   3: foreach (var handler in container.Kernel.GetAssignableHandlers(typeof(object)))
   4: {
   5:     Console.WriteLine("{0}\n\t- {1}", handler.ComponentModel.Service, handler.ComponentModel.Implementation);
   6:     container.Resolve(handler.ComponentModel.Service);
   7: }

After:

   1: IWindsorContainer container = new WindsorContainer("windsor.config.xml");
   2:  
   3: var assm = new HomeController(null).GetType().Assembly;
   4: //add all controllers
   5: foreach (Type type in assm.GetTypes())
   6: {
   7:     if (typeof(IController).IsAssignableFrom(type) && !type.IsAbstract)
   8:     {
   9:         container.AddComponent(type.Name.ToLower(), type);
  10:     }
  11: }
  12:  
  13: foreach (var handler in container.Kernel.GetAssignableHandlers(typeof(object)))
  14: {
  15:     Console.WriteLine("{0}\n\t- {1}", handler.ComponentModel.Service, handler.ComponentModel.Implementation);
  16:     container.Resolve(handler.ComponentModel.Service);
  17: }

Now that this test is in place, I am much more confident that we won't see the error we were seeing in product again, we've got a test which warns us if something isn't quite right.  Our initial test didn't quite get it all right.  Mistakes/bugs in code will happen, hopefully few and far between, but when a bug does show up, go write a test that verifies the bug before going off an fixing it.  Then make the test pass, which should fix the bug.


Posted 11-14-2008 10:38 AM by Tim Barcz

[Advertisement]

Comments

Jeremy D. Miller wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 11-14-2008 1:35 PM

Psst...

If you were using StructureMap you could just do this:

ObjectFactory.AssertConfigurationIsValid()

and get an error report of all missing configuration and/or build failures in the container.

just saying...

Tim Barcz wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 11-14-2008 1:45 PM

@Jeremy,

Are you saying that I wouldn't have to do the foreach garbage over the "GetAssignableHandlers" call?

If so, that's a nice feature.  But don't speak too loudly or Ayende will swoop in and add that feature to Windsor.

Blog post I'd be interested in seeing for you or someone pro StructureMap is an evaluation of why one would want to switch from their current IoC container (Windsor for me) to StructureMap.  The way I look at it is, unless there is a definite benefit, then it's change for the sake of change.

Don't get me wrong, I'm not saying anything is wrong with StructureMap, but I'm really not up to speed on it.   As a Windsor user I'd be curious to see what you, or SM user, see as the benefits over Windsor.

Possible blog post?

Chris Canal wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 11-15-2008 2:06 PM

You can get rid of the ForEach with Castle:

container.Register(AllTypes.FromAssembly(Assembly.Load("AssemblyName")).BasedOn<IController>());

:)

Tim Barcz wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 11-15-2008 2:17 PM

@Chris

Thanks for the tip....I definitely appreciate it.

Dew Drop - Weekend Edition - November 15-16, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - Weekend Edition - November 15-16, 2008 | Alvin Ashcraft's Morning Dew
on 11-15-2008 9:42 PM

Pingback from  Dew Drop - Weekend Edition - November 15-16, 2008 | Alvin Ashcraft's Morning Dew

ASP.NET MVC Archived Blog Posts, Page 1 wrote ASP.NET MVC Archived Blog Posts, Page 1
on 11-16-2008 11:31 PM

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

Reflective Perspective - Chris Alcock » The Morning Brew #224 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #224
on 11-17-2008 3:25 AM

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #224

social bookmarking link building wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 01-18-2013 4:33 PM

WjEeBg Enjoyed every bit of your article post.Really thank you! Much obliged.

loss weight pills wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 02-01-2013 6:19 PM

lXCJxF I loved your blog post. Will read on...

buy imitrex generic wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 02-15-2013 5:52 AM

npg3LA Looking forward to reading more. Great blog post.Thanks Again. Want more.

buy stendra online wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 02-23-2013 10:50 AM

VACwBv Wow, great blog article.Really thank you! Fantastic.

clomid no prescription wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 02-28-2013 10:02 AM

nk6nax Thanks for the article.Much thanks again. Really Cool.

buy clomid wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 02-28-2013 5:11 PM

PyLnwG Really appreciate you sharing this article.Really thank you! Awesome.

buy discount viagra online wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 03-02-2013 6:30 PM

RUZOCf This is one awesome article post.Thanks Again. Really Great.

bookmaring service wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 03-13-2013 3:14 PM

OT2GO0 Im obliged for the article post.Really thank you! Will read on...

social bookmarks wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 03-15-2013 6:40 AM

CIkxJi Very good article. Will read on...

bookmaring service wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 03-15-2013 10:37 AM

Ec7rYA Thanks so much for the blog article.Really looking forward to read more. Really Cool.

social bookmarking service wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 03-22-2013 3:28 PM

FNsYnV Im obliged for the post.Really looking forward to read more. Really Cool.

buy social bookmarks wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 03-24-2013 2:37 AM

7u0XuA Really enjoyed this post.Much thanks again. Really Cool.

social bookmarking service wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 03-25-2013 3:44 AM

rDChmm A big thank you for your blog post.Much thanks again. Want more.

grizzly bears wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 04-06-2013 8:59 AM

Thanks for sharing, this is a fantastic blog article.Really thank you! Want more.

nonsense wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 04-06-2013 9:41 AM

I cannot thank you enough for the article post. Want more.

Social bookmarks wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 04-19-2013 10:00 PM

1helNt Thanks for sharing, this is a fantastic blog article.Really thank you! Fantastic.

social bookmarking service wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 06-07-2013 1:17 AM

FXk0Pr This is one awesome blog article.Thanks Again. Really Cool.

free bookmarks wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 06-10-2013 11:29 AM

I6G3Cb Fantastic article post.Thanks Again. Awesome. http://crork.com/

social bookmarks wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 06-21-2013 12:25 AM

5Qnwrc This is one awesome article.Thanks Again. Awesome.

moldavian news wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 07-09-2013 1:25 AM

Tp9SqO Thanks again for the blog.Thanks Again. Cool.

super news wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 07-12-2013 12:03 AM

KSogi8 Thank you for your article.Thanks Again. Want more.

social bookmarks wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 07-28-2013 8:46 AM

tsWGzg Thank you ever so for you article. Really Cool.

greatest news wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 08-03-2013 9:19 PM

AMRIgf Thank you for your blog article.Really thank you! Awesome.

great link buildng wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 08-23-2013 12:10 AM

jTTiD4 Very neat article.Much thanks again. Awesome.

link building wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 09-07-2013 3:57 PM

WVbktq Im grateful for the blog article.Really thank you! Cool.

only for 5 dollars wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 09-13-2013 8:09 AM

shBAz5 A round of applause for your article post.Really thank you! Will read on...

awesome linkbuilding site wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 10-01-2013 12:43 AM

SXkdoR Very informative article.Really looking forward to read more. Awesome.

check out these guys! wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 10-15-2013 11:55 PM

zSWjqb Thanks for sharing, this is a fantastic article.Thanks Again. Great.

crorkz wrote re: Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests
on 08-06-2014 7:32 AM

mJaE06 Very neat blog.Really looking forward to read more. Fantastic.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)