<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>LA.NET [EN]</title>
	
	<link>http://luisabreu.wordpress.com</link>
	<description>Ramblings about C#, .NET and Programming</description>
	<lastBuildDate>Thu, 04 Apr 2013 11:08:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain="luisabreu.wordpress.com" port="80" path="/?rsscloud=notify" registerProcedure="" protocol="http-post" />
<image>
		<url>http://1.gravatar.com/blavatar/f72534352f1aa2540d398b6d8e515b66?s=96&amp;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>LA.NET [EN]</title>
		<link>http://luisabreu.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://luisabreu.wordpress.com/osd.xml" title="LA.NET [EN]" />
	
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Laneten" /><feedburner:info uri="laneten" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://luisabreu.wordpress.com/?pushpress=hub" /><item>
		<title>My ASP.NET 4.5 is out</title>
		<link>http://feedproxy.google.com/~r/Laneten/~3/VKHFLsHWyM8/</link>
		<comments>http://luisabreu.wordpress.com/2013/04/04/my-asp-net-4-5-is-out/#comments</comments>
		<pubDate>Thu, 04 Apr 2013 11:08:10 +0000</pubDate>
		<dc:creator>Luis Abreu</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">https://luisabreu.wordpress.com/?p=1822</guid>
		<description><![CDATA[I’ve been so busy that I’ve forgot to mention that my ASP.NET 4.5 book (written in Portuguese) is out.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1822&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I’ve been so busy that I’ve forgot to mention that my ASP.NET 4.5 book (written in Portuguese) is <a href="http://www.fca.pt/cgi-bin/fca_main.cgi/?isbn=978-972-722-760-0&amp;op=2">out</a>. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luisabreu.wordpress.com/1822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luisabreu.wordpress.com/1822/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1822&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" /><img src="http://feeds.feedburner.com/~r/Laneten/~4/VKHFLsHWyM8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://luisabreu.wordpress.com/2013/04/04/my-asp-net-4-5-is-out/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ef50ae4032124f87927d917ac713ee3b?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">luisabreu</media:title>
		</media:content>
	<feedburner:origLink>http://luisabreu.wordpress.com/2013/04/04/my-asp-net-4-5-is-out/</feedburner:origLink></item>
		<item>
		<title>Book review: CLR via C#</title>
		<link>http://feedproxy.google.com/~r/Laneten/~3/nkNZJq4W33E/</link>
		<comments>http://luisabreu.wordpress.com/2013/02/25/book-review-clr-via-c-2/#comments</comments>
		<pubDate>Mon, 25 Feb 2013 19:56:37 +0000</pubDate>
		<dc:creator>Luis Abreu</dc:creator>
				<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">https://luisabreu.wordpress.com/?p=1821</guid>
		<description><![CDATA[[Disclaimer: I’ve received a copy of this book for reviewing] Yesterday I’ve finally finished reading Jeffrey Richter’s last book CLR via C#, 4th edition. I’ve been getting a copy of this book (buying or being lucky enough to get a review copy) since it was called Applied Microsoft .NET Framework Programming. Each new edition ends [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1821&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>[Disclaimer: I’ve received a copy of this book for reviewing]</p>
<p>Yesterday I’ve finally finished reading Jeffrey Richter’s last book <a href="http://www.amazon.co.uk/CLR-Via-Edition-Developer-Reference/dp/0735667454/ref=sr_1_1?ie=UTF8&amp;qid=1361821594&amp;sr=8-1">CLR via C#, 4th edition</a>. I’ve been getting a copy of this book (buying or being lucky enough to get a review copy) since it was called <a href="http://www.amazon.co.uk/Applied-Microsoft-NET-Framework-Programming/dp/0735614229/ref=sr_1_14?ie=UTF8&amp;qid=1361821739&amp;sr=8-14">Applied Microsoft .NET Framework Programming</a>. Each new edition ends up adding new cool stuff and this last edition didn’t disappoint. Besides Garbage Collector chapter rewrite, this latest edition introduces the new Reflection API (introduced by .NET 4.5)&#160; and presents several new concepts associated with the new WinRT framework. If you’re a die hard C# programmer, you should be pleased to know that the book has been updated to illustrate the new C# 5.0 async features introduced by the latest .NET framework release.</p>
<p>So, this is really a book I love and fortunately for us developers, Jeffrey has been doing an excellent job of maintaining and updating it during these last years. In my opinion, this is still the .NET reference book every developer should buy and read. My score: 10/10.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luisabreu.wordpress.com/1821/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luisabreu.wordpress.com/1821/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1821&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" /><img src="http://feeds.feedburner.com/~r/Laneten/~4/nkNZJq4W33E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://luisabreu.wordpress.com/2013/02/25/book-review-clr-via-c-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ef50ae4032124f87927d917ac713ee3b?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">luisabreu</media:title>
		</media:content>
	<feedburner:origLink>http://luisabreu.wordpress.com/2013/02/25/book-review-clr-via-c-2/</feedburner:origLink></item>
		<item>
		<title>Book review: The art of SEO</title>
		<link>http://feedproxy.google.com/~r/Laneten/~3/MwsfpE-rncM/</link>
		<comments>http://luisabreu.wordpress.com/2013/02/18/book-review-the-art-of-seo/#comments</comments>
		<pubDate>Mon, 18 Feb 2013 15:22:12 +0000</pubDate>
		<dc:creator>Luis Abreu</dc:creator>
				<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">https://luisabreu.wordpress.com/?p=1820</guid>
		<description><![CDATA[[Disclaimer: I’ve received a free copy of this book for reviewing] In these last couple of days, I’ve spent most of my free time reading this book. It’s a really thick book (I must confess that is was way more thicker than I was expecting initially). It does cover a lot of ground and it’s [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1820&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>[Disclaimer: I’ve received a free copy of this book for reviewing]</p>
<p>In these last couple of days, I’ve spent most of my free time reading this <a href="http://www.amazon.com/The-Art-SEO-Mastering-Optimization/dp/B008SMJ1SO/ref=cm_cr_pr_product_top">book</a>. It’s a really thick book (I must confess that is was way more thicker than I was expecting initially). It does cover a lot of ground and it’s probably the best reference on the subject available on print. It’s a little dense and I wouldn’t recommend it to someone who is just starting doing web development. Even though it covers several topics in depth, I must say that chapter 7 is really a must for anyone interested in understanding how links are used for ranking pages.</p>
<p>Even though the book has lots of examples (I think it would be better to reduce them, specially the ones presented in the first chapters), this isn’t a platform specific book. In other words, don’t expect to get instructions on how to perform a specific task for server A or platform B. Instead, expect to find lots of examples that can be used across all platforms. Overall, I liked the book and that’s why I’m giving it an 8/10.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luisabreu.wordpress.com/1820/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luisabreu.wordpress.com/1820/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1820&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" /><img src="http://feeds.feedburner.com/~r/Laneten/~4/MwsfpE-rncM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://luisabreu.wordpress.com/2013/02/18/book-review-the-art-of-seo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ef50ae4032124f87927d917ac713ee3b?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">luisabreu</media:title>
		</media:content>
	<feedburner:origLink>http://luisabreu.wordpress.com/2013/02/18/book-review-the-art-of-seo/</feedburner:origLink></item>
		<item>
		<title>The–LiteralPath parameter</title>
		<link>http://feedproxy.google.com/~r/Laneten/~3/i1Z-YOIZ1RM/</link>
		<comments>http://luisabreu.wordpress.com/2013/02/15/theliteralpath-parameter/#comments</comments>
		<pubDate>Fri, 15 Feb 2013 13:02:46 +0000</pubDate>
		<dc:creator>Luis Abreu</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">https://luisabreu.wordpress.com/?p=1819</guid>
		<description><![CDATA[It’s 2013, we’ve got Powershell for a couple of years now, so it’s only natural to automate things, right? At least, that’s what I try to do. So, one of the things I’ve ended up doing some time ago was writing a simple script for copying files from an USB pen to a specific folder. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1819&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>It’s 2013, we’ve got <a href="http://technet.microsoft.com/en-us/library/bb978526.aspx">Powershell</a> for a couple of years now, so it’s only natural to automate things, right? At least, that’s what I try to do. So, one of the things I’ve ended up doing some time ago was writing a simple script for copying files from an USB pen to a specific folder. As you can see, it’s really simple:</p>
<pre><code>ls j:\ | foreach { 
  $itemName = $_.Name.Replace('.', ' ') 
  $destination = ls | where { $itemName -match $_.Name } | select -First 1 
  if( $destination -ne $null){       
   mi $_.PSPath $destination.PSPath -Verbose -WhatIf  
  } 
}</code></pre>
<p>After some time where everything seemed to be running ok, I’ve noticed that some of the files weren’t being copied. After further analysis, I’ve noted that files contained the [ and ] chars weren’t being copied. Fortunately, we have <a href="http://stackoverflow.com/">StackOverflow</a> and I’ve ended posting my <a href="http://stackoverflow.com/questions/14842927/powershell-moving-items-not-working-when-filenames-that-have-chars">question</a> over there. One of the answers suggested to use the –LiteralPath parameter for the <a href="http://technet.microsoft.com/en-us/library/hh849852.aspx">move-item</a> cmdlet. According to the docs, the –LiteralPath parameter :</p>
<blockquote>
<p>Specifies the path to the current location of the items. Unlike Path, the value of LiteralPath is used <strong>exactly as it is typed</strong>. No characters are interpreted as <strong>wildcards</strong>. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.</p>
</blockquote>
<p><font style="background-color:#1b1814;">Did you notice the wildcards part? Yep, <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa717088(v=vs.85).aspx">wildcards</a>…unfortunately form me, I had completely forgotten that [] define wildcards which match a range of characters (note to self: do read the PowerShell docs). For instance, and this is just copied from the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa717088(v=vs.85).aspx">docs</a>, here’s a quick example:</font></p>
<p><font style="background-color:#1b1814;"></font>[a-l]ook matches book, cook or look, but not took.</p>
<p>Aha! ok, now it makes sense…that’s why those files which contained the [ ] on its name weren’t being copied. PowerShell was translating the values defined within [ ]&#160; into ranges. Another note to self: don’t forget about wildcards when you’re writing PowerShell scripts. </p>
<p>And that’s it for now. Stay tuned for more.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luisabreu.wordpress.com/1819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luisabreu.wordpress.com/1819/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1819&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" /><img src="http://feeds.feedburner.com/~r/Laneten/~4/i1Z-YOIZ1RM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://luisabreu.wordpress.com/2013/02/15/theliteralpath-parameter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ef50ae4032124f87927d917ac713ee3b?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">luisabreu</media:title>
		</media:content>
	<feedburner:origLink>http://luisabreu.wordpress.com/2013/02/15/theliteralpath-parameter/</feedburner:origLink></item>
		<item>
		<title>Updated material for ASP.NET MVC book</title>
		<link>http://feedproxy.google.com/~r/Laneten/~3/GuYefX2cpko/</link>
		<comments>http://luisabreu.wordpress.com/2013/02/14/updated-material-for-asp-net-mvc-book/#comments</comments>
		<pubDate>Thu, 14 Feb 2013 21:04:04 +0000</pubDate>
		<dc:creator>Luis Abreu</dc:creator>
				<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">https://luisabreu.wordpress.com/?p=1818</guid>
		<description><![CDATA[If you’ve bought the ASP.NET book I’ve written, you’ll probably be happy to know that I’ve added a new online chapter that deepens the initial cover of the Web API presented in chapter 9 of the book. If you’ve bought the book, just head to the web site and download the extra material associated with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1818&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>If you’ve bought the <a href="http://www.fca.pt/cgi-bin/fca_main.cgi/?op=2&amp;isbn=978-972-722-742-6">ASP.NET book</a> I’ve written, you’ll probably be happy to know that I’ve added a new <a href="http://www.fca.pt/cgi-bin/fca_main.cgi/?op=2&amp;isbn=978-972-722-742-6">online chapter</a> that deepens the initial cover of the <a href="http://www.asp.net/web-api">Web API</a> presented in chapter 9 of the book. If you’ve bought the book, just head to the <a href="http://www.fca.pt/cgi-bin/fca_main.cgi/?op=2&amp;isbn=978-972-722-742-6">web site</a> and download the extra material associated with the book.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luisabreu.wordpress.com/1818/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luisabreu.wordpress.com/1818/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1818&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" /><img src="http://feeds.feedburner.com/~r/Laneten/~4/GuYefX2cpko" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://luisabreu.wordpress.com/2013/02/14/updated-material-for-asp-net-mvc-book/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ef50ae4032124f87927d917ac713ee3b?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">luisabreu</media:title>
		</media:content>
	<feedburner:origLink>http://luisabreu.wordpress.com/2013/02/14/updated-material-for-asp-net-mvc-book/</feedburner:origLink></item>
		<item>
		<title>Modules: overriding properties</title>
		<link>http://feedproxy.google.com/~r/Laneten/~3/c8IVMGV0ou0/</link>
		<comments>http://luisabreu.wordpress.com/2013/02/14/modules-overriding-properties/#comments</comments>
		<pubDate>Thu, 14 Feb 2013 15:33:19 +0000</pubDate>
		<dc:creator>Luis Abreu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://luisabreu.wordpress.com/?p=1817</guid>
		<description><![CDATA[As we’ve seen in the previous post, we can add properties to an object exposed by a module through a technique known as augmentation. Even though there are some advantages associated with the approach shown (ex.: no need for loading scripts in a specific order), the truth is that we’re out of luck if we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1817&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>As we’ve seen in the previous <a href="http://feedproxy.google.com/~r/Laneten/~3/VJEssoUxqsU/">post</a>, we can add properties to an object exposed by a module through a technique known as augmentation. Even though there are some advantages associated with the <a href="http://feedproxy.google.com/~r/Laneten/~3/VJEssoUxqsU/">approach shown</a> (ex.: no need for loading scripts in a specific order), the truth is that we’re out of luck if we need to “override” a property defined in one of the files of the module. As I’ve said, in this case order of loading is important and there’s no way around it (after all, if we’re augmenting a property, it surely must have already been defined before being augmented, right?).</p>
<p>In order to illustrate this approach, we’ll rewrite the <a href="http://A partir de Janeiro passa a vigorar um novo modelo para emiss&atilde;o das facturas, a factura simplificada que deve ser entregue ao ao cliente, mas para esse documento dar direito &agrave; dedu&ccedil;&atilde;o, o cliente ter&aacute; que disponibilizar o n&uacute;mero de identifica&ccedil;&atilde;o fiscal, que deve ser incluido na factura.">previous example</a>. We’ll remove the jQuery dependency and will change the code so that each file defines&#160; a single printValue (file 2 overrides and augments this method). The following snippet shows the code I’ve ended writing in each file (assume we’re using strict mode and that file 1 is loaded before file 2):</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:022fdba7-d99b-4d8b-bde8-8d2bb29db723" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#000000;color:#808080;">//file1</span><br /> <span style="background:#000000;color:#cc7832;">var</span><span style="background:#000000;color:#ffffff;"> obj = (</span><span style="background:#000000;color:#cc7832;">function</span><span style="background:#000000;color:#ffffff;"> (baseModule) {</span><br /> <span style="background:#000000;color:#ffffff;">    </span><span style="background:#000000;color:#cc7832;">var</span><span style="background:#000000;color:#ffffff;"> someValue = </span><span style="background:#000000;color:#a5c25c;">&quot;private&quot;</span><span style="background:#000000;color:#ffffff;">;</span><br /> <span style="background:#000000;color:#ffffff;">    </span><span style="background:#000000;color:#cc7832;">function</span><span style="background:#000000;color:#ffffff;"> printValue(elemSelector) {</span><br /> <span style="background:#000000;color:#ffffff;">        </span><span style="background:#000000;color:#cc7832;">return</span><span style="background:#000000;color:#ffffff;"> someValue;</span><br /> <span style="background:#000000;color:#ffffff;">    }</span><br /> <span style="background:#000000;color:#ffffff;">    </span><span style="background:#000000;color:#cc7832;">return</span><span style="background:#000000;color:#ffffff;"> {</span><br /> <span style="background:#000000;color:#ffffff;">        printValue: printValue</span><br /> <span style="background:#000000;color:#ffffff;">    };</span><br /> <span style="background:#000000;color:#ffffff;">})();</span><br /> <span style="background:#000000;color:#808080;">//file2</span><br /> <span style="background:#000000;color:#ffffff;">(</span><span style="background:#000000;color:#cc7832;">function</span><span style="background:#000000;color:#ffffff;"> (baseModule) {</span><br /> <span style="background:#000000;color:#ffffff;">    </span><span style="background:#000000;color:#cc7832;">var</span><span style="background:#000000;color:#ffffff;"> someOtherValue = </span><span style="background:#000000;color:#a5c25c;">&quot;private 2&quot;</span><span style="background:#000000;color:#ffffff;">;</span><br /> <span style="background:#000000;color:#ffffff;">    </span><span style="background:#000000;color:#808080;">//save reference to overriden method</span><br /> <span style="background:#000000;color:#ffffff;">    </span><span style="background:#000000;color:#cc7832;">var</span><span style="background:#000000;color:#ffffff;"> otherModulePrintAnotherValue = baseModule.printValue;</span><br /> <span style="background:#000000;color:#ffffff;">    </span><span style="background:#000000;color:#cc7832;">function</span><span style="background:#000000;color:#ffffff;"> printValueOverride(elemSelector) {</span><br /> <span style="background:#000000;color:#ffffff;">        </span><span style="background:#000000;color:#cc7832;">return</span><span style="background:#000000;color:#ffffff;"> otherModulePrintAnotherValue() + </span><span style="background:#000000;color:#a5c25c;">&quot;&#8212;&quot;</span><span style="background:#000000;color:#ffffff;"> + someOtherValue;</span><br /> <span style="background:#000000;color:#ffffff;">    }</span><br /> <span style="background:#000000;color:#ffffff;">    </span><span style="background:#000000;color:#808080;">//change reference to new method</span><br /> <span style="background:#000000;color:#ffffff;">    baseModule.printValue = printValueOverride;</span><br /> <span style="background:#000000;color:#ffffff;">})(obj);</span></div>
</p></div>
</p></div>
<p>If you compare this code with the one we had before, you’ll notice several differences. The most interesting part happens within the anonymous function defined in file2. As you can see, I start by saving a reference to the printValue method which was defined by the module introduced by file1. This reference is used by another function which will end up overriding the initial printValue method. Without this reference, it would be impossible to call the function after performing the override. From now on, you can simply call the overridden function like this (assuming we’ve got an element with ID set to info):</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a55d6571-e25a-44f7-9075-c20fbd1a88c5" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#000000;color:#ffffff;">&lt;</span><span style="background:#000000;color:#e8bc64;">script</span><span style="background:#000000;color:#ffffff;">&gt;</span><br /> <span style="background:#000000;color:#ffffff;">    </span><span style="background:#000000;color:#a5c25c;">&quot;use strict&quot;</span><span style="background:#000000;color:#ffffff;">;</span><br /> <span style="background:#000000;color:#ffffff;">    $(</span><span style="background:#000000;color:#a5c25c;">&quot;#info&quot;</span><span style="background:#000000;color:#ffffff;">).text(obj.printValue());</span><br /> <span style="background:#000000;color:#ffffff;">&lt;/</span><span style="background:#000000;color:#e8bc64;">script</span><span style="background:#000000;color:#ffffff;">&gt;</span></div>
</p></div>
</p></div>
<p>And that’s it for now. Stay tuned for more.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luisabreu.wordpress.com/1817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luisabreu.wordpress.com/1817/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1817&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" /><img src="http://feeds.feedburner.com/~r/Laneten/~4/c8IVMGV0ou0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://luisabreu.wordpress.com/2013/02/14/modules-overriding-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ef50ae4032124f87927d917ac713ee3b?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">luisabreu</media:title>
		</media:content>
	<feedburner:origLink>http://luisabreu.wordpress.com/2013/02/14/modules-overriding-properties/</feedburner:origLink></item>
		<item>
		<title>More about modules: augmentation</title>
		<link>http://feedproxy.google.com/~r/Laneten/~3/VJEssoUxqsU/</link>
		<comments>http://luisabreu.wordpress.com/2013/02/07/more-about-modules-augmentation/#comments</comments>
		<pubDate>Thu, 07 Feb 2013 22:15:55 +0000</pubDate>
		<dc:creator>Luis Abreu</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">https://luisabreu.wordpress.com/?p=1816</guid>
		<description><![CDATA[In the previous posts, I’ve introduced some basics aspects related to the implementation of the module pattern in JavaScript. Even though it introduces several benefits, there are some problems that might make you think that this pattern isn’t for you. For instance, if you’re working in a team where several members are contributing to the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1816&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In the <a href="http://feedproxy.google.com/~r/Laneten/~3/hhu2a1_xsto/">previous</a> <a href="http://feedproxy.google.com/~r/Laneten/~3/UTNX279FvAo/">posts</a>, I’ve introduced some basics aspects related to the implementation of the module pattern in JavaScript. Even though it introduces several benefits, there are some problems that might make you think that this pattern isn’t for you. For instance, if you’re working in a team where several members are contributing to the same module at the same time, then your best option is to spread&#160; the module across different files. </p>
<p>Before going on, I must confess that splitting modules across different files isn’t really something I love. Whenever I’m facing this decision, I always try to re-check my code and see if I can refactor it so that a module is defined in a single file. There are, however, some cases where that isn’t possible and in those cases we need strategies like augmentation. The best way to illustrate it is to show an example. We’ll augment the module introduced in the previous <a href="http://feedproxy.google.com/~r/Laneten/~3/UTNX279FvAo/">post</a> by adding a new method. Here’s one way to do it (notice that this code should be placed into a different JavaScript file):</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2c678316-bcd6-4002-9d40-ca69f2145355" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#1e1e1e;color:#569cd6;">var</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#569cd6;">function</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">baseModule</span><span style="background:#1e1e1e;color:#b4b4b4;">,</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">$</span><span style="background:#1e1e1e;color:#b4b4b4;">)</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">var</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">someOtherValue</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#d69d85;">&quot;private 2&quot;</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">function</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">printAnotherValue</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">elemSelector</span><span style="background:#1e1e1e;color:#b4b4b4;">)</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">        </span><span style="background:#1e1e1e;color:#ffffff;">$</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">elemSelector</span><span style="background:#1e1e1e;color:#b4b4b4;">).</span><span style="background:#1e1e1e;color:#ffffff;">text</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">someOtherValue</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    }</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#ffffff;">baseModule</span><span style="background:#1e1e1e;color:#b4b4b4;">.</span><span style="background:#1e1e1e;color:#ffffff;">printAnotherValue</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">printAnotherValue</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">return</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">baseModule</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">}</span><span style="background:#1e1e1e;color:#b4b4b4;">)(</span><span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#b4b4b4;">,</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">jQuery</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span><br /> <span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#b4b4b4;">.</span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#d69d85;">&quot;#info&quot;</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span><br /> <span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#b4b4b4;">.</span><span style="background:#1e1e1e;color:#ffffff;">printAnotherValue</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#d69d85;">&quot;#info2&quot;</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span></div>
</p></div>
</p></div>
<p>Notice that this second anonymous function receives a reference to the original module which is being augmented (besides the jQuery global reference). There’s a slight gotcha with the previous code: you need to preserve the correct order when importing the files of the module. In the previous sample, that is not needed because none of the files uses properties defined in the other module, so we should do everything in our power to remove that dependency. Fortunately for us, we can remove that restriction by slightly changing the code: </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5edbd45a-26b5-4053-944c-b10a5d590070" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#1e1e1e;color:#569cd6;">var</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#569cd6;">function</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">baseModule</span><span style="background:#1e1e1e;color:#b4b4b4;">,</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">$</span><span style="background:#1e1e1e;color:#b4b4b4;">)</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">var</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">someValue</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#d69d85;">&quot;private&quot;</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">function</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">elemSelector</span><span style="background:#1e1e1e;color:#b4b4b4;">)</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">        </span><span style="background:#1e1e1e;color:#ffffff;">$</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">elemSelector</span><span style="background:#1e1e1e;color:#b4b4b4;">).</span><span style="background:#1e1e1e;color:#ffffff;">text</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">someValue</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    }</span></p>
<p> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#ffffff;">baseModule</span><span style="background:#1e1e1e;color:#b4b4b4;">.</span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">return</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">baseModule</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">}</span><span style="background:#1e1e1e;color:#b4b4b4;">)(</span><span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">||</span><span style="background:#1e1e1e;color:#dcdcdc;"> {}</span><span style="background:#1e1e1e;color:#b4b4b4;">,</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">jQuery</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span><br /> <span style="background:#1e1e1e;color:#569cd6;">var</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#569cd6;">function</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">baseModule</span><span style="background:#1e1e1e;color:#b4b4b4;">,</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">$</span><span style="background:#1e1e1e;color:#b4b4b4;">)</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">var</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">someOtherValue</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#d69d85;">&quot;private 2&quot;</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">function</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">printAnotherValue</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">elemSelector</span><span style="background:#1e1e1e;color:#b4b4b4;">)</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">        </span><span style="background:#1e1e1e;color:#ffffff;">$</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">elemSelector</span><span style="background:#1e1e1e;color:#b4b4b4;">).</span><span style="background:#1e1e1e;color:#ffffff;">text</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">someOtherValue</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    }</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#ffffff;">baseModule</span><span style="background:#1e1e1e;color:#b4b4b4;">.</span><span style="background:#1e1e1e;color:#ffffff;">printAnotherValue</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">printAnotherValue</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">return</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">baseModule</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">}</span><span style="background:#1e1e1e;color:#b4b4b4;">)(</span><span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">||</span><span style="background:#1e1e1e;color:#dcdcdc;"> {}</span><span style="background:#1e1e1e;color:#b4b4b4;">,</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">jQuery</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span></div>
</p></div>
</p></div>
<p>According to the standard, the operator || will return the first expression when it’s convertible to true. If that isn’t possible, then it will return the second expression. In my example, we can use an empty object ({}) to ensure that we’re always working with a valid object within the anonymous functions that define the module. Since there is no dependency between the files, then this is more than enough for removing the order dependency introduced by our first snippet.</p>
<p>Even though the previous snippet allows one method to call another (ie, if, for instance, we need to call printValue from within printAnotherValue at runtime, then we should be fine), if we need to access&#160; properties in order to override them, then we’re out of luck . In these cases, we need to change the and there’s no way to escape the need to preserve the loading order. That will be the topic for another post. Stay tuned for more.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luisabreu.wordpress.com/1816/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luisabreu.wordpress.com/1816/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1816&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" /><img src="http://feeds.feedburner.com/~r/Laneten/~4/VJEssoUxqsU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://luisabreu.wordpress.com/2013/02/07/more-about-modules-augmentation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ef50ae4032124f87927d917ac713ee3b?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">luisabreu</media:title>
		</media:content>
	<feedburner:origLink>http://luisabreu.wordpress.com/2013/02/07/more-about-modules-augmentation/</feedburner:origLink></item>
		<item>
		<title>JavaScript: more about the module pattern</title>
		<link>http://feedproxy.google.com/~r/Laneten/~3/UTNX279FvAo/</link>
		<comments>http://luisabreu.wordpress.com/2013/02/05/javascript-more-about-the-module-pattern/#comments</comments>
		<pubDate>Tue, 05 Feb 2013 22:28:39 +0000</pubDate>
		<dc:creator>Luis Abreu</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">https://luisabreu.wordpress.com/?p=1815</guid>
		<description><![CDATA[In the previous post, we’ve started looking at the module pattern and how we can use it to write modular JavaScript code. I’ve ended the post mentioning that we still need to discuss an important topic before wrapping up the basics aspects associated with the pattern. If you’ve been following along, you know I’m talking [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1815&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In the previous <a href="http://feedproxy.google.com/~r/Laneten/~3/hhu2a1_xsto/">post</a>, we’ve started looking at the module pattern and how we can use it to write modular JavaScript code. I’ve ended the <a href="http://feedproxy.google.com/~r/Laneten/~3/hhu2a1_xsto/">post</a> mentioning that we still need to discuss an important topic before wrapping up the basics aspects associated with the pattern. If you’ve been following along, you know I’m talking about dependencies. In order to illustrate the topic, I’ll modify the previous example so that we use <a href="www.jquery.com">jQuery</a> to print the message inside an element whose ID will be passed as a parameter. The next snippet shows our final code:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:52621442-cb80-4b2f-a587-a33a1e4f247a" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#1e1e1e;color:#569cd6;">var</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#569cd6;">function</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">()</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">var</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">someValue</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#d69d85;">&quot;private&quot;</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">function</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">elemSelector</span><span style="background:#1e1e1e;color:#b4b4b4;">)</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">        </span><span style="background:#1e1e1e;color:#ffffff;">$</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">elemSelector</span><span style="background:#1e1e1e;color:#b4b4b4;">).</span><span style="background:#1e1e1e;color:#ffffff;">text</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">someValue</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    }</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">return</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">        </span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><span style="background:#1e1e1e;color:#b4b4b4;">:</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    }</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">}</span><span style="background:#1e1e1e;color:#b4b4b4;">)();</span><br /> <span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#b4b4b4;">.</span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#d69d85;">&quot;#info&quot;</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span></div>
</p></div>
</p></div>
<p>Now, if you’ve been doing JS for some time, you’ve probably noticed something which might get you worried: we’re depending on a global. Technically, $ is an alias to the jQuery global object. In other words, were depending on an implied global and whenever the JS interpreter engine finds one, it’ll have to walk the scope chain backwards until it finds a valid reference (in our example, it’ll walk until it gets to the global scope). Now, in our case, we’d end with an error if jQuery hasn’t been imported before our script gets run. Things could be worse though. For instance, suppose we were using the implied global in the left sign of an assignment expression. In that case, if the global didn’t exist, we’d end up creating a new one (if we were not running in strict mode).</p>
<p>Fortunately, there’s an easy solution for this kind of problem: I’m talking about global imports. Whenever you need a reference to a global variable within the closure created by the anonymous function, you just pass it as a parameter. The next snippet shows this strategy in action:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e915476d-a09b-4c14-ba90-1cb41d8cad89" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#1e1e1e;color:#569cd6;">var</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">obj</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#569cd6;">function</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">$</span><span style="background:#1e1e1e;color:#b4b4b4;">)</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">var</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">someValue</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#b4b4b4;">=</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#d69d85;">&quot;private&quot;</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">function</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">elemSelector</span><span style="background:#1e1e1e;color:#b4b4b4;">)</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">        </span><span style="background:#1e1e1e;color:#ffffff;">$</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">elemSelector</span><span style="background:#1e1e1e;color:#b4b4b4;">).</span><span style="background:#1e1e1e;color:#ffffff;">text</span><span style="background:#1e1e1e;color:#b4b4b4;">(</span><span style="background:#1e1e1e;color:#ffffff;">someValue</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    }</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    </span><span style="background:#1e1e1e;color:#569cd6;">return</span><span style="background:#1e1e1e;color:#dcdcdc;"> {</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">        </span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><span style="background:#1e1e1e;color:#b4b4b4;">:</span><span style="background:#1e1e1e;color:#dcdcdc;"> </span><span style="background:#1e1e1e;color:#ffffff;">printValue</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">    }</span><span style="background:#1e1e1e;color:#b4b4b4;">;</span><br /> <span style="background:#1e1e1e;color:#dcdcdc;">}</span><span style="background:#1e1e1e;color:#b4b4b4;">)(</span><span style="background:#1e1e1e;color:#ffffff;">jQuery</span><span style="background:#1e1e1e;color:#b4b4b4;">);</span></div>
</p></div>
</p></div>
<p>Even though we’re still using the $ alias, this time it refers to the local parameter defined by the anonymous function which is initialized with a reference to the jQuery global variable. With this strategy, we’ve ended with better code which is also faster. </p>
<p>Now that we already have gone through the basics, we’re ready to take a look at some more advanced scenarios. Stay tuned for more.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luisabreu.wordpress.com/1815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luisabreu.wordpress.com/1815/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1815&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" /><img src="http://feeds.feedburner.com/~r/Laneten/~4/UTNX279FvAo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://luisabreu.wordpress.com/2013/02/05/javascript-more-about-the-module-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ef50ae4032124f87927d917ac713ee3b?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">luisabreu</media:title>
		</media:content>
	<feedburner:origLink>http://luisabreu.wordpress.com/2013/02/05/javascript-more-about-the-module-pattern/</feedburner:origLink></item>
		<item>
		<title>JavaScript: Getting started with the Module pattern</title>
		<link>http://feedproxy.google.com/~r/Laneten/~3/hhu2a1_xsto/</link>
		<comments>http://luisabreu.wordpress.com/2013/02/04/javascript-getting-started-with-the-module-pattern/#comments</comments>
		<pubDate>Mon, 04 Feb 2013 23:16:28 +0000</pubDate>
		<dc:creator>Luis Abreu</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">https://luisabreu.wordpress.com/?p=1814</guid>
		<description><![CDATA[Today, the module pattern is one of the most used JavaScript patterns and that means that we, developers that need to write and build Web Apps, need to understand it well. In this post, I’ll introduce the pattern and I’ll come back to the topic in future posts. To the best of my knowledge, the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1814&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Today, the module pattern is one of the most used JavaScript patterns and that means that we, developers that need to write and build Web Apps, need to understand it well. In this post, I’ll introduce the pattern and I’ll come back to the topic in future posts. To the best of my knowledge, the pattern was introduced by Douglas Crockford and its main objective is to restrict the use of global variables and the pollution of the global namespace (leading to less collusions).</p>
<p>It goes without saying that global variables are evil. They’re really a source of unreliability and even insecurity. Reducing their usage leads to less collisions, which is a great thing in our days, where we need to use and mix several JavaScript libraries. </p>
<p>As we’ll see in the next paragraphs, the module pattern depends heavily in the use of anonymous closures. Anonymous closures are created by introducing an anonymous function which is executed immediately. The code run inside that function ensures privacy and state throughout the lifetime of the app. Notice also that this function will typically return an object which can be used to access that internal state. The following snippet illustrates the previous points:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ce0f6dfc-8125-43e0-907f-5d727350d7db" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#000000;color:#cc7832;">var</span><span style="background:#000000;color:#ffffff;"> obj = (</span><span style="background:#000000;color:#cc7832;">function</span><span style="background:#000000;color:#ffffff;">() {</span><br />     <span style="background:#000000;color:#ffffff;"></span><span style="background:#000000;color:#cc7832;">var</span><span style="background:#000000;color:#ffffff;"> someValue = </span><span style="background:#000000;color:#a5c25c;">&quot;private&quot;</span><span style="background:#000000;color:#ffffff;">;</span><br />     <span style="background:#000000;color:#ffffff;"></span><span style="background:#000000;color:#cc7832;">function</span><span style="background:#000000;color:#ffffff;"> printValue() {</span><br />         <span style="background:#000000;color:#ffffff;"></span><span style="background:#000000;color:#cc7832;">return</span><span style="background:#000000;color:#ffffff;"> someValue;</span><br />     <span style="background:#000000;color:#ffffff;">}</span></p>
<p>     <span style="background:#000000;color:#ffffff;"></span><span style="background:#000000;color:#cc7832;">return</span><span style="background:#000000;color:#ffffff;"> {</span><br />         <span style="background:#000000;color:#ffffff;">printValue: printValue  </span><br />     <span style="background:#000000;color:#ffffff;">};</span><br /> <span style="background:#000000;color:#ffffff;">})();</span><br /> <span style="background:#000000;color:#ffffff;">console.log(obj.printValue());</span></div>
</p></div>
</p></div>
<p>As you can see, we’re defining and invoking an anonymous function. The invocation part is really important. If you miss the () operator, you’ll end up injecting&#160; a function in the current scope (which, in the previous example, is the global scope). In&#160; other words, we would end up polluting the global namespace,&#160; which is precisely the problem we’re trying to solve. As I said, the execution of the function ends up creating a closure. Since I had the good sense of using the var keyword when I introduced the someValue variable, I’ve ended up creating a local variable and it will only be available to code defined within that anonymous function (this is the privacy advantage I’ve mentioned before). The same thing happens to the internal printValue function: it’s only available to other code written within the anonymous function.</p>
<p>You’ve also noticed that the anonymous function returns an object with a single property. In practice, this is known exporting a value from the module. In this case, the single property of the exported object is a reference to the private printValue defined within the anonymous function and that means that we can call that function through the obj reference (as illustrated in the last line).</p>
<p>The previous code shows how to create a basic module, but we still have an additional aspect to discuss before wrapping up the modules: what about dependencies? I’ll leave that topic to a future post. Stay tuned for more.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luisabreu.wordpress.com/1814/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luisabreu.wordpress.com/1814/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1814&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" /><img src="http://feeds.feedburner.com/~r/Laneten/~4/hhu2a1_xsto" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://luisabreu.wordpress.com/2013/02/04/javascript-getting-started-with-the-module-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ef50ae4032124f87927d917ac713ee3b?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">luisabreu</media:title>
		</media:content>
	<feedburner:origLink>http://luisabreu.wordpress.com/2013/02/04/javascript-getting-started-with-the-module-pattern/</feedburner:origLink></item>
		<item>
		<title>NHibernate and inverse: my view on that subject</title>
		<link>http://feedproxy.google.com/~r/Laneten/~3/aiMYu_h7ll4/</link>
		<comments>http://luisabreu.wordpress.com/2013/01/28/nhibernate-and-inverse-my-view-on-that-subject/#comments</comments>
		<pubDate>Mon, 28 Jan 2013 22:34:45 +0000</pubDate>
		<dc:creator>Luis Abreu</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">https://luisabreu.wordpress.com/?p=1813</guid>
		<description><![CDATA[NHibernate’s inverse concept is one of&#160; most discussed mappings features (if not the most!) defined by it. In practice, you’ll be using the inverse when you need to set the owner of a “relationship”. Before taking a dive into it and how it affects the way NH generates SQL and manages relationships between entities, here [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1813&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>NHibernate’s inverse concept is one of&#160; most discussed mappings features (if not the most!) defined by it. In practice, you’ll be using the inverse when you need to set the owner of a “relationship”. Before taking a dive into it and how it affects the way NH generates SQL and manages relationships between entities, here are some facts about inverse:</p>
<ul>
<li>It’s a Boolean attribute used only in collection&#160; and join mappings;</li>
<li>By default, it’s set to false;</li>
<li>It only makes sense when you’re configuring bidirectional relationships between entities;</li>
<li>You should only set inverse to true on one of the sides of the relationship;</li>
<li>Not setting it on any of the sides will generate superfluous SQL instructions.</li>
</ul>
<p>Ok, before trying to understand what this means, lets start with a RDE (ie, a really dumb example): Blogs and posts will do it for now!</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:dc114937-b815-46e1-9b29-9a61ade437be" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">class</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">Blog</span><span style="background:#22282a;color:#f1f2f3;"> {</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">Int32</span><span style="background:#22282a;color:#f1f2f3;"> BlogId { </span><span style="background:#22282a;color:#93c763;">get</span><span style="background:#22282a;color:#f1f2f3;">; </span><span style="background:#22282a;color:#93c763;">set</span><span style="background:#22282a;color:#f1f2f3;">; }</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">Int32</span><span style="background:#22282a;color:#f1f2f3;"> Version { </span><span style="background:#22282a;color:#93c763;">get</span><span style="background:#22282a;color:#f1f2f3;">; </span><span style="background:#22282a;color:#93c763;">set</span><span style="background:#22282a;color:#f1f2f3;">; }</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">String</span><span style="background:#22282a;color:#f1f2f3;"> Description { </span><span style="background:#22282a;color:#93c763;">get</span><span style="background:#22282a;color:#f1f2f3;">; </span><span style="background:#22282a;color:#93c763;">set</span><span style="background:#22282a;color:#f1f2f3;">; }</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">private</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#8c8cb4;">IList</span><span style="background:#22282a;color:#e8e2b7;">&lt;</span><span style="background:#22282a;color:#678cb1;">Post</span><span style="background:#22282a;color:#e8e2b7;">&gt;</span><span style="background:#22282a;color:#f1f2f3;"> _posts </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">new</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">List</span><span style="background:#22282a;color:#e8e2b7;">&lt;</span><span style="background:#22282a;color:#678cb1;">Post</span><span style="background:#22282a;color:#e8e2b7;">&gt;</span><span style="background:#22282a;color:#f1f2f3;">();</span></p>
<p> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#8c8cb4;">IEnumerable</span><span style="background:#22282a;color:#e8e2b7;">&lt;</span><span style="background:#22282a;color:#678cb1;">Post</span><span style="background:#22282a;color:#e8e2b7;">&gt;</span><span style="background:#22282a;color:#f1f2f3;"> Posts {</span><br /> <span style="background:#22282a;color:#f1f2f3;">        </span><span style="background:#22282a;color:#93c763;">get</span><span style="background:#22282a;color:#f1f2f3;"> { </span><span style="background:#22282a;color:#93c763;">return</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">new</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">ReadOnlyCollection</span><span style="background:#22282a;color:#e8e2b7;">&lt;</span><span style="background:#22282a;color:#678cb1;">Post</span><span style="background:#22282a;color:#e8e2b7;">&gt;</span><span style="background:#22282a;color:#f1f2f3;">(_posts); }</span><br /> <span style="background:#22282a;color:#f1f2f3;">        </span><span style="background:#22282a;color:#93c763;">set</span><span style="background:#22282a;color:#f1f2f3;"> { _posts </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">new</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">List</span><span style="background:#22282a;color:#e8e2b7;">&lt;</span><span style="background:#22282a;color:#678cb1;">Post</span><span style="background:#22282a;color:#e8e2b7;">&gt;</span><span style="background:#22282a;color:#f1f2f3;">(</span><span style="background:#22282a;color:#93c763;">value</span><span style="background:#22282a;color:#f1f2f3;">); }</span><br /> <span style="background:#22282a;color:#f1f2f3;">    }</span></p>
<p> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">void</span><span style="background:#22282a;color:#f1f2f3;"> AddPost(</span><span style="background:#22282a;color:#678cb1;">Post</span><span style="background:#22282a;color:#f1f2f3;"> post) {</span><br /> <span style="background:#22282a;color:#f1f2f3;">        post</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Blog </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">this</span><span style="background:#22282a;color:#f1f2f3;">;</span><br /> <span style="background:#22282a;color:#f1f2f3;">        _posts</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Add(post);</span><br /> <span style="background:#22282a;color:#f1f2f3;">    }</span></p>
<p> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">void</span><span style="background:#22282a;color:#f1f2f3;"> RemoveAll() {</span><br /> <span style="background:#22282a;color:#f1f2f3;">        </span><span style="background:#22282a;color:#93c763;">foreach</span><span style="background:#22282a;color:#f1f2f3;"> (</span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> post </span><span style="background:#22282a;color:#93c763;">in</span><span style="background:#22282a;color:#f1f2f3;"> _posts) {</span><br /> <span style="background:#22282a;color:#f1f2f3;">            post</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Blog </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">null</span><span style="background:#22282a;color:#f1f2f3;">;</span><br /> <span style="background:#22282a;color:#f1f2f3;">        }</span><br /> <span style="background:#22282a;color:#f1f2f3;">        _posts</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Clear();</span><br /> <span style="background:#22282a;color:#f1f2f3;">    }</span><br /> <span style="background:#22282a;color:#f1f2f3;">}</span></p>
<p> <span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">class</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">Post</span><span style="background:#22282a;color:#f1f2f3;"> {</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">Int32</span><span style="background:#22282a;color:#f1f2f3;"> PostId { </span><span style="background:#22282a;color:#93c763;">get</span><span style="background:#22282a;color:#f1f2f3;">; </span><span style="background:#22282a;color:#93c763;">set</span><span style="background:#22282a;color:#f1f2f3;">; }</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">String</span><span style="background:#22282a;color:#f1f2f3;"> Description { </span><span style="background:#22282a;color:#93c763;">get</span><span style="background:#22282a;color:#f1f2f3;">; </span><span style="background:#22282a;color:#93c763;">set</span><span style="background:#22282a;color:#f1f2f3;">; }</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">Blog</span><span style="background:#22282a;color:#f1f2f3;"> Blog { </span><span style="background:#22282a;color:#93c763;">get</span><span style="background:#22282a;color:#f1f2f3;">; </span><span style="background:#22282a;color:#93c763;">set</span><span style="background:#22282a;color:#f1f2f3;">; }</span><br /> <span style="background:#22282a;color:#f1f2f3;">}</span></div>
</p></div>
</p></div>
<p>As you can see, there’s nothing too fancy going on here. Each blog has a collection of posts (with each post referencing back its parent blog) and you’ll typically use the AddPost method to add a new Post. After building the classes, we can concentrate in building the mappings. And nothing like <a href="http://www.fluentnhibernate.org/">Fluent NHibernate</a> to give us a hand. Since I’m not really a great fan of automapping, here’s the code I’ve written to get us started:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ee0d6f75-468f-4b87-b893-8dd2e1fb113a" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">class</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">PostMapping</span><span style="background:#22282a;color:#f1f2f3;"> : </span><span style="background:#22282a;color:#678cb1;">ClassMap</span><span style="background:#22282a;color:#e8e2b7;">&lt;</span><span style="background:#22282a;color:#678cb1;">Post</span><span style="background:#22282a;color:#e8e2b7;">&gt;</span><span style="background:#22282a;color:#f1f2f3;"> {</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> PostMapping() {</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Table(</span><span style="background:#22282a;color:#ec7600;">&quot;Post&quot;</span><span style="background:#22282a;color:#f1f2f3;">);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Not</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">LazyLoad();</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Id(p </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> p</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">PostId)</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">GeneratedBy</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Identity()</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Default(</span><span style="background:#22282a;color:#ffcd22;">0</span><span style="background:#22282a;color:#f1f2f3;">);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Map(p </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> p</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Description);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        References(p </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> p</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Blog, </span><span style="background:#22282a;color:#ec7600;">&quot;BlogId&quot;</span><span style="background:#22282a;color:#f1f2f3;">)</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Not</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">LazyLoad();</span><br /> <span style="background:#22282a;color:#f1f2f3;">    }</span><br /> <span style="background:#22282a;color:#f1f2f3;">}</span></p>
<p> <span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">class</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">BlogMapping</span><span style="background:#22282a;color:#f1f2f3;"> : </span><span style="background:#22282a;color:#678cb1;">ClassMap</span><span style="background:#22282a;color:#e8e2b7;">&lt;</span><span style="background:#22282a;color:#678cb1;">Blog</span><span style="background:#22282a;color:#e8e2b7;">&gt;</span><span style="background:#22282a;color:#f1f2f3;"> {</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> BlogMapping() {</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Table(</span><span style="background:#22282a;color:#ec7600;">&quot;Blog&quot;</span><span style="background:#22282a;color:#f1f2f3;">);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Not</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">LazyLoad();</span></p>
<p> <span style="background:#22282a;color:#f1f2f3;">        Id(b </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> b</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">BlogId)</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">GeneratedBy</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Identity()</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Default(</span><span style="background:#22282a;color:#ffcd22;">0</span><span style="background:#22282a;color:#f1f2f3;">);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Map(b </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> b</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Description);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Version(b </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> b</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Version);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        HasMany(b </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> b</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Posts)</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Access</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">CamelCaseField(</span><span style="background:#22282a;color:#678cb1;">Prefix</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Underscore)</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">AsBag()</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Cascade</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">AllDeleteOrphan()</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Not</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">LazyLoad()</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">KeyColumn(</span><span style="background:#22282a;color:#ec7600;">&quot;BlogId&quot;</span><span style="background:#22282a;color:#f1f2f3;">);</span><br /> <span style="background:#22282a;color:#f1f2f3;">    }</span><br /> <span style="background:#22282a;color:#f1f2f3;">}</span></div>
</p></div>
</p></div>
<p>This is more than enough for you to reproduce the tables (if you’re interested in running the examples). I’m also not showing the code I’ve written to create NH’s session factory from these mappings (Fluent’s wiki <a href="https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started">shows you</a> how to get started, so I’m not repeating it here). Having said that, lets start with the code required to create a new Blog:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ac94642a-8b55-4c44-9b07-b28430955726" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#22282a;color:#93c763;">private</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">static</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">void</span><span style="background:#22282a;color:#f1f2f3;"> CreateNewBlog() {</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> sessionFactory </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">SessionFactory</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">CreateSessionFactory();</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">using</span><span style="background:#22282a;color:#f1f2f3;"> (</span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> session </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> sessionFactory</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">OpenSession()) {</span><br /> <span style="background:#22282a;color:#f1f2f3;">        </span><span style="background:#22282a;color:#93c763;">using</span><span style="background:#22282a;color:#f1f2f3;"> (</span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> tran </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> session</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">BeginTransaction()) {</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> blog </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">new</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">Blog</span><span style="background:#22282a;color:#f1f2f3;">() {Description </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#ec7600;">&quot;Testing blog&quot;}</span><span style="background:#22282a;color:#f1f2f3;">;</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> post </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">new</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">Post</span><span style="background:#22282a;color:#f1f2f3;">() {Description </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#ec7600;">&quot;Post 1&quot;}</span><span style="background:#22282a;color:#f1f2f3;">;</span><br /> <span style="background:#22282a;color:#f1f2f3;">            blog</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">AddPost(post);</span><br /> <span style="background:#22282a;color:#f1f2f3;">            session</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">SaveOrUpdate(blog);</span><br /> <span style="background:#22282a;color:#f1f2f3;">            tran</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Commit();</span><br /> <span style="background:#22282a;color:#f1f2f3;">        }</span><br /> <span style="background:#22282a;color:#f1f2f3;">    }</span><br /> <span style="background:#22282a;color:#f1f2f3;">}</span></div>
</p></div>
</p></div>
<p>If we were writing the SQL by hand, we could probably agree that 2 SQL instructions would be more than enough for getting the entities stored in the database: we’d start by saving Blog, getting its ID and then we’d insert all its associated posts into the Post’s table (oh, and since Post is also an entity, with probably get its ID too since I’m using autogenerated IDs – not the best of choices for real world projects, but more than enough for our current discussion). The following snippet shows the SQL generated by NHibernate to save the previous objects to the database:</p>
<p>INSERT INTO Blog (Version, Description) VALUES (@p0, @p1);@p0 = 1 [Type: Int32 (0)], @p1 = &#8216;Testing blog&#8217; [Type: String (0)]&#160;&#160;&#160;&#160; <br />select @@IDENTITY&#160;&#160;&#160;&#160; <br />INSERT INTO Post (Description, BlogId) VALUES (@p0, @p1);@p0 = &#8216;Post 1&#8242; [Type: String (0)], @p1 = 4 [Type: Int32 (0)]&#160;&#160;&#160;&#160; <br />select @@IDENTITY&#160;&#160;&#160;&#160; <br />UPDATE Post SET BlogId = @p0 WHERE PostId = @p1;@p0 = 4 [Type: Int32 (0)], @p1 = 14 [Type: Int32 (0)]&#160;&#160;&#160;&#160; </p>
<p>The first SQL instructions seem to be going as expected, but then there’s an extra update instruction right at the end which, at first sight, makes no sense: we’re updating the Post table’s BlogId field to the ID of the inserted post and this isn’t really needed. </p>
<p>If we change the mappings by setting the inverse flag on the Blog’s Posts mapping, then the results are completely different:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bd535eeb-1925-4462-b84a-bd2db75e62ee" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">class</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">BlogMapping</span><span style="background:#22282a;color:#f1f2f3;">:</span><span style="background:#22282a;color:#678cb1;">ClassMap</span><span style="background:#22282a;color:#e8e2b7;">&lt;</span><span style="background:#22282a;color:#678cb1;">Blog</span><span style="background:#22282a;color:#e8e2b7;">&gt;</span><span style="background:#22282a;color:#f1f2f3;"> {</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">public</span><span style="background:#22282a;color:#f1f2f3;"> BlogMapping() {</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Table(</span><span style="background:#22282a;color:#ec7600;">&quot;Blog&quot;</span><span style="background:#22282a;color:#f1f2f3;">);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Not</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">LazyLoad();</span></p>
<p> <span style="background:#22282a;color:#f1f2f3;">        Id(b </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> b</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">BlogId)</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">GeneratedBy</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Identity()</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Default(</span><span style="background:#22282a;color:#ffcd22;">0</span><span style="background:#22282a;color:#f1f2f3;">);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Map(b </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> b</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Description);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        Version(b </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> b</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Version);</span><br /> <span style="background:#22282a;color:#f1f2f3;">        HasMany(b </span><span style="background:#22282a;color:#e8e2b7;">=&gt;</span><span style="background:#22282a;color:#f1f2f3;"> b</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Posts)</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Access</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">CamelCaseField(</span><span style="background:#22282a;color:#678cb1;">Prefix</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Underscore)</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">AsBag()</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Cascade</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">AllDeleteOrphan()</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Not</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">LazyLoad()</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">KeyColumn(</span><span style="background:#22282a;color:#ec7600;">&quot;BlogId&quot;</span><span style="background:#22282a;color:#f1f2f3;">)</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Inverse();</span><br /> <span style="background:#22282a;color:#f1f2f3;">    }</span><br /> <span style="background:#22282a;color:#f1f2f3;">}</span></div>
</p></div>
</p></div>
<p>And here’s NH’s generated SQL for the previous insertion:</p>
<p>INSERT INTO Blog (Version, Description) VALUES (@p0, @p1);@p0 = 1 [Type: Int32 (0)], @p1 = &#8216;Testing blog&#8217; [Type: String (0)]&#160;&#160;&#160;&#160; <br />select @@IDENTITY&#160;&#160;&#160;&#160; <br />INSERT INTO Post (Description, BlogId) VALUES (@p0, @p1);@p0 = &#8216;Post 1&#8242; [Type: String (0)], @p1 = 5 [Type: Int32 (0)]&#160;&#160;&#160;&#160; <br />select @@IDENTITY&#160;&#160;&#160;&#160; </p>
<p>Oh yes, now we’re in business! But what’s going on here? What does Inverse do?&#160; </p>
<p>IMO, the main problem in getting the inverse attribute is it’s “negative” meaning. If we set inverse to true, we’re saying that this entity is not responsible for maintaining the relationship. On the other hand, setting inverse to false does mean that that entity is the one responsible for managing the relationship. So, when we added the Inverse call to the Blog side, we’re saying that the Blog entity isn’t responsible for managing its relationship with Post (at the database level). In this case, managing the relationship should be done on the Post’s side. This is a little bit counterintuitive, but if we take a step back and recall how things work at the database level, it might make sense.</p>
<p>In a database, each relationship is represented by a foreign key&#160; on the many side (going back to our RDE, the Post table will have a foreign key – BlogId – which references the primary key of the Blog table). Taking this into consideration does help understand the invert attribute: we can insert entries in the Blog table without caring about its associated Posts, but we can’t really add a Post without setting its BlogId foreign key. And that’s why the inverse attribute we’ve set in the Blog side of the mappings did eliminate the superfluous UPDATE call: with it, Blog is no longer responsible for making sure that the foreign key of each post is correctly set up (that will be taken care of by Post). </p>
<p>It goes without saying that in order for the persistence of the Blog aggregate to work, Post must have all the required info so that it doesn’t violate the foreign key rule when it persists itself to the database. And in fact, my initial code already does all the necessary setup. Notice that adding a Post to a Blog will always set its Blog field to its parent (the owning Blog) and that that field is defined as a reference to the Blog table on the mapping file.</p>
<p>Before ending, one extra note: did you notice that Cascade.AllDeleteOrphan() call in the mappings? Well, that’s really important when you want to remove a Post that has previously been persisted from the database. Here’s some code that will help you understand what I’m saying:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5f76e919-96da-433e-b2f8-8050b2c8233a" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#000000;max-height:500px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#22282a;color:#93c763;">private</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">static</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">void</span><span style="background:#22282a;color:#f1f2f3;"> UpdateBlog() {</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> sessionFactory </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">SessionFactory</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">CreateSessionFactory();</span><br /> <span style="background:#22282a;color:#f1f2f3;">    </span><span style="background:#22282a;color:#93c763;">using</span><span style="background:#22282a;color:#f1f2f3;"> (</span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> session </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> sessionFactory</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">OpenSession()) {</span><br /> <span style="background:#22282a;color:#f1f2f3;">        </span><span style="background:#22282a;color:#93c763;">using</span><span style="background:#22282a;color:#f1f2f3;"> (</span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> tran </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> session</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">BeginTransaction()) {</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> blog </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> session</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Get</span><span style="background:#22282a;color:#e8e2b7;">&lt;</span><span style="background:#22282a;color:#678cb1;">Blog</span><span style="background:#22282a;color:#e8e2b7;">&gt;</span><span style="background:#22282a;color:#f1f2f3;">(</span><span style="background:#22282a;color:#ffcd22;">2</span><span style="background:#22282a;color:#f1f2f3;">);</span><br /> <span style="background:#22282a;color:#f1f2f3;">                    </span><br /> <span style="background:#22282a;color:#f1f2f3;">            blog</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">RemoveAll();</span><br /> <span style="background:#22282a;color:#f1f2f3;">            </span><span style="background:#22282a;color:#93c763;">var</span><span style="background:#22282a;color:#f1f2f3;"> newPost </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#93c763;">new</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#678cb1;">Post</span><span style="background:#22282a;color:#f1f2f3;"> {Description </span><span style="background:#22282a;color:#e8e2b7;">=</span><span style="background:#22282a;color:#f1f2f3;"> </span><span style="background:#22282a;color:#ec7600;">&quot;Post 2&quot;}</span><span style="background:#22282a;color:#f1f2f3;">;</span><br /> <span style="background:#22282a;color:#f1f2f3;">            blog</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">AddPost(newPost);</span></p>
<p> <span style="background:#22282a;color:#f1f2f3;">            tran</span><span style="background:#22282a;color:#e8e2b7;">.</span><span style="background:#22282a;color:#f1f2f3;">Commit();</span><br /> <span style="background:#22282a;color:#f1f2f3;">        }</span><br /> <span style="background:#22282a;color:#f1f2f3;">    }</span><br /> <span style="background:#22282a;color:#f1f2f3;">}</span></div>
</p></div>
</p></div>
<p>After loading a previously saved Blog, I’m removing all its posts and adding a new one. Here’s the log for the generated SQL when you don’t delete orphan’s entities:</p>
<p>SELECT blog0_.BlogId as BlogId0_0_, blog0_.Version as Version0_0_, blog0_.Description as Descript3_0_0_ FROM Blog blog0_ WHERE blog0_.BlogId=@p0;@p0 = 2 [Type: Int32 (0)]&#160;&#160;&#160;&#160; <br />SELECT posts0_.BlogId as BlogId1_, posts0_.PostId as PostId1_, posts0_.PostId as PostId1_0_, posts0_.Description as Descript2_1_0_, posts0_.BlogId as BlogId1_0_ FROM Post posts0_ WHERE posts0_.BlogId=@p0;@p0 = 2 [Type: Int32 (0)]&#160;&#160;&#160;&#160; <br />INSERT INTO Post (Description, BlogId) VALUES (@p0, @p1);@p0 = &#8216;Post 2&#8242; [Type: String (0)], @p1 = 2 [Type: Int32 (0)]&#160;&#160;&#160;&#160; <br />select @@IDENTITY&#160;&#160;&#160;&#160; <br />UPDATE Blog SET Version = @p0, Description = @p1 WHERE BlogId = @p2 AND Version = @p3;@p0 = 5 [Type: Int32 (0)], @p1 = &#8216;Testing blog&#8217; [Type: String (0)], @p2 = 2 [Type: Int32 (0)], @p3 = 4 [Type: Int32 (0)]&#160;&#160;&#160;&#160; <br />UPDATE Post SET Description = @p0, BlogId = @p1 WHERE PostId = @p2;@p0 = &#8216;Post 2&#8242; [Type: String (0)], @p1 = NULL [Type: Int32 (0)], @p2 = 12 [Type: Int32 (0)]&#160;&#160;&#160;&#160; </p>
<p>As you can see, the last SQL instruction is trying to set Post table’s foreign key to null and that will not end well. Setting the delete orphan entities attribute does ensure that NH will generate a DELETE instead of an UPDATE. Here’s the SQL generated when you add the delete orphan entities mapping:</p>
<p>SELECT blog0_.BlogId as BlogId0_0_, blog0_.Version as Version0_0_, blog0_.Description as Descript3_0_0_ FROM Blog blog0_ WHERE blog0_.BlogId=@p0;@p0 = 2 [Type: Int32 (0)]&#160;&#160;&#160;&#160; <br />SELECT posts0_.BlogId as BlogId1_, posts0_.PostId as PostId1_, posts0_.PostId as PostId1_0_, posts0_.Description as Descript2_1_0_, posts0_.BlogId as BlogId1_0_ FROM Post posts0_ WHERE posts0_.BlogId=@p0;@p0 = 2 [Type: Int32 (0)]&#160;&#160;&#160;&#160; <br />INSERT INTO Post (Description, BlogId) VALUES (@p0, @p1);@p0 = &#8216;Post 2&#8242; [Type: String (0)], @p1 = 2 [Type: Int32 (0)]&#160;&#160;&#160;&#160; <br />select @@IDENTITY&#160;&#160;&#160;&#160; <br />UPDATE Blog SET Version = @p0, Description = @p1 WHERE BlogId = @p2 AND Version = @p3;@p0 = 7 [Type: Int32 (0)], @p1 = &#8216;Testing blog&#8217; [Type: String (0)], @p2 = 2 [Type: Int32 (0)], @p3 = 6 [Type: Int32 (0)]&#160;&#160;&#160;&#160; <br />DELETE FROM Post WHERE PostId = @p0;@p0 = 18 [Type: Int32 (0)]&#160;&#160;&#160;&#160; </p>
<p>There’s still more to say about inverse (for instance, we haven’t discussed many-to-many relationships), but I believe that this is more than enough for getting you started. </p>
<p>That’s it for now. Stay tuned for more!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luisabreu.wordpress.com/1813/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luisabreu.wordpress.com/1813/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luisabreu.wordpress.com&#038;blog=35871519&#038;post=1813&#038;subd=luisabreu&#038;ref=&#038;feed=1" width="1" height="1" /><img src="http://feeds.feedburner.com/~r/Laneten/~4/aiMYu_h7ll4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://luisabreu.wordpress.com/2013/01/28/nhibernate-and-inverse-my-view-on-that-subject/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ef50ae4032124f87927d917ac713ee3b?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">luisabreu</media:title>
		</media:content>
	<feedburner:origLink>http://luisabreu.wordpress.com/2013/01/28/nhibernate-and-inverse-my-view-on-that-subject/</feedburner:origLink></item>
	</channel>
</rss>
