<?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 : Castle</title><link>http://devlicio.us/blogs/casey/archive/tags/Castle/default.aspx</link><description>Tags: Castle</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>ASP.NET MVC in the Wild</title><link>http://devlicio.us/blogs/casey/archive/2010/02/09/asp-net-mvc-in-the-wild.aspx</link><pubDate>Tue, 09 Feb 2010 11:02:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:55322</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=55322</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=55322</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2010/02/09/asp-net-mvc-in-the-wild.aspx#comments</comments><description>&lt;p&gt;Just before I emigrated to Australia, I took on a small contract to build a website for a UK company who wanted to start up a new kind of UK recruitment site, one where employers could advertise directly, and more specifically one where recruitment agencies couldn&amp;rsquo;t.&lt;/p&gt;
&lt;p&gt;The result was Empty Lemon (&lt;a href="http://www.emptylemon.co.uk"&gt;www.emptylemon.co.uk&lt;/a&gt;) &amp;ndash; I leave you to figure out why it is called Empty Lemon, took me a fair while to be honest!&lt;/p&gt;
&lt;p&gt;This site was built in around 4 weeks, using ASP.NET MVC2, NHibernate, Fluent NHibernate, Windsor, JQuery, Automapper, and a host of other great tools and libraries.&lt;/p&gt;
&lt;p&gt;The site is actually, despite the occasional bit of code I&amp;rsquo;m not too delighted with, a great example of how fast a site can be developed with ASP.NET MVC and the chosen tooling. In just four weeks we went from more or less nothing, to a full domain model (all be it a fairly simple one) running over a SQL Server 2008 database, using a combination of NH, Fulltext search and the Spatial services from SQL Server 2008.&lt;/p&gt;
&lt;p&gt;I also want to thank James Gregory, Steve Strong and Hadi Hariri for (sometimes unknowingly) helping me write the site, with useful snippets of code, and even in Steve&amp;rsquo;s case a lot of work on getting NH to play fair with the spatial services.&lt;/p&gt;
&lt;p&gt;So, if you are looking for a job in the UK, or if you are looking to employ someone, I strongly suggest you take a look at &lt;a href="http://www.emptylemon.co.uk"&gt;www.emptylemon.co.uk&lt;/a&gt;. It&amp;rsquo;s a little empty right now, but some large employers are coming on board soon, and other companies are signing up day by day &amp;ndash; these things are always chicken and egg, so if you like the site, feel free to tell others about it &amp;ndash; after all, who really likes recruitment agents anyway ;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=55322" width="1" height="1"&gt;</description><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><category domain="http://devlicio.us/blogs/casey/archive/tags/JQuery/default.aspx">JQuery</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Castle/default.aspx">Castle</category><category domain="http://devlicio.us/blogs/casey/archive/tags/NHibernate/default.aspx">NHibernate</category></item><item><title>What Is The Difference Between an IoC Container and MEF?</title><link>http://devlicio.us/blogs/casey/archive/2009/12/18/what-is-the-difference-between-an-ioc-container-and-mef.aspx</link><pubDate>Fri, 18 Dec 2009 06:50:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:54625</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=54625</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=54625</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/12/18/what-is-the-difference-between-an-ioc-container-and-mef.aspx#comments</comments><description>&lt;p&gt;Today a conversation sparked off on Twitter, started by &lt;a target="_blank" href="http://www.twitter.com/jbogard"&gt;Jimmy Bogard&lt;/a&gt; and &lt;a target="_blank" href="http://www.twitter.com/mhinze"&gt;Matt Hinze&lt;/a&gt;, and then carried on by myself and &lt;a target="_blank" href="http://www.twitter.com/gblock"&gt;Glenn Block&lt;/a&gt;. The basic starting point was what the difference was between using an IoC container like &lt;a target="_blank" href="http://www.castleproject.org/container/index.html"&gt;Windsor&lt;/a&gt; or &lt;a target="_blank" href="http://structuremap.sourceforge.net/Default.htm"&gt;StructureMap&lt;/a&gt; and using MEF (the &lt;a target="_blank" href="http://www.codeplex.com/MEF"&gt;Managed Extensibility Framework&lt;/a&gt;)   &lt;/p&gt;
&lt;p&gt;I started off by answering a question from Jimmy, he wanted the one paragraph sales pitch for MEF that didn&amp;rsquo;t include technical terminology &amp;hellip; I guess he was asking why MEF is a better option than one of the IoC containers we all love and hopefully all use religiously.&lt;/p&gt;
&lt;p&gt;My key answers were:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;MEF allows you to write your application as a series of much simpler mini applications, and have those brought together to create a much great whole&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;IoC container = service level components, MEF = Autonomous Components. A MEF &amp;quot;part&amp;quot; will probably use an IoC container&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This sparked some comments from Glenn where he wanted to clarify that sometimes an IoC container is the right choice, sometimes MEF is, and sometimes both are. Luckily we are both pragmatic, so not much to disagree with there. He pointed to two of his blogs on the subject, I recommend reading them highly, they will do a much better job than I could at explaining the detail of the decision points, &lt;a target="_blank" href="http://codebetter.com/blogs/glenn.block/archive/2009/08/16/should-i-use-mef-for-my-general-ioc-needs.aspx"&gt;Should I use MEF for my general IoC needs?&lt;/a&gt; and &lt;a target="_blank" href="http://codebetter.com/blogs/glenn.block/archive/2009/10/31/should-i-use-mef-with-an-ioc-container.aspx"&gt;Should I use MEF with an IoC container?&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;What&amp;rsquo;s Wrong With IoC Containers?&lt;/h3&gt;
&lt;p&gt;What an IoC container is very good at is low level control of your code, your dependency graphs, your lifestyle management, and gives you great low level ways to deal with facilities, factories, decorators and AOP. In just about any code base that goes past spike stage, I use an IoC container of some sort, it is just a fools errand to try and write and application without one these days. The keyword &amp;ldquo;new&amp;rdquo; is in my code smells book.&lt;/p&gt;
&lt;p&gt;However, what an IoC container is pretty poor at is enforcing boundaries. Sure you could use an IoC container to write much larger applications, but as Glenn rightly pointed out, the central configuration becomes a headache. In fact, the central registration can rapidly become the equivalent of using global variables, you never really know what is using what as everything has access to everything else.&lt;/p&gt;
&lt;p&gt;Most of the containers give you the capability to have child containers to try and deal with this situation, and while they do limit the global access issue, they are pretty hard to manage in their own right, and you can without some proper thought end up with a spaghetti mess of containers replacing your spaghetti mess of components.&lt;/p&gt;
&lt;h3&gt;An Architectural Solution To The Problem&lt;/h3&gt;
&lt;p&gt;To me, MEF is an architectural solution, while an IoC container is a code level solution. They both have their places in an application, but what MEF is exceptionally good at is creating process boundaries.&lt;/p&gt;
&lt;p&gt;One of the core underlying principles of good use of an IoC container is that you only ever reference a container at a process boundary &amp;ndash; in most applications that means it is probably referenced in your bootstrapping code inside your web application (maybe Global.ascx), or inside an HttpModule around your web services, but from that point on, your code should never reference the container again &amp;ndash; the magic of an IoC container comes from relinquishing all object construction and dependency graph resolution to it.&lt;/p&gt;
&lt;p&gt;But this means in a larger application, you may end up with massive containers with hundreds of registered components, and in reality most operations will only use a small handful at a time. &lt;/p&gt;
&lt;p&gt;In most larger systems you would naturally have parts of the system you want to devolve into discrete components, or Autonomous Components. Doing this with an IoC container starts to lead you down an architectural design geared around the limitations of the IoC container, and not necessarily driven by good architectural principles.&lt;/p&gt;
&lt;p&gt;What MEF allows you to do is to break the system apart easily, or as Glenn referred to it, MEF allows you to easily create composite applications. These composite &lt;strong&gt;parts&lt;/strong&gt; can operate as Autonomous Components, in almost total isolation from the core application, and therefore they provide a natural boundary without having to use things like web of WCF service layers just for that purpose. Oddly the MEF terminology that Jimmy wanted to avoid in his original question was &amp;ldquo;part&amp;rdquo; and yet it fits naturally in this context.&lt;/p&gt;
&lt;h3&gt;Russian Dolls&lt;/h3&gt;
&lt;p&gt;As I started off by saying, I would almost always end up using an IoC container in any application I was to write, even a fairly small scale one. If we look at these MEF parts as now being Autonomous Components, and therefore operating as mini applications, it now becomes apparent that they will themselves need a way to create service components and dependency graphs, and while MEF could in theory do a lot of that, it&amp;rsquo;s just not the best fit for the job.&lt;/p&gt;
&lt;p&gt;So it&amp;rsquo;s pretty certain that inside your MEF part you will be using yet another IoC container for object resolution, but now you don&amp;rsquo;t have a massive central registry, but one specifically geared to the MEF part in question.&lt;/p&gt;
&lt;p&gt;And this pattern can recurse if needed, each time providing a clean boundary within your application.&lt;/p&gt;
&lt;h3&gt;A Real World Example&lt;/h3&gt;
&lt;p&gt;The last &amp;ldquo;large scale&amp;rdquo; system I worked on where I had the opportunity to put some of these ideas into practice was an insurance portal. This portal was responsible for providing insurance quotations, but across a massively diverse and inconsistent set of products.&lt;/p&gt;
&lt;p&gt;The solution we came to in the end was a combination of all of the above principles &amp;ndash; we had a web application that held a Castle Windsor container in Global.ascx, that container was responsible for instantiating everything from controllers backwards, and all of the service components they used.&lt;/p&gt;
&lt;p&gt;But some of those controllers needed to call our product plugins, and in this case we actually used Castle Windsor to use MEF to instantiate the plugins (MEF parts). Within each product plugin we had another set of bootstrapping code that had it&amp;rsquo;s own instance of a Castle Windsor container, and it&amp;rsquo;s own registry specific to that plugin.&lt;/p&gt;
&lt;p&gt;The plugins were actually used by a number of points in the application, some in process, and some out of process, either on other servers or just behind service or service bus boundaries &amp;ndash; what MEF allowed us to do was create an independently deployable package that was self contained and self sufficient, without the need for managing masses of registration information &amp;ndash; trying to use an IoC container for this would have been a mini-nightmare, but with MEF it was almost a pleasure.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=54625" 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/.NET/default.aspx">.NET</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://devlicio.us/blogs/casey/archive/tags/MEF/default.aspx">MEF</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Castle/default.aspx">Castle</category></item><item><title>Frustration Friday - How NHibernate Can Waste Your Day</title><link>http://devlicio.us/blogs/casey/archive/2008/10/17/frustration-friday-how-nhibernate-can-waste-your-day.aspx</link><pubDate>Fri, 17 Oct 2008 21:11:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:42702</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=42702</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=42702</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2008/10/17/frustration-friday-how-nhibernate-can-waste-your-day.aspx#comments</comments><description>&lt;p&gt;Well, OK I exaggerate slightly, it wasn&amp;#39;t &lt;a class="" href="http://sourceforge.net/projects/nhibernate"&gt;NHibernate&lt;/a&gt; that wasted most of my day, but the fact that we are having to use a slightly out of date version of 2.0 to allow us to use &lt;a class="" href="http://sourceforge.net/projects/nhcontrib/"&gt;NHSearch&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;NHibernate Search is a great piece of code, it abstracts &lt;a class="" href="http://incubator.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; away from your application, and lets you pretty much pretend that you are dealing with NHibernate. However NHSearch has slipped a little in development behind the main NHibernate trunk.&lt;/p&gt;  &lt;p&gt;So earlier today, one of our devs got a problem with his seemingly correct query:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;var newsGroupTitles = &lt;span class="kwrd"&gt;new&lt;/span&gt;[] { &lt;span class="str"&gt;&amp;quot;Steel News&amp;quot;&lt;/span&gt; };

  &lt;br /&gt;var tagTitles = &lt;span class="kwrd"&gt;new&lt;/span&gt;[] { &lt;span class="str"&gt;&amp;quot;Asia&amp;quot;&lt;/span&gt; }; 

  &lt;br /&gt;var criteria = DetachedCriteria.For(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Article), &lt;span class="str"&gt;&amp;quot;article&amp;quot;&lt;/span&gt;)

  &lt;br /&gt;.CreateCriteria(&lt;span class="str"&gt;&amp;quot;article.NewsGroupList&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;newsGroups&amp;quot;&lt;/span&gt;)

  &lt;br /&gt;.Add(Property.ForName(&lt;span class="str"&gt;&amp;quot;newsGroups.Title&amp;quot;&lt;/span&gt;).In(newsGroupTitles))

  &lt;br /&gt;.CreateCriteria(&lt;span class="str"&gt;&amp;quot;article.TagList&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;tags&amp;quot;&lt;/span&gt;)

  &lt;br /&gt;.Add(Property.ForName(&lt;span class="str"&gt;&amp;quot;tags.Title&amp;quot;&lt;/span&gt;).In(tagTitles))

  &lt;br /&gt;.AddOrder(&lt;span class="kwrd"&gt;new&lt;/span&gt; Order(&lt;span class="str"&gt;&amp;quot;article.Date&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;true&lt;/span&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;/pre&gt;

&lt;p&gt;Well, actually that query worked just fine, until you added a simple .SetMaxResults() to it ... at which point it threw an exception and told us that the id had been duplicated in the query:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;System.Data.SqlClient.SqlException : The column &amp;#39;articleid&amp;#39; was 
    &lt;br /&gt;specified multiple times for &amp;#39;query&amp;#39;. 

    &lt;br /&gt;The column &amp;#39;articleid&amp;#39; was specified multiple times for &amp;#39;page&amp;#39;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Of course this didn&amp;#39;t look right. So I dropped it onto the NH users group on Google, and awaited someone far more knowledgeable to point out what I had done wrong. Tuna Toks&amp;amp;#xF6;z promptly pointed out, somewhat less than helpfully, that this was probably fixed in the last two days. Of course Tuna thought he was being very helpful - unfortunately we were running a few versions behind the trunk due to NHSearch - so I couldn&amp;#39;t get the latest version to see this bug disappear ... my only option was very loud cursing.&lt;/p&gt;

&lt;p&gt;So with some to and fro, and some suggestions from Ayende and Fabio, I stumbled along trying to get something to work - and as per the last time I tried, I failed miserably to get all the NH components to compile against each other, NHSearch was still the limiting factor.&lt;/p&gt;

&lt;p&gt;Eventually Tuna provided me with the HQL version of the query we were trying to do, which alleviated my initial problem, though in a less than perfect way. But, solve the problem it did, and so it will get into our code base on Monday, and probably be semi-replicated across other entities we need to query in similar ways. &lt;strong&gt;BIG thanks go to Tuna!&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;IList result = sess.CreateQuery(
  &lt;br /&gt;&lt;span class="str"&gt;&amp;quot;from Article a join a.TagList tag 
    &lt;br /&gt;join a.NewsGroupList newsGroup 

    &lt;br /&gt;where tag.Title in (:tagtitle) 

    &lt;br /&gt;or newsGroup.Title in (:grouptitle) 

    &lt;br /&gt;order by a.Date&amp;quot;&lt;/span&gt;)

  &lt;br /&gt;.SetParameterList(&lt;span class="str"&gt;&amp;quot;tagtitle&amp;quot;&lt;/span&gt;, tagTitles)

  &lt;br /&gt;.SetParameterList(&lt;span class="str"&gt;&amp;quot;grouptitle&amp;quot;&lt;/span&gt;, newsGroupTitles)

  &lt;br /&gt;.SetMaxResults(15).List(); &lt;/pre&gt;

&lt;p&gt;An even better result, it turns out that Ayende has NHSearch on his radar imminently (this weekend was mentioned), so I am really hopeful that I can get an up to date version of all my favourite components working in harmony again, I may even be able to put Rhino Commons back in and remove my awful home grown versions of Repository and UoW!&lt;/p&gt;

&lt;p&gt;This has to be the biggest reason to go with a mature open-source project like NHibernate, apart from it being pretty much the market leader in it&amp;#39;s field, it has possibly the best support network you could hope for.&lt;/p&gt;

&lt;p&gt;For all those companies that worry that using an open-source component in their code will be risky due to lack of a &amp;quot;real company&amp;quot; backing it up, I can only say:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;&lt;em&gt;&amp;quot;that big company could never be half as effective or responsive as the community support that exists around projects like NHibernate, Castle, Moq, Rhino, xUnit, and all the other great open-source work on which I depend to help me deliver high quality software&amp;quot;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=42702" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Castle/default.aspx">Castle</category><category domain="http://devlicio.us/blogs/casey/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Rhino/default.aspx">Rhino</category></item></channel></rss>