<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" 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:georss="http://www.georss.org/georss" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Nesterovsky bros - .NET</title>
    <link>http://www.nesterovsky-bros.com/weblog/</link>
    <description />
    <language>en-us</language>
    <copyright>Nesterovsky bros</copyright>
    <lastBuildDate>Mon, 13 May 2013 23:09:02 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.12105.0</generator>
    <managingEditor>contact@nesterovsky-bros.com</managingEditor>
    <webMaster>contact@nesterovsky-bros.com</webMaster>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p style="direction: ltr">
Although WCF REST service + JSON is outdated comparing to Web API, there are yet a
lot of such solutions (and probably will appear new ones) that use such "old"
technology. 
</p>
        <p>
One of the crucial points of any web application is an error handler that allows gracefully
resolve server-side exceptions and routes them as JSON objects to the client for further
processing. There are dozen approachesin Internet that solve this issue  (e.g. <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f&amp;url=http%3a%2f%2fblog.manglar.com%2fhow-to-provide-custom-json-exceptions-from-as-wcf-service%2f">http://blog.manglar.com/how-to-provide-custom-json-exceptions-from-as-wcf-service/</a>),
but there is no one that demonstrates error handling ot the client-side. We realize
that it's impossible to write something general that suits for every web application,
but we'd like to show a client-side error handler that utilizes JSON and KendoUI.
</p>
        <p style="direction: ltr">
On our opinion, the successfull error handler must display an understandable error
message on one hand, and on the other hand it has to provide technical info for developers
in order to investigate the exception reason (and to fix it, if need):
</p>
        <table border="0">
          <tr>
            <td style="vertical-align: top;">
              <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fimages%2fcolapsed-error-dialog.png">
                <img src="http://www.nesterovsky-bros.com/images/thumb-colapsed-error-dialog.png" alt="collapsed error dialog" />
              </a>
            </td>
            <td>
              <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fimages%2fexpanded-error-dialog.png">
                <img src="http://www.nesterovsky-bros.com/images/thumb-expanded-error-dialog.png" alt="collapsed error dialog" />
              </a>
            </td>
          </tr>
        </table>
        <p>
You may <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fErrorHandling.zip">download
demo project here</a>. It contains three crucial parts:
</p>
        <ul>
          <li>
A server-side error handler that catches all exceptions and serializes them as JSON
objects (see <strong>/Code/JsonErrorHandler.cs</strong> and <strong>/Code/JsonWebHttpBehaviour.cs</strong>).</li>
          <li>
An error dialog that's based on user-control defined in previous articles (see <strong>/scripts/controls/error.js</strong>,<strong> /scripts/controls/error.resources.js</strong> and <strong>/scripts/templates/error.tmpl.html</strong>).</li>
          <li>
A client-side error handler that displays errors in user-friendly's manner (see <strong>/scripts/api/api.js</strong>,
method <strong>defaultErrorHandler()</strong>).</li>
        </ul>
        <p>
Of course this is only a draft solution, but it defines a direction for further customizations
in your web applications.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f" />
      </body>
      <title>Error handling in WCF based web applications</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2013/05/13/ErrorHandlingInWCFBasedWebApplications.aspx</link>
      <pubDate>Mon, 13 May 2013 23:09:02 GMT</pubDate>
      <description>   &lt;p style="direction: ltr"&gt;
Although WCF REST service + JSON is outdated comparing to Web API, there are yet a
lot of such solutions (and probably will appear new ones) that use such &amp;quot;old&amp;quot;
technology. 
&lt;/p&gt;
&lt;p&gt;
One of the crucial points of any web application is an error handler that allows gracefully
resolve server-side exceptions and routes them as JSON objects to the client for further
processing. There are dozen approachesin Internet that solve this issue&amp;nbsp; (e.g. &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f&amp;amp;url=http%3a%2f%2fblog.manglar.com%2fhow-to-provide-custom-json-exceptions-from-as-wcf-service%2f"&gt;http://blog.manglar.com/how-to-provide-custom-json-exceptions-from-as-wcf-service/&lt;/a&gt;),
but there is no one that demonstrates error handling ot the client-side. We realize
that it&amp;#39;s impossible to write something general that suits for every web application,
but we&amp;#39;d like to show a client-side error handler that utilizes JSON and KendoUI.
&lt;/p&gt;
&lt;p style="direction: ltr"&gt;
On our opinion, the successfull error handler must display an understandable error
message on one hand, and on the other hand it has to provide technical info for developers
in order to investigate the exception reason (and to fix it, if need):
&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tr&gt;
&lt;td style="vertical-align: top;"&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fimages%2fcolapsed-error-dialog.png"&gt; &lt;img src="http://www.nesterovsky-bros.com/images/thumb-colapsed-error-dialog.png" alt="collapsed error dialog" /&gt; &lt;/a&gt; 
&lt;/td&gt;
&lt;td&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fimages%2fexpanded-error-dialog.png"&gt; &lt;img src="http://www.nesterovsky-bros.com/images/thumb-expanded-error-dialog.png" alt="collapsed error dialog" /&gt; &lt;/a&gt; 
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;
You may &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fErrorHandling.zip"&gt;download
demo project here&lt;/a&gt;. It contains three crucial parts:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
A server-side error handler that catches all exceptions and serializes them as JSON
objects (see &lt;strong&gt;/Code/JsonErrorHandler.cs&lt;/strong&gt; and &lt;strong&gt;/Code/JsonWebHttpBehaviour.cs&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;
An error dialog that&amp;#39;s based on user-control defined in previous articles (see &lt;strong&gt;/scripts/controls/error.js&lt;/strong&gt;,&lt;strong&gt; /scripts/controls/error.resources.js&lt;/strong&gt; and &lt;strong&gt;/scripts/templates/error.tmpl.html&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;
A client-side error handler that displays errors in user-friendly&amp;#39;s manner (see &lt;strong&gt;/scripts/api/api.js&lt;/strong&gt;,
method &lt;strong&gt;defaultErrorHandler()&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Of course this is only a draft solution, but it defines a direction for further customizations
in your web applications.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>javascript</category>
      <category>kendoui</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=b17502e5-9edf-43d2-9fab-7a5a3124ddf5</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b17502e5-9edf-43d2-9fab-7a5a3124ddf5.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,b17502e5-9edf-43d2-9fab-7a5a3124ddf5.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=b17502e5-9edf-43d2-9fab-7a5a3124ddf5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Kendo UI Docs contains an article "<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=b17502e5-9edf-43d2-9fab-7a5a3124ddf5&amp;url=http%3a%2f%2fdocs.kendoui.com%2fhowto%2fload-templates-external-files">How
To: Load Templates from External Files</a>", where authors review two way of
dealing with Kendo UI templates.
</p>
        <p>
While using Kendo UI we have found our own answer to: <i>where will the Kendo UI templates
be defined and maintained?</i></p>
        <p style="float: right">
          <img alt="solution tree" src="content/binary/templates.png" style="width: 320px" />
        </p>
        <p>
In our .NET project we have decided to keep templates separately, and to store them
under the "templates" folder. Those templates are in fact include html,
head, and stylesheet links. This is to help us to present those tempates in the design
view. 
</p>
        <p>
In our scripts folder, we have defined a small text transformation template: "templates.tt",
which produces "templates.js" file. This template takes body contents of
each "*.tmpl.html" file from "templates" folder and builds string
of the form:
</p>
        <p style="padding-left: 1em">
          <code>document.write('&lt;script id="footer-template" type="text/x-kendo-template"&gt;...&lt;/script&gt;&lt;script
id="row-template" type="text/x-kendo-template"&gt;...&lt;/script&gt;');</code>
        </p>
        <p>
In our page that uses templates, we include "templates.js":
</p>
        <p style="padding-left: 1em">
          <code> &lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;script src="scripts/templates.js"&gt;&lt;/script&gt; 
<br />
...</code>
        </p>
        <p>
Thus, we have:
</p>
        <ul>
          <li>
clean separation of templates and page content;</li>
          <li>
automatically generated templates include file.</li>
        </ul>
        <p>
          <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=b17502e5-9edf-43d2-9fab-7a5a3124ddf5&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fWebTemplates.zip">WebTemplates.zip</a> contains
a web project demonstrating our technique. "<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=b17502e5-9edf-43d2-9fab-7a5a3124ddf5&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2ftemplates.tt.txt">templates.tt</a>"
is text template transformation used in the project.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=b17502e5-9edf-43d2-9fab-7a5a3124ddf5" />
      </body>
      <title>How To: Load KendoUI Templates from External Files</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b17502e5-9edf-43d2-9fab-7a5a3124ddf5.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2013/01/06/HowToLoadKendoUITemplatesFromExternalFiles.aspx</link>
      <pubDate>Sun, 06 Jan 2013 19:43:19 GMT</pubDate>
      <description>&lt;p&gt;
Kendo UI Docs contains an article &amp;quot;&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=b17502e5-9edf-43d2-9fab-7a5a3124ddf5&amp;amp;url=http%3a%2f%2fdocs.kendoui.com%2fhowto%2fload-templates-external-files"&gt;How
To: Load Templates from External Files&lt;/a&gt;&amp;quot;, where authors review two way of
dealing with Kendo UI templates.
&lt;/p&gt;
&lt;p&gt;
While using Kendo UI we have found our own answer to: &lt;i&gt;where will the Kendo UI templates
be defined and maintained?&lt;/i&gt;
&lt;/p&gt;
&lt;p style="float: right"&gt;
&lt;img alt="solution tree" src="content/binary/templates.png" style="width: 320px" /&gt;
&lt;/p&gt;
&lt;p&gt;
In our .NET project we have decided to keep templates separately, and to store them
under the &amp;quot;templates&amp;quot; folder. Those templates are in fact include html,
head, and stylesheet links. This is to help us to present those tempates in the design
view. 
&lt;/p&gt;
&lt;p&gt;
In our scripts folder, we have defined a small text transformation template: &amp;quot;templates.tt&amp;quot;,
which produces &amp;quot;templates.js&amp;quot; file. This template takes body contents of
each &amp;quot;*.tmpl.html&amp;quot; file from &amp;quot;templates&amp;quot; folder and builds string
of the form:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;document.write('&amp;lt;script id="footer-template" type="text/x-kendo-template"&amp;gt;...&amp;lt;/script&amp;gt;&amp;lt;script
id="row-template" type="text/x-kendo-template"&amp;gt;...&amp;lt;/script&amp;gt;');&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
In our page that uses templates, we include &amp;quot;templates.js&amp;quot;:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; &amp;lt;!DOCTYPE html&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;script src=&amp;quot;scripts/templates.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt; 
&lt;br /&gt;
...&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Thus, we have:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
clean separation of templates and page content;&lt;/li&gt;
&lt;li&gt;
automatically generated templates include file.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=b17502e5-9edf-43d2-9fab-7a5a3124ddf5&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fWebTemplates.zip"&gt;WebTemplates.zip&lt;/a&gt; contains
a web project demonstrating our technique. &amp;quot;&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=b17502e5-9edf-43d2-9fab-7a5a3124ddf5&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2ftemplates.tt.txt"&gt;templates.tt&lt;/a&gt;&amp;quot;
is text template transformation used in the project.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=b17502e5-9edf-43d2-9fab-7a5a3124ddf5" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,b17502e5-9edf-43d2-9fab-7a5a3124ddf5.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>javascript</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=40a04a86-ce1f-44a2-8daa-f54306b84e20</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,40a04a86-ce1f-44a2-8daa-f54306b84e20.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,40a04a86-ce1f-44a2-8daa-f54306b84e20.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=40a04a86-ce1f-44a2-8daa-f54306b84e20</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Our goal is to generate reports in streaming mode.
</p>
        <p>
At some point we need to deal with data streams (e.g. xml streams for xslt transformations).
Often a nature of report demands several passes through the data. To increase performance
we have defined a class named <code>StreamResource</code>. This class encapsulates
input data, reads it once and caches it into a temp file; thus data can be traversed
many times. <code>StreamResource</code> can read data lazily or in a eager way thus
releasing resources early. This class can be used as a variation of <code>PipeStream</code>,
which never blocks, as if a size of a buffer is not limited, and which can be read
many times. 
</p>
        <p>
The API looks like this:
</p>
        <p style="padding-left: 1em">
          <code> public class StreamResource: IDisposable<br />
{<br />
/// &lt;summary&gt;<br />
/// Creates a StreamSource instance.<br />
/// &lt;/summary&gt;<br />
/// &lt;param name="source"&gt;<br />
/// A function that returns source as an input stream.<br />
/// &lt;/param&gt;<br />
/// &lt;param name="settings"&gt;Optional settings.&lt;/param&gt;<br />
public StreamResource(Func&lt;Stream&gt; source, Settings settings = null);<br /><br />
/// &lt;summary&gt;<br />
/// Creates a StreamSource instance.<br />
/// &lt;/summary&gt;<br />
/// &lt;param name="source"&gt;<br />
/// A function that writes source data into an output stream.<br />
/// &lt;/param&gt;<br />
/// &lt;param name="settings"&gt;Optional settings.&lt;/param&gt;<br />
public StreamResource(Action&lt;Stream&gt; source, Settings settings = null);<br /><br />
/// &lt;summary&gt;<br />
/// Gets an input stream.<br />
/// &lt;/summary&gt;<br />
/// &lt;param name="shared"&gt;<br />
/// Indicates that this StreamResouce should be disposed when returned<br />
/// stream is closed and there are no more currently opened cache streams.<br />
/// &lt;/param&gt;<br />
/// &lt;returns&gt;A input stream.&lt;/returns&gt;<br />
public Stream GetStream(bool shared = false);<br />
} </code>
        </p>
        <p>
The use pattern is following:
</p>
        <p style="padding-left: 1em">
          <code>// Acquire resource.<br />
using(var resource = new StreamResource(() =&gt; CallService(params...)))<br />
{<br />
// Read stream.<br />
using(var stream = resource.GetStream())<br />
{<br />
...<br />
}<br /><br />
...<br /><br />
// Read stream again.<br />
using(var stream = resource.GetStream())<br />
{<br />
...<br />
}<br />
}</code>
        </p>
        <p style="direction: ltr">
          <code>StreamResource</code> is efficient even if you need to process content only
once, as it monitors timings of reading of source data and compares it with timings
of data consumption. If the difference exceeds some threshold then <code>StreamResource</code> caches
source greedily, otherwise source is pooled lazily. Thus, input resources can be released
promptly. This is important, for example, when the source depends on a database connection.
</p>
        <p>
The use pattern is following:
</p>
        <p style="padding-left: 1em">
          <code>// Acquire resource and get shared stream.<br />
using(var stream = new StreamResource(() =&gt; CallService(params...)).GetStream(true))<br />
{<br />
...<br />
}</code>
        </p>
        <p>
Finally, <code>StreamResource</code> allows to process data in a pipe stream mode.
This is when you have a generator function <code>Action&lt;Stream&gt;</code> that
can write to a stream, and you want to read that data. The advantage of <code>StreamResource</code> over
real pipe stream is that it can work without blocking of generator, thus releasing
resources early.
</p>
        <p>
The use pattern is similar to the previous one:
</p>
        <p style="padding-left: 1em">
          <code>using(var stream = new StreamResource(output =&gt; Generate(output, params...)).GetStream(true))<br />
{<br />
...<br />
}</code>
        </p>
        <p>
The source of the class can be found at <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=40a04a86-ce1f-44a2-8daa-f54306b84e20&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fStreaming.zip">Streaming.zip</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=40a04a86-ce1f-44a2-8daa-f54306b84e20" />
      </body>
      <title>StreamResource</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,40a04a86-ce1f-44a2-8daa-f54306b84e20.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/11/20/StreamResource.aspx</link>
      <pubDate>Tue, 20 Nov 2012 07:01:57 GMT</pubDate>
      <description>&lt;p&gt;
Our goal is to generate reports in streaming mode.
&lt;/p&gt;
&lt;p&gt;
At some point we need to deal with data streams (e.g. xml streams for xslt transformations).
Often a nature of report demands several passes through the data. To increase performance
we have defined a class named &lt;code&gt;StreamResource&lt;/code&gt;. This class encapsulates
input data, reads it once and caches it into a temp file; thus data can be traversed
many times. &lt;code&gt;StreamResource&lt;/code&gt; can read data lazily or in a eager way thus
releasing resources early. This class can be used as a variation of &lt;code&gt;PipeStream&lt;/code&gt;,
which never blocks, as if a size of a buffer is not limited, and which can be read
many times. 
&lt;/p&gt;
&lt;p&gt;
The API looks like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; public class StreamResource: IDisposable&lt;br /&gt;
{&lt;br /&gt;
/// &amp;lt;summary&amp;gt;&lt;br /&gt;
/// Creates a StreamSource instance.&lt;br /&gt;
/// &amp;lt;/summary&amp;gt;&lt;br /&gt;
/// &amp;lt;param name="source"&amp;gt;&lt;br /&gt;
/// A function that returns source as an input stream.&lt;br /&gt;
/// &amp;lt;/param&amp;gt;&lt;br /&gt;
/// &amp;lt;param name="settings"&amp;gt;Optional settings.&amp;lt;/param&amp;gt;&lt;br /&gt;
public StreamResource(Func&amp;lt;Stream&amp;gt; source, Settings settings = null);&lt;br /&gt;
&lt;br /&gt;
/// &amp;lt;summary&amp;gt;&lt;br /&gt;
/// Creates a StreamSource instance.&lt;br /&gt;
/// &amp;lt;/summary&amp;gt;&lt;br /&gt;
/// &amp;lt;param name="source"&amp;gt;&lt;br /&gt;
/// A function that writes source data into an output stream.&lt;br /&gt;
/// &amp;lt;/param&amp;gt;&lt;br /&gt;
/// &amp;lt;param name="settings"&amp;gt;Optional settings.&amp;lt;/param&amp;gt;&lt;br /&gt;
public StreamResource(Action&amp;lt;Stream&amp;gt; source, Settings settings = null);&lt;br /&gt;
&lt;br /&gt;
/// &amp;lt;summary&amp;gt;&lt;br /&gt;
/// Gets an input stream.&lt;br /&gt;
/// &amp;lt;/summary&amp;gt;&lt;br /&gt;
/// &amp;lt;param name="shared"&amp;gt;&lt;br /&gt;
/// Indicates that this StreamResouce should be disposed when returned&lt;br /&gt;
/// stream is closed and there are no more currently opened cache streams.&lt;br /&gt;
/// &amp;lt;/param&amp;gt;&lt;br /&gt;
/// &amp;lt;returns&amp;gt;A input stream.&amp;lt;/returns&amp;gt;&lt;br /&gt;
public Stream GetStream(bool shared = false);&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The use pattern is following:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;// Acquire resource.&lt;br /&gt;
using(var resource = new StreamResource(() =&amp;gt; CallService(params...)))&lt;br /&gt;
{&lt;br /&gt;
// Read stream.&lt;br /&gt;
using(var stream = resource.GetStream())&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
// Read stream again.&lt;br /&gt;
using(var stream = resource.GetStream())&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p style="direction: ltr"&gt;
&lt;code&gt;StreamResource&lt;/code&gt; is efficient even if you need to process content only
once, as it monitors timings of reading of source data and compares it with timings
of data consumption. If the difference exceeds some threshold then &lt;code&gt;StreamResource&lt;/code&gt; caches
source greedily, otherwise source is pooled lazily. Thus, input resources can be released
promptly. This is important, for example, when the source depends on a database connection.
&lt;/p&gt;
&lt;p&gt;
The use pattern is following:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;// Acquire resource and get shared stream.&lt;br /&gt;
using(var stream = new StreamResource(() =&amp;gt; CallService(params...)).GetStream(true))&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Finally, &lt;code&gt;StreamResource&lt;/code&gt; allows to process data in a pipe stream mode.
This is when you have a generator function &lt;code&gt;Action&amp;lt;Stream&amp;gt;&lt;/code&gt; that
can write to a stream, and you want to read that data. The advantage of &lt;code&gt;StreamResource&lt;/code&gt; over
real pipe stream is that it can work without blocking of generator, thus releasing
resources early.
&lt;/p&gt;
&lt;p&gt;
The use pattern is similar to the previous one:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;using(var stream = new StreamResource(output =&amp;gt; Generate(output, params...)).GetStream(true))&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The source of the class can be found at &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=40a04a86-ce1f-44a2-8daa-f54306b84e20&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fStreaming.zip"&gt;Streaming.zip&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=40a04a86-ce1f-44a2-8daa-f54306b84e20" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,40a04a86-ce1f-44a2-8daa-f54306b84e20.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=6d962206-6ca4-4ed5-828b-551082e660dc</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,6d962206-6ca4-4ed5-828b-551082e660dc.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,6d962206-6ca4-4ed5-828b-551082e660dc.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=6d962206-6ca4-4ed5-828b-551082e660dc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Two monthes ago we have started <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=6d962206-6ca4-4ed5-828b-551082e660dc&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f09%2f07%2fChangeColumnTypeInSQLServer2008R2.aspx"> a
process of changing column type from <code>smallint</code> to <code>int</code> in
a big database.</a></p>
        <p>
This was splitted in two phases:
</p>
        <ol>
          <li>
Change tables and internal stored procedures and functions.</li>
          <li>
Change interface API and update all clients.</li>
        </ol>
        <p>
The first part took almost two monthes to complete. Please read earlier post about
the technique we have selected for the implementation. In total we have transferred
about 15 billion rows. During this time database was online.
</p>
        <p>
The second part was short but the problem was that we did not control all clients,
so could not arbitrary change types of parameters and of result columns. 
</p>
        <p>
All our clients use Entity Framework 4 to access the database. All access is done
though stored procedures. So suppose there was a procedure:
</p>
        <p style="padding-left: 1em">
          <code>create procedure Data.GetReports(@type smallint) as<br />
begin<br />
select Type, ... from Data.Report where Type = @type;<br />
end;</code>
        </p>
        <p>
where column <code>"Type"</code> was of type <code>smallint</code>. Now
we were going to change it to:
</p>
        <p style="padding-left: 1em">
          <code>create procedure Data.GetReports(@type int) as<br />
begin<br />
select Type, ... from Data.Report where Type = @type;<br />
end;</code>
        </p>
        <p>
where <code>"Type"</code> column became of type <code>int</code>.
</p>
        <p>
Our tests have shown that EF bears with change of types of input parameters, but throws
exceptions when column type has been changed, even when a value fits the range. The
reason is that EF uses method <code><a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=6d962206-6ca4-4ed5-828b-551082e660dc&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.data.sqlclient.sqldatareader.getint16.aspx">SqlDataReader.GetInt16</a></code> to
access the column value. This method has a remark: "<i>No conversions are performed;
therefore, the data retrieved must already be a 16-bit signed integer.</i>"
</p>
        <p>
Fortunately, we have found that EF allows additional columns in the result set. This
helped us to formulate the solution. We have updated the procedure definition like
this:
</p>
        <p style="padding-left: 1em">
          <code>create procedure Data.GetReports(@type int) as<br />
begin<br />
select 
<br />
cast(Type as smallint) Type, -- deprecated<br />
Type TypeEx, ... 
<br />
from 
<br />
Data.Report 
<br />
where 
<br />
Type = @type;<br />
end;</code>
        </p>
        <p>
This way:
</p>
        <ul>
          <li>
result column <code>"Type"</code> is declared as deprecated;</li>
          <li>
old clients still work;</li>
          <li>
all clients should be updated to use <code>"TypeEx"</code> column;</li>
          <li>
after all clients will be updated we shall remove <code>"Type"</code> column from
the result set.</li>
        </ul>
        <p>
So there is a clear migration process.
</p>
        <p>
P.S. we don't understand why <code>SqlDataReader</code> doesn't support value
conversion. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=6d962206-6ca4-4ed5-828b-551082e660dc" />
      </body>
      <title>Change column type in SQL Server, Part 2</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,6d962206-6ca4-4ed5-828b-551082e660dc.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/11/09/ChangeColumnTypeInSQLServerPart2.aspx</link>
      <pubDate>Fri, 09 Nov 2012 16:41:27 GMT</pubDate>
      <description>  &lt;p&gt;
Two monthes ago we have started &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=6d962206-6ca4-4ed5-828b-551082e660dc&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f09%2f07%2fChangeColumnTypeInSQLServer2008R2.aspx"&gt; a
process of changing column type from &lt;code&gt;smallint&lt;/code&gt; to &lt;code&gt;int&lt;/code&gt; in
a big database.&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
This was splitted in two phases:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Change tables and internal stored procedures and functions.&lt;/li&gt;
&lt;li&gt;
Change interface API and update all clients.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The first part took almost two monthes to complete. Please read earlier post about
the technique we have selected for the implementation. In total we have transferred
about 15 billion rows. During this time database was online.
&lt;/p&gt;
&lt;p&gt;
The second part was short but the problem was that we did not control all clients,
so could not arbitrary change types of parameters and of result columns. 
&lt;/p&gt;
&lt;p&gt;
All our clients use Entity Framework 4 to access the database. All access is done
though stored procedures. So suppose there was a procedure:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;create procedure Data.GetReports(@type smallint) as&lt;br /&gt;
begin&lt;br /&gt;
select Type, ... from Data.Report where Type = @type;&lt;br /&gt;
end;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
where column &lt;code&gt;&amp;quot;Type&amp;quot;&lt;/code&gt; was of type &lt;code&gt;smallint&lt;/code&gt;. Now
we were going to change it to:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;create procedure Data.GetReports(@type int) as&lt;br /&gt;
begin&lt;br /&gt;
select Type, ... from Data.Report where Type = @type;&lt;br /&gt;
end;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
where &lt;code&gt;&amp;quot;Type&amp;quot;&lt;/code&gt; column became of type &lt;code&gt;int&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
Our tests have shown that EF bears with change of types of input parameters, but throws
exceptions when column type has been changed, even when a value fits the range. The
reason is that EF uses method &lt;code&gt;&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=6d962206-6ca4-4ed5-828b-551082e660dc&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.data.sqlclient.sqldatareader.getint16.aspx"&gt;SqlDataReader.GetInt16&lt;/a&gt;&lt;/code&gt; to
access the column value. This method has a remark: &amp;quot;&lt;i&gt;No conversions are performed;
therefore, the data retrieved must already be a 16-bit signed integer.&lt;/i&gt;&amp;quot;
&lt;/p&gt;
&lt;p&gt;
Fortunately, we have found that EF allows additional columns in the result set. This
helped us to formulate the solution. We have updated the procedure definition like
this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;create procedure Data.GetReports(@type int) as&lt;br /&gt;
begin&lt;br /&gt;
select 
&lt;br /&gt;
cast(Type as smallint) Type, -- deprecated&lt;br /&gt;
Type TypeEx, ... 
&lt;br /&gt;
from 
&lt;br /&gt;
Data.Report 
&lt;br /&gt;
where 
&lt;br /&gt;
Type = @type;&lt;br /&gt;
end;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
This way:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
result column &lt;code&gt;&amp;quot;Type&amp;quot;&lt;/code&gt; is declared as deprecated;&lt;/li&gt;
&lt;li&gt;
old clients still work;&lt;/li&gt;
&lt;li&gt;
all clients should be updated to use &lt;code&gt;&amp;quot;TypeEx&amp;quot;&lt;/code&gt; column;&lt;/li&gt;
&lt;li&gt;
after all clients will be updated we shall remove &lt;code&gt;"Type"&lt;/code&gt; column from
the result set.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
So there is a clear migration process.
&lt;/p&gt;
&lt;p&gt;
P.S. we don&amp;#39;t understand why &lt;code&gt;SqlDataReader&lt;/code&gt; doesn&amp;#39;t support value
conversion. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=6d962206-6ca4-4ed5-828b-551082e660dc" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,6d962206-6ca4-4ed5-828b-551082e660dc.aspx</comments>
      <category>.NET</category>
      <category>SQL Server puzzle</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=98b58257-9069-4d13-9d10-a7061e14e834</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,98b58257-9069-4d13-9d10-a7061e14e834.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,98b58257-9069-4d13-9d10-a7061e14e834.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=98b58257-9069-4d13-9d10-a7061e14e834</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you deal with web applications you probably have already dealt with export data
to Excel. There are several options to prepare data for Excel:
</p>
        <ul>
          <li>
generate CSV;</li>
          <li>
generate HTML that excel understands;</li>
          <li>
generate XML in Spreadsheet 2003 format;</li>
          <li>
generate data using Open XML SDK or some other 3rd party libraries;</li>
          <li>
generate data in XLSX format, according to Open XML specification.</li>
        </ul>
        <p>
You may find a good article with pros and cons of each solution <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=98b58257-9069-4d13-9d10-a7061e14e834&amp;url=http%3a%2f%2fblogs.msdn.com%2fb%2ferikaehrli%2farchive%2f2009%2f01%2f30%2fhow-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx" target="_blank">here</a>.
We, in our turn, would like to share our experience in this field. Let's start from
requirements:
</p>
        <ul>
          <li>
Often we have to export huge data-sets.</li>
          <li>
We should be able to format, parametrize and to apply different styles to the exported
data.</li>
          <li>
There are cases when exported data may contain more than one table per sheet or even
more than one sheet.</li>
          <li>
Some exported data have to be illustrated with charts.</li>
        </ul>
        <p>
All these requirements led us to a solution based on XSLT processing of streamed data.
The advantage of this solution is that the result is immediately forwarded to a client
as fast as XSLT starts to generate output. Such approach is much productive than generating
of XLSX using of Open XML SDK or any other third party library, since it avoids keeping
a huge data-sets in memory on the server side.
</p>
        <p style="direction: ltr">
Another advantage - is simple maintenance, as we achieve clear separation of data
and presentation layers. On each request to change formatting or apply another style
to a cell you just have to modify xslt file(s) that generate variable parts of XLSX.
</p>
        <p>
As result, our clients get XLSX files according with Open XML specifications. The
details of implementations of our solution see in our next posts.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=98b58257-9069-4d13-9d10-a7061e14e834" />
      </body>
      <title>Export data to Excel</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,98b58257-9069-4d13-9d10-a7061e14e834.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/10/29/ExportDataToExcel.aspx</link>
      <pubDate>Mon, 29 Oct 2012 15:34:38 GMT</pubDate>
      <description>&lt;p&gt;
If you deal with web applications you probably have already dealt with export data
to Excel. There are several options to prepare data for Excel:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
generate CSV;&lt;/li&gt;
&lt;li&gt;
generate HTML that excel understands;&lt;/li&gt;
&lt;li&gt;
generate XML in Spreadsheet 2003 format;&lt;/li&gt;
&lt;li&gt;
generate data using Open XML SDK or some other 3rd party libraries;&lt;/li&gt;
&lt;li&gt;
generate data in XLSX format, according to Open XML specification.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
You may find a good article with pros and cons of each solution &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=98b58257-9069-4d13-9d10-a7061e14e834&amp;amp;url=http%3a%2f%2fblogs.msdn.com%2fb%2ferikaehrli%2farchive%2f2009%2f01%2f30%2fhow-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx" target="_blank"&gt;here&lt;/a&gt;.
We, in our turn, would like to share our experience in this field. Let's start from
requirements:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Often we have to export huge data-sets.&lt;/li&gt;
&lt;li&gt;
We should be able to format, parametrize and to apply different styles to the exported
data.&lt;/li&gt;
&lt;li&gt;
There are cases when exported data may contain more than one table per sheet or even
more than one sheet.&lt;/li&gt;
&lt;li&gt;
Some exported data have to be illustrated with charts.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
All these requirements led us to a solution based on XSLT processing of streamed data.
The advantage of this solution is that the result is immediately forwarded to a client
as fast as XSLT starts to generate output. Such approach is much productive than generating
of XLSX using of Open XML SDK or any other third party library, since it avoids keeping
a huge data-sets in memory on the server side.
&lt;/p&gt;
&lt;p style="direction: ltr"&gt;
Another advantage - is simple maintenance, as we achieve clear separation of data
and presentation layers. On each request to change formatting or apply another style
to a cell you just have to modify xslt file(s) that generate variable parts of XLSX.
&lt;/p&gt;
&lt;p&gt;
As result, our clients get XLSX files according with Open XML specifications. The
details of implementations of our solution see in our next posts.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=98b58257-9069-4d13-9d10-a7061e14e834" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,98b58257-9069-4d13-9d10-a7061e14e834.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>Thinking aloud</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=cad54317-1876-4b98-a8d2-7e44e5dda1f7</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,cad54317-1876-4b98-a8d2-7e44e5dda1f7.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,cad54317-1876-4b98-a8d2-7e44e5dda1f7.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=cad54317-1876-4b98-a8d2-7e44e5dda1f7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Earlier we have shown <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f07%2f22%2fStreamingEntityData.aspx"> how
to build streaming xml reader from business data</a> and have reminded about <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f07%2f26%2fStreamingXsltTransformationWithForwardXPathNavigator.aspx">ForwardXPathNavigator
which helps to create a streaming xslt transformation</a>. Now we want to show how
to stream content produced with xslt out of WCF service.
</p>
        <p>
To achieve streaming in WCF one needs: 
</p>
        <p>
1. To configure service to use streaming. Description on how to do this can be found
in the internet. See web.config of the sample <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fStreaming.zip"> Streaming.zip</a> for
the details.
</p>
        <p>
2. Create a service with a method returning <code>Stream</code>:
</p>
        <p>
          <code>[ServiceContract(Namespace = "http://www.nesterovsky-bros.com")]<br />
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]<br />
public class Service<br />
{<br />
[OperationContract]<br />
[WebGet(RequestFormat = WebMessageFormat.Json)]<br />
public Stream GetPeopleHtml(int count, int seed)<br />
{<br />
...<br />
}<br />
}</code>
        </p>
        <p>
2. Return a <code>Stream</code> from xsl transformation. 
</p>
        <p>
Unfortunately (we mentioned it already), <code>XslCompiledTransform</code> generates
its output into <code>XmlWriter</code> (or into output <code>Stream</code>) rather
than exposes result as <code>XmlReader</code>, while WCF gets input stream and passes
it to a client. 
</p>
        <p>
We could generate xslt output into a file or a memory <code>Stream</code> and then
return that content as input <code>Stream</code>, but this will defeat a goal of streaming,
as client would have started to get data no earlier that the xslt completed its work.
What we need instead is a pipe that form xslt output <code>Stream</code> to an input <code>Stream</code> returned
from WCF.
</p>
        <p>
.NET implements pipe streams, so our task is trivial. We have defined a utility method
that creates an input <code>Stream</code> from a generator populating an output <code>Stream</code>:
</p>
        <p>
          <code> public static Stream GetPipedStream(Action&lt;Stream&gt; generator)<br />
{<br />
var output = new AnonymousPipeServerStream();<br />
var input = new AnonymousPipeClientStream(<br />
output.GetClientHandleAsString());<br /><br />
Task.Factory.StartNew(<br />
() =&gt;<br />
{<br />
using(output)<br />
{<br />
generator(output);<br />
output.WaitForPipeDrain();<br />
}<br />
},<br />
TaskCreationOptions.LongRunning);<br /><br />
return input;<br />
} </code>
        </p>
        <p>
We wrapped xsl transformation as such a generator:
</p>
        <p>
          <code>[OperationContract]<br />
[WebGet(RequestFormat = WebMessageFormat.Json)]<br />
public Stream GetPeopleHtml(int count, int seed)<br />
{<br />
var context = WebOperationContext.Current;<br /><br />
context.OutgoingResponse.ContentType = "text/html";<br />
context.OutgoingResponse.Headers["Content-Disposition"] = 
<br />
"attachment;filename=reports.html";<br /><br />
var cache = HttpRuntime.Cache;<br />
var path = HttpContext.Current.Server.MapPath("~/People.xslt");<br />
var transform = cache[path] as XslCompiledTransform;<br /><br />
if (transform == null)<br />
{<br />
transform = new XslCompiledTransform();<br />
transform.Load(path);<br />
cache.Insert(path, transform, new CacheDependency(path));<br />
}<br /><br />
return Extensions.<strong>GetPipedStream</strong>(<br />
output =&gt;<br />
{<br />
// We have a streamed business data.<br />
var people = Data.CreateRandomData(count, seed, 0, count);<br /><br />
// We want to see it as streamed xml data.<br />
using(var stream =<br />
people.<strong>ToXmlStream</strong>("people", "http://www.nesterovsky-bros.com"))<br />
using(var reader = XmlReader.Create(stream))<br />
{<br />
// XPath forward navigator is used as an input source.<br />
transform.Transform(<br />
new <strong>ForwardXPathNavigator</strong>(reader),<br />
new XsltArgumentList(),<br />
output);<br />
}<br />
});<br />
}</code>
        </p>
        <p>
This way we have build a code that streams data directly from business data to a client
in a form of report. A set of utility functions and classes helped us to overcome
.NET's limitations and to build simple code that one can easily support.
</p>
        <p>
The sources can be found at <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fStreaming.zip"> Streaming.zip</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7" />
      </body>
      <title>Stream xslt transformation through WCF</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,cad54317-1876-4b98-a8d2-7e44e5dda1f7.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/08/03/StreamXsltTransformationThroughWCF.aspx</link>
      <pubDate>Fri, 03 Aug 2012 22:32:49 GMT</pubDate>
      <description>  &lt;p&gt;
Earlier we have shown &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f07%2f22%2fStreamingEntityData.aspx"&gt; how
to build streaming xml reader from business data&lt;/a&gt; and have reminded about &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f07%2f26%2fStreamingXsltTransformationWithForwardXPathNavigator.aspx"&gt;ForwardXPathNavigator
which helps to create a streaming xslt transformation&lt;/a&gt;. Now we want to show how
to stream content produced with xslt out of WCF service.
&lt;/p&gt;
&lt;p&gt;
To achieve streaming in WCF one needs: 
&lt;/p&gt;
&lt;p&gt;
1. To configure service to use streaming. Description on how to do this can be found
in the internet. See web.config of the sample &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fStreaming.zip"&gt; Streaming.zip&lt;/a&gt; for
the details.
&lt;/p&gt;
&lt;p&gt;
2. Create a service with a method returning &lt;code&gt;Stream&lt;/code&gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;[ServiceContract(Namespace = "http://www.nesterovsky-bros.com")]&lt;br /&gt;
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]&lt;br /&gt;
public class Service&lt;br /&gt;
{&lt;br /&gt;
[OperationContract]&lt;br /&gt;
[WebGet(RequestFormat = WebMessageFormat.Json)]&lt;br /&gt;
public Stream GetPeopleHtml(int count, int seed)&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
2. Return a &lt;code&gt;Stream&lt;/code&gt; from xsl transformation. 
&lt;/p&gt;
&lt;p&gt;
Unfortunately (we mentioned it already), &lt;code&gt;XslCompiledTransform&lt;/code&gt; generates
its output into &lt;code&gt;XmlWriter&lt;/code&gt; (or into output &lt;code&gt;Stream&lt;/code&gt;) rather
than exposes result as &lt;code&gt;XmlReader&lt;/code&gt;, while WCF gets input stream and passes
it to a client. 
&lt;/p&gt;
&lt;p&gt;
We could generate xslt output into a file or a memory &lt;code&gt;Stream&lt;/code&gt; and then
return that content as input &lt;code&gt;Stream&lt;/code&gt;, but this will defeat a goal of streaming,
as client would have started to get data no earlier that the xslt completed its work.
What we need instead is a pipe that form xslt output &lt;code&gt;Stream&lt;/code&gt; to an input &lt;code&gt;Stream&lt;/code&gt; returned
from WCF.
&lt;/p&gt;
&lt;p&gt;
.NET implements pipe streams, so our task is trivial. We have defined a utility method
that creates an input &lt;code&gt;Stream&lt;/code&gt; from a generator populating an output &lt;code&gt;Stream&lt;/code&gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; public static Stream GetPipedStream(Action&amp;lt;Stream&amp;gt; generator)&lt;br /&gt;
{&lt;br /&gt;
var output = new AnonymousPipeServerStream();&lt;br /&gt;
var input = new AnonymousPipeClientStream(&lt;br /&gt;
output.GetClientHandleAsString());&lt;br /&gt;
&lt;br /&gt;
Task.Factory.StartNew(&lt;br /&gt;
() =&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
using(output)&lt;br /&gt;
{&lt;br /&gt;
generator(output);&lt;br /&gt;
output.WaitForPipeDrain();&lt;br /&gt;
}&lt;br /&gt;
},&lt;br /&gt;
TaskCreationOptions.LongRunning);&lt;br /&gt;
&lt;br /&gt;
return input;&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
We wrapped xsl transformation as such a generator:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;[OperationContract]&lt;br /&gt;
[WebGet(RequestFormat = WebMessageFormat.Json)]&lt;br /&gt;
public Stream GetPeopleHtml(int count, int seed)&lt;br /&gt;
{&lt;br /&gt;
var context = WebOperationContext.Current;&lt;br /&gt;
&lt;br /&gt;
context.OutgoingResponse.ContentType = "text/html";&lt;br /&gt;
context.OutgoingResponse.Headers["Content-Disposition"] = 
&lt;br /&gt;
"attachment;filename=reports.html";&lt;br /&gt;
&lt;br /&gt;
var cache = HttpRuntime.Cache;&lt;br /&gt;
var path = HttpContext.Current.Server.MapPath("~/People.xslt");&lt;br /&gt;
var transform = cache[path] as XslCompiledTransform;&lt;br /&gt;
&lt;br /&gt;
if (transform == null)&lt;br /&gt;
{&lt;br /&gt;
transform = new XslCompiledTransform();&lt;br /&gt;
transform.Load(path);&lt;br /&gt;
cache.Insert(path, transform, new CacheDependency(path));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return Extensions.&lt;strong&gt;GetPipedStream&lt;/strong&gt;(&lt;br /&gt;
output =&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
// We have a streamed business data.&lt;br /&gt;
var people = Data.CreateRandomData(count, seed, 0, count);&lt;br /&gt;
&lt;br /&gt;
// We want to see it as streamed xml data.&lt;br /&gt;
using(var stream =&lt;br /&gt;
people.&lt;strong&gt;ToXmlStream&lt;/strong&gt;("people", "http://www.nesterovsky-bros.com"))&lt;br /&gt;
using(var reader = XmlReader.Create(stream))&lt;br /&gt;
{&lt;br /&gt;
// XPath forward navigator is used as an input source.&lt;br /&gt;
transform.Transform(&lt;br /&gt;
new &lt;strong&gt;ForwardXPathNavigator&lt;/strong&gt;(reader),&lt;br /&gt;
new XsltArgumentList(),&lt;br /&gt;
output);&lt;br /&gt;
}&lt;br /&gt;
});&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
This way we have build a code that streams data directly from business data to a client
in a form of report. A set of utility functions and classes helped us to overcome
.NET&amp;#39;s limitations and to build simple code that one can easily support.
&lt;/p&gt;
&lt;p&gt;
The sources can be found at &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fStreaming.zip"&gt; Streaming.zip&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,cad54317-1876-4b98-a8d2-7e44e5dda1f7.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=24094700-ca7e-4e73-8b30-fbdc9d72072a</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,24094700-ca7e-4e73-8b30-fbdc9d72072a.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,24094700-ca7e-4e73-8b30-fbdc9d72072a.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=24094700-ca7e-4e73-8b30-fbdc9d72072a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the previous <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f07%2f22%2fStreamingEntityData.aspx"> post
about streaming</a> we have dropped at the point where we have <code>XmlReader</code> in
hands, which continously gets data from <code>IEnumerable&lt;Person&gt;</code> source.
Now we shall remind about <code>ForwardXPathNavigator</code> - a class we have built
back in 2002, which adds streaming transformations to .NET's xslt processor.
</p>
        <p>
While <code>XslCompiledTransform</code> is desperately obsolete, and no upgrade will
possibly follow; still it's among the fastest xslt 1.0 processors. With <code> ForwardXPathNavigator</code> we
add ability to transform input data of arbitrary size to this processor.
</p>
        <p>
We find it interesting that <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a&amp;url=http%3a%2f%2fwww.w3.org%2fTR%2fxslt-30%2f%23streaming-concepts"> xslt
3.0 Working Draft defines streaming processing</a> in a way that closely matches rules
for <code>ForwardXPathNavigator</code>:
</p>
        <div style="padding-left: 1em; font-style: italic">
          <p>
Streaming achieves two important objectives: it allows large documents to be transformed
without requiring correspondingly large amounts of memory; and it allows the processor
to start producing output before it has finished receiving its input, thus reducing
latency.
</p>
          <p>
The rules for streamability, which are defined in detail in <i>19.3 Streamability
Analysis</i>, impose two main constraints:
</p>
          <ul>
            <li>
              <p>
The only nodes reachable from the node that is currently being processed are its attributes
and namespaces, its ancestors and their attributes and namespaces, and its descendants
and their attributes and namespaces. The siblings of the node, and the siblings of
its ancestors, are not reachable in the tree, and any attempt to use their values
is a static error. However, constructs (for example, simple forms of <code>xsl:number</code>,
and simple positional patterns) that require knowledge of the number of preceding
elements by name are permitted.
</p>
            </li>
            <li>
              <p>
When processing a given node in the tree, each descendant node can only be visited
once. Essentially this allows two styles of processing: either visit each of the children
once, and then process that child with the same restrictions applied; or process all
the descendants in a single pass, in which case it is not possible while processing
a descendant to make any further downward selection.
</p>
            </li>
          </ul>
        </div>
        <p>
The only significant difference between <code>ForwardXPathNavigator</code> and xlst
3.0 streaming is in that we reported violations of rules for streamability at runtime,
while xslt 3.0 attempts to perform this analysis at compile time.
</p>
        <p>
Here the C# code for the xslt streamed transformation:
</p>
        <p>
          <code> var transform = new XslCompiledTransform();<br /><br />
transform.Load("People.xslt");<br /><br />
// We have a streamed business data.<br />
var people = Data.CreateRandomData(10000, 0, 0, 10000);<br /><br />
// We want to see it as streamed xml data.<br />
using(var stream =<br />
people.ToXmlStream("people", "http://www.nesterovsky-bros.com"))<br />
using(var reader = XmlReader.Create(stream))<br />
using(var output = File.Create("people.html"))<br />
{<br />
// XPath forward navigator is used as an input source.<br />
transform.Transform(<br /><strong>new ForwardXPathNavigator(reader)</strong>,<br />
new XsltArgumentList(),<br />
output);<br />
} </code>
        </p>
        <p>
Notice how <code>XmlReader</code> is wrapped into <code>ForwardXPathNavigator</code>.
</p>
        <p>
To complete the picture we need xslt that follows the streaming rules:
</p>
        <p>
          <code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a&amp;url=http%3a%2f%2fwww.w3.org%2f1999%2fXSL%2fTransform">http://www.w3.org/1999/XSL/Transform</a>"<br />
xmlns:msxsl="urn:schemas-microsoft-com:xslt"<br />
xmlns:d="<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com">http://www.nesterovsky-bros.com</a>"<br />
exclude-result-prefixes="msxsl d"&gt;<br /><br />
&lt;xsl:output method="html" indent="yes"/&gt;<br /><br />
&lt;!-- Root template processed in the streaming mode. --&gt;<br />
&lt;xsl:template match="/d:people"&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;List of persons&lt;/title&gt;<br />
&lt;style type="text/css"&gt;<br />
.even<br />
{<br />
}<br /><br />
.odd<br />
{<br />
background: #d0d0d0;<br />
}<br />
&lt;/style&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;table border="1"&gt;<br />
&lt;tr&gt;<br />
&lt;th&gt;ID&lt;/th&gt;<br />
&lt;th&gt;First name&lt;/th&gt;<br />
&lt;th&gt;Last name&lt;/th&gt;<br />
&lt;th&gt;City&lt;/th&gt;<br />
&lt;th&gt;Title&lt;/th&gt;<br />
&lt;th&gt;Age&lt;/th&gt;<br />
&lt;/tr&gt;<br /><br />
&lt;xsl:for-each select="d:person"&gt;<br />
&lt;!-- 
<br />
Get element snapshot.<br />
A snapshot allows arbitrary access to the element's content.<br />
--&gt;<br />
&lt;xsl:variable name="person"&gt;<br />
&lt;xsl:copy-of select="."/&gt;<br />
&lt;/xsl:variable&gt;<br /><br />
&lt;xsl:variable name="position" select="position()"/&gt;<br /><br />
&lt;xsl:apply-templates mode="snapshot" select="msxsl:node-set($person)/d:person"&gt;<br />
&lt;xsl:with-param name="position" select="$position"/&gt;<br />
&lt;/xsl:apply-templates&gt;<br />
&lt;/xsl:for-each&gt;<br />
&lt;/table&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
&lt;/xsl:template&gt;<br /><br />
&lt;xsl:template mode="snapshot" match="d:person"&gt;<br />
&lt;xsl:param name="position"/&gt;<br /><br />
&lt;tr&gt;<br />
&lt;xsl:attribute name="class"&gt;<br />
&lt;xsl:choose&gt;<br />
&lt;xsl:when test="$position mod 2 = 1"&gt;<br />
&lt;xsl:text&gt;odd&lt;/xsl:text&gt;<br />
&lt;/xsl:when&gt;<br />
&lt;xsl:otherwise&gt;<br />
&lt;xsl:text&gt;even&lt;/xsl:text&gt;<br />
&lt;/xsl:otherwise&gt;<br />
&lt;/xsl:choose&gt;<br />
&lt;/xsl:attribute&gt;<br /><br />
&lt;td&gt;<br />
&lt;xsl:value-of select="d:Id"/&gt;<br />
&lt;/td&gt;<br />
&lt;td&gt;<br />
&lt;xsl:value-of select="d:FirstName"/&gt;<br />
&lt;/td&gt;<br />
&lt;td&gt;<br />
&lt;xsl:value-of select="d:LastName"/&gt;<br />
&lt;/td&gt;<br />
&lt;td&gt;<br />
&lt;xsl:value-of select="d:City"/&gt;<br />
&lt;/td&gt;<br />
&lt;td&gt;<br />
&lt;xsl:value-of select="d:Title"/&gt;<br />
&lt;/td&gt;<br />
&lt;td&gt;<br />
&lt;xsl:value-of select="d:Age"/&gt;<br />
&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/xsl:template&gt;<br /><br />
&lt;/xsl:stylesheet&gt;</code>
        </p>
        <p>
So, we have started with a streamed entity data, proceeded to the streamed XmlReader
and reached to the streamed xslt transformation.
</p>
        <p>
But at the final post about streaming we shall remind a simple way of building WCF
service returning html stream from our xslt transformation.
</p>
        <p>
The sources can be found at <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fStreaming.zip"> Streaming.zip</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a" />
      </body>
      <title>Streaming xslt transformation with ForwardXPathNavigator</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,24094700-ca7e-4e73-8b30-fbdc9d72072a.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/07/26/StreamingXsltTransformationWithForwardXPathNavigator.aspx</link>
      <pubDate>Thu, 26 Jul 2012 18:49:51 GMT</pubDate>
      <description>&lt;p&gt;
In the previous &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f07%2f22%2fStreamingEntityData.aspx"&gt; post
about streaming&lt;/a&gt; we have dropped at the point where we have &lt;code&gt;XmlReader&lt;/code&gt; in
hands, which continously gets data from &lt;code&gt;IEnumerable&amp;lt;Person&amp;gt;&lt;/code&gt; source.
Now we shall remind about &lt;code&gt;ForwardXPathNavigator&lt;/code&gt; - a class we have built
back in 2002, which adds streaming transformations to .NET&amp;#39;s xslt processor.
&lt;/p&gt;
&lt;p&gt;
While &lt;code&gt;XslCompiledTransform&lt;/code&gt; is desperately obsolete, and no upgrade will
possibly follow; still it&amp;#39;s among the fastest xslt 1.0 processors. With &lt;code&gt; ForwardXPathNavigator&lt;/code&gt; we
add ability to transform input data of arbitrary size to this processor.
&lt;/p&gt;
&lt;p&gt;
We find it interesting that &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a&amp;amp;url=http%3a%2f%2fwww.w3.org%2fTR%2fxslt-30%2f%23streaming-concepts"&gt; xslt
3.0 Working Draft defines streaming processing&lt;/a&gt; in a way that closely matches rules
for &lt;code&gt;ForwardXPathNavigator&lt;/code&gt;:
&lt;/p&gt;
&lt;div style="padding-left: 1em; font-style: italic"&gt;
&lt;p&gt;
Streaming achieves two important objectives: it allows large documents to be transformed
without requiring correspondingly large amounts of memory; and it allows the processor
to start producing output before it has finished receiving its input, thus reducing
latency.
&lt;/p&gt;
&lt;p&gt;
The rules for streamability, which are defined in detail in &lt;i&gt;19.3 Streamability
Analysis&lt;/i&gt;, impose two main constraints:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;
The only nodes reachable from the node that is currently being processed are its attributes
and namespaces, its ancestors and their attributes and namespaces, and its descendants
and their attributes and namespaces. The siblings of the node, and the siblings of
its ancestors, are not reachable in the tree, and any attempt to use their values
is a static error. However, constructs (for example, simple forms of &lt;code&gt;xsl:number&lt;/code&gt;,
and simple positional patterns) that require knowledge of the number of preceding
elements by name are permitted.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
When processing a given node in the tree, each descendant node can only be visited
once. Essentially this allows two styles of processing: either visit each of the children
once, and then process that child with the same restrictions applied; or process all
the descendants in a single pass, in which case it is not possible while processing
a descendant to make any further downward selection.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;
The only significant difference between &lt;code&gt;ForwardXPathNavigator&lt;/code&gt; and xlst
3.0 streaming is in that we reported violations of rules for streamability at runtime,
while xslt 3.0 attempts to perform this analysis at compile time.
&lt;/p&gt;
&lt;p&gt;
Here the C# code for the xslt streamed transformation:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; var transform = new XslCompiledTransform();&lt;br /&gt;
&lt;br /&gt;
transform.Load("People.xslt");&lt;br /&gt;
&lt;br /&gt;
// We have a streamed business data.&lt;br /&gt;
var people = Data.CreateRandomData(10000, 0, 0, 10000);&lt;br /&gt;
&lt;br /&gt;
// We want to see it as streamed xml data.&lt;br /&gt;
using(var stream =&lt;br /&gt;
people.ToXmlStream("people", "http://www.nesterovsky-bros.com"))&lt;br /&gt;
using(var reader = XmlReader.Create(stream))&lt;br /&gt;
using(var output = File.Create("people.html"))&lt;br /&gt;
{&lt;br /&gt;
// XPath forward navigator is used as an input source.&lt;br /&gt;
transform.Transform(&lt;br /&gt;
&lt;strong&gt;new ForwardXPathNavigator(reader)&lt;/strong&gt;,&lt;br /&gt;
new XsltArgumentList(),&lt;br /&gt;
output);&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Notice how &lt;code&gt;XmlReader&lt;/code&gt; is wrapped into &lt;code&gt;ForwardXPathNavigator&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
To complete the picture we need xslt that follows the streaming rules:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;&amp;lt;xsl:stylesheet version="1.0" xmlns:xsl="&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a&amp;amp;url=http%3a%2f%2fwww.w3.org%2f1999%2fXSL%2fTransform"&gt;http://www.w3.org/1999/XSL/Transform&lt;/a&gt;"&lt;br /&gt;
xmlns:msxsl="urn:schemas-microsoft-com:xslt"&lt;br /&gt;
xmlns:d="&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com"&gt;http://www.nesterovsky-bros.com&lt;/a&gt;"&lt;br /&gt;
exclude-result-prefixes="msxsl d"&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsl:output method="html" indent="yes"/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Root template processed in the streaming mode. --&amp;gt;&lt;br /&gt;
&amp;lt;xsl:template match="/d:people"&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;List of persons&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;style type="text/css"&amp;gt;&lt;br /&gt;
.even&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
.odd&lt;br /&gt;
{&lt;br /&gt;
background: #d0d0d0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/style&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;table border="1"&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;ID&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;First name&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Last name&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;City&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Title&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Age&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsl:for-each select="d:person"&amp;gt;&lt;br /&gt;
&amp;lt;!-- 
&lt;br /&gt;
Get element snapshot.&lt;br /&gt;
A snapshot allows arbitrary access to the element&amp;#39;s content.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;xsl:variable name="person"&amp;gt;&lt;br /&gt;
&amp;lt;xsl:copy-of select="."/&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:variable&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsl:variable name="position" select="position()"/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsl:apply-templates mode="snapshot" select="msxsl:node-set($person)/d:person"&amp;gt;&lt;br /&gt;
&amp;lt;xsl:with-param name="position" select="$position"/&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:apply-templates&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:for-each&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:template&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsl:template mode="snapshot" match="d:person"&amp;gt;&lt;br /&gt;
&amp;lt;xsl:param name="position"/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;xsl:attribute name="class"&amp;gt;&lt;br /&gt;
&amp;lt;xsl:choose&amp;gt;&lt;br /&gt;
&amp;lt;xsl:when test="$position mod 2 = 1"&amp;gt;&lt;br /&gt;
&amp;lt;xsl:text&amp;gt;odd&amp;lt;/xsl:text&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:when&amp;gt;&lt;br /&gt;
&amp;lt;xsl:otherwise&amp;gt;&lt;br /&gt;
&amp;lt;xsl:text&amp;gt;even&amp;lt;/xsl:text&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:otherwise&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:choose&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:attribute&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:Id"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:FirstName"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:LastName"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:City"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:Title"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:Age"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:template&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsl:stylesheet&amp;gt;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
So, we have started with a streamed entity data, proceeded to the streamed XmlReader
and reached to the streamed xslt transformation.
&lt;/p&gt;
&lt;p&gt;
But at the final post about streaming we shall remind a simple way of building WCF
service returning html stream from our xslt transformation.
&lt;/p&gt;
&lt;p&gt;
The sources can be found at &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fStreaming.zip"&gt; Streaming.zip&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,24094700-ca7e-4e73-8b30-fbdc9d72072a.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=382cb645-ac89-4cbb-b2f9-14913fb1f743</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,382cb645-ac89-4cbb-b2f9-14913fb1f743.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,382cb645-ac89-4cbb-b2f9-14913fb1f743.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=382cb645-ac89-4cbb-b2f9-14913fb1f743</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you're using .NET's <code>IDictionary&lt;K, V&gt;</code> you have probably
found its access API too boring. Indeed at each access point you have to write a code
like this:
</p>
        <p>
          <code> MyValueType value;<br />
var hasValue = dictionary.TryGetValue(key, out value);<br />
...</code>
        </p>
        <p>
In many, if not in most, cases the value is of a reference type, and you do not usually
store <code>null</code> values, so it would be fine if dictionary returned <code>null</code> when
value does not exist for the key.
</p>
        <p>
To deal with this small nuisance we have declared a couple of accessor extension methods:
</p>
        <p>
          <code>public static class Extensions<br />
{<br />
public static V Get&lt;K, V&gt;(this IDictionary&lt;K, V&gt; dictionary, K key)<br />
where V: class<br />
{<br />
V value;<br /><br />
if (key == null)<br />
{<br />
value = null;<br />
}<br />
else<br />
{<br />
dictionary.TryGetValue(key, out value);<br />
}<br /><br />
return value;<br />
}<br /><br />
public static V Get&lt;K, V&gt;(this IDictionary&lt;K, V&gt; dictionary, K? key)<br />
where V: class<br />
where K: struct<br />
{<br />
V value;<br /><br />
if (key == null)<br />
{<br />
value = null;<br />
}<br />
else<br />
{<br />
dictionary.TryGetValue(key.GetValueOrDefault(), out value);<br />
}<br /><br />
return value;<br />
}<br />
}</code>
        </p>
        <p>
These methods simplify dictionary access to:
</p>
        <p>
          <code>var value = dictionary.Get(key);<br />
...</code>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=382cb645-ac89-4cbb-b2f9-14913fb1f743" />
      </body>
      <title>Dictionary extension</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,382cb645-ac89-4cbb-b2f9-14913fb1f743.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/07/25/DictionaryExtension.aspx</link>
      <pubDate>Wed, 25 Jul 2012 05:54:33 GMT</pubDate>
      <description>  &lt;p&gt;
If you&amp;#39;re using .NET&amp;#39;s &lt;code&gt;IDictionary&amp;lt;K, V&amp;gt;&lt;/code&gt; you have probably
found its access API too boring. Indeed at each access point you have to write a code
like this:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; MyValueType value;&lt;br /&gt;
var hasValue = dictionary.TryGetValue(key, out value);&lt;br /&gt;
...&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
In many, if not in most, cases the value is of a reference type, and you do not usually
store &lt;code&gt;null&lt;/code&gt; values, so it would be fine if dictionary returned &lt;code&gt;null&lt;/code&gt; when
value does not exist for the key.
&lt;/p&gt;
&lt;p&gt;
To deal with this small nuisance we have declared a couple of accessor extension methods:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;public static class Extensions&lt;br /&gt;
{&lt;br /&gt;
public static V Get&amp;lt;K, V&amp;gt;(this IDictionary&amp;lt;K, V&amp;gt; dictionary, K key)&lt;br /&gt;
where V: class&lt;br /&gt;
{&lt;br /&gt;
V value;&lt;br /&gt;
&lt;br /&gt;
if (key == null)&lt;br /&gt;
{&lt;br /&gt;
value = null;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
dictionary.TryGetValue(key, out value);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return value;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static V Get&amp;lt;K, V&amp;gt;(this IDictionary&amp;lt;K, V&amp;gt; dictionary, K? key)&lt;br /&gt;
where V: class&lt;br /&gt;
where K: struct&lt;br /&gt;
{&lt;br /&gt;
V value;&lt;br /&gt;
&lt;br /&gt;
if (key == null)&lt;br /&gt;
{&lt;br /&gt;
value = null;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
dictionary.TryGetValue(key.GetValueOrDefault(), out value);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return value;&lt;br /&gt;
}&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
These methods simplify dictionary access to:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;var value = dictionary.Get(key);&lt;br /&gt;
...&lt;/code&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=382cb645-ac89-4cbb-b2f9-14913fb1f743" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,382cb645-ac89-4cbb-b2f9-14913fb1f743.aspx</comments>
      <category>.NET</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=60b3cb37-5da9-4f28-a35f-38d1ef3cccce</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,60b3cb37-5da9-4f28-a35f-38d1ef3cccce.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,60b3cb37-5da9-4f28-a35f-38d1ef3cccce.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=60b3cb37-5da9-4f28-a35f-38d1ef3cccce</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
For some reason neither .NET's <code>XmlSerializer</code> nor <code>DataContractSerializer</code> allow
reading data through an <code>XmlReader</code>. These APIs work other way round writing
data into an <code>XmlWriter</code>. To get data through <code>XmlReader</code> one
needs to write it to some destination like a file or memory stream, and then to read
it using <code>XmlReader</code>. This complicates streaming design considerably.
</p>
        <p>
In fact the very same happens with other .NET APIs.
</p>
        <p>
We think the reason of why .NET designers preferred <code>XmlWriter</code> to <code>XmlReader</code> in
those APIs is that <code>XmlReader</code>'s implementation is a state machine
like, while <code>XmlWriter</code>'s implementation looks like a regular procedure.
It's much harder to <strong>manually</strong> write and to support a correct state
machine logic than a procedure.
</p>
        <p>
If history would have gone slightly different way, and if yield return, lambda, and
Enumerator API appeared before <code>XmlReader</code>, and <code>XmlWriter</code> then,
we think, both these classes looked differently. Xml source would have been described
with a <code>IEnumerable&lt;XmlEvent&gt;</code> instead of <code>XmlReader</code>,
and <code>XmlWriter</code> must be looked like a function receiving <code>IEnumerable&lt;XmlEvent&gt;</code>.
Implementing <code>XmlReader</code> would have meant a creating a enumerator. Yield
return and Enumerable API would have helped to implement it in a procedural way.
</p>
        <p>
But in our present we have to deal with the fact that <code>DataContractSerializer</code> should
write the data into <code>XmlWriter</code>, so let's assume we have a project
that uses Entity Framework to access the database, and that you have a data class <code>Person</code>,
and data access method <code>GetPeople()</code>:
</p>
        <p>
          <code> [DataContract(Name = "person", Namespace = "http://www.nesterovsky-bros.com")]<br />
public class Person 
<br />
{ 
<br />
[DataMember] public int Id { get; set; } 
<br />
[DataMember] public string FirstName { get; set; } 
<br />
[DataMember] public string LastName { get; set; } 
<br />
[DataMember] public string City { get; set; } 
<br />
[DataMember] public string Title { get; set; } 
<br />
[DataMember] public DateTime BirthDate { get; set; } 
<br />
[DataMember] public int Age { get; set; } 
<br />
} 
<br /><br />
public static IEnumerable&lt;Person&gt; GetPeople() { ... }</code>
        </p>
        <p>
And your goal is to expose result of <code>GetPeople()</code> as <code>XmlReader</code>.
We achieve result with three simple steps:
</p>
        <ol>
          <li>
Define <code>JoinedStream</code> - an input <code>Stream</code> implementation that
reads data from a enumeration of streams (<code>IEnumerable&lt;Stream&gt;</code>).</li>
          <li>
Build xml parts in the form of <code>IEnumerable&lt;Stream&gt;</code>.</li>
          <li>
Combine parts into final xml stream.</li>
        </ol>
        <p>
The code is rather simple, so here we qoute its essential part:
</p>
        <p>
          <code> public static class Extensions<br />
{<br />
public static Stream JoinStreams(this IEnumerable&lt;Stream&gt; streams, bool closeStreams
= true)<br />
{<br />
return new JoinedStream(streams, closeStreams);<br />
}<br /><br />
public static Stream ToXmlStream&lt;T&gt;(<br />
this IEnumerable&lt;T&gt; items, 
<br />
string rootName = null, 
<br />
string rootNamespace = null)<br />
{<br />
return items.ToXmlStreamParts&lt;T&gt;(rootName, rootNamespace).<br />
JoinStreams(false);<br />
}<br /><br />
private static IEnumerable&lt;Stream&gt; ToXmlStreamParts&lt;T&gt;(<br />
this IEnumerable&lt;T&gt; items,<br />
string rootName = null,<br />
string rootNamespace = null)<br />
{<br />
if (rootName == null)<br />
{<br />
rootName = "ArrayOfItems";<br />
}<br /><br />
if (rootNamespace == null)<br />
{<br />
rootNamespace = "";<br />
}<br /><br />
var serializer = new DataContractSerializer(typeof(T));<br />
var stream = new MemoryStream();<br />
var writer = XmlDictionaryWriter.CreateTextWriter(stream);<br /><br />
writer.WriteStartDocument();<br />
writer.WriteStartElement(rootName, rootNamespace);<br />
writer.WriteXmlnsAttribute("s", XmlSchema.Namespace);<br />
writer.WriteXmlnsAttribute("i", XmlSchema.InstanceNamespace);<br /><br />
foreach(var item in items)<br />
{<br />
serializer.WriteObject(writer, item);<br />
writer.WriteString(" ");<br /><br />
writer.Flush();<br />
stream.Position = 0;<br /><br />
yield return stream;<br /><br />
stream.Position = 0;<br />
stream.SetLength(0);<br />
}<br /><br />
writer.WriteEndElement();<br />
writer.WriteEndDocument();<br /><br />
writer.Flush();<br />
stream.Position = 0;<br /><br />
yield return stream;<br />
}<br /><br />
private class JoinedStream: Stream<br />
{<br />
public JoinedStream(IEnumerable&lt;Stream&gt; streams, bool closeStreams = true)<br />
...<br />
}<br />
} </code>
        </p>
        <p>
The use is even more simple:
</p>
        <p>
          <code> // We have a streamed business data.<br />
var people = GetPeople();<br /><br />
// We want to see it as streamed xml data.<br />
using(var stream = people.ToXmlStream("persons", "http://www.nesterovsky-bros.com"))<br />
using(var reader = XmlReader.Create(stream))<br />
{<br />
...<br />
}</code>
        </p>
        <p>
We have packed the sample into the project <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=60b3cb37-5da9-4f28-a35f-38d1ef3cccce&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fStreaming.zip">Streaming.zip</a>.
</p>
        <p>
In the <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=60b3cb37-5da9-4f28-a35f-38d1ef3cccce&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f07%2f26%2fStreamingXsltTransformationWithForwardXPathNavigator.aspx">next
post</a> we're going to remind about streaming processing in xslt. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=60b3cb37-5da9-4f28-a35f-38d1ef3cccce" />
      </body>
      <title>Streaming entity data</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,60b3cb37-5da9-4f28-a35f-38d1ef3cccce.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/07/22/StreamingEntityData.aspx</link>
      <pubDate>Sun, 22 Jul 2012 20:38:29 GMT</pubDate>
      <description>  &lt;p&gt;
For some reason neither .NET&amp;#39;s &lt;code&gt;XmlSerializer&lt;/code&gt; nor &lt;code&gt;DataContractSerializer&lt;/code&gt; allow
reading data through an &lt;code&gt;XmlReader&lt;/code&gt;. These APIs work other way round writing
data into an &lt;code&gt;XmlWriter&lt;/code&gt;. To get data through &lt;code&gt;XmlReader&lt;/code&gt; one
needs to write it to some destination like a file or memory stream, and then to read
it using &lt;code&gt;XmlReader&lt;/code&gt;. This complicates streaming design considerably.
&lt;/p&gt;
&lt;p&gt;
In fact the very same happens with other .NET APIs.
&lt;/p&gt;
&lt;p&gt;
We think the reason of why .NET designers preferred &lt;code&gt;XmlWriter&lt;/code&gt; to &lt;code&gt;XmlReader&lt;/code&gt; in
those APIs is that &lt;code&gt;XmlReader&lt;/code&gt;&amp;#39;s implementation is a state machine
like, while &lt;code&gt;XmlWriter&lt;/code&gt;&amp;#39;s implementation looks like a regular procedure.
It&amp;#39;s much harder to &lt;strong&gt;manually&lt;/strong&gt; write and to support a correct state
machine logic than a procedure.
&lt;/p&gt;
&lt;p&gt;
If history would have gone slightly different way, and if yield return, lambda, and
Enumerator API appeared before &lt;code&gt;XmlReader&lt;/code&gt;, and &lt;code&gt;XmlWriter&lt;/code&gt; then,
we think, both these classes looked differently. Xml source would have been described
with a &lt;code&gt;IEnumerable&amp;lt;XmlEvent&amp;gt;&lt;/code&gt; instead of &lt;code&gt;XmlReader&lt;/code&gt;,
and &lt;code&gt;XmlWriter&lt;/code&gt; must be looked like a function receiving &lt;code&gt;IEnumerable&amp;lt;XmlEvent&amp;gt;&lt;/code&gt;.
Implementing &lt;code&gt;XmlReader&lt;/code&gt; would have meant a creating a enumerator. Yield
return and Enumerable API would have helped to implement it in a procedural way.
&lt;/p&gt;
&lt;p&gt;
But in our present we have to deal with the fact that &lt;code&gt;DataContractSerializer&lt;/code&gt; should
write the data into &lt;code&gt;XmlWriter&lt;/code&gt;, so let&amp;#39;s assume we have a project
that uses Entity Framework to access the database, and that you have a data class &lt;code&gt;Person&lt;/code&gt;,
and data access method &lt;code&gt;GetPeople()&lt;/code&gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; [DataContract(Name = &amp;quot;person&amp;quot;, Namespace = &amp;quot;http://www.nesterovsky-bros.com&amp;quot;)]&lt;br /&gt;
public class Person 
&lt;br /&gt;
{ 
&lt;br /&gt;
[DataMember] public int Id { get; set; } 
&lt;br /&gt;
[DataMember] public string FirstName { get; set; } 
&lt;br /&gt;
[DataMember] public string LastName { get; set; } 
&lt;br /&gt;
[DataMember] public string City { get; set; } 
&lt;br /&gt;
[DataMember] public string Title { get; set; } 
&lt;br /&gt;
[DataMember] public DateTime BirthDate { get; set; } 
&lt;br /&gt;
[DataMember] public int Age { get; set; } 
&lt;br /&gt;
} 
&lt;br /&gt;
&lt;br /&gt;
public static IEnumerable&amp;lt;Person&amp;gt; GetPeople() { ... }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
And your goal is to expose result of &lt;code&gt;GetPeople()&lt;/code&gt; as &lt;code&gt;XmlReader&lt;/code&gt;.
We achieve result with three simple steps:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Define &lt;code&gt;JoinedStream&lt;/code&gt; - an input &lt;code&gt;Stream&lt;/code&gt; implementation that
reads data from a enumeration of streams (&lt;code&gt;IEnumerable&amp;lt;Stream&amp;gt;&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
Build xml parts in the form of &lt;code&gt;IEnumerable&amp;lt;Stream&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
Combine parts into final xml stream.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The code is rather simple, so here we qoute its essential part:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; public static class Extensions&lt;br /&gt;
{&lt;br /&gt;
public static Stream JoinStreams(this IEnumerable&amp;lt;Stream&amp;gt; streams, bool closeStreams
= true)&lt;br /&gt;
{&lt;br /&gt;
return new JoinedStream(streams, closeStreams);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static Stream ToXmlStream&amp;lt;T&amp;gt;(&lt;br /&gt;
this IEnumerable&amp;lt;T&amp;gt; items, 
&lt;br /&gt;
string rootName = null, 
&lt;br /&gt;
string rootNamespace = null)&lt;br /&gt;
{&lt;br /&gt;
return items.ToXmlStreamParts&amp;lt;T&amp;gt;(rootName, rootNamespace).&lt;br /&gt;
JoinStreams(false);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
private static IEnumerable&amp;lt;Stream&amp;gt; ToXmlStreamParts&amp;lt;T&amp;gt;(&lt;br /&gt;
this IEnumerable&amp;lt;T&amp;gt; items,&lt;br /&gt;
string rootName = null,&lt;br /&gt;
string rootNamespace = null)&lt;br /&gt;
{&lt;br /&gt;
if (rootName == null)&lt;br /&gt;
{&lt;br /&gt;
rootName = "ArrayOfItems";&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (rootNamespace == null)&lt;br /&gt;
{&lt;br /&gt;
rootNamespace = "";&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var serializer = new DataContractSerializer(typeof(T));&lt;br /&gt;
var stream = new MemoryStream();&lt;br /&gt;
var writer = XmlDictionaryWriter.CreateTextWriter(stream);&lt;br /&gt;
&lt;br /&gt;
writer.WriteStartDocument();&lt;br /&gt;
writer.WriteStartElement(rootName, rootNamespace);&lt;br /&gt;
writer.WriteXmlnsAttribute("s", XmlSchema.Namespace);&lt;br /&gt;
writer.WriteXmlnsAttribute("i", XmlSchema.InstanceNamespace);&lt;br /&gt;
&lt;br /&gt;
foreach(var item in items)&lt;br /&gt;
{&lt;br /&gt;
serializer.WriteObject(writer, item);&lt;br /&gt;
writer.WriteString(" ");&lt;br /&gt;
&lt;br /&gt;
writer.Flush();&lt;br /&gt;
stream.Position = 0;&lt;br /&gt;
&lt;br /&gt;
yield return stream;&lt;br /&gt;
&lt;br /&gt;
stream.Position = 0;&lt;br /&gt;
stream.SetLength(0);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
writer.WriteEndElement();&lt;br /&gt;
writer.WriteEndDocument();&lt;br /&gt;
&lt;br /&gt;
writer.Flush();&lt;br /&gt;
stream.Position = 0;&lt;br /&gt;
&lt;br /&gt;
yield return stream;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
private class JoinedStream: Stream&lt;br /&gt;
{&lt;br /&gt;
public JoinedStream(IEnumerable&amp;lt;Stream&amp;gt; streams, bool closeStreams = true)&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The use is even more simple:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; // We have a streamed business data.&lt;br /&gt;
var people = GetPeople();&lt;br /&gt;
&lt;br /&gt;
// We want to see it as streamed xml data.&lt;br /&gt;
using(var stream = people.ToXmlStream("persons", "http://www.nesterovsky-bros.com"))&lt;br /&gt;
using(var reader = XmlReader.Create(stream))&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
We have packed the sample into the project &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=60b3cb37-5da9-4f28-a35f-38d1ef3cccce&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fStreaming.zip"&gt;Streaming.zip&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
In the &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=60b3cb37-5da9-4f28-a35f-38d1ef3cccce&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f07%2f26%2fStreamingXsltTransformationWithForwardXPathNavigator.aspx"&gt;next
post&lt;/a&gt; we&amp;#39;re going to remind about streaming processing in xslt. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=60b3cb37-5da9-4f28-a35f-38d1ef3cccce" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,60b3cb37-5da9-4f28-a35f-38d1ef3cccce.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=f6004aca-9fa1-4c23-b987-9de640d86baf</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,f6004aca-9fa1-4c23-b987-9de640d86baf.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,f6004aca-9fa1-4c23-b987-9de640d86baf.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=f6004aca-9fa1-4c23-b987-9de640d86baf</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Several days ago we've arrived to the blog "<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f6004aca-9fa1-4c23-b987-9de640d86baf&amp;url=http%3a%2f%2fblogs.msdn.com%2fb%2fmadst%2farchive%2f2007%2f05%2f11%2frecursive-lambda-expressions.aspx">Recursive
lambda expressions</a>". There, author asks how to write a lambda expression
that calculates a factorial (only expression statements are allowed).
</p>
        <p>
The problem by itself is rather artificial, but at times you feel an intellectual
pleasure solving such tasks by yourself. So, putting original blog post aside we devised
our answers. The shortest one goes like this:
</p>
        <ol>
          <li>
As C# lambda expression cannot refer to itself, so it have to receive itself as a
parameter, so:<br /><div style="padding: 1em"><code>factorial(factorial, n) = n &lt;= 1 ? 1 : n * factorial(factorial, n - 1);</code></div></li>
          <li>
To define such lambda expression we have to declare a delegate type that receives
a delegate of the same type:<br /><div style="padding: 1em"><code>delegate int Impl(Impl impl, int n);</code></div>
Fortunately, C# allows this, but a workaround could be used even if it were not possible.<br /><br /></li>
          <li>
To simplify the reasoning we've defined a two-expression version:<br /><div style="padding: 1em"><code>Impl impl = (f, n) =&gt; n &lt;= 1 ? 1 : n * f(f, n - 1); 
<br />
Func&lt;int, int&gt; factorial = i =&gt; impl(impl, i);</code></div></li>
          <li>
Finally, we've written out a one-expression version:<br /><div style="padding: 1em"><code>Func&lt;int, int&gt; factorial = i =&gt; ((Func&lt;Impl, int&gt;)(f =&gt; f(f,
i)))((f, n) =&gt; n &lt;= 1 ? 1 : n * f(f, n - 1)); </code></div></li>
          <li>
The use is:<br /><div style="padding: 1em"><code>var f = factorial(10);</code></div></li>
        </ol>
        <p>
After that excercise we've returned back to original blog and compared solutions.
We can see that author appeals to a set theory but for some reason his answer is more
complex than nesessary, but comments contain variants that analogous to our answer.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=f6004aca-9fa1-4c23-b987-9de640d86baf" />
      </body>
      <title>Weekend C# puzzle</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,f6004aca-9fa1-4c23-b987-9de640d86baf.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/04/08/WeekendCPuzzle.aspx</link>
      <pubDate>Sun, 08 Apr 2012 09:18:29 GMT</pubDate>
      <description>  &lt;p&gt;
Several days ago we&amp;#39;ve arrived to the blog &amp;quot;&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f6004aca-9fa1-4c23-b987-9de640d86baf&amp;amp;url=http%3a%2f%2fblogs.msdn.com%2fb%2fmadst%2farchive%2f2007%2f05%2f11%2frecursive-lambda-expressions.aspx"&gt;Recursive
lambda expressions&lt;/a&gt;&amp;quot;. There, author asks how to write a lambda expression
that calculates a factorial (only expression statements are allowed).
&lt;/p&gt;
&lt;p&gt;
The problem by itself is rather artificial, but at times you feel an intellectual
pleasure solving such tasks by yourself. So, putting original blog post aside we devised
our answers. The shortest one goes like this:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
As C# lambda expression cannot refer to itself, so it have to receive itself as a
parameter, so:&lt;br /&gt;
&lt;div style="padding: 1em"&gt;
&lt;code&gt;factorial(factorial, n) = n &amp;lt;= 1 ? 1 : n * factorial(factorial, n - 1);&lt;/code&gt; 
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
To define such lambda expression we have to declare a delegate type that receives
a delegate of the same type:&lt;br /&gt;
&lt;div style="padding: 1em"&gt;
&lt;code&gt;delegate int Impl(Impl impl, int n);&lt;/code&gt;
&lt;/div&gt;
Fortunately, C# allows this, but a workaround could be used even if it were not possible.&lt;br /&gt;
&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;
To simplify the reasoning we&amp;#39;ve defined a two-expression version:&lt;br /&gt;
&lt;div style="padding: 1em"&gt;
&lt;code&gt;Impl impl = (f, n) =&amp;gt; n &amp;lt;= 1 ? 1 : n * f(f, n - 1); 
&lt;br /&gt;
Func&amp;lt;int, int&amp;gt; factorial = i =&amp;gt; impl(impl, i);&lt;/code&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
Finally, we&amp;#39;ve written out a one-expression version:&lt;br /&gt;
&lt;div style="padding: 1em"&gt;
&lt;code&gt;Func&amp;lt;int, int&amp;gt; factorial = i =&amp;gt; ((Func&amp;lt;Impl, int&amp;gt;)(f =&amp;gt; f(f,
i)))((f, n) =&amp;gt; n &amp;lt;= 1 ? 1 : n * f(f, n - 1)); &lt;/code&gt; 
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
The use is:&lt;br /&gt;
&lt;div style="padding: 1em"&gt;
&lt;code&gt;var f = factorial(10);&lt;/code&gt; 
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
After that excercise we&amp;#39;ve returned back to original blog and compared solutions.
We can see that author appeals to a set theory but for some reason his answer is more
complex than nesessary, but comments contain variants that analogous to our answer.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=f6004aca-9fa1-4c23-b987-9de640d86baf" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,f6004aca-9fa1-4c23-b987-9de640d86baf.aspx</comments>
      <category>.NET</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=19c9c7e3-4b24-4407-806a-b118460873e3</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,19c9c7e3-4b24-4407-806a-b118460873e3.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,19c9c7e3-4b24-4407-806a-b118460873e3.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=19c9c7e3-4b24-4407-806a-b118460873e3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This time we <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=19c9c7e3-4b24-4407-806a-b118460873e3&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2flanguages-xom.zip">update
csharpxom</a> to adjust it to C# 4.5. Additions are<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=19c9c7e3-4b24-4407-806a-b118460873e3&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fhh156513(v%3dvs.110).aspx"><code>async</code> modifier</a> and <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=19c9c7e3-4b24-4407-806a-b118460873e3&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fhh156528(v%3dvs.110).aspx"><code>await</code> operator</a>.
</p>
        <p>
They are used to simplify asynchronous programming.
</p>
        <p>
The following example from the msdn:
</p>
        <p style="padding-left: 1em">
          <code>private async Task&lt;byte[]&gt; GetURLContentsAsync(string url)<br />
{<br />
var content = new MemoryStream();<br />
var request = (HttpWebRequest)WebRequest.Create(url);<br /><br />
using(var response = await request.GetResponseAsync())<br />
using(var responseStream = response.GetResponseStream())<br />
{<br />
await responseStream.CopyToAsync(content);<br />
}<br /><br />
return content.ToArray();<br />
}</code>
        </p>
        <p>
looks like this in csharpxom:
</p>
        <p style="padding-left: 1em">
          <code>&lt;method name="GetURLContentsAsync" access="private" <span style="font-weight: bold">async="true"</span>&gt;<br />
&lt;returns&gt;<br />
&lt;type name="Task" namespace="System.Threading.Tasks"&gt;<br />
&lt;type-arguments&gt;<br />
&lt;type name="byte" rank="1"/&gt;<br />
&lt;/type-arguments&gt;<br />
&lt;/type&gt;<br />
&lt;/returns&gt;<br />
&lt;parameters&gt;<br />
&lt;parameter name="url"&gt;<br />
&lt;type name="string"/&gt;<br />
&lt;/parameter&gt;<br />
&lt;/parameters&gt;<br />
&lt;block&gt;<br />
&lt;var name="content"&gt;<br />
&lt;initialize&gt;<br />
&lt;new-object&gt;<br />
&lt;type name="MemoryStream" namespace="System.IO"/&gt;<br />
&lt;/new-object&gt;<br />
&lt;/initialize&gt;<br />
&lt;/var&gt;<br />
&lt;var name="request"&gt;<br />
&lt;initialize&gt;<br />
&lt;cast&gt;<br />
&lt;invoke&gt;<br />
&lt;static-method-ref name="Create"&gt;<br />
&lt;type name="WebRequest" namespace="System.Net"/&gt;<br />
&lt;/static-method-ref&gt;<br />
&lt;arguments&gt;<br />
&lt;var-ref name="url"/&gt;<br />
&lt;/arguments&gt;<br />
&lt;/invoke&gt;<br />
&lt;type name="HttpWebRequest" namespace="System.Net"/&gt;<br />
&lt;/cast&gt;<br />
&lt;/initialize&gt;<br />
&lt;/var&gt;<br /><br />
&lt;using&gt;<br />
&lt;resource&gt;<br />
&lt;var name="response"&gt;<br />
&lt;initialize&gt;<br /><span style="font-weight: bold">&lt;await&gt;</span><br />
&lt;invoke&gt;<br />
&lt;method-ref name="GetResponseAsync"&gt;<br />
&lt;var-ref name="request"/&gt;<br />
&lt;/method-ref&gt;<br />
&lt;/invoke&gt;<br /><span style="font-weight: bold">&lt;/await&gt;</span><br />
&lt;/initialize&gt;<br />
&lt;/var&gt;<br />
&lt;/resource&gt;<br />
&lt;using&gt;<br />
&lt;resource&gt;<br />
&lt;var name="responseStream"&gt;<br />
&lt;initialize&gt;<br />
&lt;invoke&gt;<br />
&lt;method-ref name="GetResponseStream"&gt;<br />
&lt;var-ref name="response"/&gt;<br />
&lt;/method-ref&gt;<br />
&lt;/invoke&gt;<br />
&lt;/initialize&gt;<br />
&lt;/var&gt;<br />
&lt;/resource&gt;<br />
&lt;expression&gt;<br /><span style="font-weight: bold">&lt;await&gt;</span><br />
&lt;invoke&gt;<br />
&lt;method-ref name="CopyToAsync"&gt;<br />
&lt;var-ref name="responseStream"/&gt;<br />
&lt;/method-ref&gt;<br />
&lt;arguments&gt;<br />
&lt;var-ref name="content"/&gt;<br />
&lt;/arguments&gt;<br />
&lt;/invoke&gt;<br /><span style="font-weight: bold">&lt;/await&gt;</span><br />
&lt;/expression&gt;<br />
&lt;/using&gt;<br />
&lt;/using&gt;<br /><br />
&lt;return&gt;<br />
&lt;invoke&gt;<br />
&lt;method-ref name="ToArray"&gt;<br />
&lt;var-ref name="content"/&gt;<br />
&lt;/method-ref&gt;<br />
&lt;/invoke&gt;<br />
&lt;/return&gt;<br />
&lt;/block&gt;<br />
&lt;/method&gt;</code>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=19c9c7e3-4b24-4407-806a-b118460873e3" />
      </body>
      <title>Languages XOM update</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,19c9c7e3-4b24-4407-806a-b118460873e3.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/03/23/LanguagesXOMUpdate.aspx</link>
      <pubDate>Fri, 23 Mar 2012 00:07:35 GMT</pubDate>
      <description>  &lt;p&gt;
This time we &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=19c9c7e3-4b24-4407-806a-b118460873e3&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2flanguages-xom.zip"&gt;update
csharpxom&lt;/a&gt; to adjust it to C# 4.5. Additions are&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=19c9c7e3-4b24-4407-806a-b118460873e3&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fhh156513(v%3dvs.110).aspx"&gt; &lt;code&gt;async&lt;/code&gt; modifier&lt;/a&gt; and &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=19c9c7e3-4b24-4407-806a-b118460873e3&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fhh156528(v%3dvs.110).aspx"&gt; &lt;code&gt;await&lt;/code&gt; operator&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
They are used to simplify asynchronous programming.
&lt;/p&gt;
&lt;p&gt;
The following example from the msdn:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;private async Task&amp;lt;byte[]&amp;gt; GetURLContentsAsync(string url)&lt;br /&gt;
{&lt;br /&gt;
var content = new MemoryStream();&lt;br /&gt;
var request = (HttpWebRequest)WebRequest.Create(url);&lt;br /&gt;
&lt;br /&gt;
using(var response = await request.GetResponseAsync())&lt;br /&gt;
using(var responseStream = response.GetResponseStream())&lt;br /&gt;
{&lt;br /&gt;
await responseStream.CopyToAsync(content);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return content.ToArray();&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
looks like this in csharpxom:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;&amp;lt;method name="GetURLContentsAsync" access="private" &lt;span style="font-weight: bold"&gt;async="true"&lt;/span&gt;&amp;gt;&lt;br /&gt;
&amp;lt;returns&amp;gt;&lt;br /&gt;
&amp;lt;type name="Task" namespace="System.Threading.Tasks"&amp;gt;&lt;br /&gt;
&amp;lt;type-arguments&amp;gt;&lt;br /&gt;
&amp;lt;type name="byte" rank="1"/&amp;gt;&lt;br /&gt;
&amp;lt;/type-arguments&amp;gt;&lt;br /&gt;
&amp;lt;/type&amp;gt;&lt;br /&gt;
&amp;lt;/returns&amp;gt;&lt;br /&gt;
&amp;lt;parameters&amp;gt;&lt;br /&gt;
&amp;lt;parameter name="url"&amp;gt;&lt;br /&gt;
&amp;lt;type name="string"/&amp;gt;&lt;br /&gt;
&amp;lt;/parameter&amp;gt;&lt;br /&gt;
&amp;lt;/parameters&amp;gt;&lt;br /&gt;
&amp;lt;block&amp;gt;&lt;br /&gt;
&amp;lt;var name="content"&amp;gt;&lt;br /&gt;
&amp;lt;initialize&amp;gt;&lt;br /&gt;
&amp;lt;new-object&amp;gt;&lt;br /&gt;
&amp;lt;type name="MemoryStream" namespace="System.IO"/&amp;gt;&lt;br /&gt;
&amp;lt;/new-object&amp;gt;&lt;br /&gt;
&amp;lt;/initialize&amp;gt;&lt;br /&gt;
&amp;lt;/var&amp;gt;&lt;br /&gt;
&amp;lt;var name="request"&amp;gt;&lt;br /&gt;
&amp;lt;initialize&amp;gt;&lt;br /&gt;
&amp;lt;cast&amp;gt;&lt;br /&gt;
&amp;lt;invoke&amp;gt;&lt;br /&gt;
&amp;lt;static-method-ref name="Create"&amp;gt;&lt;br /&gt;
&amp;lt;type name="WebRequest" namespace="System.Net"/&amp;gt;&lt;br /&gt;
&amp;lt;/static-method-ref&amp;gt;&lt;br /&gt;
&amp;lt;arguments&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="url"/&amp;gt;&lt;br /&gt;
&amp;lt;/arguments&amp;gt;&lt;br /&gt;
&amp;lt;/invoke&amp;gt;&lt;br /&gt;
&amp;lt;type name="HttpWebRequest" namespace="System.Net"/&amp;gt;&lt;br /&gt;
&amp;lt;/cast&amp;gt;&lt;br /&gt;
&amp;lt;/initialize&amp;gt;&lt;br /&gt;
&amp;lt;/var&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;using&amp;gt;&lt;br /&gt;
&amp;lt;resource&amp;gt;&lt;br /&gt;
&amp;lt;var name="response"&amp;gt;&lt;br /&gt;
&amp;lt;initialize&amp;gt;&lt;br /&gt;
&lt;span style="font-weight: bold"&gt;&amp;lt;await&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&amp;lt;invoke&amp;gt;&lt;br /&gt;
&amp;lt;method-ref name="GetResponseAsync"&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="request"/&amp;gt;&lt;br /&gt;
&amp;lt;/method-ref&amp;gt;&lt;br /&gt;
&amp;lt;/invoke&amp;gt;&lt;br /&gt;
&lt;span style="font-weight: bold"&gt;&amp;lt;/await&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&amp;lt;/initialize&amp;gt;&lt;br /&gt;
&amp;lt;/var&amp;gt;&lt;br /&gt;
&amp;lt;/resource&amp;gt;&lt;br /&gt;
&amp;lt;using&amp;gt;&lt;br /&gt;
&amp;lt;resource&amp;gt;&lt;br /&gt;
&amp;lt;var name="responseStream"&amp;gt;&lt;br /&gt;
&amp;lt;initialize&amp;gt;&lt;br /&gt;
&amp;lt;invoke&amp;gt;&lt;br /&gt;
&amp;lt;method-ref name="GetResponseStream"&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="response"/&amp;gt;&lt;br /&gt;
&amp;lt;/method-ref&amp;gt;&lt;br /&gt;
&amp;lt;/invoke&amp;gt;&lt;br /&gt;
&amp;lt;/initialize&amp;gt;&lt;br /&gt;
&amp;lt;/var&amp;gt;&lt;br /&gt;
&amp;lt;/resource&amp;gt;&lt;br /&gt;
&amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;span style="font-weight: bold"&gt;&amp;lt;await&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&amp;lt;invoke&amp;gt;&lt;br /&gt;
&amp;lt;method-ref name="CopyToAsync"&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="responseStream"/&amp;gt;&lt;br /&gt;
&amp;lt;/method-ref&amp;gt;&lt;br /&gt;
&amp;lt;arguments&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="content"/&amp;gt;&lt;br /&gt;
&amp;lt;/arguments&amp;gt;&lt;br /&gt;
&amp;lt;/invoke&amp;gt;&lt;br /&gt;
&lt;span style="font-weight: bold"&gt;&amp;lt;/await&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&amp;lt;/expression&amp;gt;&lt;br /&gt;
&amp;lt;/using&amp;gt;&lt;br /&gt;
&amp;lt;/using&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;return&amp;gt;&lt;br /&gt;
&amp;lt;invoke&amp;gt;&lt;br /&gt;
&amp;lt;method-ref name="ToArray"&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="content"/&amp;gt;&lt;br /&gt;
&amp;lt;/method-ref&amp;gt;&lt;br /&gt;
&amp;lt;/invoke&amp;gt;&lt;br /&gt;
&amp;lt;/return&amp;gt;&lt;br /&gt;
&amp;lt;/block&amp;gt;&lt;br /&gt;
&amp;lt;/method&amp;gt;&lt;/code&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=19c9c7e3-4b24-4407-806a-b118460873e3" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,19c9c7e3-4b24-4407-806a-b118460873e3.aspx</comments>
      <category>.NET</category>
      <category>Announce</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=8383cfc7-10ac-4f2a-a249-4cc37ddbac70</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=8383cfc7-10ac-4f2a-a249-4cc37ddbac70</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
For a long time we were developing web applications with ASP.NET and JSF. At present
we prefer rich clients and a server with page templates and RESTful web services.
</p>
        <p>
This transition brings technical questions. Consider this one.
</p>
        <p>
Browsers allow to store session state entirely on the client, so should we maintain
a session on the server?
</p>
        <p>
Since the server is just a set of web services, so we may supply all required arguments
on each call. 
</p>
        <p>
At first glance we can assume that no session is required on the server. However,
looking further we see that we should deal with data validation (security) on the
server. 
</p>
        <p>
Think about a classic ASP.NET application, where a user can select a value from a
dropdown. Either ASP.NET itself or your program (against a list from a session) verifies
that the value received is valid for the user. That list of values and might be other
parameters constitute a user profile, which we stored in session. The user profile
played important role (often indirectly) in the validation of input data.
</p>
        <p>
When the server is just a set of web services then we have to validate all parameters
manually. There are two sources that we can rely to: (a) <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.services.webservice.session.aspx"> a
session</a>, (b) <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.services.webservice.user.aspx"> a
user principal</a>.
</p>
        <p>
The case (a) is very similar to classic ASP.NET application except that with <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fydy4x04a.aspx">EnableEventValidation="true"</a> runtime
did it for us most of the time.<br />
The case (b) requires reconstruction of the user profile for a user principal and
then we proceed with validation of parameters. 
</p>
        <p>
We may cache user profile in session, in which case we reduce (b) to (a); on the other
hand we may cache user profile in <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.caching.cache.aspx"> Cache</a>,
which is also similar to (a) but which might be lighter than (at least not heavier
than) the solution with the session.
</p>
        <p>
What we see is that the client session does not free us from server session (or its
alternative).
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70" />
      </body>
      <title>Shifting to client side web applications</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/03/08/ShiftingToClientSideWebApplications.aspx</link>
      <pubDate>Thu, 08 Mar 2012 21:56:19 GMT</pubDate>
      <description>  &lt;p&gt;
For a long time we were developing web applications with ASP.NET and JSF. At present
we prefer rich clients and a server with page templates and RESTful web services.
&lt;/p&gt;
&lt;p&gt;
This transition brings technical questions. Consider this one.
&lt;/p&gt;
&lt;p&gt;
Browsers allow to store session state entirely on the client, so should we maintain
a session on the server?
&lt;/p&gt;
&lt;p&gt;
Since the server is just a set of web services, so we may supply all required arguments
on each call. 
&lt;/p&gt;
&lt;p&gt;
At first glance we can assume that no session is required on the server. However,
looking further we see that we should deal with data validation (security) on the
server. 
&lt;/p&gt;
&lt;p&gt;
Think about a classic ASP.NET application, where a user can select a value from a
dropdown. Either ASP.NET itself or your program (against a list from a session) verifies
that the value received is valid for the user. That list of values and might be other
parameters constitute a user profile, which we stored in session. The user profile
played important role (often indirectly) in the validation of input data.
&lt;/p&gt;
&lt;p&gt;
When the server is just a set of web services then we have to validate all parameters
manually. There are two sources that we can rely to: (a) &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.services.webservice.session.aspx"&gt; a
session&lt;/a&gt;, (b) &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.services.webservice.user.aspx"&gt; a
user principal&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
The case (a) is very similar to classic ASP.NET application except that with &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fydy4x04a.aspx"&gt;EnableEventValidation="true"&lt;/a&gt; runtime
did it for us most of the time.&lt;br /&gt;
The case (b) requires reconstruction of the user profile for a user principal and
then we proceed with validation of parameters. 
&lt;/p&gt;
&lt;p&gt;
We may cache user profile in session, in which case we reduce (b) to (a); on the other
hand we may cache user profile in &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.caching.cache.aspx"&gt; Cache&lt;/a&gt;,
which is also similar to (a) but which might be lighter than (at least not heavier
than) the solution with the session.
&lt;/p&gt;
&lt;p&gt;
What we see is that the client session does not free us from server session (or its
alternative).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>Java</category>
      <category>JSF and Facelets</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=7fb6be18-2165-4027-8554-94d7aa5e2683</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7fb6be18-2165-4027-8554-94d7aa5e2683.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,7fb6be18-2165-4027-8554-94d7aa5e2683.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=7fb6be18-2165-4027-8554-94d7aa5e2683</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We were dealing with a datasource of <code>(int? id, string value)</code> pairs in
LINQ. The data has originated from a database where <code>id</code> is unique field.
In the program this datasource had to be seen as a dictionary, so we have written
a code like this:
</p>
        <p style="padding-left: 1em;">
          <code>var dictionary = CreateIDValuePairs().ToDictionary(item =&gt; item.ID, item
=&gt; item.Value)</code>;
</p>
        <p>
That was too simple-minded. This code compiles but crashes at runtime when there is
an <code>id == null</code>.
</p>
        <p>
Well, help warns about this behaviour, but anyway this does not make pain easier. 
</p>
        <p>
In our opinion this restriction is not justified and just complicates the use of <code>Dictionaty</code>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7fb6be18-2165-4027-8554-94d7aa5e2683" />
      </body>
      <title>.NET Dictionary nuisance</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7fb6be18-2165-4027-8554-94d7aa5e2683.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/02/29/NETDictionaryNuisance.aspx</link>
      <pubDate>Wed, 29 Feb 2012 20:42:46 GMT</pubDate>
      <description>&lt;p&gt;
We were dealing with a datasource of &lt;code&gt;(int? id, string value)&lt;/code&gt; pairs in
LINQ. The data has originated from a database where &lt;code&gt;id&lt;/code&gt; is unique field.
In the program this datasource had to be seen as a dictionary, so we have written
a code like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em;"&gt;
&lt;code&gt;var dictionary = CreateIDValuePairs().ToDictionary(item =&amp;gt; item.ID, item
=&amp;gt; item.Value)&lt;/code&gt;;
&lt;/p&gt;
&lt;p&gt;
That was too simple-minded. This code compiles but crashes at runtime when there is
an &lt;code&gt;id == null&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
Well, help warns about this behaviour, but anyway this does not make pain easier. 
&lt;/p&gt;
&lt;p&gt;
In our opinion this restriction is not justified and just complicates the use of &lt;code&gt;Dictionaty&lt;/code&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7fb6be18-2165-4027-8554-94d7aa5e2683" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,7fb6be18-2165-4027-8554-94d7aa5e2683.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=7a2cda28-6e38-48d8-a233-302ba6e1d75e</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=7a2cda28-6e38-48d8-a233-302ba6e1d75e</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A bit history: the <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;url=http%3a%2f%2fwww.bphx.com%2fen%2fNewsEvents%2fPressReleases%2fPages%2f072902.aspx" target="_blank">first
release</a> of this solution was about 9.5 years ago...
</p>
        <p>
Today we've run into a strange situation. One of our clients ask us about automatic
conversion of data from mainframe (that were defined as COBOL copybooks) into XML
or Java/.NET objects. On our suggestion to use <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdata%2fprojects.xml">eXperanto</a>,
which is well known to him, he stated that he wouldn't like to use a tool of a company
that is no more exists...
</p>
        <p>
The situation, in our opinion, become more strange when you consider the following:
</p>
        <ul>
          <li>
            <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdata%2fprojects.xml">eXperanto</a> (the
design-time tool and run-time libraries for Java and .NET) were developed, well tested,
and delivered by us to production already several years ago.</li>
          <li>
the client bought this set (the tool and libraries).</li>
          <li>
the set is in production yet already in another big company, and is used time to time
by our company in different migration projects.</li>
          <li>
the client talks with developers of this tool and run-time libraries, and he knows
about this fact.</li>
          <li>
the client uses widely open source solutions even without dedicated vendors or support
warranties.</li>
        </ul>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e" />
      </body>
      <title>eXperanto project actual yet...</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/11/10/eXperantoProjectActualYet.aspx</link>
      <pubDate>Thu, 10 Nov 2011 22:14:09 GMT</pubDate>
      <description>&lt;p&gt;
A bit history: the &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;amp;url=http%3a%2f%2fwww.bphx.com%2fen%2fNewsEvents%2fPressReleases%2fPages%2f072902.aspx" target="_blank"&gt;first
release&lt;/a&gt; of this solution was about 9.5 years ago...
&lt;/p&gt;
&lt;p&gt;
Today we've run into a strange situation. One of our clients ask us about automatic
conversion of data from mainframe (that were defined as COBOL copybooks) into XML
or Java/.NET objects. On our suggestion to use &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdata%2fprojects.xml"&gt;eXperanto&lt;/a&gt;,
which is well known to him, he stated that he wouldn't like to use a tool of a company
that is no more exists...
&lt;/p&gt;
&lt;p&gt;
The situation, in our opinion, become more strange when you consider the following:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdata%2fprojects.xml"&gt;eXperanto&lt;/a&gt; (the
design-time tool and run-time libraries for Java and .NET) were developed, well tested,
and delivered by us to production already several years ago.&lt;/li&gt;
&lt;li&gt;
the client bought this set (the tool and libraries).&lt;/li&gt;
&lt;li&gt;
the set is in production yet already in another big company, and is used time to time
by our company in different migration projects.&lt;/li&gt;
&lt;li&gt;
the client talks with developers of this tool and run-time libraries, and he knows
about this fact.&lt;/li&gt;
&lt;li&gt;
the client uses widely open source solutions even without dedicated vendors or support
warranties.&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</comments>
      <category>.NET</category>
      <category>Java</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=243e0e15-c07d-4a6b-87f9-c0e89f52b781</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,243e0e15-c07d-4a6b-87f9-c0e89f52b781.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,243e0e15-c07d-4a6b-87f9-c0e89f52b781.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=243e0e15-c07d-4a6b-87f9-c0e89f52b781</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We're not big fans of <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=243e0e15-c07d-4a6b-87f9-c0e89f52b781&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fbb399572.aspx"> Entity
Framework</a>, as we don't directly expose the database structure to the client
program but rather through stored procedures and functions. So, EF for us is a tool
to expose those stored procedures as .NET wrappers. This limited use of EF still greatly
automates the data access code.
</p>
        <p>
But what we have lately found is that the EF has a problem with char parameters. Namely,
if you import a procedure say <code>MyProc</code> that accepts <code>char(1)</code>,
and then will call it through the generated wrapper, the you will see in sql profiler
that <code>char(1)</code> parameter is passed with many trailing spaces as if it were <code>char(8000)</code>.
There isn't necessity to prove that this is highly ineffective.
</p>
        <p>
We can see that the problem happens in VS 2010 designer rather than in the EF runtime,
as SP's parameters are not attributed with length, see model xml (*.edmx):
</p>
        <p style="padding-left: 1em">
          <code>&lt;Function Name="MyProc" Schema="Data"&gt; 
<br />
  ...<br />
  &lt;Parameter Name="recipientType" Type="char" Mode="In"
/&gt;<br />
  ...<br />
&lt;/Function&gt;</code>
        </p>
        <p>
while if we set:
</p>
        <p style="padding-left: 1em">
          <code>  &lt;Parameter Name="recipientType" Type="char" MaxLength="1"
Mode="In" /&gt;<br /></code>
        </p>
        <p>
the runtime starts working as expected. So the workaround is to fix model file manually.
</p>
        <p>
See also: <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=243e0e15-c07d-4a6b-87f9-c0e89f52b781&amp;url=http%3a%2f%2fsocial.msdn.microsoft.com%2fForums%2fen-US%2fadodotnetentityframework%2fthread%2fa0b94f65-dc89-48f8-8b1a-a299fe0f6381%2f"> Stored
Proc and Char parm</a></p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=243e0e15-c07d-4a6b-87f9-c0e89f52b781" />
      </body>
      <title>Entity Framework and char parameters</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,243e0e15-c07d-4a6b-87f9-c0e89f52b781.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/09/06/EntityFrameworkAndCharParameters.aspx</link>
      <pubDate>Tue, 06 Sep 2011 21:11:38 GMT</pubDate>
      <description>&lt;p&gt;
We&amp;#39;re not big fans of &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=243e0e15-c07d-4a6b-87f9-c0e89f52b781&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fbb399572.aspx"&gt; Entity
Framework&lt;/a&gt;, as we don&amp;#39;t directly expose the database structure to the client
program but rather through stored procedures and functions. So, EF for us is a tool
to expose those stored procedures as .NET wrappers. This limited use of EF still greatly
automates the data access code.
&lt;/p&gt;
&lt;p&gt;
But what we have lately found is that the EF has a problem with char parameters. Namely,
if you import a procedure say &lt;code&gt;MyProc&lt;/code&gt; that accepts &lt;code&gt;char(1)&lt;/code&gt;,
and then will call it through the generated wrapper, the you will see in sql profiler
that &lt;code&gt;char(1)&lt;/code&gt; parameter is passed with many trailing spaces as if it were &lt;code&gt;char(8000)&lt;/code&gt;.
There isn&amp;#39;t necessity to prove that this is highly ineffective.
&lt;/p&gt;
&lt;p&gt;
We can see that the problem happens in VS 2010 designer rather than in the EF runtime,
as SP&amp;#39;s parameters are not attributed with length, see model xml (*.edmx):
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;&amp;lt;Function Name=&amp;quot;MyProc&amp;quot; Schema=&amp;quot;Data&amp;quot;&amp;gt; 
&lt;br /&gt;
&amp;nbsp; ...&lt;br /&gt;
&amp;nbsp; &amp;lt;Parameter Name=&amp;quot;recipientType&amp;quot; Type=&amp;quot;char&amp;quot; Mode=&amp;quot;In&amp;quot;
/&amp;gt;&lt;br /&gt;
&amp;nbsp; ...&lt;br /&gt;
&amp;lt;/Function&amp;gt;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
while if we set:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;&amp;nbsp; &amp;lt;Parameter Name=&amp;quot;recipientType&amp;quot; Type=&amp;quot;char&amp;quot; MaxLength=&amp;quot;1&amp;quot;
Mode=&amp;quot;In&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
the runtime starts working as expected. So the workaround is to fix model file manually.
&lt;/p&gt;
&lt;p&gt;
See also: &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=243e0e15-c07d-4a6b-87f9-c0e89f52b781&amp;amp;url=http%3a%2f%2fsocial.msdn.microsoft.com%2fForums%2fen-US%2fadodotnetentityframework%2fthread%2fa0b94f65-dc89-48f8-8b1a-a299fe0f6381%2f"&gt; Stored
Proc and Char parm&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=243e0e15-c07d-4a6b-87f9-c0e89f52b781" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,243e0e15-c07d-4a6b-87f9-c0e89f52b781.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
  </channel>
</rss>