<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jak Charlton - Insane World : C#</title><link>http://devlicio.us/blogs/casey/archive/tags/C_2300_/default.aspx</link><description>Tags: C#</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Combining NHibernate, NH Spatial and SQL Server 2008 Fulltext Queries</title><link>http://devlicio.us/blogs/casey/archive/2009/11/22/combining-nhibernate-nh-spatial-and-sql-server-2008-fulltext-queries.aspx</link><pubDate>Sun, 22 Nov 2009 22:42:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:54016</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=54016</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=54016</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/11/22/combining-nhibernate-nh-spatial-and-sql-server-2008-fulltext-queries.aspx#comments</comments><description>&lt;p&gt;Following &lt;a target="_blank" href="http://www.kevinwilliampang.com/"&gt;Kevin Pang&amp;rsquo;s&lt;/a&gt; request on &lt;a target="_blank" href="http://devlicio.us/blogs/casey/archive/2009/11/13/53319.aspx"&gt;my last post regarding my problems getting NHibernate, NH Spatial and SQL Server Fulltext queries to work&lt;/a&gt;, I am finally getting around to giving some tips and pointers, and the code that &lt;a target="_blank" href="http://blogs.imeta.co.uk/sstrong/Default.aspx"&gt;Steve Strong&lt;/a&gt; of &lt;a target="_blank" href="http://www.imeta.co.uk/"&gt;iMeta&lt;/a&gt; knocked up for me.&lt;/p&gt;
&lt;p&gt;I needed to get these things working together as I was writing a recruitment website, and they had two basic requirements, the ability to search in candidates CVs and job descriptions for keywords, and the ability to search for those things within a certain distance &amp;ndash; so an obvious search would be &amp;ldquo;find me all the C# developers within 10 miles of Central London&amp;rdquo;&lt;/p&gt;
&lt;h3&gt;Step One &amp;ndash; NHibernate&lt;/h3&gt;
&lt;p&gt;Firstly, I&amp;rsquo;m not dumb, and at the beginning of the project I had decided to use NHibernate for all data access. The site didn&amp;rsquo;t have much of a domain model, but what it did have was ideally suited to a relational DB and simple persistence and querying by an ORM like NHibernate. What it also had going in it&amp;rsquo;s favour was the 2nd level caching capabilities of NHibernate, which would significantly reduce the workload on a site that was expected to grow quickly (or at least the owners hoped it would)&lt;/p&gt;
&lt;p&gt;Win for NHibernate there &amp;ndash; the icing on the cake was the excellent NHProfiler which would prove to make development a breeze over other options, and which also proved to make my querying a lot smarter and more bug free than my first horrid attempts.&lt;/p&gt;
&lt;h3&gt;Step Two &amp;ndash; Spatial Searching&lt;/h3&gt;
&lt;p&gt;MS SQL Server 2008 introduced Spatial searching to it&amp;rsquo;s list of features &amp;ndash; in fact it&amp;rsquo;s not just for searching, and not just by distance either, it can do a host of clever geographical type calculations and querying.&lt;/p&gt;
&lt;p&gt;My first tests were done with &amp;ldquo;old fashioned&amp;rdquo; T-SQL and within a short while I had a reasonable distance search working, roughly speaking it looked like:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;SET @from = geography::STGeomFromText(&amp;#39;POINT(-0.094693 51.520223)&amp;#39;, 4326); -- EC2M (London&lt;/p&gt;
&lt;p&gt;SELECT * FROM JobSlot j      &lt;br /&gt;WHERE j.Location.STDistance(@from)&amp;lt;=(@distance * 1609.344)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;Step Three &amp;ndash; Fulltext Searching&lt;/h3&gt;
&lt;p&gt;Long ago, back in the age before the dawn of time, MS SQL Server had Fulltext searching, via the FREETEXT and CONTAINS syntax. Give it a keyword, or a list of them, and it will dig inside your text to find just what you want.&lt;/p&gt;
&lt;p&gt;To use it on the site I was writing, we needed to index the content of CVs (resumes to your foreigners!) and some of those were in docx format &amp;ndash; so my first step was to &lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?FamilyId=60C92A37-719C-4077-B5C6-CAC34F4227CC&amp;amp;displaylang=en"&gt;download the Microsoft Office 2007 iFilters&lt;/a&gt;. SQL Server uses the (now) age old technology of iFilters to extract the text from documents, and this filter pack allows it to look inside Office 2007 documents &amp;ndash; if you want to index any other weird document formats, or even your own document format, there is probably an iFilter available, or you can write your own. With the iFilters installed, SQL Server could now index all of my content just fine.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;SELECT *      &lt;br /&gt;FROM Candidate       &lt;br /&gt;WHERE FREETEXT(*, N&amp;#39;c#&amp;#39;)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;Combining It All&lt;/h3&gt;
&lt;p&gt;So far, so good. SQL Server can do pretty much all I need it to do, and combining those two queries is simplicity in TSQL, but as I was using NHibernate for all my data access, I wanted to try an get it all working there too.&lt;/p&gt;
&lt;p&gt;NHibernate has support for the Spatial features of SQL Server 2008, along with other spatial databases, via the NHSpatial stuff in the NH Contrib project at &lt;a href="http://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/"&gt;http://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A download and compile of this will have you making HQL queries to do similar things to my TSQL spatial query above, in fact, the HQL query is remarkably similar to the TSQL:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;select j from JobSlot j where NHSP.Distance(j.Loc, :point) &amp;lt;= (:distance * 1609.344)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Then I needed to get the Fulltext part working, after some chatting with Steve Strong, he came up with the following code:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WKTReader wtk = new WKTReader();    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IGeometry point = wtk.Read(&amp;quot;POINT( -0.123356 51.524142 )&amp;quot;);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; point.SRID = 4326; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string query =    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @&amp;quot;select j from JobSlot j     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from freetexttable(&amp;#39;JobSlot&amp;#39;, :keywords) ftt     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where NHSP.Distance(j.Loc, :point) &amp;lt;= (:distance * 1609.344)&amp;quot;; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var results = session.CreateQuery(query)    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SetParameter(&amp;quot;point&amp;quot;, point, SpatialDialect.GeometryTypeOf(session))     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SetParameter(&amp;quot;distance&amp;quot;, 1)     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SetParameter(&amp;quot;keywords&amp;quot;, &amp;quot;hello world&amp;quot;)     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SetFirstResult(5)     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SetMaxResults(10)     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .List();&lt;/p&gt;
&lt;p&gt;And to get this to work we need 2 more classes, though the FreeText class is more an example of how to create an ISqlFunction for NHibernate:&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;	public class MyDialect : MsSql2008GeographyDialect
	{
		public MyDialect()
		{
			RegisterFunction(&amp;quot;freetext&amp;quot;, new FreeText());
			RegisterFunction(&amp;quot;freetexttable&amp;quot;, new StandardSQLFunction(&amp;quot;freetexttable&amp;quot;, null));
			RegisterFunction(&amp;quot;contains&amp;quot;, new StandardSQLFunction(&amp;quot;contains&amp;quot;, null));
		}
	}

	public class FreeText : ISQLFunction
	{
		public bool HasArguments
		{
			get { return true; }
		}

		public bool HasParenthesesIfNoArguments
		{
			get { return true; }
		}

		public virtual SqlString Render(IList args, ISessionFactoryImplementor factory)
		{
			SqlStringBuilder builder = new SqlStringBuilder();
			builder.Add(&amp;quot;freetext(&amp;quot;);

			if (args.Count == 1)
			{
				builder.Add(&amp;quot;*, &amp;quot;);
				builder.AddObject(args[0]);
			}
			else
			{
				builder.AddObject(args[0]);
				builder.Add(&amp;quot;, &amp;quot;);
				builder.AddObject(args[1]);
			}

			builder.Add(&amp;quot;)&amp;quot;);

			return builder.ToSqlString();
		}

		public virtual IType ReturnType(IType columnType, IMapping mapping)
		{
			return null;
		}
	}&lt;/pre&gt;
&lt;h3&gt;Why I&amp;rsquo;m Not Using The Above Code&lt;/h3&gt;
&lt;p&gt;Yep, in my usual fashion I go through explaining all of that, just to say, I&amp;rsquo;m not doing it that way.&lt;/p&gt;
&lt;p&gt;Eventually, I went back to another approach, that of using TSQL dynamically constructed in my code. I did this for a number of reasons, but primarily as it was easier for me to tune the query and add more options and variants in old fashioned SQL than in HQL by adding more extensions on to NHibernate. It also left the code base in a much easier to follow and extend state for the developers who would be maintaining the project &amp;ndash; with the amount of investigation it took me to get this far, I figured extending it could be a nightmare for my successors.&lt;/p&gt;
&lt;p&gt;The benefits I got from NHibernate I still retained, as this is a search function it was returning DTOs anyway, not entities, and these were just as efficient to read with a DataReader as with NHibernate (in fact a lot easier). When one of the results from the search is selected, the item is retrieved by NHibernate and we are back in ORM land.&lt;/p&gt;
&lt;p&gt;It was also little effort to cache my SQL query results, so I also had all that NH was offering me in that respect too &amp;ndash; a simple hash of the query parameters allowed me to cache the results object.&lt;/p&gt;
&lt;p&gt;So there ya go, how to use Spatial and Fulltext with NHibernate, and why I didn&amp;rsquo;t do it! :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=54016" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://devlicio.us/blogs/casey/archive/tags/.NET/default.aspx">.NET</category><category domain="http://devlicio.us/blogs/casey/archive/tags/NHibernate/default.aspx">NHibernate</category></item><item><title>Nasty "Gotcha" with ASP.NET MVC and Views</title><link>http://devlicio.us/blogs/casey/archive/2008/09/04/nasty-quot-gotcha-quot-with-asp-net-mvc-and-views.aspx</link><pubDate>Thu, 04 Sep 2008 08:27:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:42184</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=42184</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=42184</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2008/09/04/nasty-quot-gotcha-quot-with-asp-net-mvc-and-views.aspx#comments</comments><description>&lt;p&gt;We just spent far too long on what turned out to&amp;nbsp;be a stupid problem ... we kept getting an error telling us that the view did not inherit from System.Web.UI.Page ... unfortunately the guy who was working on the particular view and controller could not think of a specific change he had made to cause this error, and looking at the code didn&amp;#39;t reveal one either.&lt;/p&gt;
&lt;p&gt;The problem was actually quite subtle, and quite stupid ... &lt;/p&gt;
&lt;p&gt;A short while before the developer had decided to change the view from being an &amp;quot;.aspx&amp;quot; to being a &amp;quot;.ascx&amp;quot;, so removed the view from the solution, added a new view that was a UserControl, and carried on working.&amp;nbsp; What he hadn&amp;#39;t done was to actually delete the .aspx file, and even if he had done that, the other developers would have got this weird error too unless they pulled a clean copy of the solution, or manually deleted the files from their disk. &lt;em&gt;(clarification: El Guapo pointed out I was being ambiguous here ... if the file is *deleted* from the *solution* then it will go from disk and from SCM, if if is removed from the solution and then deleted it will remain in other developer&amp;#39;s directories *if* a VS addin like Ankh is being used, as the Update will operate against the solution and referenced files, not the folder structure)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When ASP.NET MVC is told to render a view, by using &lt;em&gt;RenderPartial(&amp;quot;MyView&amp;quot;)&lt;/em&gt; or &lt;em&gt;return (&amp;quot;MyView&amp;quot;)&lt;/em&gt; for example, it goes off merrily and scans the file system for a file matching &amp;quot;MyView.ascx&amp;quot; or &amp;quot;MyView.aspx&amp;quot; (presuming the default ViewEngine) - however if the view exists on disk in both forms, it is a bit of luck as to which actually gets returned and is rendered. Removing the old file fromn the solution is not enough, it still exists on disk - delete the old one and all is well. Hopefully this is a fairly infrequent problem, but it was&amp;nbsp;a real pain to figure out.&lt;/p&gt;
&lt;p&gt;So if you get an odd problem in ASP.NET MVC relating to Views ... just make sure you haven&amp;#39;t got old files in your directory structure!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=42184" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://devlicio.us/blogs/casey/archive/tags/.NET/default.aspx">.NET</category><category domain="http://devlicio.us/blogs/casey/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item><item><title>Logging with Castle Windsor, the Logging Facility and log4net</title><link>http://devlicio.us/blogs/casey/archive/2008/06/18/logging-with-castle-windsor-the-logging-facility-and-log4net.aspx</link><pubDate>Wed, 18 Jun 2008 18:58:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:41018</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=41018</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=41018</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2008/06/18/logging-with-castle-windsor-the-logging-facility-and-log4net.aspx#comments</comments><description>&lt;p&gt;A discussion on the altdotnet list just came up around logging. After some various suggestions, I asked what was wrong with using optional&amp;nbsp;dependencies and&amp;nbsp;the built in logging facility in &lt;a class="" href="http://www.castleproject.org/container/index.html"&gt;Castle Windsor&lt;/a&gt;. It&amp;nbsp;seemed the most obvious answer &lt;/p&gt;
&lt;p&gt;Of course, I completely forgot that when I first tried to figure this one out, it had me a little stumped and I took a while to find a good example of it. So here is how it happens.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Optional Dependencies in Windsor&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When you ask the Windsor container for an instance of a class, it goes off and merrily tries to resolve all of the dependencies your class has. Firstly the constructor parameters, Windsor must find a constructor that it can provide all of the dependencies for - constructor parameters represent non-optional dependencies, they must be able to be fulfilled or Windsor will not give you an instance of the class back. You can have multiple constructors of course, and in this case Windsor will try and&amp;nbsp;find the best match it can.&lt;/p&gt;
&lt;p&gt;This lead to one of the early suggestions on the thread, putting the instance of the Logger as a parameter on the class constructor. To me this is a horrible idea, logging, along with things like security and&amp;nbsp;auditing are cross cutting concerns, the class really shouldn&amp;#39;t have to care about them - their behaviour lives elsewhere.&lt;/p&gt;
&lt;p&gt;But Windsor has a really simple solution to this - optional dependencies. While Windsor must be able to fulfill at least one constructor signature, it will also try and resolve any public properties it can find on the class. These public properties may or may not be set by Windsor, if it has a suitable match for a property it will set it, otherwise it will ignore it.&lt;/p&gt;
&lt;p&gt;So the first part of the solution to the problem is to put a public property on the class with the logger interface - now Windsor will populate this property if it has a match in the container.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Logging Facility&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Next is the magic bit, Castle already has simple integration for logging in the form of the &lt;a class="" href="http://www.castleproject.org/container/facilities/v1rc3/logging/index.html"&gt;Logging Facility&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;While you could just put your own class, that implemented your own logging, and passed this onto log4net, by using the Castle faciltiy you save a lot of messing around, and get a great deal of flexibility. The Castle facility supports:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;log4net (requires Castle.Services.Logging.Log4netIntegration.dll) 
&lt;li&gt;NLog (requires Castle.Services.Logging.NLogIntegration.dll) 
&lt;li&gt;ConsoleLogger 
&lt;li&gt;DiagnosticsLogger 
&lt;li&gt;StreamLogger 
&lt;li&gt;WebLogger (TraceContext) 
&lt;li&gt;NullLogger (used as placeholder) &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Of course in the thread, log4net was the target of choice, but Castle lets you switch this simply, and maintain a single interface.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How To Tie it Together&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;1) Any class you are going to resolve from the Windsor container that you want to have logging on, add a public property of ILogger:&lt;/p&gt;
&lt;p&gt;
&lt;style&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/style&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyClass
{
   &lt;span class="kwrd"&gt;private&lt;/span&gt; ILogger logger;

   &lt;span class="kwrd"&gt;public&lt;/span&gt; ILogger Logger
   {
      get
      {
         &lt;span class="kwrd"&gt;if&lt;/span&gt; (logger == &lt;span class="kwrd"&gt;null&lt;/span&gt;) logger = NullLogger.Instance;
         &lt;span class="kwrd"&gt;return&lt;/span&gt; logger;
      }
      set { logger = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }
   }
}&lt;/pre&gt;
&lt;p&gt;A key thing here is that we use the NullLogger as the default value - if the facility wasn&amp;#39;t put into the container, or if the class was not instantiated&amp;nbsp;via Windsor, then this will mean that&amp;nbsp;logging just doesn&amp;#39;t happen, rather than it creating an exception. The NullLogger just swallows the message and carries on.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;2) Setup the Logging Facility in the Windsor container&lt;/p&gt;
&lt;p&gt;In the XML configuration:&lt;/p&gt;
&lt;p&gt;
&lt;style&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/style&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; 
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;facility&lt;/span&gt; 
    &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;logging&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;loggingApi&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;null|console|diagnostics|web|nlog|log4net|custom&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;customLoggerFactory&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;type name that implements ILoggerFactory&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;configFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;optional config file location&amp;quot;&lt;/span&gt;    &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Or in the code intialization:&lt;/p&gt;
&lt;p&gt;
&lt;style&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/style&gt;
&lt;pre class="csharpcode"&gt;   container.AddFacility(
     &lt;span class="str"&gt;&amp;quot;logging&amp;quot;&lt;/span&gt;, 
     &lt;span class="kwrd"&gt;new&lt;/span&gt; LoggingFacility(LoggerImplementation.Console)); &lt;/pre&gt;
&lt;p&gt;3) Use the Logger instance from the class:&lt;/p&gt;
&lt;p&gt;
&lt;style&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/style&gt;
&lt;pre class="csharpcode"&gt;   Logger.Debug(&lt;span class="str"&gt;&amp;quot;Null shipment returned&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;That is all there is to it, any object that is resolved via Windsor will now be inspected for ILogger, and if found it will get an instance of the chosen logger. A simple solution, and a perfect example of why an IoC container can simplify your code.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=41018" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Sample+Code/default.aspx">Sample Code</category><category domain="http://devlicio.us/blogs/casey/archive/tags/.NET/default.aspx">.NET</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Windsor/default.aspx">Windsor</category></item><item><title>Always One Step Ahead - Unit Test for Missing Attributes</title><link>http://devlicio.us/blogs/casey/archive/2008/05/06/always-one-step-ahead-unit-test-for-missing-attributes.aspx</link><pubDate>Tue, 06 May 2008 08:38:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40349</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=40349</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=40349</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2008/05/06/always-one-step-ahead-unit-test-for-missing-attributes.aspx#comments</comments><description>&lt;p&gt;&lt;a class="" href="http://www.ayende.com/Blog/"&gt;Oren&lt;/a&gt; always amazes me - I think he is psychic!&amp;nbsp; Whenever I have a problem, I almost always find Oren blogged about it a few days before - and today&amp;#39;s bugbear is a perfect example.&lt;/p&gt;
&lt;p&gt;I started the day off with a really weird timeout exception in WCF. The code was just fine apparently, and I spent a very long time debugging code I was pretty sure was working. The error (in typical framework style) was not able to be caught, debugged, or otherwise detected - it just &amp;quot;happened&amp;quot; somewhere in the WCF call.&lt;/p&gt;
&lt;p&gt;To cut a long story short ... it turned out an attribute was missing on a DTO class, and instead of WCF giving me a meaningful error, it just gave me a worthless message. One addition of [DataContract] later and all was working fine again.&lt;/p&gt;
&lt;p&gt;So, it just happens that &lt;a class="" href="http://www.ayende.com/Blog/"&gt;Oren&lt;/a&gt; (in association with &lt;a class="" href="http://blogs.msdn.com/gblock/"&gt;Glenn&lt;/a&gt;) had &lt;a class="" href="http://www.ayende.com/Blog/archive/2008/05/05/Actively-enforce-your-conventions.aspx"&gt;done something similar&lt;/a&gt;, and so taking a leaf out of their book, here is a really basic unit test to check that the DTO classes have the right attributes. Some discussion on Oren&amp;#39;s blog discussed whether unit tests were the right place for this kind of checking (as it is pretty much static code analysis), and in some ways I can see their point - however, frankly, a unit test is just as valid a way of testing the same thing, fits nicely into CI, and is in a common format to all the other checks.&lt;/p&gt;
&lt;style&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/style&gt;
&lt;pre class="csharpcode"&gt;[Test]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Ensure_DTO_classes_have_WCF_attributes()
{
  &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var type &lt;span class="kwrd"&gt;in&lt;/span&gt; &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(ClassInDTOAssembly).Assembly.GetTypes())
  {
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (type.IsInterface || type.IsAbstract)
        &lt;span class="kwrd"&gt;continue&lt;/span&gt;;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (type.Namespace.StartsWith(&lt;span class="str"&gt;&amp;quot;Interfaces.DTO&amp;quot;&lt;/span&gt;))
    {
       var attributes = type.GetCustomAttributes(&lt;br /&gt;             &lt;span class="kwrd"&gt;typeof&lt;/span&gt; (DataContractAttribute), &lt;span class="kwrd"&gt;true&lt;/span&gt;);
       &lt;span class="kwrd"&gt;if&lt;/span&gt; (attributes.Length == 0)
           Assert.Fail(&lt;span class="str"&gt;&amp;quot;Unable to identify [DataContract] attribute on type &amp;quot;&lt;/span&gt; &lt;br /&gt;                  + type.Name + &lt;span class="str"&gt;&amp;quot; in DTO namespace&amp;quot;&lt;/span&gt;);
       &lt;span class="kwrd"&gt;else&lt;/span&gt;
          &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var memberInfo &lt;span class="kwrd"&gt;in&lt;/span&gt; type.GetMembers())
            {
              &lt;span class="kwrd"&gt;if&lt;/span&gt; (memberInfo != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
              {
                 var memberAttributes = &lt;br /&gt;                   memberInfo.GetCustomAttributes(&lt;span class="kwrd"&gt;typeof&lt;/span&gt; (DataMemberAttribute), &lt;span class="kwrd"&gt;true&lt;/span&gt;);
                 &lt;span class="kwrd"&gt;if&lt;/span&gt; (memberAttributes.Length == 0)
                   Assert.Fail(&lt;span class="str"&gt;&amp;quot;Unable to identify [DataMember] attribute on member &amp;quot;&lt;/span&gt; &lt;br /&gt;                       + memberInfo.Name + &lt;span class="str"&gt;&amp;quot; on type &amp;quot;&lt;br /&gt;                       &lt;/span&gt;+ type.Name + &lt;span class="str"&gt;&amp;quot; in DTO namespace&amp;quot;&lt;/span&gt;);
              }
           }
        }
    }
}&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40349" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/TDD/default.aspx">TDD</category><category domain="http://devlicio.us/blogs/casey/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Sample+Code/default.aspx">Sample Code</category><category domain="http://devlicio.us/blogs/casey/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Switching to a Different User Inside a Unit Test</title><link>http://devlicio.us/blogs/casey/archive/2008/04/15/switching-to-a-different-user-inside-a-unit-test.aspx</link><pubDate>Tue, 15 Apr 2008 17:51:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40289</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=40289</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=40289</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2008/04/15/switching-to-a-different-user-inside-a-unit-test.aspx#comments</comments><description>&lt;p&gt;Update: Re-published from old blog&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I found a need yesterday to test some code in particular security contexts. After some (as always) prompt guidance from the &lt;a href="http://groups.yahoo.com/group/altdotnet/"&gt;altdotnet&lt;/a&gt; mailing list, I had the (as it turns out) all too simple solution. &lt;a href="http://www.codeplex.com/xunit/SourceControl/DirectoryView.aspx?SourcePath=%24%2fxunit%2fMain%2ftest.xunitext%2fAssumeIdentity&amp;amp;changeSetId=16734"&gt;xUnit&lt;/a&gt; includes an attribute for this, but as I&amp;#39;m using NUnit and just wanted a clean way to do this without writing an extension, I figured I would just play around with it a bit. &lt;/p&gt;
&lt;p&gt;One quick bit of refactoring later, and we have a class to handle this for us:&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt; 1: &lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SecurityContextSwitcher : IDisposable&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 2: &lt;/span&gt; {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt; 3: &lt;/span&gt; &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; IPrincipal originalPrincipal;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 4: &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt; 5: &lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; SecurityContextSwitcher(&lt;span class="kwrd"&gt;string&lt;/span&gt; username, &lt;span class="kwrd"&gt;string&lt;/span&gt;[] roles)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 6: &lt;/span&gt; {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt; 7: &lt;/span&gt; originalPrincipal = Thread.CurrentPrincipal;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 8: &lt;/span&gt; var identity = &lt;span class="kwrd"&gt;new&lt;/span&gt; GenericIdentity(username);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt; 9: &lt;/span&gt; var principal = &lt;span class="kwrd"&gt;new&lt;/span&gt; GenericPrincipal(identity, roles);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 10: &lt;/span&gt; Thread.CurrentPrincipal = principal;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt; 11: &lt;/span&gt; }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 12: &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt; 13: &lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Dispose()&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 14: &lt;/span&gt; {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt; 15: &lt;/span&gt; Thread.CurrentPrincipal = originalPrincipal;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 16: &lt;/span&gt; }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt; 17: &lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;
&lt;style&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;This class allows us to test quite simply with the following syntax:&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 1: &lt;/span&gt; [Test]&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 2: &lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Handler_will_allow_access_to_anyone_who_is_in_Users_role()&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 3: &lt;/span&gt; {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 4: &lt;/span&gt; &lt;span class="kwrd"&gt;using&lt;/span&gt; (&lt;span class="kwrd"&gt;new&lt;/span&gt; SecurityContextSwitcher(&lt;span class="str"&gt;&amp;quot;Test&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt;[] {&lt;span class="str"&gt;&amp;quot;Users&amp;quot;&lt;/span&gt;}))&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 5: &lt;/span&gt; {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 6: &lt;/span&gt; var request = &lt;span class="kwrd"&gt;new&lt;/span&gt; GetRequest {Id = &lt;span class="kwrd"&gt;new&lt;/span&gt; Guid()};&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 7: &lt;/span&gt; handlerUnderTest.Handle(request);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 8: &lt;/span&gt; }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 9: &lt;/span&gt; }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 10: &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 11: &lt;/span&gt; [PrincipalPermissionAttribute(SecurityAction.Demand, Role = &lt;span class="str"&gt;&amp;quot;Users&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 12: &lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Handle(GetRequest message)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 13: &lt;/span&gt; {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 14: &lt;/span&gt; ...&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 15: &lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;
&lt;style&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40289" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/TDD/default.aspx">TDD</category><category domain="http://devlicio.us/blogs/casey/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Sample+Code/default.aspx">Sample Code</category><category domain="http://devlicio.us/blogs/casey/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Windsor Container Registration of Generic Component via C# Code</title><link>http://devlicio.us/blogs/casey/archive/2008/04/13/windsor-container-registration-of-generic-component-via-c-code.aspx</link><pubDate>Sun, 13 Apr 2008 17:56:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40290</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=40290</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=40290</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2008/04/13/windsor-container-registration-of-generic-component-via-c-code.aspx#comments</comments><description>&lt;p&gt;Update: Re-published from old blog&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve done it before, but my mind went totally blank yesterday ... due to some typically quick responses from the &lt;a href="http://groups.google.com/group/castle-project-users?hl=en"&gt;Castle Users Google Group&lt;/a&gt; ... here is the code to do it for future generations to avoid my same silly mistakes ... &lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 1: &lt;/span&gt;WindsorContainer container = &lt;span class="kwrd"&gt;new&lt;/span&gt; WindsorContainer();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 2: &lt;/span&gt;Type eventBrokerServiceType = &lt;span class="kwrd"&gt;typeof&lt;/span&gt; (IEventBroker&amp;lt;&amp;gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 3: &lt;/span&gt;Type brokerType = &lt;span class="kwrd"&gt;typeof&lt;/span&gt; (EventBroker&amp;lt;&amp;gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 4: &lt;/span&gt;container.AddComponent(&lt;span class="str"&gt;&amp;quot;key&amp;quot;&lt;/span&gt;, eventBrokerServiceType, brokerType);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 5: &lt;/span&gt;IEventBroker&amp;lt;EventArgs&amp;gt; eventBroker = &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 6: &lt;/span&gt; container.Resolve&amp;lt;IEventBroker&amp;lt;EventArgs&amp;gt;&amp;gt;();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 7: &lt;/span&gt;Assert.IsNotNull(eventBroker);&lt;/pre&gt;&lt;/div&gt;
&lt;style&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40290" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Agile/default.aspx">Agile</category><category domain="http://devlicio.us/blogs/casey/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Sample+Code/default.aspx">Sample Code</category><category domain="http://devlicio.us/blogs/casey/archive/tags/.NET/default.aspx">.NET</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Windsor/default.aspx">Windsor</category></item></channel></rss>