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
Test Your Windsor Container Configuration

We use Windsor as our IoC container.  Currently we use xml as the method by which components are registered.  Having done both, I prefer the xml mapping to code registration because I can change something in the app on the fly if I need to. I know some of you out there will disagree with me on this but that's ok.

One of the annoyances with xml is that you have to type the namespaces and classes along with assembly names.  We've found there is friction when we refactor our classes, changing names, moving classes,  and adding namespaces in that Windsor bombs.  When using the xml for configuration you don't get compile time checking, so a successful build may not mean working code, which in my opinion is bad.

We've simply added the following test to our unit-test project.  It still doesn't fail when compiling, but fails instead when we run unit-tests, which is closer to compilation than run-time.

   1: [Test]
   2: public void Can_initiate_Windsor()
   3: {
   4:     IWindsorContainer container = new WindsorContainer("windsor.config.xml");
   5: }

That's it!  It's very straight forward because all it's trying to do is start up windsor, the same way your application would, any exception that is thrown fails the tests, which causes the build to fail.


Posted 08-27-2008 4:54 PM by Tim Barcz
Filed under:

[Advertisement]

Comments

Chris Martin wrote re: Test Your Windsor Container Configuration
on 08-27-2008 6:43 PM

Unless you use the exact same config file for your tests (which I find unlikely), this won't work 100%.

Tim Barcz wrote re: Test Your Windsor Container Configuration
on 08-27-2008 9:58 PM

@Chris

Very good point.  I should point out that the goal of this test is to test the windsor xml configuration for the application, NOT the windsor xml configration (if one exists) that drives the test project.

Matt Everson wrote re: Test Your Windsor Container Configuration
on 08-27-2008 10:59 PM

This test is great for testing XML syntax, but it won't tell you if a registered service cannot be instantiated because a constructor dependency is not found.

That's easy enough to fix by adding this:

foreach (IHandler handler in container.Kernel.GetAssignableHandlers(typeof(object)))

           {

               container.Resolve(handler.ComponentModel.Service);

           }

Patrick De Boeck wrote re: Test Your Windsor Container Configuration
on 08-28-2008 3:17 AM

Actually the following code tests your windsor container configuration.  It includes support for generics as Matt's solution doesn't:

foreach(IHandler handler in GetHandlersFor(typeof(object)))

           {

               if(handler is DefaultGenericHandler)

               {

                   Type[] genericArguments = handler

                       .ComponentModel

                       .Service

                       .GetGenericArguments();

                   foreach(Type genericArgument in genericArguments)

                   {

                       Type[] genericParameterConstraints =

                           genericArgument.GetGenericParameterConstraints();

                       foreach(Type genericParameterConstraint in genericParameterConstraints)

                       {

                           _windsorContainer.Resolve(

                               handler

                                   .ComponentModel

                                   .Service

                                   .MakeGenericType(genericParameterConstraint));

                       }

                   }

               }

               else

               {

                   _windsorContainer.Resolve(handler.ComponentModel.Service);

               }

           }

P.

Kyle Baley wrote re: Test Your Windsor Container Configuration
on 08-28-2008 10:22 AM

Bil Simser posted a couple of tips on this. The first is similar to yours but actually attempts to resolve things:

weblogs.asp.net/.../the-first-spec-you-should-write-when-using-castle.aspx

weblogs.asp.net/.../testing-castle-windsor-mappings-part-deux.aspx

Tim Barcz wrote re: Test Your Windsor Container Configuration
on 09-01-2008 2:32 PM

All,

I've expanded my test to also run through each of the components and see if they could be instantiated.

While the original test insulated us from our current pain points, the new additions are very helpful.  Thank you all for you comments.

Brian wrote re: Test Your Windsor Container Configuration
on 07-28-2009 12:57 PM

Here is a version that tests generic classes with no constraints and generic classes with multiple generic arguments (e.g. MyClass<T,R>)

       [Test]

       public void WindsorContainerMappingConfigurationShouldBeCorrect()

       {

           IServiceLocator locator = (new ServiceLocatorProvider()).GetServiceLocator(false);

           IoC.Load(locator);

           IWindsorContainer container = ((IContainerAccessor)locator).Container;

           foreach (IHandler handler in container.Kernel.GetAssignableHandlers(typeof(object)))

           {

               if (handler is DefaultGenericHandler)

               {

                   Type[] genericArguments = handler

                       .ComponentModel

                       .Service

                       .GetGenericArguments();

                   Type[][] genericArgumentConstraintMatrix = new Type[genericArguments.Length][];

                   for (int i = 0; i < genericArguments.Length; i++)

                   {

                       genericArgumentConstraintMatrix[i] = genericArguments[i].GetGenericParameterConstraints();

                   }

                   // This doesn't test every combination that can be instantiated,

                   // only the first constraint on every generic argument.

                   Type[] genericArgumentConstraints = new Type[genericArguments.Length];

                   for (int i = 0; i < genericArguments.Length; i++)

                   {

                       if (genericArgumentConstraintMatrix[i].Length > 0)

                       {

                           genericArgumentConstraints[i] = genericArgumentConstraintMatrix[i][0];

                       }

                       else

                       {

                           genericArgumentConstraints[i] = typeof (object);

                       }

                   }

                   container.Resolve(

                       handler

                           .ComponentModel

                           .Service

                           .MakeGenericType(genericArgumentConstraints));

               }

               else

               {

                   container.Resolve(handler.ComponentModel.Service);

               }

           }

       }

buy generic cialis wrote re: Test Your Windsor Container Configuration
on 01-26-2013 11:22 PM

oYnWlj A round of applause for your article.Thanks Again. Really Cool.

pills for lose weight wrote re: Test Your Windsor Container Configuration
on 02-02-2013 1:15 AM

kz0dcS Muchos Gracias for your article post.

buy discount viagra wrote re: Test Your Windsor Container Configuration
on 02-03-2013 4:00 AM

2gLJko Really enjoyed this article post.Really looking forward to read more. Great.

buy imitrex generic wrote re: Test Your Windsor Container Configuration
on 02-15-2013 7:51 AM

2ZMBqF Very informative blog.Really thank you! Want more.

buy generic cialis online wrote re: Test Your Windsor Container Configuration
on 02-24-2013 12:38 PM

bP9obs Hey, thanks for the blog article.Really thank you! Cool.

generic clomid wrote re: Test Your Windsor Container Configuration
on 02-27-2013 12:39 PM

6vHHxs Great blog article. Really Great.

buy discount viagra online wrote re: Test Your Windsor Container Configuration
on 03-03-2013 4:25 AM

S1zFY3 Thanks-a-mundo for the article post.Much thanks again. Fantastic.

bookmaring service wrote re: Test Your Windsor Container Configuration
on 03-13-2013 1:51 PM

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

social bookmarks wrote re: Test Your Windsor Container Configuration
on 03-15-2013 6:06 AM

Z2JRfY Really enjoyed this blog post.Really thank you! Keep writing.

social bookmarking service wrote re: Test Your Windsor Container Configuration
on 03-24-2013 11:16 AM

sPgJj5 I appreciate you sharing this blog.Really thank you! Fantastic.

Social bookmarks wrote re: Test Your Windsor Container Configuration
on 04-03-2013 1:20 PM

Qy6kQ0 Thanks for the blog.Thanks Again. Want more.

comedy wrote re: Test Your Windsor Container Configuration
on 04-05-2013 6:18 PM

Great blog post.Much thanks again. Will read on...

buy social bookmarks wrote re: Test Your Windsor Container Configuration
on 04-07-2013 9:54 PM

PFNJLN Fantastic post.Really thank you! Really Great.

social bookmarking service wrote re: Test Your Windsor Container Configuration
on 04-13-2013 11:36 PM

LdLYqx Muchos Gracias for your post.

buy social bookmarks wrote re: Test Your Windsor Container Configuration
on 04-28-2013 3:16 AM

mPPlFR Im obliged for the article.Thanks Again. Fantastic.

social bookmarking service wrote re: Test Your Windsor Container Configuration
on 06-07-2013 12:54 AM

BPQ8p5 Thank you ever so for you blog article.Thanks Again. Great.

cheap social bookmarks wrote re: Test Your Windsor Container Configuration
on 06-19-2013 8:25 AM

7ompws Thanks for sharing, this is a fantastic blog post.Much thanks again. Great.

good social bookmarks wrote re: Test Your Windsor Container Configuration
on 06-21-2013 5:17 PM

4YqgnX Wow, great post.Really looking forward to read more. Great.

great service wrote re: Test Your Windsor Container Configuration
on 07-04-2013 10:55 AM

QJSMSJ Great, thanks for sharing this blog article.Really thank you! Really Great.

moldavian news wrote re: Test Your Windsor Container Configuration
on 07-09-2013 12:06 PM

u75Q9n Thanks-a-mundo for the post.Really thank you! Keep writing.

news wrote re: Test Your Windsor Container Configuration
on 07-12-2013 11:01 PM

y3UL2n Im thankful for the blog.Much thanks again. Cool.

here wrote re: Test Your Windsor Container Configuration
on 07-24-2013 4:09 AM

Really informative article post.Really thank you! Much obliged.

buy cialis online cheap wrote re: Test Your Windsor Container Configuration
on 07-25-2013 6:09 AM

Hey, thanks for the blog.Much thanks again. Awesome.

news wrote re: Test Your Windsor Container Configuration
on 07-26-2013 11:25 AM

HPX0Ma Im grateful for the article post.Really thank you!

buy social bookmarks wrote re: Test Your Windsor Container Configuration
on 07-28-2013 2:14 PM

u1aWyA Im obliged for the blog post.Much thanks again. Fantastic.

best news of them all wrote re: Test Your Windsor Container Configuration
on 08-03-2013 6:26 AM

OfQD5r Muchos Gracias for your blog.Really looking forward to read more. Really Great.

best news wrote re: Test Your Windsor Container Configuration
on 08-04-2013 2:48 AM

VzG7Ik Thanks for sharing, this is a fantastic article. Keep writing.

awesome links for you wrote re: Test Your Windsor Container Configuration
on 08-19-2013 8:21 PM

wiVmDa I loved your post.Really looking forward to read more. Cool.

great link buildng wrote re: Test Your Windsor Container Configuration
on 08-22-2013 11:51 AM

2W3QGr I think this is a real great blog post.Thanks Again. Great.

buy viagra online cheap wrote re: Test Your Windsor Container Configuration
on 09-02-2013 8:28 AM

Very informative post.Thanks Again. Much obliged.

best link building wrote re: Test Your Windsor Container Configuration
on 09-03-2013 9:22 PM

agIEFt Muchos Gracias for your blog post.Much thanks again. Will read on...

the best seo service wrote re: Test Your Windsor Container Configuration
on 09-06-2013 11:40 PM

3uRGuG Great, thanks for sharing this article post.

learn more wrote re: Test Your Windsor Container Configuration
on 09-08-2013 6:28 AM

Im obliged for the post. Keep writing.

make money online wrote re: Test Your Windsor Container Configuration
on 09-13-2013 4:53 AM

DgGM2N Great, thanks for sharing this blog article.Really looking forward to read more.

awesome link building wrote re: Test Your Windsor Container Configuration
on 09-24-2013 4:14 PM

jzEsXE wow, awesome blog article.Really looking forward to read more. Great.

check out seo wrote re: Test Your Windsor Container Configuration
on 09-29-2013 1:53 PM

QVP1WM I truly appreciate this article post. Want more.

awesome linkbuilding site wrote re: Test Your Windsor Container Configuration
on 10-01-2013 1:18 AM

zXUEM4 Looking forward to reading more. Great blog.Much thanks again. Really Cool.

check out these guys! wrote re: Test Your Windsor Container Configuration
on 10-15-2013 8:27 PM

QMcTUY Enjoyed every bit of your article. Want more.

top seo guys wrote re: Test Your Windsor Container Configuration
on 10-25-2013 9:39 PM

L0ICiR Looking forward to reading more. Great article post.Really looking forward to read more. Really Great.

take a look at it! wrote re: Test Your Windsor Container Configuration
on 10-30-2013 11:13 PM

aTSASp Fantastic blog post. Will read on...

watch this wrote re: Test Your Windsor Container Configuration
on 11-17-2013 9:14 PM

40ENm6 Muchos Gracias for your blog post. Much obliged.

check this out wrote re: Test Your Windsor Container Configuration
on 11-20-2013 11:20 PM

7WKlme I really like and appreciate your article.Really looking forward to read more. Really Cool.

check this out wrote re: Test Your Windsor Container Configuration
on 12-14-2013 8:52 PM

cANXOj Very informative post.Really looking forward to read more.

awesome things! wrote re: Test Your Windsor Container Configuration
on 01-08-2014 7:40 PM

dGvNZa A big thank you for your article post.Really thank you! Want more.

check it out wrote re: Test Your Windsor Container Configuration
on 01-09-2014 7:22 PM

uGxj2w Very good blog article.Really looking forward to read more. Great.

nice site here wrote re: Test Your Windsor Container Configuration
on 01-15-2014 6:04 PM

gHNBss I truly appreciate this blog post.Much thanks again. Want more.

check it out wrote re: Test Your Windsor Container Configuration
on 01-22-2014 11:19 AM

p75U6l Wow, great blog post. Fantastic.

stunning seo guys wrote re: Test Your Windsor Container Configuration
on 02-28-2014 12:01 PM

eI5BsM I value the article.Thanks Again. Will read on...

smashing site wrote re: Test Your Windsor Container Configuration
on 03-13-2014 4:09 AM

WYCg9n Major thankies for the article.Thanks Again. Much obliged.

check it out wrote re: Test Your Windsor Container Configuration
on 03-22-2014 9:37 AM

POBvOx Enjoyed every bit of your article post.Really looking forward to read more. Great.

best prices wrote re: Test Your Windsor Container Configuration
on 03-25-2014 8:21 AM

HbJpqa Awesome article post.Much thanks again. Want more.

stunning service wrote re: Test Your Windsor Container Configuration
on 04-05-2014 6:07 AM

LB5L6E Really informative blog article.Thanks Again. Want more.

matt wrote re: Test Your Windsor Container Configuration
on 04-08-2014 2:55 PM

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)