<?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>Technobabble: by AgileHead</title>
	<atom:link href="http://blog.agilehead.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agilehead.com</link>
	<description>About building great applications before christmas.</description>
	<lastBuildDate>Fri, 04 Sep 2009 17:57:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Solving Entity Framework Inheritance Performance Issues, with a custom QueryProvider</title>
		<link>http://blog.agilehead.com/content/solving-entity-framework-inheritance-performance-issues-with-a-custom-queryprovider/</link>
		<comments>http://blog.agilehead.com/content/solving-entity-framework-inheritance-performance-issues-with-a-custom-queryprovider/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 17:46:20 +0000</pubDate>
		<dc:creator>jeswinpk</dc:creator>
				<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://blog.agilehead.com/?p=47</guid>
		<description><![CDATA[Entity Framework Inheritance
We had used the Entity Framework in our projects earlier, but it was only recently that we tried its Inheritance support (or the lack of it, as we will soon see).
Here are the different Inheritance Types supported by the Entity Framework:
Table Per Hierarchy (TPH) &#8211; With this approach, we create a single &#8220;Super [...]]]></description>
			<content:encoded><![CDATA[<h3>Entity Framework Inheritance</h3>
<p>We had used the Entity Framework in our projects earlier, but it was only recently that we tried its Inheritance support (or the lack of it, as we will soon see).</p>
<p>Here are the different Inheritance Types supported by the Entity Framework:</p>
<p><a href="http://weblogs.asp.net/zeeshanhirani/archive/2008/08/16/single-table-inheritance-in-entity-framework.aspx">Table Per Hierarchy (TPH)</a> &#8211; With this approach, we create a single &#8220;Super Wide Table&#8221; for a hierarchy, containing the fields required for all classes in an inheritance hierarchy. If we had a Lawyer, Physician and an Artist, all inheriting from a Person class, we would create a table containing all fields from each of these types.</p>
<p>Huh! I am left wondering why this approach is even supported in the first place. Not only does this give a horrible database schema, you will also have to make compromises like making non-nullable fields nullable.</p>
<p><a href="http://blogs.msdn.com/bags/archive/2009/03/06/entity-framework-modeling-table-per-type-inheritance.aspx">Table Per Type (TPT)</a> &#8211; TPT gives you a good database schema with normalization, plus inheritance. In fact, with TPT the schema is very similar to an approach using Composition instead of Inheritance. This was the approach we adopted for the application early on, since there seemed to be nothing wrong with it.</p>
<p>But when we finished modeling the domain and hit the code, we got a bad surprise. The TPT implementation in Entity Framework is so slow that it can only be called broken. A simple join on an inherited table gave me a 3200 line query, 170KB in size, and it took 15 seconds for Entity Framework to compile the expression to SQL. And this was just for _generating_ the SQL query!</p>
<p>There are <a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/e412859c-4c85-4769-8c14-96bd0f3e022b">some discussions</a> on this, but nobody seemed to have an answer. There is no way this code can go anywhere near a production server.</p>
<p>One option was to avoid Inheritance and use Composition. Using Composition leads to code that makes less semantic sense; if you had to access a lawyer&#8217;s FirstName you had to call &#8211; Lawyer.User.FirstName. And base class methods that should be available in the Lawyer class now belong to the the User inside Lawyer. It gets worse when Lawyer inherits from another class, say LegalWorker which in turn inherits from User. We decided not to go this route.</p>
<h3>Option 2: Translating Inheritance to Composition at runtime</h3>
<p>We know that the Entity Framework handles composition queries without (too many) performance issues. If we can transform our queries involving inheritance into ones having only composition, Entity Framework can do the rest without measurable loss of performance.</p>
<p>To do that, while we design our Entity classes using the EDMX Designer we will avoid using inheritance. If ideally a Lawyer <span style="text-decoration: underline;">is a</span> User, we use composition instead of inheritance and say Lawyer <span style="text-decoration: underline;">has a</span> User.</p>
<p><img class="alignnone size-full wp-image-66" title="ef-designer" src="http://blog.agilehead.com/wp-content/uploads/2009/08/ef-designer.gif" alt="ef-designer" width="400" height="300" /></p>
<p>Sounds ugly? Yes it is. But don&#8217;t worry, we will not be using these classes in code. They are being used only to make Entity Framework do the heavy lifting. Alright. Let us assume that we have created our entire domain model using the EDMX designer.</p>
<p>The next task is to define the same set of classes, but using inheritance. These are the classes we will be using while writing code.</p>
<h3><img class="alignnone size-full wp-image-58" title="inheritance-to-composition" src="http://blog.agilehead.com/wp-content/uploads/2009/08/inheritance-to-composition.gif" alt="inheritance-to-composition" width="500" height="300" /></h3>
<p>Fig: Relation between classes with inheritance, and those with composition.</p>
<h3>Auto-generating Models from EDMX</h3>
<p>Manually modifying our Models every time we make a modification to the database or the EDMX is cumbersome and error prone. We wrote a code-generator for this, which analyzes EDMX and outputs C# classes with Inheritance.</p>
<p>For the figure represented above,  the code generator creates</p>
<p><span style="color: #99cc00;">public class User<br />
{</span><span style="color: #99cc00;"><br />
public string FirstName { get; set; }<br />
&#8230;&#8230;..<br />
}</span></p>
<p><span style="color: #99cc00;">public class Lawyer : User<br />
{<br />
</span><span style="color: #99cc00;"> public ICollection&lt;Customer&gt; Customers { get; set; }<br />
&#8230;&#8230;..<br />
}</span></p>
<p>This code generator is available under <a href="http://agilefx.codeplex.com/">AgileFx.Tools</a>.</p>
<p>Now we can write queries like:<br />
<span style="color: #0000ff;">from lawyer in Lawyers<br />
where lawyer.FirstName == &#8220;Hemchand&#8221;<br />
select lawyer;<br />
<span style="color: #000000;">//although FirstName did not belong to Lawyer in the Entity Framework Designer generated model.<br />
</span></span></p>
<p><span style="color: #0000ff;"><span style="color: #000000;">But that was the easy part. If we pass this expression (compiler converts this query to an expression tree) to the Entity Framework, it will not know what to do with it. This is because the classes used in the expression are not classes that the Entity Framework can translate to an SQL query. The Lawyer class referenced in the expression is just a mirror (albeit with inheritance) of the EF.Lawyer class, which is the one the Entity Framework is aware of.</span></span></p>
<p><span style="color: #0000ff;"><span style="color: #000000;">We need to write a Query Translator, to convert an expression referencing our new classes into ones that reference Entity Framework Designer-Generated classes (ie, from App.Models.Lawyer to App.Models.EF.Lawyer).<br />
</span></span></p>
<h3>Writing the Query Translator</h3>
<h4>How it works</h4>
<p>Let us consider a simple Where query on the Entity set lawyers.<br />
<span style="color: #0000ff;"> lawyers.Where</span>(<span style="color: #993300;">lawyer =&gt; lawyer.FirstName == &#8220;Hemchand&#8221;</span>)</p>
<p>1. The compiler translates <em><span style="color: #993300;">lawyer =&gt; lawyer.FirstName == &#8220;Hemchand&#8221; </span></em>into an expression, say EXP.</p>
<p>2. <span style="color: #0000ff;">lawyers.Where</span> is an extension method defined in System.Linq.Queryable<br />
so the actual call is &#8211; <span style="color: #0000ff;">Queryable.Where(lawyers, EXP)</span></p>
<p>3. &#8220;Queryable.Where&#8221; calls<span style="color: #0000ff;"> lawyers.<span style="text-decoration: underline;">Provider</span>.CreateQuery</span>(expression-representing-the-Where-call, EXP, &#8230;)</p>
<p>4. Since we implemented our own provider to replace Entity Framework&#8217;s Query Provider, Provider is an instance of our CustomQueryProvider. Once we receive EXP in CustomQueryProvider.CreateQuery() we parse EXP using a <a href="http://blogs.msdn.com/mattwar/archive/2007/07/31/linq-building-an-iqueryable-provider-part-ii.aspx">Query ExpressionVisitor</a> and rewrite the query after converting references of lawyer.FirstName to eflawyer.User.FirstName.</p>
<p><img class="alignnone size-full wp-image-59" title="query-provider" src="http://blog.agilehead.com/wp-content/uploads/2009/08/query-provider.gif" alt="query-provider" width="500" height="400" /></p>
<h4>References</h4>
<p>If you are writing your own IQueryable/IQueryProvider implementations, this <a href="http://blogs.msdn.com/mattwar/pages/linq-links.aspx">15-post series by Matt Warren</a> will be very helpful. Actually, it is not just helpful; it is the TextBook for writing your own Query Providers. Matt has also made the code available on CodePlex (<a href="http://www.codeplex.com/IQToolkit">IQToolkit</a>), which we have re-used in our implementation.</p>
<p>If you want to learn more about LINQ and Querying itself, you should see this <a href="http://blogs.msdn.com/wesdyer/archive/2007/01/09/about-queries.aspx">series by Wes Dyer</a>. Reading this blog is highly recommended, even if you are not writing an IQueryable Provider. These posts are profoundly insightful.</p>
<h3>Shortcomings of the Translation</h3>
<p>When we ask Entity Framework to fetch a User, it fetches an instance of the actual type of the User; like a Lawyer. <em>(Trying to get this working is what possibly caused the performance issue in Entity Framework; just guessing here)</em>.</p>
<p>That is,<br />
User u = &lt;some_query&gt;;<br />
Now u.GetType() will get you Lawyer, Artist etc.</p>
<p>With our translation layer, you get an instance of User when we issue the same query. Based on the a field like UserType we have to fetch the actual Lawyer or the Artist.</p>
<p>There are two reasons why this is not so bad; one is that you probably want just the User (base class) since that was what you queried for. The second reason is that there is no way we can fetch the actual instance without querying all the tables in the inheritance hierarchy, and the associated performance problems that come with it.</p>
<h3>Some useful side effects</h3>
<p>By implementing Query Translation, our framework became conceptually ORM Framework independent. We can now translate queries to any framework. Presently we support only Entity Framework, but the next goal is to support Linq to Sql.</p>
<h3>Someone fix the Entity Framework!</h3>
<p>Finally, some complaining. Considering that the Entity Framework shipped with .Net Framework SP1, one would expect it to be sufficiently tested. But it seems not. If inheritance is a supported feature, how is it that even the simplest queries involving inheritance result in 15 second query generation times?</p>
<p>On the other hand, Linq to Sql is awesome. It is lightweight, and generated simpler SQL. Which is why we are happy that our code base is not ORM dependent anymore. There is some talk of Linq to Sql being abandoned, which I hope is not true. Many of us need just a lightweight ORM, and Linq to Sql is just that.</p>
<p>Thanks.<em><br />
</em></p>
<p><em>AgileFx is not yet ready for public release or even a BETA, but you could take a look at it on codeplex -  <a href="http://agilefx.codeplex.com/">http://agilefx.codeplex.com/</a></em></p>
<p><em>Credits:<br />
Most of the code for the Query Translator was written by our in-house C# grandmaster <a href="http://agilehead.com/about">Hemchand T</a> . If something blows up, catch him. ;)</em></p>
<p>&#8211; Jeswin P</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.agilehead.com/content/solving-entity-framework-inheritance-performance-issues-with-a-custom-queryprovider/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>From C# on Mono, to Clojure on the JVM</title>
		<link>http://blog.agilehead.com/content/from-c-on-mono-to-clojure-on-the-jvm/</link>
		<comments>http://blog.agilehead.com/content/from-c-on-mono-to-clojure-on-the-jvm/#comments</comments>
		<pubDate>Tue, 26 May 2009 15:27:36 +0000</pubDate>
		<dc:creator>jeswinpk</dc:creator>
				<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://blog.agilehead.com/?p=10</guid>
		<description><![CDATA[Last week, we had to take a decision that reminded us of a similar situation we faced three years back. On which platform and language will we develop back-end services?
Going with Mono, in 2006
In early 2006, we were working on a fairly large Social Networking project which included building a few long running processes. We [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Last week, we had to take a decision that reminded us of a similar situation we faced three years back. On which platform and language will we develop back-end services?</p>
<h3 style="text-align: justify;">Going with Mono, in 2006</h3>
<p style="text-align: justify;">In early 2006, we were working on a fairly large Social Networking project which included building a few long running processes. We decided to build these in the popular, but not yet widely deployed platform called Mono. Two factors tilted the decision in favor of Mono, the most important being that we were building the application on Linux, and the second being that we had decided to use a mainstream object-oriented, statically-typed language for building these components. That leaves us with Java and C#; while Java was left in the quagmire of community-driven, slow design process, C# was getting increasingly productive leaving Java behind in terms of expressiveness. By 2006, Mono had an excellent C# compiler, an efficient runtime, and good coverage of the .Net Base Class Libraries.</p>
<p style="text-align: justify;">Though the project eventually failed due to non-technical reasons, Mono itself served us well for 3 years. Over that period, Mono improved by leaps and bounds. Compatibility is great with Net 2.0 libraries being almost entirely supported, compilers are exceptional, MonoDevelop 2.0 has become a capable IDE, and performance is comparable to Microsoft .Net itself. In specific benchmarks, the Mono runtime even bests the official  .Net runtime. Often, the mono compiler supports upcoming C# features ahead of the official Microsoft release.</p>
<p style="text-align: justify;">We could say that the &#8220;Mono Project&#8221; has done well over the years. I actually admire the Mono Team; they have faced unnecessary criticism within the Open Source community, often by people who knew little about the project. That did not stop them from building a great product.</p>
<p style="text-align: justify;">However, my confidence in Mono has been waning for some time.</p>
<h3 style="text-align: justify;">In the Shadows of .Net</h3>
<p style="text-align: justify;">Back in 2006, we put our trust in Mono because we refused, or perhaps disliked, to vilify a project solely because it emulated something created at Microsoft. While Open Source backers generally dislike Microsoft technology, with Mono they had another argument that being a clone it could be affected by a number of patents that Microsoft holds related to the .Net framework. This point often comes up in debates about the &#8220;safety&#8221; of the Mono project, the defense of Mono being that large parts of the .Net specification are an open, published ECMA standard. I sided with the Mono supporters then, downplaying the risk of patents from Microsoft. But then in November, Microsoft and Novell announced their <a href="http://en.wikipedia.org/wiki/Novell#Agreement_with_Microsoft">Patent Agreement</a>, which guarantees patent protection exclusively for users of Novell Linux. The Mono project is largely supported by Novell, and such an agreement is disastrous for a community project like Mono. At this point, the fence-sitters in the Open Source community largely crossed over to the anti-Mono camp. Perhaps, they were justified in doing so. I could no longer defend Mono, and my belief in the framework getting wider acceptance has diminished significantly since then.</p>
<p style="text-align: justify;">One might wonder why acceptance or ideological reasons should affect the platform we choose to build a product on. It just needs to work well, right? It is not that easy. Today my company works with customers on Microsoft and non-Microsoft platforms. We have skills on Microsoft and Open Source technologies, and while consulting or providing services to our customers we choose the right tool for the job irrespective of our ideological leaning. But when building our own products, we  are convinced about the advantages of Open Source. And as a company, we see Open Source as a principle and ideology &#8211; not just for its commercial benefit. Building a new product requires a lot of sacrifice, stamina, coffee and redbull. And you will only build it on a technology you believe in.</p>
<p style="text-align: justify;">There are other minor issues that make Mono a stranger in Open Source land. Mono uses very Windows specific conventions, like “.exe” for executables and “.dll” for libraries. For non-windows users some of these conventions look odd. Again, this is not Mono&#8217;s fault; rather a consequence of emulating a platform designed for a very different Operating System.</p>
<p style="text-align: justify;">It is entirely possible that Mono can suddenly gain acceptance if Microsoft decides to relinquish patent claims regarding the .Net framework. If it happens, .Net and Mono could well become an powerful challenger to the dominance of Java. This is very unlikely, Microsoft&#8217;s current strategy seems to be relying strongly on patents and IP to ward off the looming threat from Linux.</p>
<p style="text-align: justify;">For now, we decided to look beyond Mono and C#.</p>
<h3 style="text-align: justify;">What about Python?</h3>
<p style="text-align: justify;">In the last two years, I have mostly used Python with Django for web development. It is easy to learn and to apply. Documentation is plentiful, and there are libraries for getting most things done. While Python is  easy and practical, there was nothing quite remarkable about it. I felt Python&#8217;s strengths were not a consequence of well thought-out language design; instead it was the flexibility inherent in the dynamic nature of the language.</p>
<p style="text-align: justify;">The language itself seems to be going backwards. I do not deny that I have an affection for functional-style programming, but its relevance cannot be overlooked anymore. In a variety of situations, Functional Programming (FP) is more concise, and it is also more suited for the dawning Parallel Programming era. For this reason alone, languages like C# are incorporating functional concepts in future versions. But not Guido;  Python&#8217;s benevolent dictator has a well known dislike for functional style programming. Guido deserves credit for creating a very popular programming language, but you wonder if he is paying attention to the evolution of contemporary programming languages.</p>
<p style="text-align: justify;">Some people say that Python&#8217;s legacy will be like that of Visual Basic; if it ever gets that popular. I doubt whether it will be as popular as &#8220;VeeBee&#8221;, but I don&#8217;t disagree. There are more capable languages out there. If we ever accomplish interoperability across dynamic languages, I predict that Python would have a bleak future. Why aren&#8217;t the <a href="http://www.parrot.org/">Parrots</a> speaking, yet?</p>
<h3 style="text-align: justify;">The JVM</h3>
<p style="text-align: justify;">Java is the most common language for building applications today; due to its maturity, multi-platform support, proven reliability and the huge-huge ecosystem built around it. But Java itself is a really sucky language. It is boring to write code in Java, and even simple tasks tend to be very lengthy. Steve Yegge&#8217;s &#8216;<a href="http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html">Kingdom of Nouns</a>&#8216; captures this verbosity in a humorous style.</p>
<p style="text-align: justify;">The JVM is however, quite an interesting platform.  While originally designed for the Java language, it is now the target of many new languages. Some of these languages have been around for some time now and have gained some momentum, like Scala, JRuby, Groovy and Clojure. Groovy has not caught on as much as the others; so it fails my &#8220;excellent community&#8221; criteria. JRuby&#8217;s biggest problem is Ruby itself; rockstars are unlikely to use the JVM.</p>
<p style="text-align: justify;">But Scala is impressive; has great interop with Java, has a growing community around it,  and recently acquired a really high-profile user &#8211; Twitter. If you have been programming in Java or C#, Scala is a wonderful alternative. For Java developers, Scala offers a leap in productivity and the ability to call and get called by Java code. If you are on the Microsoft stack and hate the JVM because you hate Java, Scala offers the expressiveness that was recently introduced into C#.</p>
<p style="text-align: justify;">But I knew Scala wasn&#8217;t what I was looking for. I actually knew what I wanted; I wanted a language with few syntactic rules and constructs. I wanted to move to a language I do not have to discard any time soon.  I wanted something like the &#8216;<a href="http://www.google.com/search?q=Hundred+Year+Language">Hundred Year Language</a>&#8216;.</p>
<h3 style="text-align: justify;">LISP will win, eventually</h3>
<p style="text-align: justify;">LISP is a bit like our company&#8217;s motto: <em>“Simplicity is the ultimate sophistication.”</em> &#8211; originally by Leonardo DaVinci. The language itself is so simple that you could learn the semantics in less time than it takes to read this article. This simplicity is achieved by abstracting ideas in programming languages to the extreme; yielding a simple form which is infinitely extensible. This extensibility comes from the ability to treat a program as data, and manipulate the program itself, using a feature called Macros.</p>
<p style="text-align: justify;">While the syntax of LISP is very simple and elegant, writing a typical business or web application in LISP is (or was) much harder; perhaps an order of magnitude harder. This is especially true for newbies; unless they are really determined it is difficult to find documentation and useful libraries. This happened to me as well. I had read many books on LISP, but I did not have the confidence to execute a real project in LISP. It has been done before by so many others, but there is a big challenge in transitioning from a theoretical understanding of LISP and getting it to work in practice.</p>
<p style="text-align: justify;">Although I was unable to use LISP in any production code, what I learnt from LISP profoundly impacted the way I saw programming. And since then, I have been keeping a close watch on upcoming languages bearing any resemblance to LISP. One such language was <a href="http://clojure.org/">Clojure</a>.</p>
<h3 style="text-align: justify;">Clojure, the acceptable LISP</h3>
<p style="text-align: justify;">Clojure is a dialect of LISP and a dynamic programming language, running on the JVM.   It has all the benefits of LISP, including the powerful macro system. It also has useful features which help with parallel programming; like Software Transactional Memory for dealing with shared state without worries. Clojure makes some of LISP&#8217;s biggest problems go away. Lack of Libraries and other supporting infrastructure like Application Servers. Java has libraries for everything, from connecting to SAP to talking to Twitter. And you could deploy on robust servers like Tomcat.</p>
<p style="text-align: justify;">Everything ever built for Java can be used with Clojure.</p>
<p style="text-align: justify;">Clojure is very young, and like any new programming language you would encounter minor inconsistencies and bugs. But it is stable enough to get work done. There is an active and rapidly growing community around the language now, contributing useful modules and supporting new users. Clojure is like a breath of fresh air.</p>
<p style="text-align: justify;">
<p style="text-align: justify;">We are excited about building our new product on Clojure. Such unconventional choices are only possible when you are building your own product; but hey, that&#8217;s one good reason to do one. Enthusiasm is  vital ingredient in building products; they sprout from a variety of reasons. Like usefulness, financial rewards, challenges, technology, ..and programming languages like Clojure.</p>
<p style="text-align: justify;"><em>Jeswin P Kumar<br />
(jeswinpk@my-company-name-is-AGILEHEAD.com)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.agilehead.com/content/from-c-on-mono-to-clojure-on-the-jvm/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>His Master&#8217;s Voice: by Francis Barraud</title>
		<link>http://blog.agilehead.com/content/his-masters-voice-by-francis-barraud/</link>
		<comments>http://blog.agilehead.com/content/his-masters-voice-by-francis-barraud/#comments</comments>
		<pubDate>Sat, 23 May 2009 12:12:23 +0000</pubDate>
		<dc:creator>jeswinpk</dc:creator>
				<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://blog.agilehead.com/?p=3</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-4" title="his_masters_voice" src="http://blog.agilehead.com/wp-content/uploads/2009/05/his_masters_voice-300x214.jpg" alt="his_masters_voice" width="300" height="214" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.agilehead.com/content/his-masters-voice-by-francis-barraud/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
