<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><!-- generator="wordpress/2.2.3" --><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Sidar Ok Technical Blog !</title>
	<link>http://www.sidarok.com/web/blog</link>
	<description>still believes in coding to make dreams come true</description>
	<pubDate>Tue, 02 Jun 2009 19:38:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/SidarOk" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Feed update</title>
		<link>http://feedproxy.google.com/~r/SidarOk/~3/KQbLXCD_lRE/feed-update.html</link>
		<comments>http://www.sidarok.com/web/blog/content/2008/12/12/feed-update.html#comments</comments>
		<pubDate>Fri, 12 Dec 2008 13:19:10 +0000</pubDate>
		<dc:creator>Sidar Ok</dc:creator>
		
		<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://www.sidarok.com/web/blog/content/2008/12/12/feed-update.html</guid>
		<description><![CDATA[I am doing a feed migration before starting a series of posts.
The old feed is obsolete from now on, and is going to change. The new feed is http://feeds.feedburner.com/sidarok . Please update it as I am depressed to see a very small number on the right.
Thanks all.
]]></description>
			<content:encoded><![CDATA[<p>I am doing a feed migration before starting a series of posts.</p>
<p>The old feed is obsolete from now on, and is going to change. The new feed is <a href="http://feeds.feedburner.com/sidarok" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://feeds.feedburner.com/sidarok');">http://feeds.feedburner.com/sidarok</a> . Please update it as I am depressed to see a very small number on the right.</p>
<p>Thanks all.</p>
<img src="http://feeds.feedburner.com/~r/SidarOk/~4/KQbLXCD_lRE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sidarok.com/web/blog/content/2008/12/12/feed-update.html/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sidarok.com/web/blog/content/2008/12/12/feed-update.html</feedburner:origLink></item>
		<item>
		<title>Lazy Loading with Linq to SQL POCO s</title>
		<link>http://feedproxy.google.com/~r/SidarOk/~3/PMIpFlwul58/lazy-loading-with-linq-to-sql-poco-s.html</link>
		<comments>http://www.sidarok.com/web/blog/content/2008/10/29/lazy-loading-with-linq-to-sql-poco-s.html#comments</comments>
		<pubDate>Wed, 29 Oct 2008 01:35:40 +0000</pubDate>
		<dc:creator>Sidar Ok</dc:creator>
		
		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[SQL]]></category>

		<category><![CDATA[.NET 3.5]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.sidarok.com/web/blog/content/2008/10/29/lazy-loading-with-linq-to-sql-poco-s.html</guid>
		<description><![CDATA[Yes, here is that post. While doing Linq to SQL POCO screen cast, and writing the last post about how to achieve POCOs, one of the issues that came up was how to do lazy loading - since we are using pure IList, we of course were not getting lazy loading. That&#8217;s something that we [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Yes, here is that post. While doing Linq to SQL POCO screen cast, and writing <a href="http://www.sidarok.com/web/blog/content/2008/10/14/achieving-poco-s-in-linq-to-sql.html" >the last post about how to achieve POCOs</a>, one of the issues that came up was how to do lazy loading - since we are using pure IList, we of course were not getting lazy loading. That&#8217;s something that we didn&#8217;t want, so there had to be some workaround this limitation.</p>
<p align="justify">The reason that it took me a while to come up with a fair solution, is not that only I am very lazy, but also this was a challenging task. Before I outline the solution, let me explain my train of thoughts so you would understand my pain while hitting the Linq to SQL design decision blocks.</p>
<h3 align="justify"></h3>
<h3 align="justify">PITA Points</h3>
<p align="justify">So, to get lazy loading, what we need to do is to intercept a call to a collection, then load it because it is requested at that time. For this, an obvious solution is using Dynamic Proxies. For this matter, I chose<a href="http://www.codeproject.com/KB/cs/LinFuPart1.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.codeproject.com/KB/cs/LinFuPart1.aspx');"> LinFu Dynamic proxy</a> and it gave me lots of playground. This is in one pocket.</p>
<p align="justify">The most usual thing to do would be, to make the process transparent from user as much as possible. So my idea was to Proxy out the table definitions in context, and I went ahead for that, but what&#8217;s that? Table&lt;T&gt; is sealed ! Well done ! So I can&#8217;t proxy the sealed tables, because DynamicProxy works on an inheritance basis (with LinFu duck typing, it is possible to unseal, but again Table&lt;T&gt; doesn&#8217;t have 1 aggregate interface to choose as a contract. Nice isn&#8217;t it ?).</p>
<p align="justify">Then I have to proxy the entities, and their related properties. This comes with the implication of marking the to-be lazily loaded properties as virtual.</p>
<p align="justify">And another point is that EntitySet doesn&#8217;t have a non-generic implementation, so I can&#8217;t reach the association without type. This comes with another implication that the Interceptor I am going to write needs to know about the relationship to load, but hey, we can hide this into a repository, and that&#8217;s what repositories are for, aren&#8217;t they ? (This will make more sense at the end of the post)</p>
<h3 align="justify">Let&#8217;s go ahead !</h3>
<p align="justify">I am still going to use the same simple Questions - Answers model from the last article, but I need to slightly change Question entity as following (it is still a POCO):</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Question</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> _QuestionId;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">int</span> QuestionId</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>             <span style="color: #0000ff">return</span> _QuestionId;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>             _QuestionId = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _QuestionText;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">string</span> QuestionText</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  21:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  22:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  23:</span>             <span style="color: #0000ff">return</span> _QuestionText;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  24:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  25:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  26:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  27:</span>             _QuestionText = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  28:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  29:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  30:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  31:</span>     <span style="color: #0000ff">private</span> IList&lt;Answer&gt; _Answer;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  32:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  33:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> IList&lt;Answer&gt; Answer</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  34:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  35:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  36:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  37:</span>             <span style="color: #0000ff">return</span> _Answer;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  38:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  39:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  40:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  41:</span>             _Answer = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  42:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  43:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  44:</span> }</pre>
<p align="justify">Answers list is virtual from now on,so that LinFu can override comfotably.</p>
<p align="justify">So let&#8217;s look at the tests for lazy loading. What I am going to check for it first is, of course, after getting the instance can I demand for the list and get it successfully ? For e.g, can I do a count ?</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> [TestMethod()]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> should_get_correct_answer_count_when_lazily_loaded()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>   LazyLoadingRepository target = <span style="color: #0000ff">new</span> LazyLoadingRepository(); <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>   <span style="color: #0000ff">int</span> id = 1; <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>   <span style="color: #0000ff">using</span> (QuestionDataContext context = <span style="color: #0000ff">new</span> QuestionDataContext())</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>     Question actual;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>     actual = target.GetQuestion(context, id);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>     Assert.IsNotNull(actual);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>     Assert.AreEqual(actual.Answer.Count, 1);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>    }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span> }</pre>
<p align="justify">Note that this can always be further refactored in order to get by specification, which I am leaving that as an exercise to reader (I always wanted to do that).</p>
<p align="justify">Now I need to do a negative test, to see that function does not fool me and does not eagerly load everything. So when I try to access the answers without a context, I should get a Data Context disposed exception:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> [TestMethod()]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> should_throw_when_lazily_loaded_and_reached_outside_the_context()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>   LazyLoadingRepository target = <span style="color: #0000ff">new</span> LazyLoadingRepository(); <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>   <span style="color: #0000ff">int</span> id = 1; <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>   Question actual;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>   <span style="color: #0000ff">using</span> (QuestionDataContext context = <span style="color: #0000ff">new</span> QuestionDataContext())</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>      actual = target.GetQuestion(context, id);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>      Assert.IsNotNull(actual);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>   }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>   <span style="color: #0000ff">try</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>     <span style="color: #0000ff">int</span> count = actual.Answer.Count;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>   }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>   <span style="color: #0000ff">catch</span> (Exception ex)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span>      Assert.IsInstanceOfType(ex, <span style="color: #0000ff">typeof</span>(ObjectDisposedException));</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span>      <span style="color: #0000ff">return</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  21:</span>    }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  22:</span>    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> Exception(<span style="color: #006080">&#8220;Should have thrown Object disposed exception, sorry !&#8221;</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  23:</span> }</pre>
<p align="justify">Good, my tests are failing, what a depressive world we inhabit. With the previous implementation which was without lazy loading, The first test would fail with &#8220;Object Reference not set to instance of an object&#8221; exception, and int the second test Assert would fail because the exception is a NullReferenceException, not an ObjectDisposedException.</p>
<p align="justify">Now let&#8217;s try to pass these tests with some magic. Here is where LinFu calls us to the dark side. In GetQuestion, we are not going to return the actual object, but instead a proxied Question object. To create a proxy with LinFu, we need a custom interceptor which implements Linfu.DynamicProxy.IInvokeWrapper. Our invoke wrapper needs to know the DataContext, to load the entities and the relationship specification to load the related data. With the light of this info, here is how GetQuestion looks like :</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> Question GetQuestion(QuestionDataContext context, <span style="color: #0000ff">int</span> id)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span>   EntityInvokeWrapper&lt;Answer&gt; interceptor = <span style="color: #0000ff">new</span> EntityInvokeWrapper&lt;Answer&gt;(context, (Answer a) =&gt; a.QuestionId == id);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>   ProxyFactory factory = <span style="color: #0000ff">new</span> ProxyFactory();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>   Question retVal = factory.CreateProxy&lt;Question&gt;(interceptor);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>   <span style="color: #0000ff">return</span> retVal;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span> }</pre>
<p align="justify">This will help me to override Answers list, and replace it with my own implementation. In IInvokeWrapper, we need to implement BeforeInvoke, DoInvoke and AfterInvoke. We are only interested in DoInvoke. I am trying to not to reinvent the wheel, so in background I am using EntitySet&#8217;s lazy loading mechanism but that&#8217;s transparent to user, since I am proxying the IList&lt;T&gt; with an EntitySet&lt;T&gt; too. But how do I assoicate it with the table in the context and the relationship that I get ? Here is the answer:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> EntityInvokeWrapper&lt;TChi&gt; : IInvokeWrapper</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span>        <span style="color: #0000ff">where</span> TChi : <span style="color: #0000ff">class</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>   <span style="color: #0000ff">private</span> DataContext Context</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>     get;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>     set;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>   }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>   <span style="color: #0000ff">private</span> Func&lt;TChi, <span style="color: #0000ff">bool</span>&gt; RelationshipSpecification</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>     get;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>     set;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>   }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>   <span style="color: #0000ff">public</span> EntityInvokeWrapper(DataContext context, Func&lt;TChi, <span style="color: #0000ff">bool</span>&gt; relationship)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">this</span>.Context = context;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span>     <span style="color: #0000ff">this</span>.RelationshipSpecification = relationship;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span>   }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  21:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  22:</span>   <span style="color: #cc6633">#region</span> IInvokeWrapper Members</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  23:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  24:</span>   <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> AfterInvoke(InvocationInfo info, <span style="color: #0000ff">object</span> returnValue)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  25:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  26:</span>      <span style="color: #008000">//Console.WriteLine(&#8221;After&#8221;);</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  27:</span>   }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  28:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  29:</span>   <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> BeforeInvoke(InvocationInfo info)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  30:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  31:</span>     <span style="color: #008000">//Console.WriteLine(&#8221;Before&#8221;);</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  32:</span>   }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  33:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  34:</span>   <span style="color: #0000ff">public</span> <span style="color: #0000ff">object</span> DoInvoke(InvocationInfo info)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  35:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  36:</span>     <span style="color: #008000">//Console.WriteLine(&#8221;During&#8221;);</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  37:</span>     <span style="color: #0000ff">string</span> name = info.TargetMethod.Name;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  38:</span>     <span style="color: #0000ff">if</span> (name.StartsWith(<span style="color: #006080">&#8220;get_&#8221;</span>) &amp;&amp;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  39:</span>   info.TargetMethod.ReturnType.GetInterfaces().Contains(<span style="color: #0000ff">typeof</span>(IEnumerable&lt;TChi&gt;)))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  40:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  41:</span>       <span style="color: #008000">//Console.WriteLine(&#8221;Enumerable detected!&#8221;);</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  42:</span>       EntitySet&lt;TChi&gt; wrapper = <span style="color: #0000ff">new</span> EntitySet&lt;TChi&gt;();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  43:</span>       wrapper.SetSource(<span style="color: #0000ff">this</span>.Context.GetTable&lt;TChi&gt;().Where&lt;TChi&gt;(<span style="color: #0000ff">this</span>.RelationshipSpecification));</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  44:</span>       <span style="color: #0000ff">return</span> wrapper;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  45:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  46:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  47:</span>     <span style="color: #0000ff">return</span> OriginalCall(info);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  48:</span>    }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  49:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  50:</span>   <span style="color: #0000ff">private</span> <span style="color: #0000ff">object</span> OriginalCall(InvocationInfo info)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  51:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  52:</span>     <span style="color: #008000">//Console.WriteLine(&#8221;Original = &#8221; + info.Target);</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  53:</span>     <span style="color: #0000ff">return</span> info.TargetMethod.Invoke(info.Target, info.Arguments);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  54:</span>    }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  55:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  56:</span>     <span style="color: #cc6633">#endregion</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  57:</span> }</pre>
<p align="justify">So as you see, we are doing the magic in DoInvoke, and checking that if it is a property, with an access to the enumeration of the child type (TChi) that we are interested in, we are silently stepping and saying that, hey, what you need for this is an EntitySet, but you can use it as an IList <img src='http://www.sidarok.com/web/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> Line 43 does the association between our EntitySet and the context table, and we are returning the EntitySet which has the full lazy loading support - so when it is accessed, it will perform the necessary query on its source. But the Actual Entity is clueless about what&#8217;s happening, and all the consumers of the entity who need that lazy loading will treat it as List&lt;T&gt; as it is one.</p>
<h3 align="justify">Conclusion</h3>
<p align="justify">In this article, I outlined a solution to enable the lazy loading while using POCOs in Linq to SQL. I tried to reuse as much as possible, and wanted to show the pain points what kept me away from a best design. Of course I expect this will always not be a comprehensive solution, but as always, if it gives some ideas, I am happy.</p>
<p align="justify">Before leaving, be sure to check out great LinFu stuff from <a href="http://plaureano.blogspot.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://plaureano.blogspot.com/');">Philip Laureno</a> as it also has other goodies such as simulated duck typing and mixin support, and even a dependency injection framework. Comments and free beers are welcome as usual.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f10%2f29%2flazy-loading-with-linq-to-sql-poco-s.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f10%2f29%2flazy-loading-with-linq-to-sql-poco-s.html');"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f10%2f29%2flazy-loading-with-linq-to-sql-poco-s.html&#038;bgcolor=FF0033&#038;cfgcolor=FF0033&#038;cbgcolor=FFFF66" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<img src="http://feeds.feedburner.com/~r/SidarOk/~4/PMIpFlwul58" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sidarok.com/web/blog/content/2008/10/29/lazy-loading-with-linq-to-sql-poco-s.html/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sidarok.com/web/blog/content/2008/10/29/lazy-loading-with-linq-to-sql-poco-s.html</feedburner:origLink></item>
		<item>
		<title>Achieving POCO s in Linq to SQL</title>
		<link>http://feedproxy.google.com/~r/SidarOk/~3/XgazV6cXhJw/achieving-poco-s-in-linq-to-sql.html</link>
		<comments>http://www.sidarok.com/web/blog/content/2008/10/14/achieving-poco-s-in-linq-to-sql.html#comments</comments>
		<pubDate>Tue, 14 Oct 2008 01:18:23 +0000</pubDate>
		<dc:creator>Sidar Ok</dc:creator>
		
		<category><![CDATA[SQL]]></category>

		<category><![CDATA[C# 3.0]]></category>

		<category><![CDATA[.NET 3.5]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.sidarok.com/web/blog/content/2008/10/14/achieving-poco-s-in-linq-to-sql.html</guid>
		<description><![CDATA[After the nice talk with developers.ie, it is really nice to see that people have interest in the topic. Unfortunately the quality of the recording was not very nice and connection dropped twice, so I decided to put together this blog post to show how we can work with leaving persistence polluted entities on our [...]]]></description>
			<content:encoded><![CDATA[<p align="justify"><a href="http://www.developers.ie/Webcasts.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.developers.ie/Webcasts.aspx');">After the nice talk with developers.ie</a>, it is really nice to see that people have interest in the topic. Unfortunately the quality of the recording was not very nice and connection dropped twice, so I decided to put together this blog post to show how we can work with leaving persistence polluted entities on our behind.</p>
<h3>Why is it so important ?</h3>
<p align="justify">I can hear lots of comments from people around me mainly concerning around &#8220;Why do we need this much hassle, when we can already have designer support, and VS integrated goodies of an ORM mapper ?&#8221;. First, I have to say that it is fair enough to think in this way. But when things start to go beyond trivial, you start to have problems with persistence or technology polluted entities. On top of my head, I can think of the following:</p>
<ol>
<li>
<p align="justify"><strong>Technology Agnosticism is a bliss</strong> : This concept is usually revolving around PI (Persistence Ignorance), but it is not only that. Persistence Ignorance means that your entities should be cleared of any persistence related code constraints that a framework - usually an ORM - forces on you. This is, for e.g. if you have attribute level mapping where those attributes are not part of your domain but are there just because some framework wants them to be there, then your domain is not persistence ignorant. Or, if your framework requests you to have specific types for handling associations to be used, like EntitySet and EntityRef s in Linq to SQL , same goes for you. This can also be another technology that wants your entities to be serializable for some reason. We need to try to avoid them as much as possible and concentrate on our business concerns there, not to bend our domain to be fitting into those technological discrepancies. This approach will also promote testability. The same goes for the need of implementing an abstract class, or interfaces like INotifyPropertyChanged when you don&#8217;t want them.</p>
</li>
<li>
<p align="justify"><strong>Relying on Linq to SQL Designer is painful</strong>: Designer puts everything in one file, regenerates files each time when you save so you loose your changes such as xml comments. Needless to say, the only OOTB support is attribute level configuration, even for XML you need to use sqlmetal tool out of designer process.</p>
</li>
<li>
<p align="justify"><strong>Configuration should not be anything that your domain to be concerned</strong> <strong>about</strong>: Unless you are building a configuration system <img src='http://www.sidarok.com/web/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
</li>
</ol>
<h3>Let&#8217;s get geared</h3>
<p align="justify">In the light of this, when we are working with Linq to SQL designer, we tend to think that it is impossible to achieve POCOs, but indeed it is: solution is don&#8217;t ditch POCOs, just ditch the designer <img src='http://www.sidarok.com/web/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> While implementing POCOs, we need to know a couple of things beforehand about Linq to SQL internals, because we will be on our own when we have any problems.</p>
<ol>
<li>
<p align="justify">EntitySet and EntityRef are indeed useful classes, and they are there to achieve something. When you add an entity to an association, EntitySet manages the identity and back references. That is, for children you need to assign the correct parent id to the child otherwise you will loose relationship. Same goes for EntityRef and for 1-1 relations.</p>
</li>
<li>
<p align="justify">INotifyPropertyChanging and INotifyPropertyChanged are there not only because of informing us by providing the ability to subscribe to necessary events and get notified when a property is changed, but to leverage lazy loading as well. When we discard them, we are back to eager loading.</p>
</li>
</ol>
<h3>Enough Rambling, let me see the wild world of code</h3>
<p align="justify">For this post, I will only focus on the first part, so the lazy loading is a matter of another one. The approach we are going to take is, use the XML mapping instead of attribute based modeling. I am gonna use the trivial Questions and Answers model, where one question can have multiple Answers associated to them. Here is how it looks like :</p>
<p align="justify">&nbsp;</p>
<p align="center"><a href="http://www.sidarok.com/web/blog/wp-content/uploads/2008/10/image.png" ><img src="http://www.sidarok.com/web/blog/wp-content/uploads/2008/10/image-thumb.png" style="border-width: 0px" alt="image" border="0" width="554" height="277" /></a></p>
<p align="center">Question and Answers entities</p>
<p align="justify">And their related code is pretty simple, nothing fancy. Here is the Answer POCO :</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Answer</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">public</span> Answer()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> _QuestionId;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> QuestionId</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>             <span style="color: #0000ff">return</span> _QuestionId;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span>             _QuestionId = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  21:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  22:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> _AnswerId;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  24:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  25:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> AnswerId</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  26:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  27:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  28:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  29:</span>             <span style="color: #0000ff">return</span> _AnswerId;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  30:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  31:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  32:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  33:</span>             _AnswerId = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  34:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  35:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  36:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  37:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _AnswerText;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  38:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  39:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> AnswerText</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  40:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  41:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  42:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  43:</span>             <span style="color: #0000ff">return</span> _AnswerText;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  44:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  45:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  46:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  47:</span>             _AnswerText = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  48:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  49:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  50:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  51:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _IsMarkedAsCorrect;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  52:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  53:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsMarkedAsCorrect</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  54:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  55:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  56:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  57:</span>             <span style="color: #0000ff">return</span> _IsMarkedAsCorrect;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  58:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  59:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  60:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  61:</span>             _IsMarkedAsCorrect = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  62:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  63:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  64:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  65:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  66:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> _Vote;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  67:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  68:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Vote</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  69:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  70:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  71:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  72:</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>._Vote;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  73:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  74:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  75:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  76:</span>             _Vote = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  77:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  78:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  79:</span> }</pre>
<p>Yeah, clean, pure C#: No attributes, EntityRef s, nothing. Same goes for Questions as well, where the association is achieved through the good old simple List&lt;T&gt;:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Question</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> _QuestionId;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> QuestionId</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>             <span style="color: #0000ff">return</span> _QuestionId;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>             _QuestionId = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _QuestionText;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> QuestionText</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  21:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  22:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  23:</span>             <span style="color: #0000ff">return</span> _QuestionText;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  24:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  25:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  26:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  27:</span>             _QuestionText = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  28:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  29:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  30:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  31:</span>     <span style="color: #0000ff">private</span> List&lt;Answer&gt; _Answer;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  32:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  33:</span>     <span style="color: #0000ff">public</span> List&lt;Answer&gt; Answer</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  34:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  35:</span>         get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  36:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  37:</span>             <span style="color: #0000ff">return</span> _Answer;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  38:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  39:</span>         set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  40:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  41:</span>             _Answer = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  42:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  43:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  44:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  45:</span> }</pre>
<p align="justify">To use these entities as POCOs, I need a way to externally define the mappings between db tables, columns to the relevant object fields. I chose the other OOTB supported way, XML. As I am so lazy to write it on my own, I ran the following sql metal command to generate it from the DB:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> sqlmetal /server:sidarok-pc /database:QuestionsAnswers /code:a.cs /map:Questions.xml</pre>
<p>As you see, it also generates the code in a.cs file but I am gonna throw it out. Let&#8217;s check if the generated XML maps to our fields:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&#8221;1.0&#8243;</span> <span style="color: #ff0000">encoding</span><span style="color: #0000ff">=&#8221;utf-8&#8243;</span>?<span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">Database</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;questionsanswers&#8221;</span> <span style="color: #ff0000">xmlns</span><span style="color: #0000ff">=&#8221;http://schemas.microsoft.com/linqtosql/mapping/2007&#8243;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span>   <span style="color: #0000ff">&lt;</span><span style="color: #800000">Table</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;dbo.Answer&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;Answer&#8221;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Type</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;Answer&#8221;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">Column</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;AnswerId&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;AnswerId&#8221;</span> <span style="color: #ff0000">Storage</span><span style="color: #0000ff">=&#8221;_AnswerId&#8221;</span> <span style="color: #ff0000">DbType</span><span style="color: #0000ff">=&#8221;Int NOT NULL IDENTITY&#8221;</span> <span style="color: #ff0000">IsPrimaryKey</span><span style="color: #0000ff">=&#8221;true&#8221;</span> <span style="color: #ff0000">IsDbGenerated</span><span style="color: #0000ff">=&#8221;true&#8221;</span> <span style="color: #ff0000">AutoSync</span><span style="color: #0000ff">=&#8221;OnInsert&#8221;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">Column</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;QuestionId&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;QuestionId&#8221;</span> <span style="color: #ff0000">Storage</span><span style="color: #0000ff">=&#8221;_QuestionId&#8221;</span> <span style="color: #ff0000">DbType</span><span style="color: #0000ff">=&#8221;Int NOT NULL&#8221;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">Column</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;AnswerText&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;AnswerText&#8221;</span> <span style="color: #ff0000">Storage</span><span style="color: #0000ff">=&#8221;_AnswerText&#8221;</span> <span style="color: #ff0000">DbType</span><span style="color: #0000ff">=&#8221;Text NOT NULL&#8221;</span> <span style="color: #ff0000">CanBeNull</span><span style="color: #0000ff">=&#8221;false&#8221;</span> <span style="color: #ff0000">UpdateCheck</span><span style="color: #0000ff">=&#8221;Never&#8221;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">Column</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;IsMarkedAsCorrect&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;IsMarkedAsCorrect&#8221;</span> <span style="color: #ff0000">Storage</span><span style="color: #0000ff">=&#8221;_IsMarkedAsCorrect&#8221;</span> <span style="color: #ff0000">DbType</span><span style="color: #0000ff">=&#8221;Bit NOT NULL&#8221;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">Column</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;Vote&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;Vote&#8221;</span> <span style="color: #ff0000">Storage</span><span style="color: #0000ff">=&#8221;_Vote&#8221;</span> <span style="color: #ff0000">DbType</span><span style="color: #0000ff">=&#8221;Int NOT NULL&#8221;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">Association</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;FK_GoodAnswer_Question&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;Question&#8221;</span> <span style="color: #ff0000">Storage</span><span style="color: #0000ff">=&#8221;_Question&#8221;</span> <span style="color: #ff0000">ThisKey</span><span style="color: #0000ff">=&#8221;QuestionId&#8221;</span> <span style="color: #ff0000">OtherKey</span><span style="color: #0000ff">=&#8221;QuestionId&#8221;</span> <span style="color: #ff0000">IsForeignKey</span><span style="color: #0000ff">=&#8221;true&#8221;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Type</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>   <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Table</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>   <span style="color: #0000ff">&lt;</span><span style="color: #800000">Table</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;dbo.Question&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;Question&#8221;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Type</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;Question&#8221;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">Column</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;QuestionId&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;QuestionId&#8221;</span> <span style="color: #ff0000">Storage</span><span style="color: #0000ff">=&#8221;_QuestionId&#8221;</span> <span style="color: #ff0000">DbType</span><span style="color: #0000ff">=&#8221;Int NOT NULL IDENTITY&#8221;</span> <span style="color: #ff0000">IsPrimaryKey</span><span style="color: #0000ff">=&#8221;true&#8221;</span> <span style="color: #ff0000">IsDbGenerated</span><span style="color: #0000ff">=&#8221;true&#8221;</span> <span style="color: #ff0000">AutoSync</span><span style="color: #0000ff">=&#8221;OnInsert&#8221;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">Column</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;QuestionText&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;QuestionText&#8221;</span> <span style="color: #ff0000">Storage</span><span style="color: #0000ff">=&#8221;_QuestionText&#8221;</span> <span style="color: #ff0000">DbType</span><span style="color: #0000ff">=&#8221;NVarChar(300) NOT NULL&#8221;</span> <span style="color: #ff0000">CanBeNull</span><span style="color: #0000ff">=&#8221;false&#8221;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">Association</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&#8221;FK_GoodAnswer_Question&#8221;</span> <span style="color: #ff0000">Member</span><span style="color: #0000ff">=&#8221;Answer&#8221;</span> <span style="color: #ff0000">Storage</span><span style="color: #0000ff">=&#8221;_Answer&#8221;</span> <span style="color: #ff0000">ThisKey</span><span style="color: #0000ff">=&#8221;QuestionId&#8221;</span> <span style="color: #ff0000">OtherKey</span><span style="color: #0000ff">=&#8221;QuestionId&#8221;</span> <span style="color: #ff0000">DeleteRule</span><span style="color: #0000ff">=&#8221;NO ACTION&#8221;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Type</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span>   <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Table</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Database</span><span style="color: #0000ff">&gt;</span></pre>
<p>Now, let&#8217;s write the simple select test to see if it just works. This repository test is intentionally an integration test, to see that if I can get the question entity along with its children:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> [TestMethod()]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> GetQuestionTest()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>   QuestionsRepository target = <span style="color: #0000ff">new</span> QuestionsRepository(); <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>   <span style="color: #0000ff">int</span> id = 2; <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>   Question actual;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>   actual = target.GetQuestion(id);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>   Assert.IsNotNull(actual);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>   Assert.IsTrue(actual.Answer.Count &gt; 0);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span> }</pre>
<p>And after this the implementation is quite trivial. Just note the eager loading that is needed explicitly because otherwise the Answers list will never get assigned and remain null :</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> Question GetQuestion(<span style="color: #0000ff">int</span> id)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">using</span> (QuestionDataContext context = <span style="color: #0000ff">new</span> QuestionDataContext())</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>         DataLoadOptions options = <span style="color: #0000ff">new</span> DataLoadOptions();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>         options.LoadWith&lt;Question&gt;(q =&gt; q.Answer);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>         context.LoadOptions = options;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>         <span style="color: #0000ff">return</span> context.Questions.Single&lt;Question&gt;(q =&gt; q.QuestionId == id);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span> }</pre>
<p>Aha,we don&#8217;t have a DataContext yet ! Let&#8217;s create it, we need to feed with connection string and XML file. Note the Table&lt;T&gt; implementations are there just for convenience:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> QuestionDataContext : DataContext</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span>   <span style="color: #0000ff">static</span> XmlMappingSource source = XmlMappingSource.FromXml(File.ReadAllText(<span style="color: #006080">@&#8221;C:UserssidarokDesktopPocoDemoPocoDemoquestions.xml&#8221;</span>));</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>   <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> connStr = <span style="color: #006080">&#8220;Data Source=sidarok-pc;Initial Catalog=QuestionsAnswers;Integrated Security=True&#8221;</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>   <span style="color: #0000ff">public</span> QuestionDataContext()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>     : <span style="color: #0000ff">base</span>(connStr, source)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>   }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>   <span style="color: #0000ff">public</span> Table&lt;Question&gt; Questions</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>     get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>       <span style="color: #0000ff">return</span> <span style="color: #0000ff">base</span>.GetTable&lt;Question&gt;();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>    }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span>    <span style="color: #0000ff">public</span> Table&lt;Answer&gt; Answers</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span>    {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span>      get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  21:</span>      {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  22:</span>        <span style="color: #0000ff">return</span> <span style="color: #0000ff">base</span>.GetTable&lt;Answer&gt;();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  23:</span>      }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  24:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  25:</span> }</pre>
<p>Now the test passes, hurray, we are happy let&#8217;s party! Before let&#8217;s take a step forward and write a test for Insert:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> [TestMethod()]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> InsertQuestionTest()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>   QuestionsRepository target = <span style="color: #0000ff">new</span> QuestionsRepository(); <span style="color: #008000">// TODO: Initialize to an appropriate value        </span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>   Question question = <span style="color: #0000ff">new</span> Question()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>   {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>     QuestionText = <span style="color: #006080">&#8220;Temp Question&#8221;</span>,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>     Answer = <span style="color: #0000ff">new</span> List&lt;Answer&gt;()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>       <span style="color: #0000ff">new</span> Answer()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>       {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>         AnswerText = <span style="color: #006080">&#8220;Temp Answer 1&#8243;</span>,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>         IsMarkedAsCorrect = <span style="color: #0000ff">true</span>,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>         Vote = 10,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>        },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>        <span style="color: #0000ff">new</span> Answer()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>        {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span>          AnswerText = <span style="color: #006080">&#8220;Temp Answer 2&#8243;</span>,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span>          IsMarkedAsCorrect = <span style="color: #0000ff">false</span>,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span>          Vote = 10,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  21:</span>         },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  22:</span>         <span style="color: #0000ff">new</span> Answer()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  23:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  24:</span>           AnswerText = <span style="color: #006080">&#8220;Temp Answer 3&#8243;</span>,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  25:</span>           IsMarkedAsCorrect = <span style="color: #0000ff">true</span>,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  26:</span>           Vote = 10,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  27:</span>          },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  28:</span>       }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  29:</span>       };</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  30:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  31:</span>       <span style="color: #0000ff">using</span> (TransactionScope scope = <span style="color: #0000ff">new</span> TransactionScope())</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  32:</span>       {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  33:</span>         target.InsertQuestion(question);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  34:</span>         Assert.IsTrue(question.QuestionId &gt; 0);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  35:</span>         Assert.IsTrue(question.Answer[0].AnswerId &gt; 0);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  36:</span>         Assert.IsTrue(question.Answer[1].AnswerId &gt; 0);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  37:</span>         Assert.IsTrue(question.Answer[2].AnswerId &gt; 0);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  38:</span>        }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  39:</span> }</pre>
<p>Simple insert test, insert questions along with its children, answers and check that if they have been assigned any Ids. The implementation is again, nothing different from the usual implementation :</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> InsertQuestion(Question q)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">using</span> (QuestionDataContext context = <span style="color: #0000ff">new</span> QuestionDataContext(connStr))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>         context.Questions.InsertOnSubmit(q);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>         context.SubmitChanges();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span> }</pre>
<p>When we run this test, we will run into this error:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> Test method QuestionRepositoryTest.QuestionsRepositoryTest.InsertQuestionTest threw exception:  System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint &#8220;FK_GoodAnswer_Question&#8221;. The conflict occurred in database &#8220;QuestionsAnswers&#8221;, table &#8220;dbo.Question&#8221;, column &#8216;QuestionId&#8217;.</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> The statement has been terminated</pre>
<p>Aha, well this was kinda expected. We knew that we had to maintain the identity and back references, but we didn&#8217;t. Shame on us. But how are we gonna do that ? We don&#8217;t know the ID value before we insert, how do we tell Linq to SQL to pick the new identity ? Are we back to square 1, @@IDENTITY_SCOPE ?</p>
<p>Of course if I am writing this post, the answer has to be no <img src='http://www.sidarok.com/web/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> The secret is in the back reference, the back reference is there just because for this matter.</p>
<p>What we need to do now is, in each Answer we need to preserve a reference to the parent Question and for each question that is added, or when the list is overriden we need to assign the Answer&#8217;s QuestionId property to this back reference&#8217;s one. As we now don&#8217;t have the EntitySet, we need to do that on our own, but it is easy enough. For Answers, here is the back reference:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> Question _Question;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> <span style="color: #0000ff">public</span> Question Question</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>     get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>._Question;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>     set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>         <span style="color: #0000ff">this</span>._Question = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>         <span style="color: #0000ff">this</span>._QuestionId = <span style="color: #0000ff">value</span>.QuestionId;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span> }</pre>
<p>And for Question POCO, when the List is overriden, we need to put our own logic to handle this, which is: for every child answer, ensure that back reference and the reference id is set:</p>
<p style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4">&nbsp;</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> List&lt;Answer&gt; _Answer;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> <span style="color: #0000ff">public</span> List&lt;Answer&gt; Answer</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>    get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>    {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>        <span style="color: #0000ff">return</span> _Answer;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>    }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>    set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>    {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>        _Answer = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>        <span style="color: #0000ff">foreach</span> (var answer <span style="color: #0000ff">in</span> _Answer)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>        {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>            answer.QuestionId = <span style="color: #0000ff">this</span>.QuestionId;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>            answer.Question = <span style="color: #0000ff">this</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>        }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>    }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span> }</pre>
<p>And Test passes after doing this.  Hope this gives some idea what you can do and what you need to know beforehand.</p>
<h3>Conclusion</h3>
<p>Desire to decouple domain entities from Technological aspects is important in SoC &amp; SRP, and these principles are important for nearly everything, varying from pure basic to DDD. To achieve this in Linq to SQL, we need to say good bye rid to EntiyRef, EntitySet, INotifyPropertyChanged, INotifyPropertyChanging interfaces.</p>
<p>The next subject I am going to attack is Lazy Lading with POCOs, stay tuned till then !</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f10%2f14%2fachieving-poco-s-in-linq-to-sql.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f10%2f14%2fachieving-poco-s-in-linq-to-sql.html');"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f10%2f14%2fachieving-poco-s-in-linq-to-sql.html&amp;bgcolor=FF0033&amp;cfgcolor=FF0033&amp;cbgcolor=FFFF66" alt="kick it on DotNetKicks.com" border="0" /></a></p>
<img src="http://feeds.feedburner.com/~r/SidarOk/~4/XgazV6cXhJw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sidarok.com/web/blog/content/2008/10/14/achieving-poco-s-in-linq-to-sql.html/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sidarok.com/web/blog/content/2008/10/14/achieving-poco-s-in-linq-to-sql.html</feedburner:origLink></item>
		<item>
		<title>Screencast on supporting POCO with Linq to SQL</title>
		<link>http://feedproxy.google.com/~r/SidarOk/~3/tqAiPZI6HJ8/screencast-on-supporting-poco-with-linq-to-sql.html</link>
		<comments>http://www.sidarok.com/web/blog/content/2008/09/29/screencast-on-supporting-poco-with-linq-to-sql.html#comments</comments>
		<pubDate>Mon, 29 Sep 2008 11:09:36 +0000</pubDate>
		<dc:creator>Sidar Ok</dc:creator>
		
		<category><![CDATA[Talk]]></category>

		<category><![CDATA[SQL]]></category>

		<category><![CDATA[.NET 3.5]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.sidarok.com/web/blog/content/2008/09/29/screencast-on-supporting-poco-with-linq-to-sql.html</guid>
		<description><![CDATA[  http://www.developers.ie was kind enough to invite me to do a screencast for them, where I will be talking in .NET Coffee Break show about POCO support in Linq to SQL.
The event is on Thursday, 11:00 A.M Greenwich time. Registration is free through http://www.developers.ie/Webcasts.aspx .
Hope to see you all there !
UPDATE : Thanks to Paschal [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.developers.ie/images/coffeebreak_small.gif" width="150" height="150" />  <a href="http://www.developers.ie" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.developers.ie');">http://www.developers.ie</a> was kind enough to invite me to do a screencast for them, where I will be talking in .NET Coffee Break show about POCO support in Linq to SQL.</p>
<p>The event is on Thursday, 11:00 A.M Greenwich time. Registration is free through <a href="http://www.developers.ie/Webcasts.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.developers.ie/Webcasts.aspx');">http://www.developers.ie/Webcasts.aspx</a> .</p>
<p>Hope to see you all there !</p>
<p>UPDATE : Thanks to Paschal and <a href="http://www.developers.ie" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.developers.ie');">http://www.developers.ie</a>, to provide me this opportunity. <a href="http://www.sidarok.com/files/PocoDemo-Screencast.rar" >Here</a> are the source for the demo. Sorry for the unluckiness that connection dropped twice (yes, twice <img src='http://www.sidarok.com/web/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> ). Thanks everybody for listening, and as son as Pascal provides the link I will post it here.</p>
<p>UPDATE 2: Still waiting the show to go online.</p>
<p>UPDATE 3: Here it is :<a href="http://82.141.251.117/Inda/lv1.wmv" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://82.141.251.117/Inda/lv1.wmv');">Part1 </a>, <a href="http://82.141.251.117/Inda/lv2.wmv" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://82.141.251.117/Inda/lv2.wmv');">Part2</a>.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f09%2f29%2fscreencast-on-supporting-poco-with-linq-to-sql.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f09%2f29%2fscreencast-on-supporting-poco-with-linq-to-sql.html');"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f09%2f29%2fscreencast-on-supporting-poco-with-linq-to-sql.html&amp;bgcolor=FF0033&amp;cfgcolor=FF0033&amp;cbgcolor=FFFF66" alt="kick it on DotNetKicks.com" border="0" /></a></p>
<img src="http://feeds.feedburner.com/~r/SidarOk/~4/tqAiPZI6HJ8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sidarok.com/web/blog/content/2008/09/29/screencast-on-supporting-poco-with-linq-to-sql.html/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sidarok.com/web/blog/content/2008/09/29/screencast-on-supporting-poco-with-linq-to-sql.html</feedburner:origLink></item>
		<item>
		<title>What is this Managed Extensibility Framework thing all about ?</title>
		<link>http://feedproxy.google.com/~r/SidarOk/~3/pg1JnipBxok/what-is-this-managed-extensibility-framework-thing-all-about.html</link>
		<comments>http://www.sidarok.com/web/blog/content/2008/09/26/what-is-this-managed-extensibility-framework-thing-all-about.html#comments</comments>
		<pubDate>Fri, 26 Sep 2008 00:39:30 +0000</pubDate>
		<dc:creator>Sidar Ok</dc:creator>
		
		<category><![CDATA[Managed Extensibility Framework (MEF)]]></category>

		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[Dependency Injection]]></category>

		<guid isPermaLink="false">http://www.sidarok.com/web/blog/content/2008/09/26/what-is-this-managed-extensibility-framework-thing-all-about.html</guid>
		<description><![CDATA[No, this is not another introduction post. Being one of the earliest adopter of MEF, I started to see a great confusion around what MEF is, and what it really is suppose to do in this wild world of DI containers&#8230; Well ok, I can&#8217;t lie anymore about, as people have twitter proofs to throw [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">No, this is not another <a href="http://www.sidarok.com/web/blog/content/2008/06/16/managed-extensibility-framework-mef-at-a-glance.html" >introduction post</a>. Being one of the earliest adopter of <a href="http://www.codeplex.com/MEF" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.codeplex.com/MEF');">MEF</a>, I started to see a great confusion around what MEF is, and what it really is suppose to do in this wild world of DI containers&#8230; Well ok, I can&#8217;t lie anymore about, as people have twitter proofs to throw at me, I was a part of that confusion :</p>
<p align="center"><a href="http://www.sidarok.com/web/blog/wp-content/uploads/2008/09/image.png" ><img src="http://www.sidarok.com/web/blog/wp-content/uploads/2008/09/image-thumb.png" alt="image" style="border-width: 0px" border="0" width="705" height="86" /></a></p>
<p align="justify">As you see, I was pretty biased as an avid DI user, that MEF <em>is</em> an IoC container so why doesn&#8217;t it behave in a normal DI ish way ? I was seeing MEF as an only IoC container so much, and in that limited perspective its design has sucked big time for me, especially because of the unnatural Export - Import system. And for that matter, I even implemented a <a href="http://www.sidarok.com/web/blog/content/category/managed-extensibility-framework-mef" >way of an external configuration for MEF</a>.</p>
<p align="justify">This has proven to be really painful. <a href="http://blogs.msdn.com/gblock/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://blogs.msdn.com/gblock/');">Glenn Block</a>, saw my pain through twitter and called me for an hour of a very nice and informative chat.</p>
<p align="justify">Here is the result: MEF is not a DI container. DI is only a part of the solution that MEF tries to bring into the scene. <a href="http://ayende.com/Blog/archive/2008/09/25/the-managed-extensibility-framework.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://ayende.com/Blog/archive/2008/09/25/the-managed-extensibility-framework.aspx');">Ayende just got quicker in the game, and explained it well</a>, so I couldn&#8217;t have the joy of telling the world first <img src='http://www.sidarok.com/web/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p align="justify">I won&#8217;t repeat what <a href="http://ayende.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://ayende.com');">Ayende</a> said, I&#8217;ll just try to make things a bit more concrete in perspective of my understanding as a person who likes to talk over examples.</p>
<h3 align="justify">Understanding MEF with an example</h3>
<p align="justify">Now, it is time to take those DI glasses off and look at a different view. Forget about DI, containers, IoCs et all. Let&#8217;s start thinking with a concrete example we all know about. Visual Studio has a plug in infrastructure. Say that we are handed to build that, what do we do ?</p>
<ol>
<li>
<p align="justify">First, we need to come up with an interface (be it an SDK or API reference), sort of a hook to outside, so addin implementors can code against it.</p>
<ol>
<li>
<p align="justify">This involves the lifecycle of the addin. You should be able to achieve basic needs create instance / retrieve already created one (singleton).</p>
</li>
</ol>
</li>
<li>
<p align="justify">We should be able to provide a way of interact with add-in. Add-in should be able to easily send you messages about, say show solution explorer or open a file in editor.</p>
</li>
<li>
<p align="justify">We should be able to lazy load it if needed. E.g there is no need to load an add-in at start up when the add in is supposed to be performing within a context menu event.</p>
</li>
<li>
<p align="justify">A new add-in, or a replace of an old one should be as easy as throwing the dll in bin folder. This means that if a new set of addins is in place, you need to be able to choose amongst them. (meta-data based discovery)</p>
</li>
<li>
<p align="justify">When an add-in is replaced or simply dead, we need to unload it.</p>
</li>
</ol>
<p>So in all this sense, VS is a composite application. It needs to be <strong><em>composed</em></strong> dynamically.Beware, during all these steps we still don&#8217;t care about the actual add-in. We just care that if it complies to our needs that we specified in the contract. We need something that quacks, and looks like a duck. Anything that supplies these demands, even if it is a dog, more than welcome to be part of our Visual Studio.</p>
<p>Now, these are all huge works. Imagine just lazy loading of a component has a lot of edge cases already(#3), let alone solving the unloading problem from AppDomain(#5). But let&#8217;s say that we finished and shipped our Visual Studio. Happy days, counting the money.</p>
<p>And now, we are assigned on a new task and writing Lutz Roeder&#8217;s great tool Reflector. And guess what, Lutz Roeder wants us to implement an add-in infrastructure so that they can perform several tasks in reflected code. To accomplish this, we need to do ALL the steps above again, from 1 to 5. All that drudge work, and none of it is reusable because we designed it in an application specific way (and that perfectly makes sense since our concern was not to build that framework). Now you got the point: <strong>MEF is a core .NetFx component that adds ability of composability to any app by default.  </strong>Its aim in short term is, not <em>replacing</em> IoC containers but <em>working with </em>IoC containers, which fits this greater vision.</p>
<h3 align="justify">Another Example - Publisher Subscriber</h3>
<p>A lot of confusion is also going around between MEF and System.Addin Namespace, similar to the confusion addressed above. Let&#8217;s look at another example that has nothing to do with add-ins, to show that problem is not an add-in bound system.</p>
<p>Say we have a banking system, with a strong domain model. We need services like Accounting, Personal Info, Leasing etc. And in this bank, in every two weeks or so there comes a new service.For the discovery and configuration of these new services, you would normally use an ESB (Enterprise Service Bus). That discovery part has indeed very similar roots with MEF.  <strong>MEF provides you a lightweight bus-alike approach for your extensions</strong>. By hosting MEF, you are delegating this discovery system to MEF, and it provides you a rich Meta Data to be used. The Export - Import model indeed looks very similar to Publisher and Subscriber model in this sense.</p>
<h3>Conclusion</h3>
<p>MEF is by no means a replacement to today&#8217;s IoC containers, nor to System.Addin . It aims to be a reusable solution to provide a common base for the heavy lifting code of extensible applications, so we (hopefully) will need to worry less about extensibility concerns of the application. If you like, you can use MEF as your IoC container, but this doesn&#8217;t mean that you should. We can also use our favorite IoC container of choice with a free (as in speech) collaboration with MEF by Resolvers.I am planning to cover the concepts with a hands on application<strong> </strong>which will hopefully help to build better understanding, stay tuned.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f09%2f26%2fwhat-is-this-managed-extensibility-framework-thing-all-about.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f09%2f26%2fwhat-is-this-managed-extensibility-framework-thing-all-about.html');"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f09%2f26%2fwhat-is-this-managed-extensibility-framework-thing-all-about.html&amp;bgcolor=FF0033&amp;cfgcolor=FF0033&amp;cbgcolor=FFFF66" alt="kick it on DotNetKicks.com" border="0" /></a></p>
<img src="http://feeds.feedburner.com/~r/SidarOk/~4/pg1JnipBxok" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sidarok.com/web/blog/content/2008/09/26/what-is-this-managed-extensibility-framework-thing-all-about.html/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sidarok.com/web/blog/content/2008/09/26/what-is-this-managed-extensibility-framework-thing-all-about.html</feedburner:origLink></item>
		<item>
		<title>After Linq to Sql Talk in Cork</title>
		<link>http://feedproxy.google.com/~r/SidarOk/~3/nZDehfVJZEM/after-linq-to-sql-talk-in-cork.html</link>
		<comments>http://www.sidarok.com/web/blog/content/2008/09/23/after-linq-to-sql-talk-in-cork.html#comments</comments>
		<pubDate>Tue, 23 Sep 2008 16:39:24 +0000</pubDate>
		<dc:creator>Sidar Ok</dc:creator>
		
		<category><![CDATA[Talk]]></category>

		<category><![CDATA[Unit Testing]]></category>

		<category><![CDATA[SQL]]></category>

		<category><![CDATA[.NET 3.5]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.sidarok.com/web/blog/content/2008/09/23/after-linq-to-sql-talk-in-cork.html</guid>
		<description><![CDATA[The talk last night was awesome. Thanks to all who showed up, there was a good turnout. Also thanks to Joe Gill and MTUG for organizing the event, and to Microsoft for sponsoring. 
 
I had the great chance of sharing my thoughts, knowledge and experience on Linq to SQL, analyzing upsides and downsides of it. It was [...]]]></description>
			<content:encoded><![CDATA[<p>The talk last night was awesome. Thanks to all who showed up, there was a good turnout. Also thanks to Joe Gill and MTUG for organizing the event, and to Microsoft for sponsoring. </p>
<p><a href="http://www.sidarok.com/web/blog/wp-content/uploads/2008/09/pic1.jpg" ><img border="0" width="424" src="http://www.sidarok.com/web/blog/wp-content/uploads/2008/09/pic1-thumb.jpg" alt="pic1" height="319" style="border-width: 0px" /></a> </p>
<p>I had the great chance of sharing my thoughts, knowledge and experience on Linq to SQL, analyzing upsides and downsides of it. It was targeted for advanced audience, so I enjoyed talking to a bunch of geeks.</p>
<p><a href="http://www.sidarok.com/files/linq2sqlms200.ppt" onclick="javascript:pageTracker._trackPageview('/downloads/files/linq2sqlms200.ppt');">Here are the slides for the presentation</a>. I also made a demo on how we can support Domain First Design and create POCOs with Linq to SQL, and didn&#8217;t have the time to do the second demo on a short multi tier development demonstration. <a href="http://www.sidarok.com/files/presentationdemos.rar" >You can find them here in</a> rar format.</p>
<p>It is always great to have good techies around you, and free beers along with chicken wings !!</p>
<p>This was the beginning event of the year and I am honored to have done it. Hope this will encourage more people to share the views and experiences on subject matters.</p>
<img src="http://feeds.feedburner.com/~r/SidarOk/~4/nZDehfVJZEM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sidarok.com/web/blog/content/2008/09/23/after-linq-to-sql-talk-in-cork.html/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sidarok.com/web/blog/content/2008/09/23/after-linq-to-sql-talk-in-cork.html</feedburner:origLink></item>
		<item>
		<title>Agenda on Linq to SQL talk</title>
		<link>http://feedproxy.google.com/~r/SidarOk/~3/aq-kImyT6_8/agenda-on-linq-to-sql-talk.html</link>
		<comments>http://www.sidarok.com/web/blog/content/2008/09/20/agenda-on-linq-to-sql-talk.html#comments</comments>
		<pubDate>Sat, 20 Sep 2008 21:18:38 +0000</pubDate>
		<dc:creator>Sidar Ok</dc:creator>
		
		<category><![CDATA[Talk]]></category>

		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[SQL]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.sidarok.com/web/blog/content/2008/09/20/agenda-on-linq-to-sql-talk.html</guid>
		<description><![CDATA[I decided on some of the bullet points that I will talk about at 22nd. This will be a MS-200 level talk, means that it requires also basic level knowledge of Linq to SQL. Although if you don&#8217;t have the basic knowledge, you are still welcome to fill the seats and make the place look [...]]]></description>
			<content:encoded><![CDATA[<p>I decided on some of the bullet points that I will talk about at 22nd. This will be a MS-200 level talk, means that it requires also basic level knowledge of Linq to SQL. Although if you don&#8217;t have the basic knowledge, you are still welcome to <a href="http://www.cork.mtug.ie/Events/EventInfo.aspx?ID=b2515894-866f-4d70-8c87-ebaa69c69b7d" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.cork.mtug.ie/Events/EventInfo.aspx?ID=b2515894-866f-4d70-8c87-ebaa69c69b7d');">fill the seats</a> and make the place look crowded <img src='http://www.sidarok.com/web/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>In this first event of the year, here is what I am going to talk about ORM s and Linq to SQL in particular:</p>
<ol>
<li>Building a Common Glossary</li>
<li>Defining the Problem</li>
<li>Building in house ORM/DAL vs Use an existing one</li>
<li>Linq to SQL Comes into play : Myths and Realities</li>
<li>Linq to SQL beyond drag and drop : Concepts</li>
<li>Linq to SQL Entity Model</li>
<li>Mapping Engine</li>
<li>Attribute Level or External ?<br />
<blockquote><p>SQL Metal to rescue</p></blockquote>
<blockquote><p>What it does, what it lacks</p></blockquote>
</li>
<li>Understanding DataContext</li>
<li>Change Management &amp; Change Communication Strategies</li>
<li>Advanced Topics (If time permits)
<ol>
<li>Debugging and Troubleshooting</li>
<li>Transaction Handling</li>
<li>Concurrency &amp; Conflict Handling Scenarios</li>
<li>Entity Validation</li>
<li>Security Model</li>
<li>Serialization</li>
<li>Performance Advices &amp; Best practices</li>
</ol>
</li>
</ol>
<p>I also prepared a couple of demos to serve some of the practical implementation of the concepts that I want to share.</p>
<p>Looking forward to see you at Imperial Hotel on Monday!</p>
<img src="http://feeds.feedburner.com/~r/SidarOk/~4/aq-kImyT6_8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sidarok.com/web/blog/content/2008/09/20/agenda-on-linq-to-sql-talk.html/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sidarok.com/web/blog/content/2008/09/20/agenda-on-linq-to-sql-talk.html</feedburner:origLink></item>
		<item>
		<title>Talking on Linq to SQL on 22nd of September</title>
		<link>http://feedproxy.google.com/~r/SidarOk/~3/6jcxWITaeLM/talking-on-linq-to-sql-on-22nd-of-september.html</link>
		<comments>http://www.sidarok.com/web/blog/content/2008/09/05/talking-on-linq-to-sql-on-22nd-of-september.html#comments</comments>
		<pubDate>Fri, 05 Sep 2008 09:19:00 +0000</pubDate>
		<dc:creator>Sidar Ok</dc:creator>
		
		<category><![CDATA[Talk]]></category>

		<category><![CDATA[SQL]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.sidarok.com/web/blog/content/2008/09/05/talking-on-linq-to-sql-on-22nd-of-september.html</guid>
		<description><![CDATA[I am going to be talking about Linq to SQL, one of the ORM mappers coming out of Redmond. I am planning to cover how to utilize Linq to SQL to get the best benefit out of it, Linq to SQL&#8217;s place in the ORM world and pros-cons and of course ways to apply patterns [...]]]></description>
			<content:encoded><![CDATA[<p>I am going to be talking about Linq to SQL, one of the ORM mappers coming out of Redmond. I am planning to cover how to utilize Linq to SQL to get the best benefit out of it, Linq to SQL&#8217;s place in the ORM world and pros-cons and of course ways to apply patterns and practices with a couple of demos.  I&#8217;ll be more than happy to see any of you there and do some geek chat , and have a couple of pints. Pints and chicken tenders are free and Microsoft&#8217;s courtesy (Himm, free tenders&#8230;That is more appealing then the talk&#8230;tenders&#8230;)</p>
<p>Registration is free through this link :<a href=" http://www.cork.mtug.ie/Events/EventInfo.aspx?ID=b2515894-866f-4d70-8c87-ebaa69c69b7d" onclick="javascript:pageTracker._trackPageview('/outbound/article/ http://www.cork.mtug.ie/Events/EventInfo.aspx?ID=b2515894-866f-4d70-8c87-ebaa69c69b7d');"> http://www.cork.mtug.ie/Events/EventInfo.aspx?ID=b2515894-866f-4d70-8c87-ebaa69c69b7d</a> </p>
<p> See you there !</p>
<img src="http://feeds.feedburner.com/~r/SidarOk/~4/6jcxWITaeLM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sidarok.com/web/blog/content/2008/09/05/talking-on-linq-to-sql-on-22nd-of-september.html/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sidarok.com/web/blog/content/2008/09/05/talking-on-linq-to-sql-on-22nd-of-september.html</feedburner:origLink></item>
		<item>
		<title>Documents 2.0 - Consumable Documents</title>
		<link>http://feedproxy.google.com/~r/SidarOk/~3/nIzLPLgiYMY/documents-20-consumable-documents.html</link>
		<comments>http://www.sidarok.com/web/blog/content/2008/09/03/documents-20-consumable-documents.html#comments</comments>
		<pubDate>Wed, 03 Sep 2008 00:29:33 +0000</pubDate>
		<dc:creator>Sidar Ok</dc:creator>
		
		<category><![CDATA[Opinionated]]></category>

		<category><![CDATA[Documentation]]></category>

		<category><![CDATA[Agile]]></category>

		<guid isPermaLink="false">http://www.sidarok.com/web/blog/content/2008/09/03/documents-20-consumable-documents.html</guid>
		<description><![CDATA[I was in Brussels last weekend to visit a friend, and had a chance of meeting up with a group of highly skilful and visionary people. We had a very interesting chat, and one point was about documentation came up. Seemed like everybody has the same pain, reading and writing technical documents consume an incredible [...]]]></description>
			<content:encoded><![CDATA[<p>I was in Brussels last weekend to visit a <a href="http://javicrespotech.blogspot.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://javicrespotech.blogspot.com/');">friend</a>, and had a chance of meeting up with a group of highly skilful and visionary people. We had a very interesting chat, and one point was about documentation came up. Seemed like everybody has the same pain, reading and writing technical documents consume an incredible part of any developer&#8217;s day, usually most of the work done is about searching the right needle through a pile of garbage.</p>
<p>It is amazing that over the years that hardware technology is evolved a lot. Not so much as it, software has also progressed and proved some quality.</p>
<p>But one thing couldn&#8217;t make the big move: Documentation. No, I know we have better word processors, heck, I am using one for this entry. We have the enough technology to write more old styled documents. We have been reading tons of papers 30 years ago to accomplish a single development task, thanks to the internet, web 2.0 or whatever brought us, we now have wikis, podcasts, screenCasts and blogs which speak to different senses and styles. Of course there still is some technical problems, such as indexability, searchability, and relevancy to the need of these but it is getting better over the time.So what is my problem ?</p>
<p>Ok, I am gonna say it. It is that we DON&#8217;T get enough benefit of all these in our daily business environment.How many of you have been left with a legacy code ? Surely most of you. How much are them was documented ? I bet since most aren&#8217;t, it is not a few either. And why don&#8217;t we get any podcast, or a web cast on, let&#8217;s say Provider Infrastructure for Insurance, or Dealing with Leasing Options in Banking where we are more than happily get provided hundreds of pages ?</p>
<p>Now known that while handing over the legacy code, pairing with the person who originally wrote it is one of the best things that you can do, why does this information fly? When that person goes away either you loose all the info, or if you have a documenting process it might be even worse: you&#8217;ll face the hungry monster of 200 pages, which hasn&#8217;t been maintained like 10 years ago.</p>
<p>My point is, let&#8217;s produce those documents keeping in mind that a person will read it - not that because writing a thick book on the subject makes us seem more intelligent and hardworking. I want to see more agile documents. Those include:</p>
<ol>
<li>If a document is longer than 20 pages, please consider breaking it down to multiple ones.</li>
<li>If you can tell more with less words, please do so. Usage of patterns for e.g, applies well here.</li>
<li>If you can tell more without any word, even more appreciated. Using pictures, diagrams or even comics sometimes, usually reach to the point</li>
<li>Know your audience, and produce the document for him/her not for comfort of yourself. If I am your audience, know that I am a highly technical guy and not a native English speaker. Don&#8217;t assume that I know what you know, such as your sector&#8217;s details and don&#8217;t think that I am dying to look at your design document to learn what a Memento Pattern is all about.</li>
<li>If you think that you can express better if you speak, do a podcast and link it from that document. If you want to show a process, let&#8217;s say a migration chain, don&#8217;t hesitate to record a screen cast. It will take only 2 mins and will stay there forever, so you won&#8217;t spend your time going over and over it to the newbies joining the team. You don&#8217;t need to be an expert, as these will stay internal.</li>
<li>Blog about what you found. Put it in the company wiki. Get angry with the people asking you questions before looking at the wiki. Girr !</li>
<li>Use documents to increase the quality of communication, not the time spent on it. If you need a meeting to talk about a 100 page document, either the document or meeting is not to its full intent.</li>
</ol>
<p>In business environments, I see an incremental Agile Documenting Fear (term stolen from M.Fowler as in Parser Fear) that leads us to the apocalypse of inconsumable documents. IMHO, that&#8217;s something that we should avoid for our own goodness.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f09%2f03%2fdocuments-20-consumable-documents.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f09%2f03%2fdocuments-20-consumable-documents.html');"><img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f09%2f03%2fdocuments-20-consumable-documents.html&amp;bgcolor=FF0033&amp;cfgcolor=FF0033&amp;cbgcolor=FFFF66" alt="kick it on DotNetKicks.com" /></a></p>
<img src="http://feeds.feedburner.com/~r/SidarOk/~4/nIzLPLgiYMY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sidarok.com/web/blog/content/2008/09/03/documents-20-consumable-documents.html/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sidarok.com/web/blog/content/2008/09/03/documents-20-consumable-documents.html</feedburner:origLink></item>
		<item>
		<title>Localizing Linq to SQL Entities</title>
		<link>http://feedproxy.google.com/~r/SidarOk/~3/NRySoDXYyMI/localizing-linq-to-sql-entities.html</link>
		<comments>http://www.sidarok.com/web/blog/content/2008/08/18/localizing-linq-to-sql-entities.html#comments</comments>
		<pubDate>Mon, 18 Aug 2008 02:00:50 +0000</pubDate>
		<dc:creator>Sidar Ok</dc:creator>
		
		<category><![CDATA[SQL]]></category>

		<category><![CDATA[ADO.NET]]></category>

		<category><![CDATA[C# 3.0]]></category>

		<category><![CDATA[.NET 3.5]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.sidarok.com/web/blog/content/2008/08/18/localizing-linq-to-sql-entities.html</guid>
		<description><![CDATA[Back from the holidays! Not getting too much sun certainly encourages to write code rather than chilling out. Writing on this subject was on my list as Linq to SQL got more mature, need for it in multi-cultural applications has arisen respectively. Also an old post of Ayende beat me to think about how a [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Back from the holidays! Not getting too much sun certainly encourages to write code rather than chilling out. Writing on this subject was on my list as Linq to SQL got more mature, need for it in multi-cultural applications has arisen respectively. <a href="http://ayende.com/Blog/archive/2006/12/26/7001.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://ayende.com/Blog/archive/2006/12/26/7001.aspx');">Also an old post of Ayende</a> beat me to think about how a similar problem could be solved in Linq to SQL.</p>
<p align="justify">I&#8217;ll use the same model that he provided, and it is the following:</p>
<p align="center"><a href="http://www.sidarok.com/web/blog/wp-content/uploads/2008/08/tmpd45.png" ><img src="http://www.sidarok.com/web/blog/wp-content/uploads/2008/08/tmpd45-thumb.png" alt="tmpD45" style="border-width: 0px" width="433" border="0" height="122" /></a></p>
<p align="center">Figure 1. Table structure for multi-lingual products</p>
<p align="justify">&nbsp;</p>
<p align="justify">As in the original post, the challenge is just to load the Product Names for the current culture(or specific culture), not all of them related to one product. So in <a href="http://www.hibernate.org/343.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.hibernate.org/343.html');">nhibernate</a>, there are filters to solve this problem in an elegant way. It is elegant because it is externally configurable and includes no intrusiveness in your design.</p>
<p align="justify">When internationalising-localizing comes into play, there are 2 main approaches from a Domain Perspective and it lies behind the answer of the question :</p>
<p align="justify">&#8220;Is localization a concern of my domain?&#8221;</p>
<p align="justify">In fairness, the answer changes for every domain (to my experience in most cases it is either no, or part of a different domain, such as administration). A simple way of determining if this is an issue is, to check that if domain needs to know about different cultures or domain elements with different languages need to talk to each other or not (Can Reuters publish news in Portugese ?). If the answer is yes, then even eager loading all language translations can be an option. But otherwise, we&#8217;ll need to abstract away so that domain won&#8217;t know about this infrastructurel concern.</p>
<p align="justify">In the original post, <a href="http://www.ayende.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.ayende.com/');">Ayende</a> uses filters in NHibernate. In Linq to SQL we don&#8217;t have filters but <a href="http://www.sidarok.com/web/blog/content/2008/05/02/10-tips-to-improve-your-linq-to-sql-application-performance.html" >as mentioned before</a>, we have <a href="http://ayende.com/Blog/archive/2006/12/26/7001.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://ayende.com/Blog/archive/2006/12/26/7001.aspx');">Load Options</a> to give a criteria and reduce the amount of data we retrieve.</p>
<p align="justify">As a matter of fact, we expect following test to pass. Note that this is a state based test to test the data retrieved is not more than one.</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #008000">///A test for GetProduct</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> <span style="color: #008000">///&lt;/summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span> [TestMethod()]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> GetProductTest()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>   ProductsRepository target = <span style="color: #0000ff">new</span> ProductsRepository(); <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>   <span style="color: #0000ff">int</span> prodId = 1; <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>   <span style="color: #0000ff">int</span> lcId = 3; <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>   Product actual = target.GetProduct(prodId, lcId);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>   Assert.AreEqual(<span style="color: #006080">&#8220;Prod13&#8243;</span>, actual.Name);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>   Assert.IsTrue(actual.ProductNames.Count == 1);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span> }</pre>
<p align="justify">Where the records in the table are as follows:</p>
<p align="center"><a href="http://www.sidarok.com/web/blog/wp-content/uploads/2008/08/image.png" ><img src="http://www.sidarok.com/web/blog/wp-content/uploads/2008/08/image-thumb.png" alt="image" style="border-width: 0px" width="347" border="0" height="191" /></a></p>
<p align="center">Figure 2. Records in Product Names Table. As seen, there are 2 records for product id &#8216;1&#8242;</p>
<p align="justify">The entity structure that we have to use with Linq to SQL (generated by the courtesy of the designer) is as follows:</p>
<p align="center"><a href="http://www.sidarok.com/web/blog/wp-content/uploads/2008/08/image1.png" ><img src="http://www.sidarok.com/web/blog/wp-content/uploads/2008/08/image-thumb1.png" alt="image" style="border-width: 0px" width="489" border="0" height="176" /></a></p>
<p align="center">Figure 3. Object Model of Product and Product Name</p>
<p align="justify">Looks innocent doesn&#8217;t it ? The secret thing is that Product will always have a list of ProductNames, which in my case will always have 1 element. If I want to keep my domain ignorant of this, this certainly is a bad thing but this is what L2S gives me by default. There are ways to overcome this issue of course, but those are not the point of the post.</p>
<p align="justify">In addition to the model, I&#8217;ll add another field called &#8220;Name&#8221; to the model that&#8217;s not mapped to any column in db, to reach the same example. This is achieved by a partial class:</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Product</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>         get;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>         set;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span> }</pre>
<p>Now we are ready to write the code that passes the test. Note that we are utilizing <a href="http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.associatewith.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.associatewith.aspx');">AssociateWith Generic Method</a> to make the necessary filtering.</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #008000">/// Gets the product for the current culture.</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span> <span style="color: #008000">/// &lt;param name=&#8221;prodId&#8221;&gt;The prod id.&lt;/param&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span> <span style="color: #008000">/// &lt;param name=&#8221;lcId&#8221;&gt;The lc id to do localization filter.&lt;/param&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span> <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span> <span style="color: #0000ff">public</span> Product GetProduct(<span style="color: #0000ff">int</span> prodId, <span style="color: #0000ff">int</span>? lcId)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">using</span> (ProductsDataContext context = <span style="color: #0000ff">new</span> ProductsDataContext())</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>         <span style="color: #008000">// set load options if localizable filter needed</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>         <span style="color: #0000ff">if</span> (lcId.HasValue)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>             DataLoadOptions options = <span style="color: #0000ff">new</span> DataLoadOptions();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>             options.AssociateWith&lt;Product&gt;(p =&gt; p.ProductNames.Where&lt;ProductName&gt;(pn =&gt; pn.CultureId == lcId));</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>             context.LoadOptions = options;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span>         Product pFromDb = context.Products.Single&lt;Product&gt;(p =&gt; p.ProductId == prodId);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  21:</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> Product()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  22:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  23:</span>                  Amount = pFromDb.Amount,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  24:</span>                  ProductId = pFromDb.ProductId,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  25:</span>                  Size = pFromDb.Size,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  26:</span>                  Name = pFromDb.ProductNames.First&lt;ProductName&gt;().Name,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  27:</span>                  ProductNames = pFromDb.ProductNames</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  28:</span>         };</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  29:</span>      }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  30:</span> }</pre>
<p align="justify">Now since we are done with the original post, let&#8217;s go beyond the bar and implement inserts &amp; updates too. With Inserts, there are 2 things that I am going to handle: 1 - It is a brand new insert 2 - It is just an insert of a new product name in another language.</p>
<p align="justify">For first one here is the test :</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #008000">///A test for InsertProduct</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> <span style="color: #008000">///&lt;/summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span> [TestMethod()]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Insert_for_Completely_New_Prod()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>     ProductsRepository target = <span style="color: #0000ff">new</span> ProductsRepository(); <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>     Product p = <span style="color: #0000ff">new</span> Product()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>          Amount = 31,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>          Name = <span style="color: #006080">&#8220;English Name&#8221;</span>,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>              ProductId = 0,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>              Size = 36,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>     };</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>     <span style="color: #0000ff">int</span> lcId = 7;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>     <span style="color: #0000ff">using</span> (TransactionScope scope = <span style="color: #0000ff">new</span> TransactionScope(TransactionScopeOption.Suppress))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span>         target.InsertProduct(p, lcId);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span>         Assert.IsTrue(p.ProductId &gt; 0);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span>         Assert.IsTrue(p.ProductNames.Count &gt; 0);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  21:</span>      }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  22:</span> }</pre>
<p align="justify">And for the second one:</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #008000">///A test for InsertProduct</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> <span style="color: #008000">///&lt;/summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span> [TestMethod()]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Insert_Name_for_Existing_Prod()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>     ProductsRepository target = <span style="color: #0000ff">new</span> ProductsRepository(); <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>     Product p = target.GetProduct(1);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">int</span> firstCount = p.ProductNames.Count;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>     p.Name = <span style="color: #006080">&#8220;Kurdish Name&#8221;</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">int</span> lcId = 9;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">using</span> (TransactionScope scope = <span style="color: #0000ff">new</span> TransactionScope(TransactionScopeOption.Suppress))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>         target.InsertProduct(p, lcId);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>         Product prAfterInsert = target.GetProduct(p.ProductId);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>         Assert.AreEqual(firstCount + 1, prAfterInsert.ProductNames.Count);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span> }</pre>
<p align="justify">So, passing test is obvious. I need to do an extra insert to the product tables if it is a new one, and that&#8217;s it:</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #008000">/// Inserts the product.</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span> <span style="color: #008000">/// &lt;param name=&#8221;p&#8221;&gt;The p.&lt;/param&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span> <span style="color: #008000">/// &lt;param name=&#8221;lcId&#8221;&gt;The lc id.&lt;/param&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> InsertProduct(Product p, <span style="color: #0000ff">int</span> lcId)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>     <span style="color: #0000ff">using</span> (ProductsDataContext context = <span style="color: #0000ff">new</span> ProductsDataContext())</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>         <span style="color: #0000ff">if</span> (p.ProductId == 0)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>             <span style="color: #008000">// insert only if it is new</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>             context.Products.InsertOnSubmit(p);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>         InsertProductNameForProduct(context, p, lcId);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>         context.SubmitChanges();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span> }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  20:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  21:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  22:</span> <span style="color: #008000">/// Inserts the product name for product.</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  23:</span> <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  24:</span> <span style="color: #008000">/// &lt;param name=&#8221;context&#8221;&gt;The context.&lt;/param&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  25:</span> <span style="color: #008000">/// &lt;param name=&#8221;p&#8221;&gt;The p.&lt;/param&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  26:</span> <span style="color: #008000">/// &lt;param name=&#8221;lcId&#8221;&gt;The lc id.&lt;/param&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  27:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> InsertProductNameForProduct(ProductsDataContext context, Product p, <span style="color: #0000ff">int</span> lcId)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  28:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  29:</span>     context.ProductNames.InsertOnSubmit(<span style="color: #0000ff">new</span> ProductName()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  30:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  31:</span>         CultureId = lcId,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  32:</span>         Name = p.Name,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  33:</span>         ProductId = p.ProductId,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  34:</span>         Product = p,</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  35:</span>      });</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  36:</span> }</pre>
<p align="justify">And last, for update; apart from the obvious part there is one situation we need to handle : if the name of the product is changed, than we need to update it as well. For the other fields, go on with the regular update. Here is the test that codifies the statement:</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> <span style="color: #008000">///A test for UpdateProduct</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span> <span style="color: #008000">///&lt;/summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span> [TestMethod()]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> should_update_product_and_its_current_name()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>     ProductsRepository target = <span style="color: #0000ff">new</span> ProductsRepository(); <span style="color: #008000">// TODO: Initialize to an appropriate value</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>     Product p = target.GetProduct(1, 2);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>     p.Name = <span style="color: #006080">&#8220;French Name&#8221;</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>     p.Amount = 40;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>     p.Size = 55;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">using</span> (TransactionScope scope = <span style="color: #0000ff">new</span> TransactionScope(TransactionScopeOption.Suppress))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>         target.UpdateProduct(p);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span>         Assert.AreEqual(<span style="color: #006080">&#8220;French Name&#8221;</span>, p.Name);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  16:</span>         Assert.AreEqual(40, p.Amount);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  17:</span>         Assert.AreEqual(55, p.Size);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  18:</span>      }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  19:</span> }</pre>
<p align="justify">After writing the test, the implementation below becomes obvious:</p>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> UpdateProduct(Product p)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   3:</span>    <span style="color: #008000">// since we don&#8217;t load more than one product name, we can assume that the one is updated</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   4:</span>    <span style="color: #0000ff">using</span> (ProductsDataContext context = <span style="color: #0000ff">new</span> ProductsDataContext())</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   5:</span>    {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   6:</span>        context.Products.Attach(p, <span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   7:</span>        ProductName currentName = p.ProductNames.Single&lt;ProductName&gt;();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">   8:</span>        <span style="color: #0000ff">if</span> (p.Name != currentName.Name)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">   9:</span>        {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  10:</span>            <span style="color: #008000">// it is updated, update it</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  11:</span>            currentName.Name = p.Name;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  12:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  13:</span>         context.SubmitChanges();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4"><span style="color: #606060">  14:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white"><span style="color: #606060">  15:</span> }</pre>
<p>I showed a possible strategy to localize Linq to SQL entities in this post. Of course, more complex scenarios such as child entities and lazy loading issues could be thought thoroughly, but I hope this gave some initiative to attack the whole idea.</p>
<p>Comments and critics well appreciated as always.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f08%2f18%2flocalizing-linq-to-sql-entities.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f08%2f18%2flocalizing-linq-to-sql-entities.html');"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.sidarok.com%2fweb%2fblog%2fcontent%2f2008%2f08%2f18%2flocalizing-linq-to-sql-entities.html&amp;bgcolor=FF0033&amp;cfgcolor=FF0033&amp;cbgcolor=FFFF66" alt="kick it on DotNetKicks.com" border="0" /></a></p>
<img src="http://feeds.feedburner.com/~r/SidarOk/~4/NRySoDXYyMI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sidarok.com/web/blog/content/2008/08/18/localizing-linq-to-sql-entities.html/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sidarok.com/web/blog/content/2008/08/18/localizing-linq-to-sql-entities.html</feedburner:origLink></item>
	</channel>
</rss>
