<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/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>Tales from the Evil Empire : MVC</title><link>http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx</link><description>Tags: MVC</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Orchard team looking for a new developer</title><link>http://weblogs.asp.net/bleroy/archive/2009/11/23/orchard-team-looking-for-a-new-developer.aspx</link><pubDate>Mon, 23 Nov 2009 19:24:19 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7263678</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7263678</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/11/23/orchard-team-looking-for-a-new-developer.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 10px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2005 Bertrand Le Roy" border="0" alt="(c) 2005 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/Dragonfly_5E7683B7.jpg" width="244" height="164" /&gt; My team is looking for a new full-time developer. The project is to build a completely new open-source CMS based on ASP.NET MVC 2. It’s a lot of fun :)&lt;/p&gt;  &lt;p&gt;&lt;a href="https://careers.microsoft.com/JobDetails.aspx?ss=&amp;amp;pg=0&amp;amp;so=&amp;amp;rw=1&amp;amp;jid=9434&amp;amp;jlang=EN"&gt;https://careers.microsoft.com/JobDetails.aspx?ss=&amp;amp;pg=0&amp;amp;so=&amp;amp;rw=1&amp;amp;jid=9434&amp;amp;jlang=EN&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7263678" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/CSS/default.aspx">CSS</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>Walking the tight rope</title><link>http://weblogs.asp.net/bleroy/archive/2009/08/27/walking-the-tight-rope.aspx</link><pubDate>Thu, 27 Aug 2009 22:40:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7183436</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7183436</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/08/27/walking-the-tight-rope.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 10px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2004" border="0" alt="(c) Bertrand Le Roy 2004" align="left" src="http://weblogs.asp.net/blogs/bleroy/MaxetGogo010_15869DD0.jpg" width="244" height="184" /&gt; I think today is an appropriate time to write this post, as &lt;a href="http://blog.wekeroad.com/"&gt;Rob Conery&lt;/a&gt; is &lt;a href="http://blog.wekeroad.com/blog/temet-nosce/"&gt;leaving Microsoft tomorrow&lt;/a&gt;. “Who?”, you might ask. Rob is the author of the excellent &lt;a href="http://blog.wekeroad.com/category/mvc-storefront/"&gt;MVC Storefront&lt;/a&gt; and &lt;a href="http://blog.wekeroad.com/category/kona/"&gt;Kona&lt;/a&gt; series where he explored the challenges in building an MVC-bound storefront application. I’ve been working with Rob for a few months on the continuation of that, which will be the subject of this post.&lt;/p&gt;  &lt;p&gt;This is challenging for a number of reasons.&lt;/p&gt;  &lt;p&gt;First, Rob’s are large shoes to fill (he’s a 12, I’m an 11). That’s fine, I’m just going to do things my way and try to have as much fun as possible (and communicate that if I can).&lt;/p&gt;  &lt;p&gt;Second, the focus of the application has changed and that is a much more important challenge. Rob built this as a learning tool, as much for him as for his readers.&lt;/p&gt;  &lt;p&gt;In the team that I’m now a part of, we have taken that Kona code as the basis to build something that will hopefully be more like a real world application. We are still very much in an exploratory mode (hence our attempts at &lt;a href="http://weblogs.asp.net/bleroy/archive/tags/NHibernate/default.aspx"&gt;migrating the application to NHibernate&lt;/a&gt; and SQLite) but our ultimate goal is to provide an application that people can download and build a business on, with a &lt;a href="http://blog.wekeroad.com/mvc-storefront/kona-1/"&gt;focus on simplicity and extensibility&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Fine, you may say, but why build something new instead of contributing to existing applications? Well, the very short answer is that we are also going to do that. One of the things we are already doing is to work with various application developers on reusing things like the plug-in engine and what sharing opportunities that brings.&lt;/p&gt;  &lt;p&gt;But I can already hear some of you saying “oh so you’re not just building an application, what you’re really doing is building yet another framework”. To which I’m tempted to answer with another question: what do you mean “another”? Where is my Django-style application-level framework for ASP.NET? But that may sound like I’m downplaying the great efforts that many people are putting towards exactly that. I clearly am not. Actually, if you are working on something like that and we’re not already talking, I would love to know about it. I want to know how we can help.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7183436" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Kona/default.aspx">Kona</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category></item><item><title>A total n00b’s guide to migrating from a custom data layer to Nhibernate: getting started</title><link>http://weblogs.asp.net/bleroy/archive/2009/08/17/a-total-n00b-s-guide-to-migrating-from-a-custom-data-layer-to-nhibernate-getting-started.aspx</link><pubDate>Mon, 17 Aug 2009 23:29:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7171785</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>21</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7171785</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/08/17/a-total-n00b-s-guide-to-migrating-from-a-custom-data-layer-to-nhibernate-getting-started.aspx#comments</comments><description>&lt;P&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; MARGIN: 0px 10px 10px 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="(c) Bertrand Le Roy 2003" border=0 alt="(c) Bertrand Le Roy 2003" align=left src="http://weblogs.asp.net/blogs/bleroy/DSCN0989_1A90F5CF.jpg" width=164 height=244 mce_src="http://weblogs.asp.net/blogs/bleroy/DSCN0989_1A90F5CF.jpg"&gt;(Screencast can be found at the end of the post)&lt;/P&gt;
&lt;P&gt;To be clear when I say “total n00b”, I’m not talking about you, dear reader, I’m talking about me. The last time I wrote any serious data access code was &lt;EM&gt;circa&lt;/EM&gt; 2002. Since then, I got hired by the Evil Empire and started developing new tools to make it easier to build demos of Northwind master-details. I jest, I jest. Or do I?&lt;/P&gt;
&lt;P&gt;So let me explain what I’m going to talk about in this and future related blog posts. We have &lt;A href="http://blog.wekeroad.com/category/mvc-storefront" mce_href="http://blog.wekeroad.com/category/mvc-storefront"&gt;this e-commerce application that Rob started&lt;/A&gt; and that we’re going to continue developing. Last time Rob touched the data access, he wanted to experiment with going back to less abstraction and to working directly with that interesting data-centric Domain Specific Language, you know, SQL. So he went ahead and played with that T4-driven DAL generator. It was interesting as an experiment, and Rob’s blog series is all about experimenting publicly, but let’s face it, that data access &lt;A href="http://ayende.com/Blog/archive/2009/08/15/the-least-common-denominator-approach.aspx" mce_href="http://ayende.com/Blog/archive/2009/08/15/the-least-common-denominator-approach.aspx"&gt;didn’t fly much farther than that&lt;/A&gt;. To be clear, I’m not saying that Rob was to blame for our sucky data access layer, but the truth is that we have had this franken-DAL from the nineties in the code for a few months now and it needs replacing. So what do we do?&lt;/P&gt;
&lt;P&gt;First, we look around and find out what other people out there are doing. We could go for the Microsoft flavor of ORM, EF, but we’re not going to do that (at least not yet) and will instead go for the one that is the most widely used by the community as of today, and that is, I believe, &lt;A href="http://nhforge.org/" mce_href="http://nhforge.org"&gt;NHibernate&lt;/A&gt;. The home page for the project itself can be found at &lt;A title=http://nhforge.org href="http://nhforge.org/" mce_href="http://nhforge.org"&gt;http://nhforge.org&lt;/A&gt; and the best place to get started is probably the tutorial section of that site: &lt;A href="http://nhforge.org/doc/nh/en/index.html#quickstart" mce_href="http://nhforge.org/doc/nh/en/index.html#quickstart"&gt;http://nhforge.org/doc/nh/en/index.html#quickstart&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;In this first post, I’ll just play with the library and try to get a list of products to display on a page. In future posts, I’ll look at the actual app and start migrating it. For this time, because I’m just trying to get to grips with a library I don’t know, it’s going to be quick and it’s going to be dirty. Definitely don’t take any of what you are about to see as best practices. Ever.&lt;/P&gt;
&lt;P&gt;The first step is to add the NHibernate and dependencies to the project. The library can be downloaded from SourceForge (&lt;A href="http://sourceforge.net/projects/nhibernate" mce_href="http://sourceforge.net/projects/nhibernate"&gt;http://sourceforge.net/projects/nhibernate&lt;/A&gt;). Nhibernate is itself under &lt;A href="http://www.gnu.org/licenses/lgpl.html" mce_href="http://www.gnu.org/licenses/lgpl.html"&gt;LGPL&lt;/A&gt; but it comes with the following dependencies:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://www.antlr.org/" mce_href="http://www.antlr.org"&gt;Antlr&lt;/A&gt;: a library to construct grammar parsers, interpreters, etc. typically for domain specific languages. It’s under a &lt;A href="http://www.antlr.org/license.html" mce_href="http://www.antlr.org/license.html"&gt;BSD license&lt;/A&gt;. &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.codeproject.com/KB/recipes/sets.aspx" mce_href="http://www.codeproject.com/KB/recipes/sets.aspx"&gt;Iesi.Collections&lt;/A&gt;: a library that implements sets (collections with unique elements) and that doesn’t have a definite license. A &lt;A href="http://www.codeproject.com/Messages/1622667/Re-Licensing-terms.aspx" mce_href="http://www.codeproject.com/Messages/1622667/Re-Licensing-terms.aspx"&gt;comment from the author&lt;/A&gt; seems to implicate public domain. &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://logging.apache.org/log4net/index.html" mce_href="http://logging.apache.org/log4net/index.html"&gt;Log4net&lt;/A&gt;: the leading logging library for .NET, licensed under &lt;A href="http://logging.apache.org/log4net/license.html" mce_href="http://logging.apache.org/log4net/license.html"&gt;Apache 2.0&lt;/A&gt; and managed by the &lt;A href="http://www.apache.org/" mce_href="http://www.apache.org/"&gt;Apache Foundation&lt;/A&gt;. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;You may not care too much about that, but your boss and his lawyer might… Technically, all you need to add to the project is a reference to the NHibernate dll and the others will follow.&lt;/P&gt;
&lt;P&gt;Once the reference has been added to our project, we can start configuring. This can be done in a variety of manners but the easiest is to do it through web.config. The basic configuration for NHibernate consists in the connection string, the SQL dialect and the proxy factory to use. Because we don’t want to repeat ourselves and because I prefer my connection strings to live in the connection strings section of web.config, I’ll use a “connection.connection_string_name” setting instead of a “connection.connection_string” setting:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;hibernate-configuration &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;urn:nhibernate-configuration-2.2&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
  &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;session-factory&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;property &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;connection.connection_string_name&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;BR&gt;      &lt;/SPAN&gt;KonaConnectionString&lt;BR&gt;    &lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;property&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;property &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;dialect&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;BR&gt;      &lt;/SPAN&gt;NHibernate.Dialect.MsSql2000Dialect&lt;BR&gt;    &lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;property&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;property &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;proxyfactory.factory_class&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;BR&gt;      &lt;/SPAN&gt;NHibernate.ByteCode.LinFu.ProxyFactoryFactory,&lt;BR&gt;      NHibernate.ByteCode.LinFu&lt;BR&gt;    &lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;property&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
  &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;session-factory&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;hibernate-configuration&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The proxy factory factory class stuff looks a little intimidating and a little over-architected from just looking at it (a factory factory? To create proxies?) but it’s not as bad as it looks. What this is doing is declaring what library to use to generate &lt;A href="http://davybrion.com/blog/2009/03/must-everything-be-virtual-with-nhibernate/" mce_href="http://davybrion.com/blog/2009/03/must-everything-be-virtual-with-nhibernate/"&gt;dynamic proxies for our data classes&lt;/A&gt;. It is a good thing that NHibernate is open to multiple providers here, and the good news is this is probably the first and last time you need to know about this. Just make a choice if you care (and know why you care), or use &lt;A href="http://code.google.com/p/linfu/" mce_href="http://code.google.com/p/linfu/"&gt;this one&lt;/A&gt; (licensed under LGPL) if you don’t.&lt;/P&gt;
&lt;P&gt;But what is a dynamic proxy, you may ask? First and foremost, it is a proxy class that will be used in place of the POCO class that you will provide to represent your data. Its purpose is to intercept all calls into the object’s properties, both getters and setters, and to handle things like lazy loading. The dynamic aspect of it is that those proxy classes are dynamically generated, usually using Reflection.Emit. This means that until .NET 4.0 is here, using lazy loading in NHibernate will prevent the application from running in medium trust. More on that in future posts, but know that &lt;A href="http://davybrion.com/blog/2009/03/must-everything-be-virtual-with-nhibernate/" mce_href="http://davybrion.com/blog/2009/03/must-everything-be-virtual-with-nhibernate/"&gt;lazy loading is not mandatory and can easily be disabled from the mapping file&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The next thing to do is to grab the NHibernate configuration in order to create a session out of it. The configuration can be built from the web.config data like this:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;nhconfig = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;NHibernate.Cfg.&lt;SPAN style="COLOR: #2b91af"&gt;Configuration&lt;/SPAN&gt;().Configure();&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;A session is the mediator between your code and Nhibernate. According to &lt;A href="http://nhforge.org/doc/nh/en/index.html#architecture-overview" mce_href="http://nhforge.org/doc/nh/en/index.html#architecture-overview"&gt;the documentation&lt;/A&gt;, it is a “short-lived object representing a conversation between the application and the persistent store”. Its lifetime is usually the same as the lifetime of the request. Here’s how you can create a session:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;session = nhconfig.BuildSessionFactory().OpenSession();&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;With all that we are pretty much set-up but we are still lacking any data. Let’s fix that and attempt our first mapping. I’m going to use a simplified version of the product database we have in the commerce app (which is none other than AdventureWorks). In there, I have a Products table that looks like this:&lt;/P&gt;
&lt;P&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title="Products Table" border=0 alt="Products Table" src="http://weblogs.asp.net/blogs/bleroy/ProductsTable_4FDA0834.png" width=388 height=393 mce_src="http://weblogs.asp.net/blogs/bleroy/ProductsTable_4FDA0834.png"&gt; &lt;/P&gt;
&lt;P&gt;We don’t have to map everything in that table so we won’t (yet). Instead, we’ll create a simple and incomplete class to represent a product:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Product &lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public virtual string &lt;/SPAN&gt;Sku { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
    &lt;SPAN style="COLOR: blue"&gt;public virtual &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Guid &lt;/SPAN&gt;SiteID { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
    &lt;SPAN style="COLOR: blue"&gt;public virtual string &lt;/SPAN&gt;Name { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
    &lt;SPAN style="COLOR: blue"&gt;public virtual double &lt;/SPAN&gt;BasePrice { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Notice how all fields are virtual here. This, again, is done &lt;A href="http://davybrion.com/blog/2009/03/must-everything-be-virtual-with-nhibernate/" mce_href="http://davybrion.com/blog/2009/03/must-everything-be-virtual-with-nhibernate/"&gt;so that a dynamic proxy class that overrides those properties can be created&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Now we have the O (object) and the R (relational), so let’s do the M. The mapping is an XML file that we’ll put next to our Product class:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;?&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;xml &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;version&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;1.0&lt;/SPAN&gt;" &lt;SPAN style="COLOR: red"&gt;encoding&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;utf-8&lt;/SPAN&gt;" &lt;SPAN style="COLOR: blue"&gt;?&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;hibernate-mapping &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;urn:nhibernate-mapping-2.2&lt;/SPAN&gt;"
    &lt;SPAN style="COLOR: red"&gt;namespace&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Nhib.Models&lt;/SPAN&gt;" &lt;SPAN style="COLOR: red"&gt;assembly&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Nhib&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

  &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;class &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Product&lt;/SPAN&gt;" &lt;SPAN style="COLOR: red"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Products&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;id &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Sku&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
      &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;column &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;SKU&lt;/SPAN&gt;" &lt;SPAN style="COLOR: red"&gt;sql-type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;nvarchar(50)&lt;/SPAN&gt;" &lt;SPAN style="COLOR: red"&gt;not-null&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
      &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;generator &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;uuid.hex&lt;/SPAN&gt;" &lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;property &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;SiteID&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;property &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Name&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
      &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;column &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;ProductName&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;property&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;property &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;BasePrice&lt;/SPAN&gt;" &lt;SPAN style="COLOR: blue"&gt;/&amp;gt;
  &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;

&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;hibernate-mapping&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The mapping class is named like the Product class, with a “.hbm.xml” extension. It is also configured to build as an embedded resource so that Hibernate can reflect on it at runtime. This is done by setting the Build Action in the properties of the file in Project Explorer:&lt;/P&gt;
&lt;P&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=EmbeddedResource border=0 alt=EmbeddedResource src="http://weblogs.asp.net/blogs/bleroy/EmbeddedResource_3CB8DB88.png" width=283 height=305 mce_src="http://weblogs.asp.net/blogs/bleroy/EmbeddedResource_3CB8DB88.png"&gt; &lt;/P&gt;
&lt;P&gt;And we’re done as far as setup is concerned. Now all that remains to do is to actually query and display that data.&lt;/P&gt;
&lt;P&gt;Because I’m just playing with the framework at this point, I’ll just query from the Index controller action:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;nhconfig = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;NHibernate.Cfg.&lt;SPAN style="COLOR: #2b91af"&gt;Configuration&lt;/SPAN&gt;().Configure();
nhconfig.AddClass(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt;));
&lt;SPAN style="COLOR: blue"&gt;using &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;session = nhconfig.BuildSessionFactory().OpenSession()) {
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = session.CreateQuery(&lt;SPAN style="COLOR: #a31515"&gt;"select p from Product as p"&lt;/SPAN&gt;);
    ViewData.Model = query.List&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt;&amp;gt;();
}&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt; &lt;A href="http://ayende.com/" mce_href="http://ayende.com"&gt;Ayende&lt;/A&gt; is right to attract my attention to the fact that building a session factory is an expensive operation&amp;nbsp;and so&amp;nbsp;you should treat it as a singleton. In other&amp;nbsp;words, create&amp;nbsp;the factory&amp;nbsp;once and store it in a static variable. For the sake of the simplicity of the example, I'm leaving the code above, but again, don't do this at home...&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And finally, let’s render this from the view:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;ul&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;p &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;Model) { &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;li&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;= &lt;/SPAN&gt;Html.Encode(p.Name) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt; - $&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;= &lt;/SPAN&gt;p.BasePrice &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;li&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; } &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;ul&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;The results look like this:&lt;/P&gt;
&lt;P&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title="Product rendering" border=0 alt="Product rendering" src="http://weblogs.asp.net/blogs/bleroy/ProductRendering_3BE0759E.png" width=292 height=187 mce_src="http://weblogs.asp.net/blogs/bleroy/ProductRendering_3BE0759E.png"&gt; &lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope this gives a good idea of how simple it is to get started with NHibernate, because it really is. I knew near to nothing about it and almost didn’t have to fight the framework to get it running. Seeing the first successful rendering on my very first CTRL+F5 was very encouraging.&lt;/P&gt;
&lt;P&gt;In a future post, I’ll show how this applies to a real-world application and how we migrated such an application from its existing data layer to one that uses NHibernate.&lt;/P&gt;
&lt;P&gt;The source code can be downloaded from here: &lt;A href="http://weblogs.asp.net/blogs/bleroy/Samples/Nhib.zip" mce_href="http://weblogs.asp.net/blogs/bleroy/Samples/Nhib.zip"&gt;NHibGettingStarted.zip&lt;/A&gt; (warning: all files provided under their respective licenses).&lt;/P&gt;
&lt;P&gt;… and here’s the screencast:&lt;/P&gt;&lt;IFRAME style="WIDTH: 500px; HEIGHT: 375px" src="http://silverlight.services.live.com/invoke/106043/Getting%20started%20with%20NHibernate/iframe.html" frameBorder=0 scrolling=no mce_src="http://silverlight.services.live.com/invoke/106043/Getting%20started%20with%20NHibernate/iframe.html"&gt;&lt;/IFRAME&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7171785" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Screencast/default.aspx">Screencast</category></item><item><title>Mocking indexer getters with Moq</title><link>http://weblogs.asp.net/bleroy/archive/2009/07/16/mocking-indexer-getters-with-moq.aspx</link><pubDate>Thu, 16 Jul 2009 23:42:17 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7148938</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7148938</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/07/16/mocking-indexer-getters-with-moq.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy" border="0" alt="(c) Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/DSCN2687_61E2AA99.jpg" width="244" height="184" /&gt; This is a follow-up on that other post: &lt;a href="http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx"&gt;Mocking indexer setters with Moq&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;So thanks to that post, we now know how to intercept the setting of a particular indexed property (in our example, an application variable) and set a local variable with the value that was set by the tested code.&lt;/p&gt;  &lt;p&gt;Now if you want the application to return that same value when queried by the tested code, you also need to mock the indexer getter.&lt;/p&gt;  &lt;p&gt;This operation is also not entirely trivial. Here’s how you do it: you do a SetupGet chained with a Returns with a lambda expression as the parameter:&lt;/p&gt;  &lt;pre class="code"&gt;mockHttpContext&lt;br /&gt;    .SetupGet(c =&amp;gt; c.Application[&lt;span style="color: #2b91af"&gt;&amp;quot;foo&lt;span style="color: #2b91af"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;])
    .Returns(() =&amp;gt; (&lt;span style="color: blue"&gt;object&lt;/span&gt;)map);&lt;/pre&gt;

&lt;p&gt;That last point about using a lambda is pretty important. If you just use map as the parameter for Returns, Moq will hold on to a reference to whatever object map contained &lt;em&gt;at the time the call to Returns is made&lt;/em&gt;. This might very well be null, if you started with the code in our previous setter example. If you use a lambda on the other hand, Moq will not hold on to the value of map but to the expression that returns the value of map. The execution is deferred. Now every time the tested code exercises that path, Moq will evaluate the expression, which will return the &lt;em&gt;current&lt;/em&gt; value of map.&lt;/p&gt;

&lt;p&gt;Also of interest, whereas we had to explicitly have an index parameter for the callback lambda in the case of the setter, Returns needs no such thing, and this example actually has little code that is specific to indexed properties. The trick about the lambda in the Returns in particular applies just as well to mocking getters for non-indexed properties.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7148938" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MoQ/default.aspx">MoQ</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category></item><item><title>Tell me what smells in WebForms as a view engine</title><link>http://weblogs.asp.net/bleroy/archive/2009/07/13/tell-me-what-smells-in-webforms-as-a-view-engine.aspx</link><pubDate>Mon, 13 Jul 2009 17:58:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7146686</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>27</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7146686</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/07/13/tell-me-what-smells-in-webforms-as-a-view-engine.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 20px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="Fisherman&amp;#39;s Friend" border="0" alt="Fisherman&amp;#39;s Friend" align="left" src="http://weblogs.asp.net/blogs/bleroy/FishermansFriend_29543AA9.jpg" width="192" height="136" /&gt; Don’t read too much into this, but I’d love to read your feedback on this. I’m compiling a list of stuff that smells in WebForms when used as a view engine in MVC.&lt;/p&gt;  &lt;p&gt;Along the lines of:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Noisy page directives that are useless for MVC&lt;/li&gt;    &lt;li&gt;runat=server&lt;/li&gt;    &lt;li&gt;Page lifecycle&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;For once, you can let the inside troll take over :)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7146686" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>Mocking indexer setters with Moq</title><link>http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx</link><pubDate>Mon, 15 Jun 2009 22:27:52 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7124027</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7124027</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2004" border="0" alt="(c) Bertrand Le Roy 2004" align="left" src="http://weblogs.asp.net/blogs/bleroy/DSCN2906_5A7A1A17.jpg" width="244" height="184" /&gt; I quite like &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt; because it makes sense for me. Shamefully, I’ve always had some trouble understanding test code that was using mocks built with other frameworks. With &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt;, I can just grok it for some reason. It’s just super-clear to me. It doesn’t mean I have any idea how it really works but for now I’m just happy with the magic.&lt;/p&gt;  &lt;p&gt;Anyway, yesterday I wanted to check that a controller action was setting some Application variable (let’s not get into the debate on whether or not it should do that at all). Something like this:&lt;/p&gt;  &lt;pre class="code"&gt;application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now how do I enable the object to be set by the tested code? Well, that one is easy, I can use &lt;a href="http://www.clariusconsulting.net/labs/moq/html/87C20A5E.htm"&gt;SetupSet&lt;/a&gt; on indexers just fine:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;mockHttpContext = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpContextBase&lt;/span&gt;&amp;gt;();
mockHttpContext.SetupSet(&lt;br /&gt;    c =&amp;gt; c.Application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: #2b91af"&gt;It&lt;/span&gt;.IsAny&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;());&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This call tells the mock that it can accept to run code that attempts to set the “Foo” application variable with any object.&lt;/p&gt;

&lt;p&gt;But now, what if I want to get a reference to that object from my test code in order to check the value of some of its properties?&lt;/p&gt;

&lt;p&gt;Well, &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt; has a Callback method that you can hook to the result of any Setup call. The action that you provide it will be run whenever the setup code is called. The problem with that callback method is that its signature must match that of the setter exactly. Unfortunately, that signature is implicit. If you get it wrong, the test will fail more or less silently (it will just tell you setup failed with little details). To get this right, you need to know what the setter syntactic sugar compiles to, which kinda sucks, but the good news is that you only have to figure it out once, which I just spent some time doing for you (and for myself too, let’s be honest):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;mockHttpContext = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpContextBase&lt;/span&gt;&amp;gt;();
&lt;span style="color: #2b91af"&gt;Foo &lt;/span&gt;map = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;br /&gt;mockHttpContext&lt;br /&gt;    .SetupSet(c =&amp;gt; c.Application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: #2b91af"&gt;It&lt;/span&gt;.IsAny&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;())
    .Callback((&lt;span style="color: blue"&gt;string &lt;/span&gt;name, &lt;span style="color: blue"&gt;object &lt;/span&gt;m) =&amp;gt; { map = (&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;)m; });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Because this is an indexer setter, the compiled code actually takes a name and a value, which is reflected by the signature of the callback lambda. We can now call into the code to test, knowing that when it sets our “Foo” application variable, the local “map” variable of the test code will get set. The test code can then party on the object and assert whatever it wants.&lt;/p&gt;

&lt;p&gt;I hope this saves some time whoever is trying to do the same thing.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7124027" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MoQ/default.aspx">MoQ</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category></item><item><title>Some MIX talks</title><link>http://weblogs.asp.net/bleroy/archive/2009/03/22/some-mix-talks.aspx</link><pubDate>Mon, 23 Mar 2009 04:14:22 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6994071</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6994071</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/03/22/some-mix-talks.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 15px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2005 Bertrand Le Roy" border="0" alt="(c) 2005 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/bleroy04_2F523812.jpg" width="244" height="164" /&gt; Stephen Walther just published links to the video, slides and sample code for his Ajax talk at MIX09:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://stephenwalther.com/blog/archive/2009/03/22/mix-slides-code-and-session-recording.aspx"&gt;http://stephenwalther.com/blog/archive/2009/03/22/mix-slides-code-and-session-recording.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It’s pretty cool to see all the work we put into Ajax this past year or so presented at MIX. This is a really nice presentation, like Stephen’s always are.&lt;/p&gt;  &lt;p&gt;Another presentation I had lots of fun watching (not just because the speaker is making an incredible impression of me but also because I’ve been spending a good part of my time lately contributing to the application he’s showing) is Rob Conery’s. Rob is showing an interesting way to develop ASP.NET applications, aimed at ease of use and customization rather than architectural purity. Check it out, let me know what you think…&lt;/p&gt;  &lt;p&gt;&lt;a href="http://videos.visitmix.com/MIX09/T62F"&gt;http://videos.visitmix.com/MIX09/T62F&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6994071" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category></item><item><title>ASP.NET 4.0 Roadmap talk available online</title><link>http://weblogs.asp.net/bleroy/archive/2008/10/28/asp-net-4-0-roadmap-talk-available-online.aspx</link><pubDate>Tue, 28 Oct 2008 22:09:26 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6708711</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6708711</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2008/10/28/asp-net-4-0-roadmap-talk-available-online.aspx#comments</comments><description>&lt;p&gt;Scott Hunter’s talk on the ASP.NET 4.0 roadmap (in which I’m doing a 10 minute demo) is available from Channel9:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://channel9.msdn.com/pdc2008/PC20/" href="http://channel9.msdn.com/pdc2008/PC20/"&gt;http://channel9.msdn.com/pdc2008/PC20/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6708711" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category></item><item><title>Going to California</title><link>http://weblogs.asp.net/bleroy/archive/2008/10/22/going-to-california.aspx</link><pubDate>Wed, 22 Oct 2008 19:25:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6698142</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6698142</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2008/10/22/going-to-california.aspx#comments</comments><description>&lt;p&gt;I’m flying to San Jose tonight for tomorrow’s &lt;a href="http://www.openajax.org/member/wiki/2008_October_Members_Meeting"&gt;OpenAjax Alliance face to face meeting&lt;/a&gt;, which Microsoft is hosting. On Friday, we are also hosting a new event that aims at establishing a dialogue between JavaScript library developers and Microsoft. We’ll have talks from the IE, Visual Studio and ASP.NET teams, as well as talks from members of the community. This should be very interesting.&lt;/p&gt;  &lt;p&gt;Then I’ll be flying to L.A. for the &lt;a href="http://www.microsoftpdc.com/"&gt;PDC&lt;/a&gt;. If you’re going to be there and want to chat, feel free to drop me a note at bleroy at Microsoft.&lt;/p&gt;  &lt;p&gt;I’ll be in the room during &lt;a href="http://weblogs.asp.net/stephenwalther/default.aspx"&gt;Stephen Walthers&lt;/a&gt;’ session on &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; and ASP.NET on Tuesday from 5:15 to 6:30 (403AB).&lt;/p&gt;  &lt;p&gt;I’ll also do a short demo in &lt;a href="http://blogs.msdn.com/scothu/default.aspx"&gt;Scott Hunter&lt;/a&gt;’s talk on the ASP.NET 4.0 Roadmap on Monday from 1:45 to 3:00 (153).&lt;/p&gt;  &lt;p&gt;Other sessions I’ll attend include &lt;a href="http://haacked.com/Default.aspx"&gt;Phil Haack&lt;/a&gt;’s MVC session (Monday, 3:30 in 153), Jeff King’s talk on Visual Studio Web Development Futures (Monday, 5:15 in 153), the panel talk on the future of programming languages (Wednesday 10:30 in 403AB), Frank Savage’s &lt;a href="http://blogs.msdn.com/xna/default.aspx"&gt;XNA&lt;/a&gt; Game Studio talk (Wednesday, 12:00 in 501B), &lt;a href="http://en.wikipedia.org/wiki/Anders_Hejlsberg"&gt;Anders Heljsberg&lt;/a&gt;’s talk on C# (Wednesday 3:00 in 502A), &lt;a href="http://tirania.org/blog/"&gt;Miguel de Icaza&lt;/a&gt;’s talk on Mono (Wednesday at 4:45 in 515B) and of course &lt;a href="http://browse.barnesandnoble.com/booksearch/results.asp?ath=Stefan+Schackow"&gt;Stefan Schackow&lt;/a&gt;’s ASP.NET Cache Extensibility talk (Thursday 10:15 in 403B) which was the highest rated talk during our dry-run of the conference…&lt;/p&gt;  &lt;p&gt;And of course, I’ll be on stage for my own talk on &lt;a href="http://www.codeplex.com/aspnet/Wiki/View.aspx?title=AJAX&amp;amp;referringTitle=Home"&gt;ASP.NET 4.0 AJAX&lt;/a&gt; on Thursday from 1:45 to 3:00 (Petree Hall CD), which will pretty much close the show. I’ll be building a small application using client templates, bindings, some &lt;a href="http://www.jquery.com/"&gt;jQuery&lt;/a&gt; and ADO.NET Data Services.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6698142" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft/default.aspx">Microsoft</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/OpenAjax/default.aspx">OpenAjax</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/PDC/default.aspx">PDC</category></item><item><title>Using the Ajax Control Toolkit in ASP.NET MVC</title><link>http://weblogs.asp.net/bleroy/archive/2008/08/25/using-the-ajax-control-toolkit-in-asp-net-mvc.aspx</link><pubDate>Mon, 25 Aug 2008 19:20:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6564306</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6564306</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2008/08/25/using-the-ajax-control-toolkit-in-asp-net-mvc.aspx#comments</comments><description>&lt;P&gt;Stephen Walther has a pretty cool post on using the new &lt;A class="" href="http://www.codeplex.com/AjaxControlToolkit/Release/ProjectReleases.aspx?ReleaseId=16488" mce_href="http://www.codeplex.com/AjaxControlToolkit/Release/ProjectReleases.aspx?ReleaseId=16488"&gt;file-only version of the Ajax Control Toolkit&lt;/A&gt; from an MVC application:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://stephenwalther.com/blog/archive/2008/08/23/asp-net-mvc-tip-36-create-a-popup-calendar-helper.aspx"&gt;http://stephenwalther.com/blog/archive/2008/08/23/asp-net-mvc-tip-36-create-a-popup-calendar-helper.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt; the problem with Stephen is that he writes quite fast. So pretty much in the time it took me to link to his first post, he wrote a second one, this time on using the AutoComplete behavior:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://stephenwalther.com/blog/archive/2008/08/24/asp-net-mvc-tip-37-create-an-auto-complete-text-field.aspx"&gt;http://stephenwalther.com/blog/archive/2008/08/24/asp-net-mvc-tip-37-create-an-auto-complete-text-field.aspx&lt;/A&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6564306" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category></item></channel></rss>