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 Config Parameters With Non-Primitive Types

Windsor will stitch together objects for you at runtime based on what components have been registered.  When you ask for an instance of an object from the Windsor/MicroKernel it will return the object using the constructor it can satisfy.

I ran into a problem the other day when I wanted to create an object that accepted three types, the third was a non-primitive that I didn't control.  Castle's website goes into some detail on this in their documentation on Windsor/MicroKernel:

   1: <configuration>
   2:  
   3:     <components>
   4:         <component id="smtp.sender" 
   5:             service="Namespace.IEmailSender, AssemblyName"
   6:             type="Namespace.SmtpMailSender, AssemblyName">
   7:         
   8:             <parameters>
   9:                 <port>10</port>
  10:                 <host>smtphost</host>
  11:             </parameters>
  12:         
  13:         </component>
  14:     </components>
  15:  
  16: </configuration>

the problem I was running into is what if you have a third parameter, which isn't a primitive?  I have the following signature:

   1: public CommunicationGateway(string host, int port, Encoding encoding)
   2: {
   3:     this.host = host;
   4:     this.port = port;
   5:     this.encoding = encoding;
   6: }

The example above fits quite nicely until I hit the third parameter.  How do I define what that third parameter should be in Windsor given that it's not a class that I control nor a primitive type? This felt like it should be easy, but I could not find the information I needed on the Castle website.  I got some help from Castle committer Dru Sellers.  In the end Dru pointed me to a very simple solution; register the type and pass it in using the service lookup notation, ${}

   1: <component id="CommunicationGateway"
   2:            service="ABCCompany.ICommunicationGateway, ABCCompany"
   3:            type="ABCCompany.CommunicationGateway, ABCCompany">
   4:     <parameters>
   5:         <host>#{Host}</host>
   6:         <port>#{Port}</port>
   7:         <encoding>${Encoding}</encoding>
   8:     </parameters>
   9: </component>
  10:  
  11: <component id="Encoding"
  12:            service="System.Text.Encoding, mscorlib"
  13:            type="System.Text.UTF8Encoding, mscorlib">
  14: </component>

You can see from above that I'm registering a component with the service System.Text.Encoding and supplying UTF8Encoding as the implementation.  Now I can use a strongly-typed System.Text.Encoding object and have it configurable in my Windsor Configuration, much more elegant than using a string (primitive-type) and hoping it works. 

Thanks Dru!


Posted 09-18-2008 1:02 PM by Tim Barcz
Filed under: ,

[Advertisement]

Comments

Reflective Perspective - Chris Alcock » The Morning Brew #183 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #183
on 09-19-2008 3:16 AM

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

IHateSpaghetti {code} wrote Blog Carnival #5
on 09-29-2008 8:04 AM

After a couple of weeks of crappy Internet connections I finally managed to connect via my cell phone

Websites tagged "primitive" on Postsaver wrote Websites tagged "primitive" on Postsaver
on 02-18-2009 7:17 PM

Pingback from  Websites tagged "primitive" on Postsaver

devlicio.us wrote re: Windsor Config Parameters With Non-Primitive Types
on 05-15-2011 5:13 PM

Windsor config parameters with non primitive types.. WTF? :)

devlicio.us wrote re: Windsor Config Parameters With Non-Primitive Types
on 06-03-2011 5:29 AM

Windsor config parameters with non primitive types.. OMG! :)

devlicio.us wrote re: Windsor Config Parameters With Non-Primitive Types
on 06-05-2011 5:46 AM

Windsor config parameters with non primitive types.. Amazing :)

devlicio.us wrote re: Windsor Config Parameters With Non-Primitive Types
on 06-30-2011 4:34 AM

Windsor config parameters with non primitive types.. Great idea :)

l48q pon videos qa7g wrote re: Windsor Config Parameters With Non-Primitive Types
on 07-02-2011 11:36 AM

Windsor config parameters with non primitive types.. Nice :)

masi pon videos cspt wrote re: Windsor Config Parameters With Non-Primitive Types
on 07-03-2011 12:51 AM

Windsor config parameters with non primitive types.. Huh, really? :)

Jim Schubert wrote re: Windsor Config Parameters With Non-Primitive Types
on 06-28-2012 7:50 AM

Tim, I know this post is years old but I wanted to say thanks.  I've used Windsor in the past, but I've never set up the XML configuration from scratch.  I read the documentation and I didn't see where this was mentioned, but your post worked like a charm!

social bookmarking submissions wrote re: Windsor Config Parameters With Non-Primitive Types
on 01-17-2013 9:30 AM

Ztsk2O Thank you ever so for you blog article.Much thanks again. Really Great.

http://clomidnoprescription.beep.com/ wrote re: Windsor Config Parameters With Non-Primitive Types
on 02-28-2013 2:33 AM

dplVgF I really enjoy the article post.Really thank you! Great.

buy social bookmarks wrote re: Windsor Config Parameters With Non-Primitive Types
on 03-23-2013 3:01 AM

UsMCUa A big thank you for your blog.Much thanks again. Will read on...

Social bookmarks wrote re: Windsor Config Parameters With Non-Primitive Types
on 04-19-2013 4:21 PM

TOMUkV Thanks for the blog article.Much thanks again. Keep writing.

buy social bookmarks wrote re: Windsor Config Parameters With Non-Primitive Types
on 04-28-2013 12:04 AM

acDeph I cannot thank you enough for the blog article.Much thanks again. Really Cool.

digital camera guide wrote re: Windsor Config Parameters With Non-Primitive Types
on 05-14-2013 8:53 AM

pe1ZO1 I loved your blog article.Thanks Again. Cool.

cheap social bookmarks wrote re: Windsor Config Parameters With Non-Primitive Types
on 06-19-2013 10:41 AM

fMznxC Really appreciate you sharing this blog article.Much thanks again. Fantastic.

this site wrote re: Windsor Config Parameters With Non-Primitive Types
on 07-23-2013 8:04 PM

I am so grateful for your article post.Much thanks again.

visit this site wrote re: Windsor Config Parameters With Non-Primitive Types
on 07-24-2013 9:56 PM

Appreciate you sharing, great blog post.Thanks Again. Fantastic.

best news on earth wrote re: Windsor Config Parameters With Non-Primitive Types
on 07-26-2013 4:51 AM

jQWmuT This is one awesome post.Much thanks again. Want more.

greatest news wrote re: Windsor Config Parameters With Non-Primitive Types
on 08-02-2013 11:23 PM

50LhUk Looking forward to reading more. Great blog.Thanks Again. Really Cool.

best news of them all wrote re: Windsor Config Parameters With Non-Primitive Types
on 08-04-2013 6:46 PM

kGcilC Im grateful for the article. Cool.

great link buildng wrote re: Windsor Config Parameters With Non-Primitive Types
on 08-19-2013 9:11 AM

TTrLPZ Enjoyed every bit of your blog post.Really thank you! Really Great.

good seo guys wrote re: Windsor Config Parameters With Non-Primitive Types
on 09-03-2013 8:16 PM

BbnwdJ Thanks so much for the blog post.Thanks Again. Great.

online business wrote re: Windsor Config Parameters With Non-Primitive Types
on 09-11-2013 5:46 PM

CkXlmv Great, thanks for sharing this blog post.Thanks Again. Awesome.

cheap link building wrote re: Windsor Config Parameters With Non-Primitive Types
on 09-24-2013 6:31 AM

TdpY1S Major thankies for the article post.Much thanks again. Much obliged.

awesome linkbuilding site wrote re: Windsor Config Parameters With Non-Primitive Types
on 09-30-2013 6:51 PM

bBFA61 wow, awesome post.Really thank you! Really Cool.

best link build wrote re: Windsor Config Parameters With Non-Primitive Types
on 10-16-2013 8:17 AM

rSQ8NF I cannot thank you enough for the blog.Much thanks again. Will read on...

take a look at it! wrote re: Windsor Config Parameters With Non-Primitive Types
on 10-31-2013 3:09 AM

5vZwgp I really liked your blog article.Thanks Again. Cool.

crorkservice wrote re: Windsor Config Parameters With Non-Primitive Types
on 07-18-2014 4:06 PM

8zlXQ4 wow, awesome article post. Much obliged.

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)