<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Database</title>
        <link>http://www.dotnetlog.com/category/12.aspx</link>
        <description>Database</description>
        <language>en-US</language>
        <copyright>Bigyan Rajbhandari</copyright>
        <managingEditor>bigyanr@gmail.com</managingEditor>
        <generator>Subtext Version 1.9.5.177</generator>
        <item>
            <title>Using XML to perform database operations rather than parameterized query?</title>
            <link>http://dotnetlog.com/archive/2008/08/24/using-xml-to-perform-database-operations-rather-than-parameterized-query.aspx</link>
            <description>&lt;p&gt;There's no doubt that today xml is becoming more and more prevalent as platform-independent data format. Starting SQL server 2005, Microsoft also added tons of support for xml and a new native data type XML, since then I have started to see lots of applications that utilize this data type to run very complex solutions. A new trend is starting to emerge, as one of my colleagues calls it a "paradigm shift", where applications use xml serialized objects (that map to tables) to perform inserts/updates to table rather than having to pass each parameter independently.  However, the question remains if this approach is necessarily better than previous? &lt;/p&gt;
&lt;p&gt;For example consider the following scenario: &lt;br /&gt;
In middle-Tier (.Net Code) my object looks like the following&lt;/p&gt;
&lt;p&gt;&lt;img width="171" height="164" src="/images/dotnetlog_com/082408_1855_UsingXMLtop1.png" alt="" /&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt; 		&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;My serialized xml looks like below:&lt;/strong&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Customer&lt;/span&gt;&lt;span style="color: blue;"&gt; 			&lt;/span&gt;&lt;span style="color: red;"&gt;customerID&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;12&lt;/span&gt;"&lt;span style="color: blue;"&gt; 			&lt;/span&gt;&lt;span style="color: red;"&gt;customerName&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Bigyan&lt;/span&gt;"&lt;span style="color: blue;"&gt; 			&lt;/span&gt;&lt;span style="color: red;"&gt;address&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;123 Redmond Way&lt;/span&gt;"&lt;span style="color: blue;"&gt; 			&lt;/span&gt;&lt;span style="color: red;"&gt;phone&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;123-123-3434&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;And my call to database stored proc like:&lt;/strong&gt;&lt;br /&gt;
&lt;span style="color: rgb(0, 112, 192);"&gt;exec&lt;/span&gt; Customer_UpdateInformation @customerXml &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In parameterized query I would have done something like this:&lt;br /&gt;
&lt;/strong&gt;&lt;span style="color: rgb(0, 112, 192);"&gt;exec&lt;/span&gt; Customer_UpdateInformation @customerID, @customerName, @addresss, @phone&lt;strong&gt;     &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are both pros and cons to this approach, which I'll explain next. But I also want to clarify that I am NOT advocating one approach vs other, but actually want your opinion on this approach.  &lt;/p&gt;
&lt;h3&gt;Pros: &lt;/h3&gt;
&lt;ul&gt;
    &lt;li&gt;Instead of passing 4 parameters, I need to pass only 1. Thus my signature for the database contract is much more succinct. &lt;/li&gt;
    &lt;li&gt;If I need to add or modify field, I have less code to deal with and do NOT need to change my signature &lt;/li&gt;
    &lt;li&gt;If I am just performing updates on certain fields (not all), I might be able to pass only updated fields and reduce my message size &lt;/li&gt;
    &lt;li&gt;Serialization and Deserialization on my .Net objects can be automated, thus reducing effort to map those fields with database fields. &lt;/li&gt;
    &lt;li&gt;If I wanted to perform bulk updates, then I have ability to pass multiple elements in my xml; this greatly reduces chatting between .Net and Sql Server. &lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Cons: &lt;/h3&gt;
&lt;ul&gt;
    &lt;li&gt;First and foremost, I'll lose type safety on the parameters passed. &lt;/li&gt;
    &lt;li&gt;Xml parsing is little expensive (not a whole lot), compared to primitive data types. Bulk process might make up ground in this area. &lt;/li&gt;
    &lt;li&gt;
    &lt;div&gt;Data contract I am exposing through the stored procedure is not self explanatory. Just looking at the parameter, no one will know what to pass.  &lt;/div&gt;
    &lt;p&gt;&lt;br /&gt;
     &lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://dotnetlog.com/aggbug/59.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Bigyan Rajbhandari</dc:creator>
            <guid>http://dotnetlog.com/archive/2008/08/24/using-xml-to-perform-database-operations-rather-than-parameterized-query.aspx</guid>
            <pubDate>Sun, 24 Aug 2008 18:55:16 GMT</pubDate>
            <wfw:comment>http://dotnetlog.com/comments/59.aspx</wfw:comment>
            <comments>http://dotnetlog.com/archive/2008/08/24/using-xml-to-perform-database-operations-rather-than-parameterized-query.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://dotnetlog.com/comments/commentRss/59.aspx</wfw:commentRss>
            <trackback:ping>http://dotnetlog.com/services/trackbacks/59.aspx</trackback:ping>
        </item>
        <item>
            <title>Is V1 of LINQ to SQL only suitable for RAD?</title>
            <link>http://dotnetlog.com/archive/2008/03/24/is-v1-of-linq-to-sql-only-suitable-for-rad.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://dotnetlog.com/archive/2008/03/20/follow-up--best-practice-and-effective-use-of-datacontext.aspx"&gt;In my previous couple posts&lt;/a&gt;, I have been religiously trying to solve the problem or should I say 'find a best pattern' to use with linq to sql. In past few projects, I ventured into Linq to sql world abandoning ORM tools (OR mapper, NHibernate…) that I had been using; however, I have been greatly disappointed by the fact that Linq to sql doesn't support few fundamental concepts/patterns (LINQ to SQL takes a little different approach than traditional ORM tools). I am starting to have serious doubts if V1 of Linq2SQl is even cut out for any n-tier application (I am not even thinking enterprise application). Don't get me wrong, LINQ is a great framework and LINQ to SQL is an awesome extension. But in the real world business application the implementation of LINQ to SQL out of the box is not practical. How did I arrive to this conclusion? Here's how: &lt;/p&gt;
&lt;p&gt;The two fundamental problems it fails to support out of the box are: &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Working with objects in LINQ to SQL in disconnected context is painful. i.e. Most mid-large size applications are designed as n-tier architecture, thus entities within LINQ act more as a container (DTO or Business objects) for carrying data from one tier to another. The problem with LINQ to SQL is that disconnecting from one datacontext and reattaching to another is not trivial. This is a serious drawback given the fact that almost all the applications needs to use this model other than "quick demos". &lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
I have seen lots of posts/articles from developers questioning the same problem. Various people have taken a stab at solving this, if you are seriously considering LINQ to SQL read these. &lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 54pt"&gt;
    &lt;li&gt;&lt;a href="http://pampanotes.tercerplaneta.com/2008/03/implementing-n-tier-change-tracking.html"&gt;Benjamin's&lt;/a&gt; ULinqGen project in codeplex allows change tracking in pure POCO models &lt;/li&gt;
    &lt;li&gt;Post by &lt;a href="http://www.west-wind.com/WebLog/posts/134095.aspx"&gt;Rick Strahl&lt;/a&gt;, explaining problem in Linq to SQL in a disconnected environment. &lt;/li&gt;
    &lt;li&gt;
    &lt;div&gt;&lt;a href="http://www.codeplex.com/LINQ2SQLEB"&gt;Matt's Linq2SqlEB&lt;/a&gt; project in codeplex which implements synchronization. &lt;/div&gt;
    &lt;p style="MARGIN-LEFT: 18pt"&gt; &lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;2. &lt;/strong&gt;If you somehow implement an architecture to have live datacontext, one serious issue that you need to focus on is the lifetime management of DataContext. Please be aware, that poorly managed datacontext will bloat eventually and will hamper performance. &lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 54pt"&gt;
    &lt;li&gt;&lt;a href="http://www.dotnetlog.com/archive/2008/03/18/best-practice-and-effective-way-of-using-datacontext-in-linq.aspxhttp:/www.dotnetlog.com/archive/2008/03/18/best-practice-and-effective-way-of-using-datacontext-in-linq.aspx"&gt;See my previous post&lt;/a&gt; on managing lifetime of datacontext &lt;/li&gt;
    &lt;li&gt;Rick Strahl discusses datacontext lifetime management &lt;a href="http://www.west-wind.net/WebLog/posts/246222.aspx"&gt;here&lt;/a&gt; &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://weblogs.asp.net/despos/archive/2008/03/19/more-on-datacontext-in-hopefully-a-realistic-world.aspx"&gt;Dino Esposito&lt;/a&gt;, suggest that you should use atomic context, but I don't see how it would solve the problem if you can't work in a disconnected environment. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br /&gt;
Am I ready to make jump to LinqToSql in my next project from my previous ORM tools? I don't know about others but, I will be holding back until I figure out a best way to work in a disconnected environment, which might be the next project. But the point is disconnected context support will be required. &lt;a href="http://blogs.msdn.com/dinesh.kulkarni/"&gt;Dinesh Kulkarni&lt;/a&gt;, a senior dev for Linq to SQL team advises not use Active Record Pattern, but the problem is I don't see any pattern that I can use effectively in a n-tier architecture, not only the Active Record. LINQ to SQL itself uses great patterns within, but the way datacontext is managed and Entities/child entities are persisted it presents a different problem. It makes for a great presentation or RAD applications so far, but fails to support n-tier applications easily. I am considering concepts of offline datacontext and will follow up with more posts on this. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Is ADO.Net Entity Framework an answer?&lt;br /&gt;
&lt;/strong&gt;Not really. The framework uses same concept of active context as Linq to SQL and quickly runs into the problem in a disconnected environment. I haven't done an in-depth implementation of it but the problem seems to be the same. &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Let me know what you guys think of LINQ to SQL. &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;&lt;img src="http://dotnetlog.com/aggbug/56.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Bigyan Rajbhandari</dc:creator>
            <guid>http://dotnetlog.com/archive/2008/03/24/is-v1-of-linq-to-sql-only-suitable-for-rad.aspx</guid>
            <pubDate>Tue, 25 Mar 2008 03:31:38 GMT</pubDate>
            <wfw:comment>http://dotnetlog.com/comments/56.aspx</wfw:comment>
            <comments>http://dotnetlog.com/archive/2008/03/24/is-v1-of-linq-to-sql-only-suitable-for-rad.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://dotnetlog.com/comments/commentRss/56.aspx</wfw:commentRss>
            <trackback:ping>http://dotnetlog.com/services/trackbacks/56.aspx</trackback:ping>
        </item>
        <item>
            <title>(Follow Up)  Best practice and effective use of DataContext in LINQ</title>
            <link>http://dotnetlog.com/archive/2008/03/20/follow-up--best-practice-and-effective-use-of-datacontext.aspx</link>
            <description>&lt;p&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;NOTE: 3/24/2008 - The problem of working with LINQ to sql extends beyond lifetime management of DataContext, thus I am scrapping the second part of my blog. Only thing useful in this post is the performance comparision. &lt;a href="http://www.dotnetlog.com/archive/2008/03/24/is-v1-of-linq-to-sql-only-suitable-for-rad.aspx"&gt;Here's new post on LINQ to SQL&lt;/a&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.dotnetlog.com/archive/2008/03/18/best-practice-and-effective-way-of-using-datacontext-in-linq.aspx"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;In my previous post&lt;/span&gt;&lt;/a&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; BACKGROUND-COLOR: white"&gt;, I had probed a question on how to effectively incorporate DataContext in ones pattern so the ease of use and unit of work is well represented. Thanks to everyone who provided feedback, it definitely was a good read and provided lots of insight. Few reasons we got into asking this question instead of using simple create/dispose model, in the first place was &lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; BACKGROUND-COLOR: white"&gt;We wanted to make it so that it was easy to use &lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; BACKGROUND-COLOR: white"&gt;We heavily use Repository Pattern for business logic/transaction scoping and Entity model for CRUD operations, so wanted to make sure datacontext we used was as efficient as possible. &lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; BACKGROUND-COLOR: white"&gt;Concerns regarding efficiency of creating/disposing datacontext and not being able to use caching of datacontext effectively. &lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; BACKGROUND-COLOR: white"&gt;&lt;strike&gt;However, after reading several posts and looking deeper into datacontext, it seems that creating/disposing it as required is the best way to go&lt;/strike&gt;. In order, to get rid of any suspicion I even created a little test to compare Ambient Context and Instantiation as required model, results are below. In the image below, first method GetByID(int32) actually instantiates datacontext everytime, where as the second one takes in a datacontext instantiated once by the caller (code is included at the bottom of the post). The results are impressive for dynamic instantiation. 23.7 secs vs. 20.9 secs for 6200 iterations. I think 3 secs is a reasonable margin not taking into account impact in memory with ambient datacontext or the efficiency of caching. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="http://dotnetlog.com/images/dotnetlog_com/032008_2146_FollowUpBe1.png" /&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; BACKGROUND-COLOR: white"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; BACKGROUND-COLOR: white"&gt;&lt;strike&gt;Either way, I believe for our purpose and in general a repository pattern incorporated with entity model should work pretty well. One thing Jeff did point out to me however was, once you have disposed your datacontext, any attempt to load/access child objects that is marked for lazy load will throw disposed object error as below. So make sure you load your objects while the context is alive, once that is done subsequent access is successful. &lt;/strike&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="http://dotnetlog.com/images/dotnetlog_com/032008_2146_FollowUpBe2.png" /&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; BACKGROUND-COLOR: white"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Timers;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Diagnostics;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ConsoleApplication4
{
    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program
    {
        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
        {
&lt;span class="rem"&gt;//Create &amp;amp; dispose Context everytime&lt;/span&gt;
            Test1();
            
        &lt;span class="rem"&gt;//Ambient Context Concept&lt;/span&gt;
Test2();
        }

&lt;span class="rem"&gt;//Create datacontext everytime&lt;/span&gt;
        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test1()
        {
            List&amp;lt;EcardOrder&amp;gt; orders;
            &lt;span class="kwrd"&gt;using&lt;/span&gt; (MercyDataContext dbContext = &lt;span class="kwrd"&gt;new&lt;/span&gt; MercyDataContext())
            {
                &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 100; i++)
                {
                    orders = dbContext.EcardOrders.ToList();
                    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (EcardOrder order &lt;span class="kwrd"&gt;in&lt;/span&gt; orders)
                    {
                        &lt;span class="rem"&gt;//Datacontext will be created everytime &lt;/span&gt;
&lt;span class="rem"&gt;// in this function&lt;/span&gt;
                        Ecard card = Ecard.GetByID(order.EcardID);
                    }
                }
            }
        }

        
&lt;span class="rem"&gt;//Create datacontext only once&lt;/span&gt;
        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test2()
        {
            List&amp;lt;EcardOrder&amp;gt; orders;
            &lt;span class="kwrd"&gt;using&lt;/span&gt; (MercyDataContext dbContext = &lt;span class="kwrd"&gt;new&lt;/span&gt; MercyDataContext())
            {
                &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 100; i++)
                {
                    orders = dbContext.EcardOrders.ToList();
                    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (EcardOrder order &lt;span class="kwrd"&gt;in&lt;/span&gt; orders)
                    {
                        &lt;span class="rem"&gt;//Pass datacontext to the function as well&lt;/span&gt;
                        Ecard card = Ecard.GetByID(order.EcardID, dbContext);
                    }
                }
            }
        }


    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Partial Ecard class extended from database&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Ecard
    {
        &lt;span class="rem"&gt;//new datacontext is created and disposed everytime.&lt;/span&gt;
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Ecard GetByID(&lt;span class="kwrd"&gt;int&lt;/span&gt; ID)
        {
            &lt;span class="kwrd"&gt;using&lt;/span&gt; (MercyDataContext _context = &lt;span class="kwrd"&gt;new&lt;/span&gt; MercyDataContext())
            {
                &lt;span class="kwrd"&gt;return&lt;/span&gt; _context.Ecards.SingleOrDefault(e =&amp;gt; e.EcardID == ID);
            }
        }

        &lt;span class="rem"&gt;//Puesdo concept of ambient datacontext&lt;/span&gt;
        &lt;span class="rem"&gt;//NOTE: once the Ecard is loaded it is cached in the context, &lt;/span&gt;
        &lt;span class="rem"&gt;//thus items will be returned from cache&lt;/span&gt;
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Ecard GetByID(&lt;span class="kwrd"&gt;int&lt;/span&gt; ID, MercyDataContext _context)
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; _context.Ecards.SingleOrDefault(e =&amp;gt; e.EcardID == ID);
        }
    }


}
&lt;/pre&gt;&lt;img src="http://dotnetlog.com/aggbug/55.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Bigyan Rajbhandari</dc:creator>
            <guid>http://dotnetlog.com/archive/2008/03/20/follow-up--best-practice-and-effective-use-of-datacontext.aspx</guid>
            <pubDate>Thu, 20 Mar 2008 21:46:24 GMT</pubDate>
            <wfw:comment>http://dotnetlog.com/comments/55.aspx</wfw:comment>
            <comments>http://dotnetlog.com/archive/2008/03/20/follow-up--best-practice-and-effective-use-of-datacontext.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://dotnetlog.com/comments/commentRss/55.aspx</wfw:commentRss>
            <trackback:ping>http://dotnetlog.com/services/trackbacks/55.aspx</trackback:ping>
        </item>
        <item>
            <title>Best practice and effective way of using DataContext in Linq to SQL?</title>
            <link>http://dotnetlog.com/archive/2008/03/18/best-practice-and-effective-way-of-using-datacontext-in-linq.aspx</link>
            <description>&lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;NOTE: See my &lt;/strong&gt;&lt;a href="http://www.dotnetlog.com/archive/2008/03/20/follow-up--best-practice-and-effective-use-of-datacontext.aspx"&gt;&lt;strong&gt;follow up post here&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; for more info.&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;At work, Jeff and I have been throwing around ideas to find a best way to implement DataContext in Linq so that we can integrate it into the base class in our framework while achieving following goals. &lt;/font&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;font size="2"&gt;Implementation should be easy and non-redundant, so that we do not need to do &lt;span style="COLOR: blue"&gt;new DataContext(), &lt;/span&gt;every time we have to use one. &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font size="2"&gt;Portability of the DataContext should be such that we do not need to pass it around as parameters from one tier to another. &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font size="2"&gt;"Unit of work" pattern should be well represented and "very" transparent. &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font size="2"&gt;Context should not be persisted without intent, or be open to manipulation from other methods, such that "unit of work" is compromised or a transaction is ill-represented &lt;/font&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;font size="2"&gt;These seem like simple goals given the features and flexibility of LINQ, however, in reality this has become much more difficult and annoying to achieve than we previously thought. Here are several ideas we have been kicking around, and problem it presents. &lt;strong&gt;I would like to hear from people who have effectively used LINQ Datacontext in these scenarios or in a pattern that's most effective. &lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;1. Creating and disposing LINQ as required.&lt;/strong&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;font face="Arial"&gt;&lt;span style="COLOR: #2b91af"&gt;DbDataContext&lt;/span&gt; myContext = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;DbDataContext&lt;/span&gt;(); &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: green"&gt;&lt;font face="Arial"&gt;//your code goes here &lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;font face="Arial"&gt;myContext.SubmitChanges(); &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;&lt;strong&gt;Problems with this pattern:&lt;/strong&gt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 72pt"&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;Creating datacontext everytime we need to use is cumbersome and in my opinion just verbose. &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;
    &lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;If you have to use datacontext in any of the method this code calls, you'll have to pass the datacontext with it. This is one- too many parameters to pass around &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
    &lt;p&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;2. Creating a static DataContext and using it throughout the application.&lt;/strong&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;DataLayer.DataContext.Current&lt;/span&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;&lt;strong&gt;Problems with this pattern:&lt;/strong&gt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 72pt"&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;Creating static object isn't always a good thing. In this case, the context is available for manipulation at any level. And unit of work quickly becomes very hard to maintain &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;If manipulated by other threads or methods context isn't clean anymore and cannot be trusted&lt;br /&gt;
    &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;&lt;strong&gt;3. Ambient DataContext (Currently using this)&lt;/strong&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;The idea behind ambient datacontext is that context is created for a particular thread or a httpcontext(in asp.net) as soon as there is a first call for the DataContext. Then the same context is used for that thread or request unless it is disposed manually. This is done by storing the context in the request/thread data store. The approach to this pattern is similar to static DataContext, however, separation is provided for each thread and requests. This works really well in Asp.Net, however, is again plagued by some of the problems of static context.&lt;strong&gt; &lt;/strong&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;&lt;strong&gt;Problems with this pattern:&lt;/strong&gt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 72pt"&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;The context is available for manipulation at any level. And quickly becomes very hard to maintain unit of work &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;Portability across thread is very hard &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;Unit of work pattern is not transparent enough&lt;br /&gt;
    &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;4.  Finally, the last idea we've been trying to come up with is concept similar to TransactionScope.&lt;/strong&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;In this pattern, you would used &lt;strong&gt;using&lt;/strong&gt;{ } block to initialize your context and represent a "Datacontext Scope". Any method below that will automatically be able to figure out the proper context, through thread/request datastore. Then just before the dispose happens, you manually call the &lt;strong&gt;SubmitChanges&lt;/strong&gt;() method. This is the one I like so far, however, has few practical problems. &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;&lt;strong&gt;Problems with this pattern:&lt;/strong&gt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 72pt"&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;If sub method needs to implement a new context, we need to figure out how to exclude it from the parent context's using block. If allowing multiple contexts in multiple using blocks, how do you determine which context to use in sub-sub methods? &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;Please do comment on what you think is the best approach to solving this problem.&lt;/font&gt;&lt;/p&gt;&lt;img src="http://dotnetlog.com/aggbug/53.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Bigyan Rajbhandari</dc:creator>
            <guid>http://dotnetlog.com/archive/2008/03/18/best-practice-and-effective-way-of-using-datacontext-in-linq.aspx</guid>
            <pubDate>Wed, 19 Mar 2008 01:51:33 GMT</pubDate>
            <wfw:comment>http://dotnetlog.com/comments/53.aspx</wfw:comment>
            <comments>http://dotnetlog.com/archive/2008/03/18/best-practice-and-effective-way-of-using-datacontext-in-linq.aspx#feedback</comments>
            <slash:comments>14</slash:comments>
            <wfw:commentRss>http://dotnetlog.com/comments/commentRss/53.aspx</wfw:commentRss>
            <trackback:ping>http://dotnetlog.com/services/trackbacks/53.aspx</trackback:ping>
        </item>
    </channel>
</rss>