<?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>Circles and Crosses &#187; LINQ</title>
	<atom:link href="http://ox.no/posts/tag/linq/feed" rel="self" type="application/rss+xml" />
	<link>http://ox.no</link>
	<description>Håvard Stranden&#039;s website</description>
	<lastBuildDate>Sat, 19 Jun 2010 21:59:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Mocking HtmlHelper in ASP.NET MVC RC1 using Moq</title>
		<link>http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq</link>
		<comments>http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq#comments</comments>
		<pubDate>Sun, 08 Mar 2009 17:15:13 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Mock]]></category>
		<category><![CDATA[Moq]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Test]]></category>

		<guid isPermaLink="false">http://ox.no/?p=65</guid>
		<description><![CDATA[For those of you trying to mock HtmlHelper, but finding it difficult, here&#8217;s a mock that works in ASP.NET MVC RC1. The ViewDataDictionary that is passed to the HtmlHelper can be empty, or made to contain the data you want &#8230; <a href="http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For those of you trying to mock HtmlHelper, but finding it difficult, here&#8217;s a mock that works in ASP.NET MVC RC1.</p>

<p><span id="more-65"></span></p>

<p>The <code>ViewDataDictionary</code> that is passed to the <code>HtmlHelper</code> can be empty, or made to contain the data you want for your test.</p>

<p><pre class="brush: csharp; ">

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
{
  var mockViewContext = new Mock&lt;ViewContext&gt;(
    new ControllerContext(
      new Mock&lt;HttpContextBase&gt;().Object,
      new RouteData(),
      new Mock&lt;ControllerBase&gt;().Object),
    new Mock&lt;IView&gt;().Object,
    vd,
    new TempDataDictionary());&lt;/p&gt;

&lt;p&gt;var mockViewDataContainer = new Mock&lt;IViewDataContainer&gt;();
  mockViewDataContainer.Setup(v =&gt; v.ViewData)
    .Returns(vd);&lt;/p&gt;

&lt;p&gt;return new HtmlHelper(mockViewContext.Object, 
    mockViewDataContainer.Object);
}

</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Object-Relational Mappings Considered Harmful</title>
		<link>http://ox.no/posts/object-relational-mappings-considered-harmful</link>
		<comments>http://ox.no/posts/object-relational-mappings-considered-harmful#comments</comments>
		<pubDate>Tue, 04 Mar 2008 22:48:25 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[Software Design]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://ox.no/posts/object-relational-mappings-considered-harmful</guid>
		<description><![CDATA[Creating an Object-Relational Mapping (ORM) has become the de facto way of handling persistence in the object-oriented programming paradigm. Almost all systems require some form of persistent state, and relational databases have become the de facto place to put that &#8230; <a href="http://ox.no/posts/object-relational-mappings-considered-harmful">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Creating an Object-Relational Mapping (ORM) has become the de facto way of handling persistence in the object-oriented programming paradigm. Almost all systems require some form of persistent state, and relational databases have become the de facto place to put that state. Relational databases are proven, scale well, and organize data in a tabular manner suitable for many of the real world problems that we try to solve, so they are an obvious choice. Choosing them, however, means we have a new problem at our hands, known as the <a href="http://en.wikipedia.org/wiki/Object-Relational_impedance_mismatch" title="The Object-Relational Impedance Mismatch">object-relational impedance mismatch</a>.</p>

<!-- more -->

<p>The problem is that a relational database is not suited for storing an object-oriented model. An object is almost always a non-scalar value, meaning that it won&#8217;t fit well in a table row. Hence, we have to create a schema suitable for persisting our objects to a set of tables. This schema will be different for each type of object we have, so it&#8217;s a rather tedious task, but it solves the problem.</p>

<p>As always, we developers tend to dislike repetitive tasks, so we try to simplify and automate the extra work involved with creating an ORM. This has led to various design patterns such as DAO, and in recent years a set of fully automated ORM tools such as <a href="http://hibernate.org/" title="Hibernate">Hibernate (Java)</a>, <a href="http://nhibernate.org" title="NHibernate">NHibernate (.NET)</a>, <a href="http://sqlalchemy.org" title="SQLAlchemy">SQLAlchemy (Python)</a>, and <a href="http://propel.phpdb.org/" title="Propel">Propel (PHP)</a> have become very popular. These tools offer a highly transparent solution to the ORM problem, at the addition of a cost that varies heavily with the nature of the ORM problem. Still, their presence moves us closer and closer to a solution &#8211; we are creating an abstraction that fully encapsulates the difference between a relational database as our storage for persistent objects, and the objects themselves. Hopefully, we will soon be able to create an ORM that induces a linear or perhaps even constant cost on our solution, regardless of the nature of the problem we are trying to solve.</p>

<p>But wait. The real problem we are trying to solve is how to persist the state of our objects, remember? The database is indeed a store, and hence a candidate solution, but using it introduces an impedance mismatch, which is another problem we need to solve. It really is like trying to fit a square block in a round hole.</p>

<p>Let&#8217;s stop trying to solve the wrong problem of creating an ORM, and start finding a solution to our initial and real problem of persisting our objects. Recent innovations such as <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query">LINQ</a> has taken us a step in the right direction by making persistence an integral part of the language, but we&#8217;re still a long way from automated persistence for our objects. I am certain, though, that moving focus away from the challenges of an ORM and on to the challenge of persistence in general will take us further.</p>

<p>Let&#8217;s do that.</p>

<p>(And I&#8217;m sorry for reusing <a href="http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html" title="Go To Statement Considered Harmful">Dijkstra&#8217;s</a> already <a href="http://meyerweb.com/eric/comment/chech.html" title="Considered Harmful Essays Considered Harmful">overused</a> phrase.)</p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/object-relational-mappings-considered-harmful/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ vs Loop &#8211; A performance test</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test</link>
		<comments>http://ox.no/posts/linq-vs-loop-a-performance-test#comments</comments>
		<pubDate>Tue, 07 Aug 2007 22:26:12 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test</guid>
		<description><![CDATA[I just installed Visual Studio 2008 beta 2 to see what the future holds for C#. The addition of LINQ has brought a variety of query keywords to the language. &#8220;Anything&#8221; can be queried; SQL databases (naturally), XML documents, and &#8230; <a href="http://ox.no/posts/linq-vs-loop-a-performance-test">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I just installed Visual Studio 2008 beta 2 to see what the future holds for C#. The addition of LINQ has brought a variety of query keywords to the language. &#8220;Anything&#8221; can be queried; SQL databases (naturally), XML documents, and regular collections. Custom queryable objects can also be created by implementing IQueryable. Sadly, like every abstraction, these goodies all come at a cost. The question is how much? <!-- more --></p>

<p>I decided to create a simple test to see how much of a performance hit LINQ is. The simple test I deviced finds the numbers in an array that are less than 10. The code is quoted below.</p>

<p><pre class="brush: csharp; ">
public void LinqTest()
{
    const int SIZE = 10000, RUNS = 1000;
    int[] ints = new int[SIZE];
    for (int i = 0; i &amp;lt; SIZE; i++)
        ints[i] = i;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DateTime start = DateTime.Now;
for (int t = 0; t &amp;lt; RUNS; ++t)
{
    int[] less = (from int i in ints
                     where i &amp;lt; 10
                     select i).ToArray();
}
TimeSpan spent = DateTime.Now - start;
Trace.WriteLine(string.Format(&quot;LINQ: {0}, avg. {1}&quot;, 
    spent, new TimeSpan(spent.Ticks / RUNS)));

DateTime start2 = DateTime.Now;
for (int t = 0; t &amp;lt; RUNS; ++t)
{
    var l = new List&amp;lt;int&amp;gt;();
    foreach (var i in ints)
        if (i &amp;lt; 10)
            l.Add(i);
    int[] less2 = l.ToArray();
}

TimeSpan spent2 = DateTime.Now - start2;
Trace.WriteLine(string.Format(&quot;Loop: {0}, avg. {1}&quot;, 
    spent2, new TimeSpan(spent2.Ticks / RUNS)));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}
</pre></p>

<p>Initially, I assumed the performance impact would not be too large, since its equivalent is the straightforward imperative loop, which should not be too hard for a compiler to deduce given static typing and a single collection to iterate across. Or?</p>

<p><pre class="brush: csharp; ">

LINQ: 00:00:04.1052060, avg. 00:00:00.0041052
Loop: 00:00:00.0790965, avg. 00:00:00.0000790

</pre></p>

<p>As you can see, the performance impact is huge. LINQ performs 50 times worse than the traditional loop! This seems rather wild at first glance, but the explanation is this: The keywords introduced by LINQ are syntactic sugar for method invocations to a set of generic routines for iterating across collections and filtering through lambda expressions. Naturally, this will not perform as good as a traditional imperative loop, and less optimization is possible.</p>

<!--adsense-->

<p>Having seen the performance impact, I am still of the view that LINQ is a great step towards a more declarative world for developers. Instead of saying &#8220;take these numbers, iterate over all of them, and insert them into this list if they are less then ten&#8221;, which is an informal description of a classical imperative loop, you can now say &#8220;from these numbers, give me those that are less than ten&#8221;. The difference may be subtle, but the latter is in my opinion far more declarative and easy to read.</p>

<p>This may very well be the next big thing, but it comes at a cost. So far, my advice is to create simple performance tests for the cases where you consider adopting LINQ, to spot possible pitfalls as early as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/linq-vs-loop-a-performance-test/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>
