<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Alex Sarafian as Developer &#187; LINQ</title>
	<atom:link href="http://sarafianalex.wordpress.com/category/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://sarafianalex.wordpress.com</link>
	<description>Adventures in C# Land and some SQL</description>
	<lastBuildDate>Fri, 04 Sep 2009 11:27:50 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='sarafianalex.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/ea38bb69738d229a89ab00b82aad64b7?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Alex Sarafian as Developer &#187; LINQ</title>
		<link>http://sarafianalex.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sarafianalex.wordpress.com/osd.xml" title="Alex Sarafian as Developer" />
		<item>
		<title>LINQ to SQL not Suitable for LOB</title>
		<link>http://sarafianalex.wordpress.com/2008/06/17/linq-to-sql-not-suitable-for-lob/</link>
		<comments>http://sarafianalex.wordpress.com/2008/06/17/linq-to-sql-not-suitable-for-lob/#comments</comments>
		<pubDate>Tue, 17 Jun 2008 21:37:02 +0000</pubDate>
		<dc:creator>Sarafian Alex</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[LOB]]></category>

		<guid isPermaLink="false">http://sarafianalex.wordpress.com/2008/06/17/linq-to-sql-not-suitable-for-lob/</guid>
		<description><![CDATA[In my previous post I mentioned that some post or blogs on the Internet are hugely misleading about the available technologies, since they tend to hide basic facts and focus on the superficial magic, which sometimes simply don&#8217;t matter when the technology is unusable.
In the company I work for, we rejected LINQ to SQL about [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarafianalex.wordpress.com&blog=2367780&post=26&subd=sarafianalex&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In my previous <a href="http://sarafianalex.wordpress.com/2008/06/17/wcf-debuging/" target="_blank">post</a> I mentioned that some post or blogs on the Internet are hugely misleading about the available technologies, since they tend to hide basic facts and focus on the superficial magic, which sometimes simply don&#8217;t matter when the technology is unusable.</p>
<p>In the company I work for, we rejected LINQ to SQL about a month ago, after trying to solve its biggest problem that of performance combined with thread safe when cashing the Data Context. So any info I have found from various sources are not available.</p>
<h2>What is LINQ?</h2>
<p>LINQ is basically a collection of extension methods to any Enumerable object.</p>
<h2>What is LINQ to SQL?</h2>
<p>LINQ to SQL is LINQ over the enumerable objects created while drag and dropping tables and procedures in a dbml file. This classes are known as Entities and the object that manages them as a DataContext.</p>
<h2>My Sin</h2>
<p>I really don&#8217;t like SQL. It reminds me of procedural programming which I stopped writing a decade ago. So any technology that will allow me not write SQL is more than welcome for me. So when I first started reading about LINQ to SQL, I started thinking at last an ORM from Microsoft herself.</p>
<p>My Sin was that in spite of the objections of our more experienced programmer, I was standing by LINQ to SQL, mainly using arguments that came from the notion that everything was great in LINQ to SQL. This notion existed because I believed the posts on the Internet.</p>
<p>One of the biggest objections was that of security. It was unacceptable for him, for an application to have access to the tables of a database. My lack of SQL knowledge, didn&#8217;t take that in mind, so as then, supposedly there is no security objection for the rest of the post.</p>
<h2>First Impressions</h2>
<p>At first I was really disappointed because there was no support for DataSet manipulation through LINQ to SQL. At first I hadn&#8217;t realized what LINQ is exactly, so I implemented a <a href="http://sarafianalex.wordpress.com/2008/04/21/typed-dataset-linq-entities/" target="_blank">library</a> that did this job. When building N-Tier applications, Typed DataSets are the most effective solution for the business model. I wrote about it <a href="http://sarafianalex.wordpress.com/2008/04/21/linq-and-client-server-applications/" target="_blank">here</a>. Having wrote this library I was really convinced that we had a great tool for LINQ to SQL, and that it was the choice for our Data Access Layer.</p>
<h2>Problems arising</h2>
<p>Having spent time to build this library, it now came to check whether LINQ to SQL was the valid choice for out Application. So we created a huge table in SQL Server and started case testing and comparing with know DataSet methodology.</p>
<p>A Data Access Layer will be used by a Web or a Server Side of an application. In order to be thread safe, you need to create the Data Context with each call. But this is slow when data are huge, and there were some posts saying that there would be a way to cache the data context through a configuration setting. So I automatically assumed that caching and thread safe has been taken into account.</p>
<p>But that wasn&#8217;t the case. The sad truth is that, if you cache the Data Context you must create and maintain one for every thread or Http Context in order to make them thread safe, with a trick I read from someone else. Practically you don&#8217;t make them thread safe, but thread specific.</p>
<p>With caching, performance improved greatly. In some cases it was quicker than DataSet methodology. The main reason was that, while the queries run, the Data Context of each thread kept its entities in memory so no queries to server where required after a number of iterations. After this was noticed I immediately thought what about memory consumption in the server as the execution time passed. How the hell do you manage this side effect.</p>
<h2>Conclution</h2>
<p>LINQ to SQL might be very appealing when reading about simple objects and simple applications, but when the application gets big the coordination problems that might occur and produce data corruption, are clearly the death tomb of LINQ to SQL for LOB. And the sad part was that, I fell victim to all those glorious posts. I believe this is a risk that no company should ever take. Data integrity is something that one must never mess with. This includes the security objection mentioned above.</p>
<p>LINQ to SQL is a proof of concept. It is one of the things that in IT theory look great but when put in practice, it proves once again that theory sometimes doesn&#8217;t relate to practice.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sarafianalex.wordpress.com/26/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sarafianalex.wordpress.com/26/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarafianalex.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarafianalex.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarafianalex.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarafianalex.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarafianalex.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarafianalex.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarafianalex.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarafianalex.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarafianalex.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarafianalex.wordpress.com/26/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarafianalex.wordpress.com&blog=2367780&post=26&subd=sarafianalex&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarafianalex.wordpress.com/2008/06/17/linq-to-sql-not-suitable-for-lob/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64f00567ef7dda3ec36dcf0e01043d11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sarafian</media:title>
		</media:content>
	</item>
		<item>
		<title>WCF Debugging and a WCF review</title>
		<link>http://sarafianalex.wordpress.com/2008/06/17/wcf-debuging/</link>
		<comments>http://sarafianalex.wordpress.com/2008/06/17/wcf-debuging/#comments</comments>
		<pubDate>Tue, 17 Jun 2008 19:52:11 +0000</pubDate>
		<dc:creator>Sarafian Alex</dc:creator>
				<category><![CDATA[.NET C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://sarafianalex.wordpress.com/2008/06/17/wcf-debuging/</guid>
		<description><![CDATA[A comment
At some point, people need to understand that, business applications are not like console applications. So all these comments on the net, in blogs and forums, should be a little more double checked for real life development process because they mislead other people. Especially for LINQ to SQL and WCF, I am really bored [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarafianalex.wordpress.com&blog=2367780&post=24&subd=sarafianalex&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2>A comment</h2>
<p>At some point, people need to understand that, business applications are not like console applications. So all these comments on the net, in blogs and forums, should be a little more double checked for real life development process because they mislead other people. Especially for LINQ to SQL and WCF, I am really bored reading about how great they are and how easy they are, but the truth is that LINQ to SQL is not for applications (reason in another <a href="http://sarafianalex.wordpress.com/2008/06/17/linq-to-sql-not-suitable-for-lob/" target="_blank">post</a>) and I was one of the victims that tried to adopt it, and WCF although is great, it can also be a pain in the ass. When searching through the net, keep in mind always that Internet is not always correct as I have wrote <a href="http://www.codeproject.com/KB/dotnet/ClearUpSerialization.aspx" target="_blank">here</a>. I really suspect that many of the blog post although, they can be helping, are not revealing entirely the truth and the disadvantages. I have always been fun of Microsoft Technologies but there is a limit to the indirect advertisement.</p>
<h2>Following instructions</h2>
<p>Back to the post subject. Many of you have done what everybody on Internet says. Add a WCF Service and a Service Reference and Great, all is ready and done. Even for debug, every time I execute debug the application, a dummy Debug Host is raised and I can debug the service. All is great? No.</p>
<h2>Questions</h2>
<p>First of all. What if I&#8217;m not running parts of the solution that require the service. Why should I be punished with the overhead of raising the host?</p>
<p>Second and most serious. Has really anyone tried to debug the service, and every underlying class used by it? Has anybody tried to utilize edit and continue, among all these guys who say how great WCF is?</p>
<p>I tried and as you can guess from my attitude, I could not utilize Edit And Continue. When I am developing a big Application, usually there will be at least 2 layers behind the service. Should I restart the application each time? And don&#8217;t let me talk about the debug through the dummy client, when an Operation Contract of mine, will use a complex data type. It is just not possible.</p>
<h2>So what is the solution?</h2>
<p>Easy someone can say, but easier said than done. As you would with .Net Remoting, If the service is located in your output directory, by whatever trick in the solution, then just raise the host programmatically in an address of your choice and then tell the client to hit this address. This way you have always simulating data transferring through WCF channels (<strong>very important</strong>), and you can of coarse use the feature of Edit and Continue.</p>
<p>The problem is that the dummy service the solution raised keeps coming up, which is very annoying. I haven&#8217;t found a solution, mainly because in the framework I&#8217;m developing, there is a single Operation Contract handling abstract Message Types. This was another great milestone for me in WCF. I really can&#8217;t understand why they have made DataContractSerializer as complicated, and not simple as the one used in plain old fashioned remoting. Having this service in my framework, I do not have in my solution the WCF Service so , there is no penalty overhead from the Debug Host that is raised.</p>
<h2>Last problem hopefully</h2>
<p>Finally a point of interest which was my last obstacle that took me a half day to find. I had implemented a provider class for the remoting part, which if needed, fired up the service host. Everything worked just great in the test projects, but at some point I tried to extend the framework with WPF.</p>
<p>The trick was that at first request, through static constructor I checked if the service was required to be hosted, and did that. But this did not work when I made the call from WPF. The only error was a timeout exception. I was going crazy, and then it kicked in. Never trust completely a 3 party library.</p>
<h2>Solution</h2>
<p>I made three clients, one Console, one Windows one WPF and stripped down the functionality of my framework to test. On each UI Client I made the call (and raised subsequently the host) after a UI reaction. I turns out that neither Windows Forms played correctly, when for example the call is made through a button click event. When I saw that, I made the host come up before the UI part was ever initialized and guess what? It all played just fine.</p>
<p>I really can&#8217;t understand how this has not been mentioned.</p>
<h2>Conclusion</h2>
<p>For me WCF is good for the plumbing. It is much more complicated than .Net remoting, really hard to troubleshoot if you are doing something outside the ordinary, which are discussed in all those praising posts and articles. Maybe I haven&#8217;t studied it as much, maybe I&#8217;m missing something but if the case is true, tell me how something that is advertised as easy and all remoting-problem solving can be this hard to utilize and debug. You will say that WPF, has as much difficult learning curve but WPF is not advertised as the magic trick that the programming world was missing. Since I have read about it, everyone mentioned that it is hard and difficult to adopt, and it is not for all kinds of applications. For me WPF is the star of .NET3. </p>
<p>Despite all these problems I really believe in WCF, because of other great stuff that it supports.&nbsp; Regarding security MS says <a href="http://msdn.microsoft.com/en-us/library/bb552363.aspx" target="_blank">here</a> that</p>
<blockquote><p>You should not use WCF Service Host to host services in a production environment, as it was not engineered for this purpose. WCF Service Host does not support the reliability, security, and manageability requirements of such an environment. Instead, use IIS since it provides superior reliability and monitoring features, and is the preferred solution for hosting services. Once development of your services is complete, you should migrate the services from WCF Service Host to IIS.</p>
</blockquote>
<p>Having this in mind, I can&#8217;t stop thinking whether WCF is a great overhead on development, when .NET is remoting is a well tested solution under IIS. But choices have been made, mainly because the new technology I believe is here to stay.</p>
<h2>Links</h2>
<p align="justify">Same <a href="http://www.codeproject.com/KB/WCF/WCFReviewAndDebug.aspx" target="_blank">Article</a> at code project.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sarafianalex.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sarafianalex.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarafianalex.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarafianalex.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarafianalex.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarafianalex.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarafianalex.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarafianalex.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarafianalex.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarafianalex.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarafianalex.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarafianalex.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarafianalex.wordpress.com&blog=2367780&post=24&subd=sarafianalex&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarafianalex.wordpress.com/2008/06/17/wcf-debuging/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64f00567ef7dda3ec36dcf0e01043d11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sarafian</media:title>
		</media:content>
	</item>
		<item>
		<title>Typed Dataset &lt;&#8211;&gt; Linq Entities</title>
		<link>http://sarafianalex.wordpress.com/2008/04/21/typed-dataset-linq-entities/</link>
		<comments>http://sarafianalex.wordpress.com/2008/04/21/typed-dataset-linq-entities/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 09:51:20 +0000</pubDate>
		<dc:creator>Sarafian Alex</dc:creator>
				<category><![CDATA[.NET C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Typed DataSet]]></category>

		<guid isPermaLink="false">http://sarafianalex.wordpress.com/2008/04/21/typed-dataset-linq-entities/</guid>
		<description><![CDATA[Introduction
On my previous post I discussed about how LINQ entities to not fit the world of applications that do not have a constant access to the data source. I concluded that if there was a way to connect Linq Entities and Type Dataset, then the domain of Web Applications and N-Tier Applications could be supported [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarafianalex.wordpress.com&blog=2367780&post=21&subd=sarafianalex&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2>Introduction</h2>
<p>On my previous <a href="http://sarafianalex.wordpress.com/2008/04/21/linq-and-client-server-applications/">post</a> I discussed about how LINQ entities to not fit the world of applications that do not have a constant access to the data source. I concluded that if there was a way to connect Linq Entities and Type Dataset, then the domain of Web Applications and N-Tier Applications could be supported by the same Bussiness Object Model and a Data Access Layer over LINQ.</p>
<h2>Assumptions &#8211; Prerequisites</h2>
<h3>Entity and Data Table Naming</h3>
<p>Before I continue there is a basic assumption that must be kept in mind. The Business Object Model and the Typed Dataset must be constructed by their respective designer in Visual Studio, by dragging the tables into each designer. The main reason is, that the converter I have developed, assumes that the corresponding entities in LINQ and table in the Dataset have the same name.</p>
<h3>Relations and Foreign Key Constraints</h3>
<p>Also every relation between entities must have the same name as that between tables in the dataset. The above are automatically (great coincidence) kept, just by using the designer.</p>
<p>Circular Relations and all combinations have not been tested, so I do not know whether my code supports them</p>
<h3>Database construction From LINQ</h3>
<p>If you wish to construct the database schema from the LINQ designer then just do so, but before creating the typed dataset, the database must be created. To do this just call</p>
<blockquote><div><span style="color:#2b91af;">LinqTestDataContext </span>ltdc = <span style="color:#0000ff;">new </span><span style="color:#2b91af;">LinqTestDataContext</span>(connectionString); <span style="color:#0000ff;">if </span>(!ltdc.DatabaseExists()) { ltdc.CreateDatabase(); }</div>
</blockquote>
<p>where <span style="color:#777777;"><span style="color:#2b91af;">LinqTestDataContext </span></span>is the DataContext the designer has created.</p>
<h3>Column Prerequisite</h3>
<p>Each entity must have a version property. This is because Attach(entity,true) only works if there is such a property.</p>
<h2>The Database Schema used for testing</h2>
<p>The LINQ schema is name LinqTest and its dataset represantion DsLinqTest.</p>
<p>As seen in the picture below there is a RootElement with a unique key ID, a version property TimeStamp and two string properties.</p>
<p>RootElement has child relation of SubRootElement entities which also have a unique key ID, a version property TimeStamp a string property and a RootID foreign key pointing to the RootElement it belongs</p>
<p><a href="http://sarafianalex.files.wordpress.com/2008/04/image.png"><img style="border-width:0;" height="135" alt="image" src="http://sarafianalex.files.wordpress.com/2008/04/image-thumb.png?w=395&#038;h=135" width="395" border="0"></a></p>
<p>The corresponding Dataset will be. The relation name is the same, even thought it is not showing on the above image.</p>
<p><a href="http://sarafianalex.files.wordpress.com/2008/04/image1.png"><img style="border-width:0;" height="110" alt="image" src="http://sarafianalex.files.wordpress.com/2008/04/image-thumb1.png?w=396&#038;h=110" width="396" border="0"></a></p>
<p>Each of the Business Object are in a separate assembly.</p>
<h2>DataSetEntityConvertion</h2>
<p>This is name of the assembly that does the convention between an LINQ Business Object and a Typed Dataset assuming that the above prerequisites are met.</p>
<p>The assembly uses heavily reflection and generics so the understanding of the above must be at least good.</p>
<p>Keep in mind that since the dataset is typed, every type in the dataset is specifically named so it can be used to discover the entities it relates to.</p>
<h3>ToDataRow</h3>
<p>Is the part where entities are used to fill the appropriate tables in the dataset.</p>
<p>The entry point is the <span style="color:#2b91af;">Entity2DataSet</span> class, where TEntity is the entity type and TDataSet is the dataset type. In our case RootElement and DsLinqTest respectively.</p>
<p>Basically the <span style="color:#2b91af;">Entity2DataSet</span> class discovers the table that corresponds to the entity, and then calls the <span style="color:#2b91af;">Entity2DataRow </span>class which in addition takes the DataTable type discovered.</p>
<p>There are some helping functions that through reflection fill the row, from the entity and also find the child relations of the entity if there are any. If that is true the <span style="color:#2b91af;">Entity2DataSet</span> class is called again but this time TEntity should be SubRootElement in our case.</p>
<p>This side of the convention is fairly easy.</p>
<h3>ToEntity</h3>
<p>This case deals with converting a whole dataset to its entity. The entry class is <span style="color:#2b91af;">DataSet2Entity </span>where TDataContext is the type of our DataContext and TDataSet the type of the source Dataset. In our case LinqTestBigDataContext and DsLinqTest respectively.</p>
<p>The first thing that <span style="color:#2b91af;">DataSet2Entity </span>does is to find the tables have no parent relations. For each of these tables <span style="color:#2b91af;">DataTable2Entity </span>is used where in addition TDataTable and TDataRow are the types of the table and its rows.</p>
<p><span style="color:#2b91af;">DataTable2Entity </span>discovers the entity type that must create for each row it has and does so by using <span style="color:#2b91af;">DataRow2Entity</span> which is supplied with the knowledge of whether it is child row or not. This is crucial because if it is child row, it must be added to the related EntitySet of its parent entity instead of the entity Table in the data context.</p>
<p>The trick here is to know whether the original row is Added,Modified, Deleted or unchanged which is the easy part through RowState. The hard part is what to do with it.</p>
<h5>Added</h5>
<p>This case is easy. Just construct the entity and add it the table or the entityset and call InsertOnSubmit.</p>
<h5>Modified or Unmodified</h5>
<p>Here start the problems. First we must acquire the entity it self to which we will apply the values. Accordingly to if the row is a child or not, a predicate function or expression must be constructed. This part was the most difficult.</p>
<p>If the row is unmodified then there will be no applying of values.</p>
<h5>Deleted</h5>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Like in Modified the entity must be retrieved from the entitytable of the datacontext in order to call DeleteOnSubmit.</p>
<h3>Keeping track of the changes</h3>
<p>When a row is inserted or modified, various column values need to be updated by the auto generated ones from the database. So in every entity the PropertyChanged is captured. There with the help of a dictionary the new values are applied to the original rows. This happens after the SubmitChanges of the datacontext is used.</p>
<p>The rest of the <span style="color:#2b91af;">DataRow2Entity</span> finds the child rows of the row for each data relation and calls another generic version of its self.</p>
<h2>Creating Predicate Functions and Expressions</h2>
<p>This was the hardest part, and still there are some point that I can&#8217;t understand.</p>
<p>When trying to acquire an entity from the table entity of the datacontext, a simple delegate function suffices. After many attempts a managed to make the creation entirely dynamic based on the primary keys of the entity.</p>
<p>This is done by these two functions</p>
<blockquote><div><span style="color:#0000ff;">private </span>System.<span style="color:#2b91af;">Func</span>&lt;TEntity, <span style="color:#0000ff;">bool</span>&gt; CreatePredicateFunction(TDataRow row) { <span style="color:#0000ff;">return </span>p =&gt; (IsEqual(p, row)); } <span style="color:#0000ff;">private bool </span>IsEqual(TEntity entity, TDataRow row) { <span style="color:#0000ff;">for </span>(<span style="color:#0000ff;">int </span>i = 0; i &lt; Cache.<span style="color:#2b91af;">EntityPrimaryKeys</span>&lt;TEntity&gt;.Names.Count; i++) { <span style="color:#0000ff;">object </span>columnValue = <span style="color:#0000ff;">null</span>; <span style="color:#0000ff;">if </span>(row.RowState == <span style="color:#2b91af;">DataRowState</span>.Deleted) { columnValue = row[Cache.<span style="color:#2b91af;">EntityPrimaryKeys</span>&lt;TEntity&gt;.Names[i], <span style="color:#2b91af;">DataRowVersion</span>.Original]; } <span style="color:#0000ff;">else </span>{ columnValue = row[Cache.<span style="color:#2b91af;">EntityPrimaryKeys</span>&lt;TEntity&gt;.Names[i]]; } <span style="color:#0000ff;">if </span>((<span style="color:#0000ff;">bool</span>)Cache.<span style="color:#2b91af;">EntityPrimaryKeys</span>&lt;TEntity&gt;.EqualMethods[i].Invoke(<span style="color:#0000ff;">this</span>.entityType.GetProperty(Cache.<span style="color:#2b91af;">EntityPrimaryKeys</span>&lt;TEntity&gt;.Names[i]).GetValue(entity, <span style="color:#0000ff;">null</span>), <span style="color:#0000ff;">new object</span>[] { columnValue }) == <span style="color:#0000ff;">false</span>) { <span style="color:#0000ff;">return false</span>; } } <span style="color:#0000ff;">return true</span>; }</div>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>Happy as I was that I will be able to cast the above to an <span style="color:#2b91af;">Expression</span>&lt;System.<span style="color:#2b91af;">Func</span>&lt;TEntity, <span style="color:#0000ff;">bool</span>&gt;&gt; I found out that at runtime an exception is thrown telling me that IsEqual cannot be converted or something.</p>
<p>I assume the Expression is something far more complicated than a delegate. So in order for this to work a CreatePredicateExpression must by supplied in every DataRow of our dataset. I did like this</p>
<blockquote><p><span style="color:#0000ff;">public static class </span><span style="color:#2b91af;">DsLinqTestPredicators </span>{ <span style="color:#0000ff;">public static </span><span style="color:#2b91af;">Expression</span>&lt;System.<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">RootElement</span>, <span style="color:#0000ff;">bool</span>&gt;&gt; CreatePredicateExpression(<span style="color:#2b91af;">DsLinqTest</span>.<span style="color:#2b91af;">RootElementRow </span>row) { <span style="color:#0000ff;">int </span>idValue = row.RowState == System.Data.<span style="color:#2b91af;">DataRowState</span>.Deleted ? (<span style="color:#0000ff;">int</span>)row[<span style="color:#a31515;">"ID"</span>, System.Data.<span style="color:#2b91af;">DataRowVersion</span>.Original] : row.ID; <span style="color:#0000ff;">return </span>(<span style="color:#2b91af;">Expression</span>&lt;System.<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">RootElement</span>, <span style="color:#0000ff;">bool</span>&gt;&gt;)(p =&gt; p.ID.Equals(idValue)); } <span style="color:#0000ff;">public static </span><span style="color:#2b91af;">Expression</span>&lt;System.<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">SubRootElement</span>, <span style="color:#0000ff;">bool</span>&gt;&gt; CreatePredicateExpression(<span style="color:#2b91af;">DsLinqTest</span>.<span style="color:#2b91af;">SubRootElementRow </span>row) { <span style="color:#0000ff;">int </span>idValue = row.RowState == System.Data.<span style="color:#2b91af;">DataRowState</span>.Deleted ? (<span style="color:#0000ff;">int</span>)row[<span style="color:#a31515;">"ID"</span>, System.Data.<span style="color:#2b91af;">DataRowVersion</span>.Original] : row.ID; <span style="color:#0000ff;">return </span>(<span style="color:#2b91af;">Expression</span>&lt;System.<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">SubRootElement</span>, <span style="color:#0000ff;">bool</span>&gt;&gt;)(p =&gt; p.ID.Equals(idValue)); } }</p>
</blockquote>
<h2>Final Words for the Converter</h2>
<p>Extension Methods are heavily used to help making the convertion as programmatically tranparent as possible.</p>
<h2>Using the Code</h2>
<h3><span style="color:#0000ff;"><span style="color:#333333;">Extenders</span></span></h3>
<blockquote><p><span style="color:#0000ff;">public static class</span><span style="color:#2b91af;">DsLinqTestExtenders<br /></span>{<br />&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">public static void</span>Insert(<span style="color:#0000ff;">this</span><span style="color:#2b91af;">DsLinqTest </span>extented, <span style="color:#0000ff;">object</span>entity)<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((<span style="color:#2b91af;">DataSet</span>)extented).Insert(entity);<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">public static void</span>Insert(<span style="color:#0000ff;">this</span><span style="color:#2b91af;">DsLinqTest </span>extented, <span style="color:#0000ff;">object</span>[] entities)<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((<span style="color:#2b91af;">DataSet</span>)extented).Insert(entities);<br />&nbsp;&nbsp;&nbsp; }</p>
<p><span style="color:#0000ff;">public static void</span>ToEntities(<span style="color:#0000ff;">this</span><span style="color:#2b91af;">DsLinqTest </span>extented, <span style="color:#2b91af;">DataContext </span>dataContext)<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((<span style="color:#2b91af;">DataSet</span>)extented).ToEntities(dataContext);</p>
<p>}<br />}</p>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h3>Entity2Dataset</h3>
<blockquote><p><span style="color:#0000ff;">public </span><span style="color:#2b91af;">DsLinqTest </span>GetDsFromID(<span style="color:#0000ff;">int </span>id) { <span style="color:#2b91af;">LinqTestDataContext </span>ltdc = <span style="color:#0000ff;">new </span><span style="color:#2b91af;">LinqTestDataContext</span>(connectionString); <span style="color:#2b91af;">RootElement </span>re = ltdc.RootElements.Single(p =&gt; p.ID.Equals(id)); <span style="color:#2b91af;">DsLinqTest </span>ds = <span style="color:#0000ff;">new </span><span style="color:#2b91af;">DsLinqTest</span>(); ds.Insert(re); ds.AcceptChanges(); <span style="color:#0000ff;">return </span>ds;}</p>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h3>DataSet2Entity</h3>
<blockquote><p><span style="color:#0000ff;">public void </span>SaveGeneralDs(<span style="color:#2b91af;">DsLinqTest </span>dsLinqTest) { <span style="color:#2b91af;">LinqTestDataContext </span>ltdc = <span style="color:#0000ff;">new </span><span style="color:#2b91af;">LinqTestDataContext</span>(connectionString); dsLinqTest.ToEntities(ltdc); ltdc.SubmitChanges(); }</p>
</blockquote>
<p><a href="The assemblies will be upload when I post the codeproject article.">Source Code for the above assemblies</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sarafianalex.wordpress.com/21/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sarafianalex.wordpress.com/21/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarafianalex.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarafianalex.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarafianalex.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarafianalex.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarafianalex.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarafianalex.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarafianalex.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarafianalex.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarafianalex.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarafianalex.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarafianalex.wordpress.com&blog=2367780&post=21&subd=sarafianalex&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarafianalex.wordpress.com/2008/04/21/typed-dataset-linq-entities/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64f00567ef7dda3ec36dcf0e01043d11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sarafian</media:title>
		</media:content>

		<media:content url="http://sarafianalex.files.wordpress.com/2008/04/image-thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://sarafianalex.files.wordpress.com/2008/04/image-thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>LINQ and Client Server Applications</title>
		<link>http://sarafianalex.wordpress.com/2008/04/21/linq-and-client-server-applications/</link>
		<comments>http://sarafianalex.wordpress.com/2008/04/21/linq-and-client-server-applications/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 09:05:26 +0000</pubDate>
		<dc:creator>Sarafian Alex</dc:creator>
				<category><![CDATA[.NET C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Knowledge]]></category>

		<guid isPermaLink="false">http://sarafianalex.wordpress.com/2008/04/21/linq-and-client-server-applications/</guid>
		<description><![CDATA[First of all I would like to apologize for not posting links, for various sources I acquired knowledge from, because a week has passed after reading them and I don&#8217;t remember which is which.
Last week I was doing some research for LINQ and how it could be integrated in the applications I develop for my [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarafianalex.wordpress.com&blog=2367780&post=16&subd=sarafianalex&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p align="justify">First of all I would like to apologize for not posting links, for various sources I acquired knowledge from, because a week has passed after reading them and I don&#8217;t remember which is which.</p>
<p align="justify">Last week I was doing some research for LINQ and how it could be integrated in the applications I develop for my company. There is something on the net, that always keeps bugging me and I prefer to have my own opinion. Always everything is great, but there always seems to missing the parts that things are not good.</p>
<p align="justify">In a few words LINQ is an ORM that ships with .NET3 and from what I have seen it is very good. Truly all the great things that have been talked about are there, and even better for me. But something that stroked odd for me, was the fact that there was no single mention about Client &#8211; Server applications or known as N-Tier.</p>
<p align="justify">I&#8217;m not a Web Developer. I develop Smart Client Applications where the notion of disconnected from the data source is the biggest truth.</p>
<p align="justify">Every example in LINQ that I had saw, assumed that my application has a direct and steady connection to the data source, exactly like a web application. That maybe true for web in many cases, but in other applications is not. I tried LINQ with just one entity and sub collection, sending it to the client and bringing it back to the server, and all hell broke loose. For just a single entity, logic should be applied on how you should attach your entity to the data context. And I didn&#8217;t get to deletion.</p>
<p align="justify">There are two ways to manage data in disconnected state. Either you write your own mechanism or use Datasets. I prefer the latest, and especially Typed Datasets for various reasons one of which is the version mechanism that dataset have embedded inside. So I tried to search about Datasets and LINQ and there was just a little.</p>
<p align="justify">So I did a little research and found out that &#8230;</p>
<p align="justify">First of all there are two types of LINQ. <strong><a href="http://msdn2.microsoft.com/en-us/library/bb425822.aspx">LINQ to SQL</a></strong> and <strong><a href="http://www.danielmoth.com/Blog/2007/07/linq-to-dataset.html">LINQ to DataSet</a>. </strong>Both do the exact same thing, that is to query collections ,with one huge difference. LINQ to SQL queries collections directly to your data source and LINQ to Dataset queries basically your own dataset. My biggest disappointment was when I found that in the original beta, there was a functionality that converted datasets to entities and reverse, but a developer of Microsoft in his blog, mentioned that they didn&#8217;t have the resources to implement it in the current version.</p>
<p align="justify">My thought is that when building a framework with business objects and a data access layer that supports its persistence wouldn&#8217;t it be great to have Entities-like objects for web and a disconnected version of them for smart clients? This would be, that SQL would be reduces significantly and both application types would utilize the same idea. The only missing link is the mechanism to convert between entities and datasets without having to write extra code.</p>
<p align="justify">The above missing link was what I was developing a whole week. It started as a proof of concept, but it became something that I think has great potential, because I like the idea of LINQ especially for Web Applications, which I believe will be optimized even more by Microsoft, I like typed datasets for disconnected Applications and the only thing missing was something that connected them.</p>
<p align="justify">This post was like an introduction for my next post, that is the converter project it self.</p>
<p align="justify"> </p>
<p align="justify"> </p>
<p align="justify"> </p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sarafianalex.wordpress.com/16/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sarafianalex.wordpress.com/16/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarafianalex.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarafianalex.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarafianalex.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarafianalex.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarafianalex.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarafianalex.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarafianalex.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarafianalex.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarafianalex.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarafianalex.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarafianalex.wordpress.com&blog=2367780&post=16&subd=sarafianalex&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarafianalex.wordpress.com/2008/04/21/linq-and-client-server-applications/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64f00567ef7dda3ec36dcf0e01043d11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sarafian</media:title>
		</media:content>
	</item>
	</channel>
</rss>