<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>benlakey.com</title>
	<atom:link href="http://benlakey.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://benlakey.com</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Sat, 05 May 2012 08:55:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Introducing PressSharp</title>
		<link>http://benlakey.com/2012/05/05/introducing-presssharp/</link>
		<comments>http://benlakey.com/2012/05/05/introducing-presssharp/#comments</comments>
		<pubDate>Sat, 05 May 2012 08:55:13 +0000</pubDate>
		<dc:creator>benlakey</dc:creator>
				<category><![CDATA[benlakey.com]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[NuGet]]></category>

		<guid isPermaLink="false">http://benlakey.com/?p=785</guid>
		<description><![CDATA[Ever had a need to deal with exported WordPress XML data in a .NET project? I have, and since there wasn&#8217;t a good solution out there already, I decided to create one. The result is PressSharp. It&#8217;s a simple class library to provide an API to the exported WordPress XML data. It&#8217;s also available as [...]]]></description>
			<content:encoded><![CDATA[<p>Ever had a need to deal with exported WordPress XML data in a .NET project? I have, and since there wasn&#8217;t a good solution out there already, I decided to create one. The result is PressSharp. It&#8217;s a simple class library to provide an API to the exported WordPress XML data. It&#8217;s also available as a NuGet package:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Install<span style="color: #008000;">-</span>Package PressSharp</pre></div></div>

<p>Check it out!</p>
<p>On github: <a href="https://github.com/benlakey/PressSharp" title="PressSharp">https://github.com/benlakey/PressSharp</a><br />
On nuget: <a href="http://nuget.org/packages/PressSharp" title="PressSharp">http://nuget.org/packages/PressSharp</a></p>
]]></content:encoded>
			<wfw:commentRss>http://benlakey.com/2012/05/05/introducing-presssharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC 3 LabelFor Html Helper with HtmlAttributes</title>
		<link>http://benlakey.com/2012/04/22/asp-net-mvc-3-labelfor-html-helper-with-htmlattributes/</link>
		<comments>http://benlakey.com/2012/04/22/asp-net-mvc-3-labelfor-html-helper-with-htmlattributes/#comments</comments>
		<pubDate>Sun, 22 Apr 2012 21:23:29 +0000</pubDate>
		<dc:creator>benlakey</dc:creator>
				<category><![CDATA[benlakey.com]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://benlakey.com/?p=777</guid>
		<description><![CDATA[ASP.NET MVC has lots of html helpers to take models and generate html controls in your views. Most of them support passing in an anonymous object which is translated into html attributes on the markup for the element. For example: @Html.TextBoxFor&#40;m =&#62; m.SomeProperty, new &#123; foo = &#34;bar&#34; &#125;&#41; Which becomes this: &#60;input type="text" name="SomeProperty" [...]]]></description>
			<content:encoded><![CDATA[<p>ASP.NET MVC has lots of html helpers to take models and generate html controls in your views. Most of them support passing in an anonymous object which is translated into html attributes on the markup for the element. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    @Html<span style="color: #008000;">.</span><span style="color: #0000FF;">TextBoxFor</span><span style="color: #008000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m<span style="color: #008000;">.</span><span style="color: #0000FF;">SomeProperty</span>, <span style="color: #008000;">new</span> <span style="color: #008000;">&#123;</span> foo <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;bar&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span></pre></div></div>

<p>Which becomes this:</p>
<pre lab="csharp">
     &lt;input type="text" name="SomeProperty" id="SomeProperty" foo="bar" /&gt;
</pre>
<p>There is however a shortcoming in one of these helpers. LabelFor does not support passing the HtmlAttributes anonymous object. They&#8217;ve added support for that into ASP.NET MVC 4 but in the meantime, I&#8217;ve written the helper. Here it is.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> MvcHtmlString LabelFor<span style="color: #008000;">&lt;</span>TModel, TValue<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span> HtmlHelper<span style="color: #008000;">&lt;</span>TModel<span style="color: #008000;">&gt;</span> html,
            Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>TModel, TValue<span style="color: #008000;">&gt;&gt;</span> expression,
            <span style="color: #6666cc; font-weight: bold;">object</span> htmlAttributes <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            var htmlAttributesDict <span style="color: #008000;">=</span> HtmlHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">AnonymousObjectToHtmlAttributes</span><span style="color: #008000;">&#40;</span>htmlAttributes<span style="color: #008000;">&#41;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">string</span> htmlFieldName <span style="color: #008000;">=</span> ExpressionHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">GetExpressionText</span><span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            var metadata <span style="color: #008000;">=</span> ModelMetadata<span style="color: #008000;">.</span><span style="color: #0000FF;">FromLambdaExpression</span><span style="color: #008000;">&#40;</span>expression, html<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewData</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">string</span> resolvedLabelText <span style="color: #008000;">=</span> 
                metadata<span style="color: #008000;">.</span><span style="color: #0000FF;">DisplayName</span> <span style="color: #008000;">??</span> 
                metadata<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span> <span style="color: #008000;">??</span> 
                htmlFieldName<span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'.'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Last</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>resolvedLabelText<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> MvcHtmlString<span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            var tagBuilder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TagBuilder<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;label&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            tagBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Attributes</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;for&quot;</span>, TagBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateSanitizedId</span><span style="color: #008000;">&#40;</span>
                html<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ViewData</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TemplateInfo</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetFullHtmlFieldName</span><span style="color: #008000;">&#40;</span>htmlFieldName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            tagBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">SetInnerText</span><span style="color: #008000;">&#40;</span>resolvedLabelText<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            tagBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">MergeAttributes</span><span style="color: #008000;">&#40;</span>htmlAttributesDict, replaceExisting<span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> MvcHtmlString<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>tagBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span>TagRenderMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Normal</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://benlakey.com/2012/04/22/asp-net-mvc-3-labelfor-html-helper-with-htmlattributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit testing .NET projects without sacrificing appropriate visibility modifiers</title>
		<link>http://benlakey.com/2012/04/02/unit-testing-net-projects-without-sacrificing-appropriate-visibility-modifiers/</link>
		<comments>http://benlakey.com/2012/04/02/unit-testing-net-projects-without-sacrificing-appropriate-visibility-modifiers/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 05:06:18 +0000</pubDate>
		<dc:creator>benlakey</dc:creator>
				<category><![CDATA[benlakey.com]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ravendb]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[unit tests]]></category>

		<guid isPermaLink="false">http://benlakey.com/?p=771</guid>
		<description><![CDATA[Occasionally I&#8217;ll find myself in a position where I need to unit test some code that has internals that are set privately at runtime. Today I found myself in such a position, and I used some reflection to solve the need. This isn&#8217;t something I like to do often; I&#8217;d prefer to have code that [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally I&#8217;ll find myself in a position where I need to unit test some code that has internals that are set privately at runtime. Today I found myself in such a position, and I used some reflection to solve the need. This isn&#8217;t something I like to do often; I&#8217;d prefer to have code that is testable without reflection, but sometimes the path of least resistance can be ok.</p>
<h3>RavenController</h3>
<p>The RavenDB documentation <a href="http://ravendb.net/kb/3/using-ravendb-in-an-asp-net-mvc-website" title="Using RavenDB in an ASP.NET MVC Website" target="_blank">has an example</a> of a controller that can be used for managing RavenDB sessions for your derived controllers in ASP.NET MVC. I&#8217;ve taken that example and made it a little better, in that I can pass in the store through the constructor from the derived controller, which itself was injected. Here&#8217;s what that looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> abstract <span style="color: #6666cc; font-weight: bold;">class</span> RavenController <span style="color: #008000;">:</span> Controller
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> IDocumentStore documentStore<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> IDocumentSession session <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> RavenController<span style="color: #008000;">&#40;</span>IDocumentStore documentStore<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">documentStore</span> <span style="color: #008000;">=</span> documentStore<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnActionExecuting<span style="color: #008000;">&#40;</span>ActionExecutingContext filterContext<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">session</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">documentStore</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OpenSession</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnActionExecuted<span style="color: #008000;">&#40;</span>ActionExecutedContext filterContext<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>filterContext<span style="color: #008000;">.</span><span style="color: #0000FF;">IsChildAction</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">session</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>filterContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Exception</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Session</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">session</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SaveChanges</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">...</span>
&nbsp;
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> FooController <span style="color: #008000;">:</span> RavenController
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> FooController<span style="color: #008000;">&#40;</span>IDocumentStore documentStore<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span>documentStore<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Bar<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//do some stuff with the session here</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">...</span>
&nbsp;
    <span style="color: #008000;">&#125;</span></pre></div></div>

<h3>No Framework Help</h3>
<p>Now, when it comes time to unit test a controller such as this, it&#8217;s a little challenging. One nice thing with RavenDB is you can &#8216;new&#8217; up FooController, and pass it a RavenDB embedded in-memory store to test with:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
var store <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EmbeddableDocumentStore<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    RunInMemory <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>,
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
store<span style="color: #008000;">.</span><span style="color: #0000FF;">Initialize</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var entriesController <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EntriesController<span style="color: #008000;">&#40;</span>store<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The problem is that once you try to call your action directly off this object, you won&#8217;t have the benefit of the OnActionExecuting/OnActionExecuted execution ahead/behind your code. Therefor, the session in this case will be null, and the test will fail with a NullReferenceException. </p>
<p>It IS possible to write a whole bunch of ceremony to get this mocked and wired up, but in my opinion it&#8217;s not worth the extra 10-20 lines of code just for that.</p>
<h3>Dirty Reflection Tricks</h3>
<p>I want to set that session property&#8217;s private setter. Here&#8217;s how I can do that with reflection:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>FooController<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BaseType</span>
    <span style="color: #008000;">.</span><span style="color: #0000FF;">GetProperty</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;session&quot;</span>, BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">NonPublic</span> <span style="color: #008000;">|</span> BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>fooControllerInstance, session, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>If that makes you feel gross, good, it&#8217;s supposed to. But it gets the job done, and you can get on with testing FooController&#8217;s action with your embedded in-memory test store without concerning yourself too much with the RavenController infrastructure.</p>
]]></content:encoded>
			<wfw:commentRss>http://benlakey.com/2012/04/02/unit-testing-net-projects-without-sacrificing-appropriate-visibility-modifiers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RavenDB: Lessons Learned: Query Includes and Projections</title>
		<link>http://benlakey.com/2012/03/16/ravendb-lessons-learned-query-includes-and-projections/</link>
		<comments>http://benlakey.com/2012/03/16/ravendb-lessons-learned-query-includes-and-projections/#comments</comments>
		<pubDate>Fri, 16 Mar 2012 00:46:54 +0000</pubDate>
		<dc:creator>benlakey</dc:creator>
				<category><![CDATA[benlakey.com]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[ravendb]]></category>

		<guid isPermaLink="false">http://benlakey.com/?p=768</guid>
		<description><![CDATA[By default, RavenDB will only allow 30 requests per session. This is part of RavenDB&#8217;s &#8220;Safe by default&#8221; behaviors, to prevent you from making a giant number of RavenDB HTTP requests, which would be a performance quagmire. Let&#8217;s say you have an object graph that you are retrieving from RavenDB that contains referenced documents, and [...]]]></description>
			<content:encoded><![CDATA[<p>By default, RavenDB will only allow 30 requests per session. This is part of RavenDB&#8217;s &#8220;Safe by default&#8221; behaviors, to prevent you from making a giant number of RavenDB HTTP requests, which would be a performance quagmire.</p>
<p>Let&#8217;s say you have an object graph that you are retrieving from RavenDB that contains referenced documents, and it looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">stories<span style="color: #008000;">/</span><span style="color: #FF0000;">123</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">&quot;Headline&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;New iPad is key to Apple's bottom line&quot;</span>,
  <span style="color: #666666;">&quot;Author&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;Jack Smith&quot;</span>,
  <span style="color: #666666;">&quot;LastPublishedAtUtc&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;2012-03-14T23:48:00.0000000+00:00&quot;</span>, 
  <span style="color: #666666;">&quot;PublishStatus&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;Published&quot;</span>,
  <span style="color: #666666;">&quot;StoryReferences&quot;</span><span style="color: #008000;">:</span> 
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">&quot;storyreference/456213&quot;</span><span style="color: #008000;">:</span> 
    <span style="color: #008000;">&#123;</span>
      <span style="color: #666666;">&quot;Headline&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;New iPhone coming soon&quot;</span>,
      <span style="color: #666666;">&quot;Author&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;John Doe&quot;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #666666;">&quot;storyreference/789654&quot;</span><span style="color: #008000;">:</span> 
    <span style="color: #008000;">&#123;</span>
      <span style="color: #666666;">&quot;Headline&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;New iPad foils reviewers' attempts to find legitimate faults&quot;</span>,
      <span style="color: #666666;">&quot;Author&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;Jane Doe&quot;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #666666;">&quot;storyreference/555111&quot;</span><span style="color: #008000;">:</span> 
    <span style="color: #008000;">&#123;</span>
      <span style="color: #666666;">&quot;Headline&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;Now on Netflix: Search by TV network&quot;</span>,
      <span style="color: #666666;">&quot;Author&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;Jack Smith&quot;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #666666;">&quot;storyreference/942342&quot;</span><span style="color: #008000;">:</span> 
    <span style="color: #008000;">&#123;</span>
      <span style="color: #666666;">&quot;Headline&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;Apple stores to open at 8am for iPad launch&quot;</span>,
      <span style="color: #666666;">&quot;Author&quot;</span><span style="color: #008000;">:</span> <span style="color: #666666;">&quot;John Doe&quot;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">...</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And let&#8217;s say you are interested in getting a small subset of data about the referenced stories for display with the base story. What you DON&#8217;T want to do is something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
var story <span style="color: #008000;">=</span> session<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&lt;</span>Story<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;stories/123&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span>var storyReference <span style="color: #0600FF; font-weight: bold;">in</span> story<span style="color: #008000;">.</span><span style="color: #0000FF;">RelatedStories</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var otherStory <span style="color: #008000;">=</span> session<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&lt;</span>Story<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>storyReference<span style="color: #008000;">.</span><span style="color: #0000FF;">Id</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">// ... do something with otherStory ...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>That will result in the following HTTP traffic back to Raven:</p>
<ol>
<li>Make a request for &#8216;story/123&#8242;</li>
<li>Make a request for &#8216;story/456213&#8242;</li>
<li>Make a request for &#8216;story/789654&#8242;</li>
<li>Make a request for &#8216;story/555111&#8242;</li>
<li>Make a request for &#8216;story/942342&#8242;</li>
<li>&#8230;etc&#8230;</li>
</ol>
<p>You&#8217;ll consume unnecessary bandwidth and incur the cost of individual HTTP requests. What you really want to do is have the client make a single HTTP request. Fortunately RavenDB allows you to do that with Includes. A RavenDB include says &#8220;Hey server, go get this for me, but before you give it back to me, gather up these other things and return them with the request too so I can deal with them in a moment&#8221;.</p>
<p>A few weeks back we had some code that was hitting the 30 requests per session limit. At first we couldn&#8217;t understand why, since we do a pretty good job of making sure we only make 1 or 2 requests via Includes. Upon further inspection, it turned out we had misunderstood something about the RavenDB client API. </p>
<h3>What&#8217;s the problem?</h3>
<p>If we have an index that produces projections, in which it produces a server side anonymous entity containing flattened “StoryReferenceIds”, like this (this is a contrived example):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Stories_ByReferencedStories <span style="color: #008000;">:</span> AbstractIndexCreationTask<span style="color: #008000;">&lt;</span>Story, Stories_ByReferencedStories<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Result
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Headline <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> DateTimeOffset<span style="color: #008000;">?</span> LastPublishedAtUtc <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> StoryReferenceIds <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Stories_ByReferencedStories<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Map</span> <span style="color: #008000;">=</span> stories <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">from</span> story <span style="color: #0600FF; font-weight: bold;">in</span> stories
                              <span style="color: #0600FF; font-weight: bold;">select</span> <span style="color: #008000;">new</span>
                              <span style="color: #008000;">&#123;</span>
                                  Headline <span style="color: #008000;">=</span> story<span style="color: #008000;">.</span><span style="color: #0000FF;">Headline</span>,
                                  LastPublishedAtUtc <span style="color: #008000;">=</span> story<span style="color: #008000;">.</span><span style="color: #0000FF;">LastPublishedAtUtc</span>,
                                  StoryReferenceIds <span style="color: #008000;">=</span> story<span style="color: #008000;">.</span><span style="color: #0000FF;">StoryReferences</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Id</span><span style="color: #008000;">&#41;</span>,
                              <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&#8230; Then we had previously done something like the following on our Lucene queries against it:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
session<span style="color: #008000;">.</span><span style="color: #0000FF;">Advanced</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LuceneQuery</span><span style="color: #008000;">&lt;</span>Story, Stories_ByReferencedStories<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">.</span><span style="color: #0000FF;">WhereStartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Headline&quot;</span>, text<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">.</span><span style="color: #0000FF;">OrderBy</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;-LastPublishedAtUtc&quot;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">.</span><span style="color: #0000FF;">Include</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;StoryReferenceIds&quot;</span><span style="color: #008000;">&#41;</span></pre></div></div>

<p>However, it turns out that last Include line doesn’t do anything at all. The Include() call actually operates on the entries identified by the index, NOT the projection. In other words, the stories produced from the query are what the Include() call actually operates against. </p>
<p>So, with that in mind, what we actually want is something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #008000;">.</span><span style="color: #0000FF;">Include</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;StoryReferences,Id&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The syntax with the comma may look a little funny, but what it means is &#8220;For the StoryReferenceIds entities collection, Include the document identified by the Id property from each referenced document&#8221;. So if you had a story with 45 referenced stories in it, instead of making 46 requests back to Raven, you would make only 1 request. That&#8217;s much better.</p>
<p>Happy coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://benlakey.com/2012/03/16/ravendb-lessons-learned-query-includes-and-projections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Raven DB: Lessons Learned: Caching Contexts</title>
		<link>http://benlakey.com/2012/03/07/raven-db-lessons-learned-caching-contexts/</link>
		<comments>http://benlakey.com/2012/03/07/raven-db-lessons-learned-caching-contexts/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 19:08:25 +0000</pubDate>
		<dc:creator>benlakey</dc:creator>
				<category><![CDATA[benlakey.com]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[ravendb]]></category>

		<guid isPermaLink="false">http://benlakey.com/?p=745</guid>
		<description><![CDATA[Caching When you talk about caching in terms of the the full web application stack, you&#8217;ve typically got the following layers: Browser cache CDN cache Application output cache Data cache However, in a application leveraging Raven DB, the last layer actually gets split up into two layers. Some Background The way Raven DB operates is [...]]]></description>
			<content:encoded><![CDATA[<h3>Caching</h3>
<p>When you talk about caching in terms of the the full web application stack, you&#8217;ve typically got the following layers:</p>
<ul>
<li>Browser cache</li>
<li>CDN cache</li>
<li>Application output cache</li>
<li>Data cache</li>
</ul>
<p>However, in a application leveraging Raven DB, the last layer actually gets split up into two layers.</p>
<h3>Some Background</h3>
<p>The way Raven DB operates is by having the client generate HTTP requests, which are sent across the wire to the server. Therefor, the same standard caching mechanisms that HTTP provides are present. This means, that if a request is made, and Raven DB thinks the data hasn&#8217;t changed since the last time you requested that same data, then the server responds with HTTP 304 Not Modified, instructing the raven client to continue to use what it got last time.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var session <span style="color: #008000;">=</span> store<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenSession</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//if the server doesnt have anything different from the last time </span>
    <span style="color: #008080; font-style: italic;">//this was requested, it wont do any processing, and just return </span>
    <span style="color: #008080; font-style: italic;">//HTTP 304 Not Modified to the client. the client will then use </span>
    <span style="color: #008080; font-style: italic;">//what it got last time.</span>
    var foo <span style="color: #008000;">=</span> session<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&lt;</span>foo<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;foos/123&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>So the first layer of the data cache, you get for free out of the box with Raven. Fortunately the second layer is available as well, if your application needs it.</p>
<h3>Aggressive Data Caching</h3>
<p>With Raven DB, it&#8217;s possible to instruct the client to not even ask the server for data again, thereby skipping the HTTP request, even if it might result in a 304. Here&#8217;s what that looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var session <span style="color: #008000;">=</span> store<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenSession</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//set up an aggressive caching context, instructing the server to not</span>
    <span style="color: #008080; font-style: italic;">//make an http request if it made one within the last 5 seconds</span>
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>session<span style="color: #008000;">.</span><span style="color: #0000FF;">Advanced</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DocumentStore</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">AggressivelyCacheFor</span><span style="color: #008000;">&#40;</span>TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">FromMinutes</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//may or may not make a request</span>
        var foo <span style="color: #008000;">=</span> session<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&lt;</span>foo<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;foos/123&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>  
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3>Runtime Configuration?</h3>
<p>We made mention in a previous blog post about a runtime configuration setup that we&#8217;ve provided our ops team with. Having the ability to control the TTL on the Raven runtime configuration seems like a prime candidate to use with this. We wired up the runtime configuration much the same as the output caching runtime configuration from the other blog post.</p>
<h3>Clever</h3>
<p>Now, to use output caching, it was a simple line to apply the [ConfiguredOutputCache] attribute to our controller actions. However, with the raven data caching, it&#8217;s a violation of DRY to have to open an aggressive caching context, and pass in a runtime configuration value everywhere it&#8217;s needed. So, with that in mind, we came up with an extension method to encapsulate that behavior. We thought this was very clever, but it actually turned out to be quite stupid. Can you spot the problem?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> DataCachingExtensions
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T LoadAndCache<span style="color: #008000;">&lt;</span>t<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IDocumentSession session, <span style="color: #6666cc; font-weight: bold;">string</span> id<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>session<span style="color: #008000;">.</span><span style="color: #0000FF;">Advanced</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DocumentStore</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AggressivelyCacheFor</span><span style="color: #008000;">&#40;</span>
            CacheSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">RavenAggressiveCachingDurationSeconds</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> session<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&lt;</span>foo<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>id<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> IRavenQueryable<span style="color: #008000;">&lt;</span>t<span style="color: #008000;">&gt;</span> QueryAndCache<span style="color: #008000;">&lt;</span>t<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span> IDocumentSession session<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>session<span style="color: #008000;">.</span><span style="color: #0000FF;">Advanced</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DocumentStore</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AggressivelyCacheFor</span><span style="color: #008000;">&#40;</span>
            CacheSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">RavenAggressiveCachingDurationSeconds</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> session<span style="color: #008000;">.</span><span style="color: #0000FF;">Query</span><span style="color: #008000;">&lt;</span>t<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">...</span> 
&nbsp;
<span style="color: #0000FF;">session</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LoadAndCache</span><span style="color: #008000;">&lt;</span>foo<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;foos/123&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008000;">...</span>
&nbsp;
<span style="color: #0000FF;">session</span><span style="color: #008000;">.</span><span style="color: #0000FF;">QueryAndCache</span><span style="color: #008000;">&lt;</span>foo<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Where</span><span style="color: #008000;">&#40;</span>f <span style="color: #008000;">=&gt;</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">Bar</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Baz&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The first extension method is fine, but the 2nd one doesn&#8217;t do anything at all. Why?</p>
<p>It&#8217;s because Raven doesn&#8217;t actually execute the HTTP query until it&#8217;s evaluated. So since we haven&#8217;t actually executed the query, and have returned from inside the aggressive caching context, the context was disposed before we ever execute the HTTP query, resulting in no caching.</p>
<p>So after feeling pretty silly, we restructured the extension method to simply return the aggressive caching context, so that the caller can encapsulate the full  query including its execution.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> DataCachingExtensions
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NonCachingContext <span style="color: #008000;">:</span> IDisposable
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> IDisposable GetCachingContext<span style="color: #008000;">&#40;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span> IDocumentSession session<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>CacheSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">RavenAggressiveCachingDurationSeconds</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> NonCachingContext<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> session<span style="color: #008000;">.</span><span style="color: #0000FF;">Advanced</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DocumentStore</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AggressivelyCacheFor</span><span style="color: #008000;">&#40;</span>
            CacheSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">RavenAggressiveCachingDurationSeconds</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">...</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var session <span style="color: #008000;">=</span> store<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenSession</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>session<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCachingContext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        session<span style="color: #008000;">.</span><span style="color: #0000FF;">Query</span><span style="color: #008000;">&lt;</span>foo<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Where</span><span style="color: #008000;">&#40;</span>f <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">Bar</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Baz&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>It&#8217;s worth pointing out that the caching context, when used with a Query<t>, does not actually cache the items returned, but rather just caches the query/response aspect, so subsequent cache-enabled calls to .Load<t> for items that were returned from a cache-enabled query context will still make a request take place, if they weren&#8217;t already cached by a .Load<t> call themselves.</p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://benlakey.com/2012/03/07/raven-db-lessons-learned-caching-contexts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RavenDB and msnbc.com Webinar</title>
		<link>http://benlakey.com/2012/03/02/ravendb-and-msnbc-com-webinar/</link>
		<comments>http://benlakey.com/2012/03/02/ravendb-and-msnbc-com-webinar/#comments</comments>
		<pubDate>Fri, 02 Mar 2012 09:00:11 +0000</pubDate>
		<dc:creator>benlakey</dc:creator>
				<category><![CDATA[benlakey.com]]></category>
		<category><![CDATA[msnbc]]></category>
		<category><![CDATA[ravendb]]></category>

		<guid isPermaLink="false">http://benlakey.com/?p=762</guid>
		<description><![CDATA[On Monday, March 5th, we&#8217;re going to be doing a live Webinar with Ayende to talk about how msnbc.com is using RavenDB. Please register for the webinar in advance; there is a limited amount of participants. And bring your own questions! We will open it up for the audience to ask their own questions towards [...]]]></description>
			<content:encoded><![CDATA[<p>On Monday, March 5th, we&#8217;re going to be doing a live Webinar with Ayende to talk about how msnbc.com is using RavenDB.</p>
<p>Please <a href="https://www2.gotomeeting.com/register/866583610" title="RavenDB and msnbc.com Webinar" target="_blank">register for the webinar</a> in advance; there is a limited amount of participants. And bring your own questions! We will open it up for the audience to ask their own questions towards the end.</p>
<p>Link to Ayende&#8217;s blog post: <a href="http://ayende.com/blog/155041/msnbc-com-amp-ravendb-webinar" title="MSNBC.COM &#038; RavenDB Webinar" target="_blank"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://benlakey.com/2012/03/02/ravendb-and-msnbc-com-webinar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performance on-demand; Giving your ops team runtime flexibility</title>
		<link>http://benlakey.com/2012/02/20/performance-on-demand-giving-your-ops-team-runtime-flexibility/</link>
		<comments>http://benlakey.com/2012/02/20/performance-on-demand-giving-your-ops-team-runtime-flexibility/#comments</comments>
		<pubDate>Mon, 20 Feb 2012 07:02:12 +0000</pubDate>
		<dc:creator>benlakey</dc:creator>
				<category><![CDATA[benlakey.com]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://benlakey.com/?p=733</guid>
		<description><![CDATA[Performance On Demand Pretend you are in an operations position, in which your job is to maintain the infrastructure that routes traffic and the servers that serve requests. Wouldn&#8217;t it then be nice, if you suddenly had a surge in traffic or a drop in available server hardware (be it expected or unexpected), you could [...]]]></description>
			<content:encoded><![CDATA[<h3><strong>Performance On Demand</strong></h3>
<p>Pretend you are in an operations position, in which your job is to maintain the infrastructure that routes traffic and the servers that serve requests. Wouldn&#8217;t it then be nice, if you suddenly had a surge in traffic or a drop in available server hardware (be it expected or unexpected), you could alter the performance characteristics of your web applications?</p>
<p>This is a problem we&#8217;ve been tackling with our new set of web apps, and we think we&#8217;ve got a pretty good solution in place.</p>
<h3><strong>Operations Administration Panel for Runtime Configuration</strong></h3>
<p>For starters, we&#8217;ve created an administration web application for our operations folks, whose primary purpose is that of runtime configuration. Operations can control various aspects of our systems from this application, including:</p>
<ul>
<li>Logging levels</li>
<li>Caching TTLs</li>
<li>Database masters/slaves and replication strategies</li>
<li>Application settings</li>
<li>Network locations for editorial assets</li>
<li>Logical service bus participants</li>
<li>Etc.</li>
</ul>
<p>When any of these settings are updated, we send a message on the service bus informing subscribers of changes in the settings they care about. Let&#8217;s analyze the one we made reference to above, which will provide ops with a way to dial in performance on demand.</p>
<h3><strong>Output Caching</strong></h3>
<p>In the administration panel, we&#8217;ve provided a settings page where the output caching TTL and data caching TTL can be set for a given application. When this setting is updated, we publish a message on the service bus, which our front end rendering ASP.NET MVC application can subscribe to.</p>
<p>Creating a handler in the rendering application then is pretty easy. We listen for the settings type that corresponds to caching:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CacheSettingsUpdater <span style="color: #008000;">:</span> SettingsChangedHandler<span style="color: #008000;">&lt;</span>CacheSettingsData<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">bool</span> ShouldHandle<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> id<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>
                id,
                CacheSettingsData<span style="color: #008000;">.</span><span style="color: #0000FF;">StorageId</span>,
                StringComparison<span style="color: #008000;">.</span><span style="color: #0000FF;">OrdinalIgnoreCase</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Update<span style="color: #008000;">&#40;</span>CacheSettingsData settingsData<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            CacheSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">UpdateSettings</span><span style="color: #008000;">&#40;</span>settingsData<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>As you can see, the handler can then inform a settings class by calling its &#8220;UpdateSettings&#8221; method, who keeps a reference to the latest data.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> CacheSettings
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> CacheSettingsData Data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CacheSettingsData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">int</span> OutputCacheDurationSeconds
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span>
                    Data<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentAppCacheParameters</span>
                        <span style="color: #008000;">.</span><span style="color: #0000FF;">OutputCacheDurationSeconds</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> UpdateSettings<span style="color: #008000;">&#40;</span>CacheSettingsData data<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Data <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<h3><strong>Leveraging it with OutputCacheAttribute</strong></h3>
<p>Now, in ASP.NET MVC, there is an action filter for output caching: <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute.aspx">OutputCacheAttribute</a>. This attribute can be applied at the controller level, or at the individual action level. When an action is run the first time, the framework will cache the result, such that the next request won&#8217;t require processing again, and will be delivered from cache. The cached item will be delivered from cache until the TTL/Duration expires. The effect of this is that your application won&#8217;t be processing for every request, and will be able to therefor serve more requests.</p>
<p>The issue with connecting up our runtime configuration class from above (CacheSettings) to the OutputCacheAttribute, is that the settings for a filter can only be specified by constants, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #008000;">&#91;</span>OutputCache<span style="color: #008000;">&#40;</span>Duration <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Index<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span></pre></div></div>

<p>&nbsp;</p>
<p>So, we need to instead create our own action filter, which inherits from OutputCacheAttribute, so we can control where it gets its values from. I&#8217;ve simplified this for brevity to just illustrate the Duration extensibility point.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ConfiguredOutputCacheAttribute <span style="color: #008000;">:</span> OutputCacheAttribute
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">int</span> Duration
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Duration</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotSupportedException<span style="color: #008000;">&#40;</span>
                    <span style="color: #666666;">&quot;Duration cannot be set directly. &quot;</span> <span style="color: #008000;">+</span>
                    <span style="color: #666666;">&quot;Set from runtime config.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> ConfiguredOutputCacheAttribute<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Duration</span> <span style="color: #008000;">=</span> CacheSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">OutputCacheDurationSeconds</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnActionExecuting<span style="color: #008000;">&#40;</span>
            ActionExecutingContext filterContext<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Duration</span> <span style="color: #008000;">=</span> CacheSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">OutputCacheDurationSeconds</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnActionExecuting</span><span style="color: #008000;">&#40;</span>filterContext<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>As you can see, when we hit OnActionExecuting, we check the CacheSettings class for the current output cache duration, and set it on the base OutputCacheAttribute class we inherited from. The effect of this, is that during day to day traffic, operations can control the cache TTL.</p>
<p>Then we just apply it where we want to cache:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #008000;">&#91;</span>ConfiguredOutputCache<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Index<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span></pre></div></div>

<p>&nbsp;</p>
<h3><strong>Well, how did we do?</strong></h3>
<p>Let&#8217;s see what it looks like if I simulate light traffic load. The red line indicates request execution time.</p>
<div id="attachment_734" class="wp-caption alignnone" style="width: 842px"><a href="http://benlakey.com/wp-content/uploads/2012/02/output.png"><img class="size-full wp-image-734" title="Output Caching" src="http://benlakey.com/wp-content/uploads/2012/02/output.png" alt="Turning on output caching results in a dramatic drop off in request execution time." width="832" height="417" /></a><p class="wp-caption-text">Turning on output caching results in a dramatic drop off in request execution time.</p></div>
<p>The dramatic drop off occurred when I went into the operations administration panel and changed the TTL. The spikes every 10 seconds following the drop off are when the cache duration TTL expired, forcing the page to actually process again.</p>
<p>There are a number of things we can do to enhance the flexibility of this system. For example, we could specify groupings in the operations administration panel that correspond to cache policies, and then simply specify on each instance of our attribute which policy we&#8217;d like to use:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #008000;">&#91;</span>ConfiguredOutputCache<span style="color: #008000;">&#40;</span>CachePolicy <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;FooCachePolicy&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Index<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> streamSlug<span style="color: #008000;">&#41;</span></pre></div></div>

<p>&nbsp;</p>
<p>We think this feature will be particularly valuable in situations where we need more performance on demand, and look forward to extending it to have more flexibility as needed.</p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://benlakey.com/2012/02/20/performance-on-demand-giving-your-ops-team-runtime-flexibility/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iOS Development Beginnings</title>
		<link>http://benlakey.com/2011/11/21/ios-development-beginnings/</link>
		<comments>http://benlakey.com/2011/11/21/ios-development-beginnings/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 23:21:59 +0000</pubDate>
		<dc:creator>benlakey</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://benlakey.com/?p=726</guid>
		<description><![CDATA[Now that I&#8217;ve purchased a Macbook Pro (again), I&#8217;m diving into iOS development. I&#8217;ve decided to keep my notes as I go along, here on the blog. I wouldn&#8217;t necessarily say it&#8217;s for raw public consumption, but that&#8217;s ok since this blog is primarily for selfish reasons. Perhaps someone will get something out of it. [...]]]></description>
			<content:encoded><![CDATA[<p>Now that I&#8217;ve purchased a Macbook Pro (again), I&#8217;m diving into <a href="http://en.wikipedia.org/wiki/IOS" target="_blank">iOS </a>development. I&#8217;ve decided to keep my notes as I go along, here on the blog. I wouldn&#8217;t necessarily say it&#8217;s for raw public consumption, but that&#8217;s ok since this blog is primarily for selfish reasons. Perhaps someone will get something out of it.</p>
<p>I&#8217;m starting off by running through the official Apple documentation on getting started here:  <a href="http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iPhone101/Articles/00_Introduction.html" target="_blank">link</a></p>
<p><a href="http://en.wikipedia.org/wiki/Cocoa_Touch" target="_blank">Cocoa Touch</a> is the framework which iOS apps are built on. <a href="http://en.wikipedia.org/wiki/Cocoa_(API)" target="_blank">Cocoa</a> is the framework which Mac OS X apps are built on. It’s an <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" target="_blank">MVC</a> architecture in both cases.</p>
<p>According to <a href="http://en.wikipedia.org/wiki/Cocoa_Touch" target="_blank">Wikipedia</a>, Cocoa Touch is composed of the following sub-frameworks:</p>
<ul>
<li>Foundation Kit Framework</li>
<li>UIKit Framework (based on Application Kit)</li>
<li>Game Kit Framework</li>
<li>iAd Framework</li>
<li>Map Kit Framework</li>
</ul>
<p>In this first chapter, the following are some mental mappings I&#8217;ve made to my previous knowledge of other languages/frameworks/patterns. There is no 100% mapping exactly, but these are close.</p>
<ul>
<li>‘protocol’ == ‘interface’</li>
<li>‘app delegate’ == ‘message handler’</li>
</ul>
<p>The general theme I’m seeing as I go through the first chapter is that this is a <a href="http://en.wikipedia.org/wiki/Message-oriented_middleware" target="_blank">message based architecture</a>, similar to a <a href="http://en.wikipedia.org/wiki/Enterprise_service_bus" target="_blank">bus</a>, with message handlers (delegates). The delegation can pass the messages along. This is of course very high level and will require refinement as I go along.</p>
]]></content:encoded>
			<wfw:commentRss>http://benlakey.com/2011/11/21/ios-development-beginnings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Encapsulation</title>
		<link>http://benlakey.com/2011/11/19/javascript-encapsulation/</link>
		<comments>http://benlakey.com/2011/11/19/javascript-encapsulation/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 22:00:30 +0000</pubDate>
		<dc:creator>benlakey</dc:creator>
				<category><![CDATA[benlakey.com]]></category>
		<category><![CDATA[abstraction]]></category>
		<category><![CDATA[encapsulation]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[software craftsmanship]]></category>

		<guid isPermaLink="false">http://benlakey.com/?p=713</guid>
		<description><![CDATA[After having read Douglas Crockford&#8217;s book &#8220;Javascript: The Good Parts&#8221;, I had learned some ways to leverage objects and information hiding, in the following manner: var fooInstance = function&#40;&#41; &#123; &#160; var privateAlpha; &#160; return &#123; &#160; getAlpha: function&#40;&#41; &#123; return privateAlpha; &#125;, &#160; setAlpha: function&#40;a&#41; &#123; privateAlpha = a; &#125; &#160; &#125;; &#160; &#125;&#40;&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>After having read Douglas Crockford&#8217;s book &#8220;Javascript: The Good Parts&#8221;, I had learned some ways to leverage objects and information hiding, in the following manner:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fooInstance <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> privateAlpha<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		getAlpha<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> privateAlpha<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
		setAlpha<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			privateAlpha <span style="color: #339933;">=</span> a<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
fooInstance.<span style="color: #660066;">setAlpha</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>fooInstance.<span style="color: #660066;">getAlpha</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//10</span>
fooInstance.<span style="color: #660066;">setAlpha</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>fooInstance.<span style="color: #660066;">getAlpha</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//20</span></pre></div></div>

<p>However, if you do a quick search on StackOverflow, you&#8217;ll quickly encounter a huge number of answers on javascript questions which say not to bother with private variables. Their reasoning is &#8220;well, you can just look at the javascript source in the browser, so what&#8217;s the point?&#8221;. This is missing part of the point.</p>
<p>One of the primary points of encapsulation is to control the internal state of the object, by only providing a limited interface. This protects the integrity of the state.</p>
<p>BUT&#8230; the other purpose of private variables and encapsulation in any language is not simply security or protection from nefarious people. It&#8217;s abstraction. An external entity should not need or care to know how the internals are accomplished. The smaller the footprint of the interface, the less complexity exists for the consumer to understand. You should expose no more than is necessary to leverage the functionality. The result of doing so is code that has looser coupling and is easier to understand by those who wish to consume it.</p>
]]></content:encoded>
			<wfw:commentRss>http://benlakey.com/2011/11/19/javascript-encapsulation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Old Republic at Ben&#8217;s</title>
		<link>http://benlakey.com/2011/10/24/the-old-republic-at-bens/</link>
		<comments>http://benlakey.com/2011/10/24/the-old-republic-at-bens/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 06:07:33 +0000</pubDate>
		<dc:creator>benlakey</dc:creator>
				<category><![CDATA[benlakey.com]]></category>

		<guid isPermaLink="false">http://benlakey.com/?p=710</guid>
		<description><![CDATA[While not really relating to Software Development, I should point out that I&#8217;ve created another blog covering Star Wars: The Old Republic, the new MMO coming out in December. Check it out here: http://benlakey.com/tor/]]></description>
			<content:encoded><![CDATA[<p>While not really relating to Software Development, I should point out that I&#8217;ve created <a title="The Old Republic at Ben's" href="http://benlakey.com/tor/" target="_blank">another blog</a> covering <a title="Star Wars: The Old Republic" href="http://www.swtor.com" target="_blank">Star Wars: The Old Republic</a>, the new MMO coming out in December.</p>
<p>Check it out here: <a title="The Old Republic at Ben's" href="http://benlakey.com/tor/" target="_blank">http://benlakey.com/tor/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://benlakey.com/2011/10/24/the-old-republic-at-bens/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

