<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>Nesterovsky bros</title>
  <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/" />
  <link rel="self" href="http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2013-05-28T00:53:45.759316-07:00</updated>
  <author>
    <name>Nesterovsky bros</name>
  </author>
  <subtitle />
  <id>http://www.nesterovsky-bros.com/weblog/</id>
  <generator uri="http://dasblog.info/" version="2.3.12105.0">DasBlog</generator>
  <entry>
    <title>Kendo progress utility</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/05/28/KendoProgressUtility.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,c96c3810-95d1-46bd-b413-7fac0cdbf509.aspx</id>
    <published>2013-05-27T22:54:52.307-07:00</published>
    <updated>2013-05-28T00:53:45.759316-07:00</updated>
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="kendoui" label="kendoui" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,kendoui.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="html">  &lt;p&gt;
While developing with KendoUI we have found &lt;code&gt;kendo.ui.progress(container, toggle)&lt;/code&gt; function
to be very useful. It's used to show or hide a progress indicator in the container
element.
&lt;/p&gt;
&lt;p&gt;
At the same time we have found that we usually used it in a context of async operation.
This way, we want to show progress, perform some asynchronous operations, hide progress.
So, we clearly want to benifit from &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c96c3810-95d1-46bd-b413-7fac0cdbf509&amp;amp;url=http%3a%2f%2fen.wikipedia.org%2fwiki%2fResource_Acquisition_Is_Initialization"&gt; RAII&lt;/a&gt; pattern:
we would like to open a progress scope, and to perform some activity withing this
scope.
&lt;/p&gt;
&lt;p&gt;
Arguing like this, we have defined a utility function, which is the fusion of &lt;code&gt;kendo.ui.progress()&lt;/code&gt; and &lt;code&gt;$.when()&lt;/code&gt;.
Its signature is like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;nesterovskyBros.progress = function(instance /*, task ... */)&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
where &lt;code&gt;instance&lt;/code&gt; is either &lt;code&gt;Model&lt;/code&gt;, &lt;code&gt;Widget&lt;/code&gt;, &lt;code&gt;JQuery&lt;/code&gt; or
DOM &lt;code&gt;Element&lt;/code&gt;, and &lt;code&gt;task&lt;/code&gt; is one or more deferred objects. This
function shows a progress and returns a &lt;code&gt;Promise&lt;/code&gt; that will hide a progress
when all tasks will be complete. Implementation is trivial, so we quote it here:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;// Fusion of kendo.ui.progress() and $.when().&lt;br /&gt;
scope.progress = function(instance /*, task ... */)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; if (instance instanceof Model)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; instance = instance.owner &amp;&amp; instance.owner();&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; if (instance instanceof Widget)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; instance = instance.element;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; if (instance &amp;&amp; instance.nodeType)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; instance = $(instance);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; var id = ns + "-progress"; // "nesterovskyBros-progress";&lt;br /&gt;
&amp;nbsp; var progress = (instance &amp;&amp; instance.data(id)) || 0;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; if (arguments.length &amp;lt; 2)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return progress;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; var result = $.when.apply(null, [].slice.call(arguments, 1));&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; if (instance)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; instance.data(id, ++progress);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; kendo.ui.progress(instance, progress &amp;gt; 0);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; result.always(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; function()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; progress = instance.data(id) || 0;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; instance.data(id, --progress);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; kendo.ui.progress(instance, progress &amp;gt;
0);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; return result;&lt;br /&gt;
}; &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The use is like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;nesterovskyBros.progress(element, $.ajax("/service1"), $.ajax("/service2")).then(myFunc);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The code can be found at &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c96c3810-95d1-46bd-b413-7fac0cdbf509&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fcontrols.js"&gt; controls.js&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=c96c3810-95d1-46bd-b413-7fac0cdbf509" /&gt;</content>
  </entry>
  <entry>
    <title>KendoUI controls.js</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/05/18/KendoUIControlsjs.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,04f31825-657d-470b-9223-bb1e8961fc9d.aspx</id>
    <published>2013-05-18T03:59:36.858-07:00</published>
    <updated>2013-05-18T04:02:17.2722388-07:00</updated>
    <category term="Announce" label="Announce" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,Announce.aspx" />
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="kendoui" label="kendoui" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,kendoui.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
While trying to generalize our practices from KendoUI related projects we've participated
so far, we updated <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=04f31825-657d-470b-9223-bb1e8961fc9d&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fapi%2fcontrols.js"> control.js</a> -
a small javascript additions to KendoUI.
</p>
        <p>
At present we have defined:
</p>
        <p>
1. An extended model. See <a href="2013/04/03/KendoUIExtendedModel.aspx" rel="bookmark">KendoUI
extended model</a>. 
</p>
        <p>
2. A lightweight user control - a widget to bind a template and a model, and to facilitate
declarative instantiation. See <a href="2013/03/26/KendoUIUserControl.aspx" rel="bookmark">KendoUI
User control</a>. 
</p>
        <p>
3. A reworked version of nesterovskyBros.defineControl() function. 
</p>
        <p style="padding-left: 1em">
          <code>var widgetType = scope.defineControl(<br />
{<br />
   name: widget-name-string,<br />
   model: widget-model-type,<br />
   template: optional-content-template,<br />
   windowOptions: optional-window-options<br />
},<br />
base);</code>
        </p>
        <p>
When <code>optional-content-template</code> is not specified then template is calculated
as following:
</p>
        <p style="padding-left: 1em">
          <code> var template = options.temlate || proto.template || model.temlate;<br /><br />
if (template === undefined) 
<br />
{<br />
  template = scope.template(options.name.toLowerCase() + "-template"); 
<br />
} </code>
        </p>
        <p>
When <code>windowOptions</code> is specified then <code>widgetType.dialog(options)</code> function
is defined. It's used to open dialog based on the specified user control. <code>windowOptions</code> is
passed to <code>kendo.ui.Window</code> constructor. <code>windowOptions.closeOnEscape</code> indicates
whether to close opened dialog on escape.
</p>
        <p>
          <code>widgetType.dialog()</code> returns a <code>kendo.ui.Window</code> instance with
content based on the user control. Window instance contains functions: 
</p>
        <ul>
          <li>
            <code>result()</code> - a <code>$.Deffered</code> for the dialog result, and 
</li>
          <li>
            <code>model()</code> - referring to the user control model. 
</li>
        </ul>
        <p>
 The model instance has functions:
</p>
        <ul>
          <li>
 <code>dialog()</code> referring to the dialog, and 
</li>
          <li>
            <code>result()</code> referring to the dialog result.</li>
        </ul>
        <p>
          <code>widget.dialog()</code> allows all css units in <code>windowOptions.width</code> and <code>windowOptions.height</code> parameters.
</p>
        <p>
          <code>base</code> - is optional user control base. It defaults to <code>nesterovskyBros.ui.UserControl</code>.
</p>
        <p>
4. Adjusted splitter. See <a href="2013/05/14/AdjustKendoUISplitter.aspx" rel="bookmark">Adjust
KendoUI Splitter</a>.
</p>
        <p>
5. Auto resize support.
</p>
        <p>
Layout is often depends on available area. One example is <code>Splitter</code> widget
that recalculates its panes when window or container <code>Splitter</code> is resized.
There are other cases when you would like to adjust layout when a container's
area is changed like: adjust grid, tab, editor or user's control contents.
</p>
        <p>
KendoUI does not provide a solution for this problem, so we have defined our own.
</p>
        <ul>
          <li>
A widget can be marked with <code>class="auto-resize"</code> marker;</li>
          <li>
A widget may define a <code>widgetType.autoResize(element)</code> function that adapts
widget to a new size.</li>
          <li>
A code can call <code>nesterovskyBros.resize(element)</code> function at trigger resizing
of the subtree.</li>
        </ul>
        <p>
To support existing controls we have defined <code>autoResize()</code> function for <code>Grid</code>, <code>Splitter</code>, <code>TabStrip</code>,
and <code>Editor</code> widgets.
</p>
        <p>
To see how auto resizing works, it's best to look into <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=04f31825-657d-470b-9223-bb1e8961fc9d&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2findex.html">index.html</a>, <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=04f31825-657d-470b-9223-bb1e8961fc9d&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2ftemplates%2fproducts.tmpl.html"> products.tmpl.html</a>,
and into the implementation <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=04f31825-657d-470b-9223-bb1e8961fc9d&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fapi%2fcontrols.js"> controls.js</a>.
</p>
        <p>
Please note that we consider <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=04f31825-657d-470b-9223-bb1e8961fc9d&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fapi%2fcontrols.js"> controls.js</a> as
an addition to KendoUI library. If in the future the library will integrate or implement
similar features we will be happy to start using their API.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=04f31825-657d-470b-9223-bb1e8961fc9d" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Adjust KendoUI Splitter</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/05/14/AdjustKendoUISplitter.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9b30445a-be57-4140-a839-fab46b42c76e.aspx</id>
    <published>2013-05-14T00:34:59.002-07:00</published>
    <updated>2013-05-14T00:37:26.4193453-07:00</updated>
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="kendoui" label="kendoui" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,kendoui.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
We heavily use kendo.ui.Splitter widget. Unfortunately it has several drawbacks:
</p>
        <ul>
          <li>
you cannot easily configure panes declaratively;</li>
          <li>
you cannot define a pane that takes space according to its content.</li>
        </ul>
        <p>
Although we don't like to patch widgets, in this case we found no better way but
to patch two functions: <code>kendo.ui.Splitter.fn._initPanes</code>, and  <code>kendo.ui.Splitter.fn._resize</code>.
</p>
        <p>
After the fix, splitter markup may look like the following:
</p>
        <p style="padding-left: 1em">
          <code> &lt;div style="height: 100%" data-role="splitter" data-orientation="vertical"&gt;<br />
  &lt;div <strong>data-pane</strong>='{ size: "<strong>auto</strong>",
resizable: false, scrollable: false }'&gt;<br />
    Header with size depending on content.<br />
  &lt;/div&gt;<br />
  &lt;div data-pane='{ resizable: false, scrollable: true }'&gt;<br />
    Body with size equal to a remaining area.<br />
  &lt;/div&gt;<br />
  &lt;div data-pane='{ size: "auto", resizable: false, scrollable: false }'&gt;<br />
    Footer with size depending on content.<br />
  &lt;/div&gt;<br />
&lt;/div&gt;</code>
        </p>
        <p>
Each pane may define a <code>data-pane</code> attribute with pane parameters. A pane
may specify <code>size = "auto"</code> to take space according to its content.
</p>
        <p>
The code can be found at <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=9b30445a-be57-4140-a839-fab46b42c76e&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fapi%2fsplitter.js"> splitter.js</a> A
test can be seen at <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=9b30445a-be57-4140-a839-fab46b42c76e&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fsplitter.html"> splitter.html</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9b30445a-be57-4140-a839-fab46b42c76e" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Error handling in WCF based web applications</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/05/13/ErrorHandlingInWCFBasedWebApplications.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f.aspx</id>
    <published>2013-05-13T16:09:02.278-07:00</published>
    <updated>2013-05-14T00:41:35.1869553-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="ASP.NET" label="ASP.NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ASPNET.aspx" />
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="kendoui" label="kendoui" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,kendoui.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Arthur Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Cool:GEN links</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/04/29/CoolGENLinks.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7a17cd30-d829-4b17-946a-a4da45143faa.aspx</id>
    <published>2013-04-28T20:54:56.2312177-07:00</published>
    <updated>2013-04-28T20:54:56.2312177-07:00</updated>
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Useful links to those who are still dealing with Cool:GEN.
</p>
        <ul>
          <li>
            <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a17cd30-d829-4b17-946a-a4da45143faa&amp;url=http%3a%2f%2fwww.iet.co.uk%2f">Information
Engineering Technology</a>
          </li>
          <li>
            <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a17cd30-d829-4b17-946a-a4da45143faa&amp;url=http%3a%2f%2fietgen.blogspot.co.uk%2f">IET
Gen Development Blog</a>
          </li>
        </ul>
        <p>
The site is not cool by itself, but the value is in the tools that authors provide
to simplify Cool:GEN development. Especially we would like to mention:
</p>
        <ul>
          <li>
GuardIEn - Version control, change and model management and automated builds for CA
Gen, and</li>
          <li>
VerifIEr - Automated code checking and standards verification.</li>
        </ul>
        <p>
These tools help to manage clean and error free models, which simplifies next migration
to Java and C# that we perform.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7a17cd30-d829-4b17-946a-a4da45143faa" />
      </div>
    </content>
  </entry>
  <entry>
    <title>KendoUI Window turned to the worse</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/04/18/KendoUIWindowTurnedToTheWorse.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,99e6d3c9-5860-4ccf-b565-93b68e434a30.aspx</id>
    <published>2013-04-18T03:44:24.33-07:00</published>
    <updated>2013-04-18T03:45:51.152669-07:00</updated>
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="kendoui" label="kendoui" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,kendoui.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
We have upgraded KendoUI and have found that kendo window has stopped to size properly.
</p>
        <p>
In the old implementation window set dimensions like this:
</p>
        <p style="padding-left: 1em">
          <code> _dimensions: function() { 
<br />
  ...<br /><strong>  if (options.width) { 
<br />
    wrapper.width(options.width); 
<br />
  } 
<br />
  if (options.height) { 
<br />
    wrapper.height(options.height); 
<br />
  } 
<br />
  </strong>... 
<br />
}</code>
        </p>
        <p>
And here is a new implementation:
</p>
        <p style="padding-left: 1em">
          <code> _dimensions: function() { 
<br />
  ...<br /><strong>  if (options.width) { 
<br />
    wrapper.width(constrain(<span style="color: red">parseInt(options.width,
10)</span>, options.minWidth, options.maxWidth)); 
<br />
  } 
<br />
  if (options.height) { 
<br />
    wrapper.height(constrain(<span style="color: red">parseInt(options.height,
10)</span>, options.minHeight, options.maxHeight)); 
<br />
  } 
<br /></strong>  ...<br />
} </code>
        </p>
        <p>
Thus nothing but pixels are supported. Earlier we often used <code>'em'</code> units
to define dialog sizes. There was no reason to restrict it like this. That's very
unfortunate. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=99e6d3c9-5860-4ccf-b565-93b68e434a30" />
      </div>
    </content>
  </entry>
  <entry>
    <title>KendoUI extended model</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/04/03/KendoUIExtendedModel.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,3e27a213-6843-4ad2-b603-6bc29f0ea3e0.aspx</id>
    <published>2013-04-03T13:37:49.327-07:00</published>
    <updated>2013-04-04T02:16:56.111999-07:00</updated>
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
To simplify KendoUI development we have defined <code>nesterovskyBros.data.Model</code>,
which extends <code>kend.data.Model</code> class. 
</p>
        <h3>Extensions in <code>nesterovskyBros.data.Model</code></h3>
        <ol>
          <li>
As with <code>kendo.data.Model</code> there is <code>fields</code> Object - a set
of key/value pairs to configure the model fields, but fields have some more options: 
<ul><li><code>fields.fieldName.serializable Boolean</code> - indicates whether the field appears
in an object returned in <code>model.toJSON()</code>. Default is <code>true</code>.</li><li><code>fields.fieldName.updateDirty Boolean</code> - indicates whether the change of
the property should trigger <code>dirty</code> field change. Default is <code>true</code>.</li></ul></li>
          <li>
When model defines a field and there is a prototype function with the same name then
this function is used to get and set a field value.</li>
          <li>
When property is changed through the <code>model.set()</code> method then <code>dirty</code> change
event is triggered (provided that fields.fieldName.updateDirty !== false). This helps
to build a dependcy graph on that property.</li>
          <li>
When model instance is consturcted, the data passed in are validated, nullable and
default values are set.</li>
        </ol>
        <h3>Model example
</h3>
        <p>
Here is an example of a model:
</p>
        <p style="padding-left: 1em">
          <code>nesterovskyBros.data.ProductModel = nesterovskyBros.data.Model.define(<br />
{<br /><br />
fields:<br />
{<br />
  name: { type: "string", defaultValue: "Product Name" },<br />
  price: { type: "number", defaultValue: 10 },<br />
  unitsInStockValue: { type: "number", defaultValue: 10, serializable: false
},<br />
  unitsInStock: { type: "string" }<br />
},<br /><br />
unitsInStock: function(value)<br />
{<br />
  if (value === undefined)<br />
  {<br />
    var count = this.get("unitsInStockValue");<br /><br />
    return ["one", "two", "three", "four"][count] || (count + "");<br />
  }<br />
  else<br />
  {<br />
    this.set("unitsInStockValue", ({one: 1, two: 2, three: 3, four:
4 })[value] || value);<br />
  }<br />
}<br /><br />
}); </code>
        </p>
        <p>
Notice that:
</p>
        <ul>
          <li>
            <code>unitsInStock</code> property is implemented as a function - this helps to map
model values to presentation values. 
</li>
          <li>
when you call <code>model.toJSON()</code>, or <code>JSON.stringify()</code> you will
see in result <code>name</code>, <code>price</code>, <code>unitsInStock</code> values
only - this helps to get model's state and to store it somewhere (e.g. in <code>sessionStorage</code>).</li>
          <li>
in a code:<br /><code>  var model = new nesterovskyBros.data.ProductModel({ price: "7",
unitsInStock: "one" });</code><br />
the following is true: 
<br /><code>  (typeof(model.price) == "number") &amp;&amp; (mode.price ==
7) &amp;&amp; (model.name == "Product Name") &amp;&amp; (model.unitsInStockValue
== 1)</code></li>
        </ul>
        <p>
As with <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3e27a213-6843-4ad2-b603-6bc29f0ea3e0&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2013%2f03%2f26%2fKendoUIUserControl.aspx">UserControl</a> the
implemntation is defined in the <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3e27a213-6843-4ad2-b603-6bc29f0ea3e0&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fapi%2fcontrols.js">controls.js</a>.
The sample page is the same <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=3e27a213-6843-4ad2-b603-6bc29f0ea3e0&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2findex.html">index.html</a></p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=3e27a213-6843-4ad2-b603-6bc29f0ea3e0" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Windows 8 + Lenovo Yoga-13 + external SD card</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/03/28/Windows8LenovoYoga13ExternalSDCard.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,cf1d58a7-dfd3-43cb-b12c-0127adc05d1e.aspx</id>
    <published>2013-03-28T15:39:55.652-07:00</published>
    <updated>2013-03-28T21:53:27.7960595-07:00</updated>
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Arthur Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Two weeks ago we've gotten new Lenovo 13" laptops (Yoga-13 with touch screens and
Windows 8 Pro on board).
</p>
        <p>
The first expression was WOW! Touch screens! Windows 8! Now we'll try our hand on
that new (for us) API. So new, so cool...
</p>
        <p>
          <b>A day later.</b> What a shit this new UI. Where are my desktop, "Start" button,
all the programs... After googling we've understood - we're not alone.
</p>
        <p>
          <b>Few more days later.</b> We've recognized that our SSD hard disk won't live long
life with our projects. We generates output several GB a day. Thus we've decided to
buy external SD cards - additional 64Gb, class 10. That's enough for us. No sooner
said than done. After several attempts to copy our projects from hard drive to SD
card (~9Gb of sources) we strongly believe that such a vigorous mix (Lenovo + Win
8 + external SD card) won't survive. Windows 8 hangs up when display off (in middle
of data copy, after an hour of work). What a .... of .... this Windows 8, Lenovo and
SD cards all together.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=cf1d58a7-dfd3-43cb-b12c-0127adc05d1e" />
      </div>
    </content>
  </entry>
  <entry>
    <title>KendoUI User control</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/03/26/KendoUIUserControl.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,f4337446-1b5e-4007-a1f8-a62b9d99681f.aspx</id>
    <published>2013-03-26T14:40:05.473-07:00</published>
    <updated>2013-03-27T11:56:41.5681775-07:00</updated>
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Developing with KendoUI we try to formalize tasks. With this in mind we would like
to have user controls. 
</p>
        <p>
We define user control as following:
</p>
        <p style="padding-left: 1em">
It is a javascript class that extends Widget.<br />
It offers a way to reuse UI.<br />
It allows to define a model and a template with UI and data binding.
</p>
        <p>
Unfortunately, KendoUI does not have such API, though one can easily define it; so
we have defined our version.
</p>
        <p>
Here we review our solution. We have taken a grid KendoUI example and converted it
into a user control.
</p>
        <h3 style="direction: ltr">User control on the page
</h3>
        <p style="direction: ltr">
See <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2findex.html">index.html</a></p>
        <p style="padding-left: 1em">
          <code style="direction: ltr">&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
  &lt;title&gt;Test&lt;/title&gt;<br /><br />
  <strong>&lt;!-- (1) Include templates for controls. --&gt;</strong><br />
  &lt;script src="<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2ftemplates.js">scripts/templates.js</a>"&gt;&lt;/script&gt;<br /><br />
  &lt;script src="scripts/jquery/jquery.js"&gt;&lt;/script&gt;<br />
  &lt;script src="scripts/kendo/kendo.web.min.js"&gt;&lt;/script&gt;<br /><br />
  <strong>&lt;!-- (2) UserControl definition. --&gt;</strong><br />
  &lt;script src="<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fcontrols.js">scripts/controls.js</a>"&gt;&lt;/script&gt;<br /><br />
  <strong>&lt;!-- (3) Confirm dialog user control. --&gt;</strong><br />
  &lt;script src="<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fcontrols%2fconfirm.js">scripts/controls/confirm.js</a>"&gt;&lt;/script&gt;<br /><br />
  <strong>&lt;!-- (4) Products user control. --&gt;</strong><br />
  &lt;script src="<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fcontrols%2fproducts.js">scripts/controls/products.js</a>"&gt;&lt;/script&gt;<br /><br />
  &lt;link href="styles/kendo.common.min.css" rel="stylesheet" /&gt;<br />
  &lt;link href="styles/kendo.default.min.css" rel="stylesheet" /&gt;<br />
  &lt;script&gt;<br />
$(function ()<br />
{<br />
  <strong>// (5) Bind the page.</strong><br />
  kendo.bind(<br />
    document.body,<br />
    <strong>// (6) Model as a datasource.</strong><br />
    { source: [new nesterovskyBros.data.ProductsModel] });<br />
});<br />
  &lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
  <strong>&lt;!-- (7) User control and its binding. --&gt;</strong><br />
  &lt;div data-role="<strong>products</strong>" data-bind="<strong>source: source</strong>"&gt;&lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt; </code>
        </p>
        <p>
That's what we see here:
</p>
        <ol>
          <li>
Templates that define layouts. See "<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2013%2f01%2f06%2fHowToLoadKendoUITemplatesFromExternalFiles.aspx">How
To: Load KendoUI Templates from External Files</a>", and <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2ftemplates.tt">templates.tt</a>.</li>
          <li>
Definition of the UserControl widget.</li>
          <li>
Confirm dialog user control (we shall mention it later).</li>
          <li>
Products user control.</li>
          <li>
Data binding that instantiates page controls.</li>
          <li>
Model is passed to a user control through the dataSource.</li>
          <li>
Use of Products user control. Notice that "data-role" defines control type,
"source" refers to the model.</li>
        </ol>
        <h3>User Control declaration
</h3>
        <p>
Declaration consists of a view and a model. 
</p>
        <p>
View is html with data binding. See <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2ftemplates%2fproducts.tmpl.html">products.tmpl.html</a></p>
        <p>
We build our project using Visual Studio, so templates packaging is done with <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2ftemplates.tt">templates.tt</a>.
This transformation converts products template into a tag:
</p>
        <p style="padding-left: 1em">
          <code>&lt;script id="products-template" type="text/x-kendo-template"&gt;</code>
        </p>
        <p>
thus template can be referred by a utility function: <code>nesterovskyBros.template("products-template")</code>.
</p>
        <p>
Model inherits kedo.data.Model. Here how it looks:
</p>
        <p style="padding-left: 1em">
          <code>
            <strong>// (1) Define a ProducsModel class</strong>.<br />
nesterovskyBros.data.ProductsModel = <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fdocs.kendoui.com%2fapi%2fframework%2fmodel">kendo.data.Model.define</a>(<br />
{<br /><br /><strong>// (2) Model properties.</strong><br />
fields:<br />
{<br />
  productName: { type: "string", defaultValue: "Product Name" },<br />
  productPrice: { type: "number", defaultValue: 10 },<br />
  productUnitsInStock: { type: "number", defaultValue: 10 },<br />
  products: { type: "default", defaultValue: [] }<br />
},<br /><br /><strong>// (3) Model methods.</strong><br />
addProduct: function () { ... },<br />
deleteProduct: function (e) { ... },<br />
...<br /><br />
});<br /><br /><strong>// (4) Register user control.</strong><br />
nesterovskyBros.ui.Products = nesterovskyBros.defineControl(<br />
{<br />
  name: "Products",<br />
  model: nesterovskyBros.data.ProductsModel<br />
});</code>
        </p>
        <p>
That's what we have here:
</p>
        <ol>
          <li>
We define a model that inherits KendoUI Model.</li>
          <li>
We define model fields.</li>
          <li>
We define model methods.</li>
          <li>
Register user control with  <code>nesterovskyBros.defineControl(proto)</code> call,
where:<br /><ul><li><code>proto.name</code> - defines user control name;</li><li><code>proto.model</code> - defines model type;</li><li><code>proto.template</code> - defines optional template. If not specified, a template
is retrieved from <code>$("#" + proto.name.toLowerCase() + "-template").html()</code>.</li></ul></li>
        </ol>
        <h3>UserControl API
</h3>
        <p>
Now, what's remained is API for the UserControl. See <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fcontrols.js">controls.js</a>.
</p>
        <ol>
          <li>
UserControl defines following events:<br /><ul><li><code>change</code> - triggered when data source is changed;</li><li><code>dataBound</code> - triggered when widget is data bound;</li><li><code>dataBinding</code> - triggered befor widget data binding;</li><li><code>save</code> - used to notify user to save model state.</li></ul></li>
          <li>
UserControl defines following options:<br /><ul><li><code>autoBind (default false)</code> - autoBind data source;</li><li><code>template (default $.noop)</code> - user control template.</li></ul></li>
          <li>
UserControl defines <code>dataSource</code> field and <code>setDataSource()</code> method.</li>
          <li>
UserControl defines <code>rebind()</code> method to manually rebuild widget's
view from the template and model.</li>
          <li>
UserControl sets/deletes model.owner, which is a function returning a user control
widget when model is bound/unbound to the widget.</li>
          <li>
When UserControl binds/unbinds model a <code>model.refresh</code> method is called,
if any.</li>
          <li>
You usually define you control with a call <code>nesterovskyBros.defineControl(proto)</code>.
See above.</li>
          <li>
There is also a convenience method to build a dialog based on a user control: nesterovskyBros.defineDialog(options),
where<br /><ul><li><code>options.name</code> - a user control name (used in the data-role);</li><li><code>options.model</code> - a model type;</li><li><code>options.windowOptions</code> - a window options.</li></ul>
This method returns a function that recieves a user control model, and returns a dialog
(<code>kendo.ui.Window</code>) based on the user control.<br />
Dialog has <code>model()</code> function that returns an instance of model.<br />
Model has <code>dialog()</code> function that returns an instance of the dialog.<br />
Dialog and model have <code>result()</code> function that returns an instance of deferred
object used to track dialog completion.<br />
The example of user control dialog is <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fcontrols%2fconfirm.js">confirm.js</a> and <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2ftemplates%2fconfirm.tmpl.html">confirm.tmpl.html</a>.
The use is in the <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fKendoUI%2fscripts%2fcontrols%2fproducts.js">products.js</a> deleteProduct():<br /><p style="padding-left: 1em"><code>deleteProduct: function(e) 
<br />
{<br />
  var that = this;<br /><br />
  return nesterovskyBros.dialog.confirm(<br />
  {<br />
    title: "Please confirm",<br />
    message: "Do you want to delete the record?",<br />
    confirm: "Yes",<br />
    cancel: "No"<br />
  }).<br />
  open().<br />
  center().<br />
  result().<br />
  then(<br />
    function(confirmed)<br />
    {<br />
      if (!confirmed)<br />
      {<br />
        return;<br />
      }<br />
      ...<br />
   });<br />
}</code></p></li>
        </ol>
        <h3>Last
</h3>
        <p>
User controls along with technique to manage and cache templates allow us to build
robust web applications. As the added value it's became a trivial task to build
SPA.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=f4337446-1b5e-4007-a1f8-a62b9d99681f" />
      </div>
    </content>
  </entry>
  <entry>
    <title>How to associate a label and avoid id conflicts in html</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/02/11/HowToAssociateALabelAndAvoidIdConflictsInHtml.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,89749038-a489-4354-8f0a-f35c7f3c73b7.aspx</id>
    <published>2013-02-11T13:40:18.95-08:00</published>
    <updated>2013-02-11T21:30:17.5117935-08:00</updated>
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
At present we inhabit in jquery and kendoui world. 
</p>
        <p>
There you deal with MVVM design pattern and build you page from blocks. To avoid conflicts
you usually restrict yourself from assigning ids to elements, as they make code reuse
somewhat problematic.
</p>
        <p>
But what if you have a label that you would like to associate with an input. In plain
html you would write:
</p>
        <p style="padding-left: 1em">
          <code>&lt;label <strong>for="my-input"</strong>&gt;My label:&lt;/label&gt;
&lt;input <strong> id="my-input"</strong> type="text"&gt;</code>
        </p>
        <p>
Html spec suggests to use element id to build such an association.
</p>
        <p>
So, how to avoid introduction of id, and to allow to select input while clicking on
the label?
</p>
        <p>
In our projects we use a little utility function that solves exactly this task. It's
easier to quote an example than to describe implementation:
</p>
        <p style="padding-left: 1em">
          <code>&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Label&lt;/title&gt;<br />
&lt;script src="scripts/jquery.js"&gt;&lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;div class="view"&gt;<br />
&lt;div&gt;A template:&lt;/div&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;&lt;label data-for="[name=field1]"&gt;Name1:&lt;/label&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;input name="field1" type="text" /&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;&lt;label data-for="[name=field2]"&gt;Name2:&lt;/label&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;input name="field2" type="text" /&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;&lt;label data-for="[name=field3]"&gt;Name3:&lt;/label&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;input name="field3" type="text" /&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;&lt;label data-for="[name=field4]"&gt;Name4:&lt;/label&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;input name="field4" type="checkbox" /&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;&lt;label data-for="[name=field5][value=0]"&gt;Name5:&lt;/label&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;input name="field5" value="0" type="radio" /&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;&lt;label data-for="[name=field5][value=1]"&gt;Name6:&lt;/label&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;input name="field5" value="1" type="radio" /&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;/div&gt;<br />
&lt;script&gt;<br />
$(document).on(<br />
"click",<br />
"label[data-for]",<br />
function(e)<br />
{<br />
var target = $(e.target);<br /><br />
target.closest(target.attr("data-view") || ".view").<br />
find(target.attr("data-for")).<br />
filter(":visible:enabled").first().click().focus().<br />
filter("input[type=checkbox],input[type=radio]").change();<br />
});<br />
&lt;/script&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt; </code>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=89749038-a489-4354-8f0a-f35c7f3c73b7" />
      </div>
    </content>
  </entry>
  <entry>
    <title>jquery's Ticket's #7054 (closed bug: fixed)</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/02/10/jquerysTickets7054ClosedBugFixed.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9788868d-d1a2-4d8e-a1c5-2f8d41ce2aff.aspx</id>
    <published>2013-02-09T21:17:33.659-08:00</published>
    <updated>2013-02-11T05:26:10.2502197-08:00</updated>
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
In our applications we must support IE 8, and unfortunately we hit some leak, which
is registered as <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=9788868d-d1a2-4d8e-a1c5-2f8d41ce2aff&amp;url=http%3a%2f%2fbugs.jquery.com%2fticket%2f7054">Ticket
#7054(closed bug: fixed)</a>.
</p>
        <p>
While bug declared closed as fixed we can see that memory leak in <strong>IE8</strong> like
a mad. Not sure if something can be done about it. 
</p>
        <p>
The test case is:
</p>
        <p style="padding-left: 1em">
          <code>&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
  &lt;title&gt;Test&lt;/title&gt;<br />
  &lt;script src="scripts/jquery/<strong>jquery-1.9.0.js</strong>"&gt;&lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;script&gt;<br />
function testLeak()<br />
{<br />
  var handler = function () { };<br /><br />
  $('&lt;div&gt;&lt;/div&gt;').html(new Array(1000).join(new Array(1000).join('x'))).bind('abc',
handler).appendTo('#test').remove();<br />
}<br /><br />
$(function() { setInterval(testLeak, 1000); });<br />
&lt;/script&gt;<br /><strong>&lt;div id="test"&gt;&lt;/div&gt;</strong><br />
&lt;/body&gt;<br />
&lt;/html&gt; </code>
        </p>
        <p>
Update: jaubourg has pointed that we have missed to define element with id="test".
With this element leak stops.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9788868d-d1a2-4d8e-a1c5-2f8d41ce2aff" />
      </div>
    </content>
  </entry>
  <entry>
    <title>How To: Load KendoUI Templates from External Files</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2013/01/06/HowToLoadKendoUITemplatesFromExternalFiles.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b17502e5-9edf-43d2-9fab-7a5a3124ddf5.aspx</id>
    <published>2013-01-06T11:43:19.8172509-08:00</published>
    <updated>2013-01-06T11:43:19.8172509-08:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="ASP.NET" label="ASP.NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ASPNET.aspx" />
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>StreamResource</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/11/20/StreamResource.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,40a04a86-ce1f-44a2-8daa-f54306b84e20.aspx</id>
    <published>2012-11-19T23:01:57.8773735-08:00</published>
    <updated>2012-11-19T23:01:57.8773735-08:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Change column type in SQL Server, Part 2</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/11/09/ChangeColumnTypeInSQLServerPart2.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,6d962206-6ca4-4ed5-828b-551082e660dc.aspx</id>
    <published>2012-11-09T08:41:27.203-08:00</published>
    <updated>2012-11-10T00:47:59.2612087-08:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Export data to Excel</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/10/29/ExportDataToExcel.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,98b58257-9069-4d13-9d10-a7061e14e834.aspx</id>
    <published>2012-10-29T08:34:38.779-07:00</published>
    <updated>2012-11-06T07:25:02.4381177-08:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="ASP.NET" label="ASP.NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ASPNET.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="xslt" label="xslt" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,xslt.aspx" />
    <author>
      <name>Arthur Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Over optimization in sql server</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/10/29/OverOptimizationInSqlServer.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,f6c7bf47-0313-44d5-a3a2-6aefc7719112.aspx</id>
    <published>2012-10-29T01:34:39.862-07:00</published>
    <updated>2012-10-29T01:40:48.3364346-07:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Recently we had a discussion with DBA regarding optimization strategey we have selected
for some queries.
</p>
        <p>
We have a table in our database. These are facts about that table:
</p>
        <ul>
          <li>
the table is partitioned by date;</li>
          <li>
each partition contains a month worth of data;</li>
          <li>
the table contains at present about 110 million rows;</li>
          <li>
the table ever grows;</li>
          <li>
the table is most accessed in the database;</li>
          <li>
the most accessed part of the data is related to last 2-3 days, which is about 150000
rows.</li>
        </ul>
        <p>
The way we have optimized access to that table was a core of the dispute. 
</p>
        <p>
We have created filtered index that includes data for the last 3 days.
</p>
        <p>
To achieve desired effect we had to:
</p>
        <ul>
          <li>
create a job that recreates that index once a day, as filter condition is moving;</li>
          <li>
adjust queries that access the table, as we had to use several access pathes to the
table depending on date.</li>
        </ul>
        <p>
As result we can see that under the load, stored procedures that access that table
became almost 50% faster. On the other hand maintainance became more complicated.
</p>
        <p>
DBA who didn't like the database complications had to agree that there are speed
improvements. He said that there should be a better way to achieve the same effect
but could not find it.
</p>
        <p>
Are there a better way to optimize access to this table?
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=f6c7bf47-0313-44d5-a3a2-6aefc7719112" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Change column type in SQL Server 2008 R2</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/09/07/ChangeColumnTypeInSQLServer2008R2.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,a030c1e6-63cc-4bbe-8cc1-e64243b07c39.aspx</id>
    <published>2012-09-07T13:57:36.998-07:00</published>
    <updated>2012-09-08T06:46:20.5100562-07:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
We're implementing UDT changes in the big database. Earlier, that User Defined
Type was based on <code>smallint</code>, and now we have to use <code>int</code> as
the base.
</p>
        <p>
The impact here is manyfold:
</p>
        <ol>
          <li>
Clients of the database should be prepared to use wider types.</li>
          <li>
All stored procedures, functions, triggers, and views should be updated accordingly.</li>
          <li>
Impact on the database size should be analyzed.</li>
          <li>
Types of columns in tables should be changed.</li>
          <li>
Performance impact should be minimal.</li>
        </ol>
        <p>
Now, we're trying to address (3), (5) and to implement (4), while trying to keep
interface with clients using old types.
</p>
        <p>
As for database size impact, we have found that an index fragmentation is a 
primary disk space waster (see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a030c1e6-63cc-4bbe-8cc1-e64243b07c39&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f08%2f30%2fReorganizeIndexInSQLServer.aspx">Reorganize
index in SQL Server</a>). We have performed some partial index reorganization and
can see now that we can gain back hundreds of GB of a disk space. On the other hand
we use page compression, so we expect that change of types will not increase sizes
of tables considerably. Indeed, our measurments show that tables will only be ~1-3%
bigger.
</p>
        <p>
The change of types of columns is untrivial task. The problem is that if you try to
change column's type (which is part of clustered index) directly then you should
temporary remove foreign keys, and to rebuild all indices. This won't work neither
due to disk space required for the operation (a huge transaction log is required),
nor due to availability of tables (we're talking about days or even weeks to rebuild
indices).
</p>
        <p>
To work-around the problem we have selected another way. For each target table T we
performed the following:
</p>
        <ul>
          <li>
Renamed table T to T_old;</li>
          <li>
Created a table T_new with required type changes;</li>
          <li>
Created a view named T, which is union of T_old for the dates before a split date
and T_new for the dates after the split date;</li>
          <li>
Created instead of insert/update/delete triggers for the view T.</li>
          <li>
Created a procedures that move data in bulks from T_old to the T_new, update split
date in view definitions, and delete data from T_old.</li>
        </ul>
        <p>
Note that:
</p>
        <ul>
          <li>
the new view uses wider column types, so we had to change stored procedures that clients
use to cast those columns back to shorter types to prevent side effects (fortunately
all access to this database is through stored procedures and functions);</li>
          <li>
the procedures that transfer data between new and old tables may work online;</li>
          <li>
the quality of execution plans did not degrade due to switch from table to a view;</li>
          <li>
all data related to the date after the split date are inserted into T_new table.</li>
        </ul>
        <p>
After transfer will be complete we shall drop T_old tables, and T views, and will
rename T_new tables into T.
</p>
        <p>
This will complete part 4 of the whole task. Our estimations are that it will take
a month or even more to complete the transfer. However solution is rather slow, the
database will stay online whole this period, which is required condition.
</p>
        <p>
The next task is to deal with type changes in parameters of stored procedures and
column types of output result sets. We're not sure yet what's the best way
to deal with it, and probably shall complain about in in next posts.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=a030c1e6-63cc-4bbe-8cc1-e64243b07c39" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Numbers table in SQL Server - year 2012</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/09/01/NumbersTableInSQLServerYear2012.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,2776c571-a218-4163-bcd6-60364eefa40c.aspx</id>
    <published>2012-09-01T13:16:49.073-07:00</published>
    <updated>2012-09-01T13:18:13.1296049-07:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Back in 2006 and 2007 we have defined <code>dbo.Numbers</code> function: <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=2776c571-a218-4163-bcd6-60364eefa40c&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2006%2f10%2f02%2fNumbersTableInSQLServer2005.aspx"> Numbers
table in SQL Server 2005</a>, <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=2776c571-a218-4163-bcd6-60364eefa40c&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2007%2f02%2f23%2fParadeOfNumbers.aspx"> Parade
of numbers</a>. Such construct is very important in a set based programming. E.g.
XPath 2 contains a range expression like this: <code>"1 to 10"</code> to
return a sequence of numbers. Unfortunately neither SQL Server 2008 R2, nor SQL Server
2012 support such construct, so <code>dbo.Numbers</code> function is still actual. 
</p>
        <p>
After all these years the function evolved a little bit to achieve a better performance.
Here is its source:
</p>
        <p style="padding-left: 1em">
          <code>-- Returns numbers table.<br />
-- Table has a following structure: table(value int not null);<br />
-- value is an integer number that contains numbers from 1 to a specified value.<br />
create function dbo.Numbers<br />
( 
<br />
-- Number of rows to return.<br />
@count int<br />
)<br />
returns table 
<br />
as<br />
return 
<br />
with Number8 as<br />
(<br />
select 
<br />
* 
<br />
from<br />
(<br />
values 
<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0),<br />
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0)<br />
) N(Value)<br />
),<br />
Number32(Value) as<br />
(<br />
select 
<br />
0 
<br />
from 
<br />
Number8 N1<br />
left join<br />
Number8 N2 
<br />
on<br />
@count &gt; 0x100<br />
left join<br />
Number8 N3<br />
left join<br />
Number8 N4<br />
on<br />
@count &gt; 0x1000000<br />
on<br />
@count &gt; 0x10000<br />
)<br />
select top(@count) row_number() over(order by @count) Value from Number32; </code>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=2776c571-a218-4163-bcd6-60364eefa40c" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Reorganize index in SQL Server</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/08/30/ReorganizeIndexInSQLServer.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,af238304-6c85-4127-95cf-ed2f4407d468.aspx</id>
    <published>2012-08-30T01:59:29.4737904-07:00</published>
    <updated>2012-08-30T01:59:29.4737904-07:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
We're working with an online database, which is ever populated with a new data.
Database activity is mostly around recent data. Activity against older data declines
with increasing the distance from today. The ratio of an amount of a new data, say
for a last month, to the whole data, at present stays at ~1%. The size of database
is measured in TBs.
</p>
        <p>
While we're developers and not DBA's,  you will see from a later blog
posts why we're bothered with the database size. In short we're planning to
change some UDF type from <code>smallint</code> to <code>int</code>. This will impact
on many tables, and the task now is to estimate that impact.
</p>
        <p>
Our first attempts to measure the difference between table sizes before and after
type change showed that a data fragmentation often masks the difference, so we started
to look at a way to reduce fragmentation.
</p>
        <p>
Internet is full with recomentations. An advice can be found in BOL at <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=af238304-6c85-4127-95cf-ed2f4407d468&amp;url=http%3a%2f%2ftechnet.microsoft.com%2fen-us%2flibrary%2fms189858.aspx">Reorganize
and Rebuild Indexes</a>.
</p>
        <p>
So, our best help in this task is the function <code>sys.dm_db_index_physical_stats</code>,
which reports statistics about fragmentation.
</p>
        <p>
Analysing what that function has given to us we could see that we had a highly fragmented
data. There was no reason to bear with that taking into an account that the most of
the data stored in the database is historical, which is rarely accessed and even more
rarely updated.
</p>
        <p>
The next simplest instument adviced is:
</p>
        <p style="padding-left: 1em">
          <code>alter index { index_name | ALL } on &lt;object&gt; reorganize [ PARTITION =
partition_number ];</code>
        </p>
        <p>
The less trivial but often more efficient instrument is the use of online index rebuild
and index reorganize depending on index type and a level of fragmentation. 
</p>
        <p>
All in all our estimation is that rebuilding or reorganizing indices frees ~100-200GBs
of disk space. While, it's only a small percent of total database size, it gives
us several monthes worth of a disk space! 
</p>
        <p>
Earlier we overlooked SQL Server API to monitor fragmentation, rebuild, and reorganize
indices, and now we're going to create a job that will regulary defragment the
database.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=af238304-6c85-4127-95cf-ed2f4407d468" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Snapshot of data per last date available</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/08/19/SnapshotOfDataPerLastDateAvailable.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8fcf8ab3-2bc8-4ef5-b27e-dee6bac94e3a.aspx</id>
    <published>2012-08-18T22:57:55.903-07:00</published>
    <updated>2012-08-19T03:30:08.5645897-07:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Arthur Nesterovsky</name>
    </author>
    <content type="html">  &lt;p&gt;
We have a large table in the form:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;create table dbo.Data&lt;br /&gt;
(&lt;br /&gt;
Date date not null,&lt;br /&gt;
Type int not null,&lt;br /&gt;
Value nvarchar(50) null,&lt;br /&gt;
primary key clustered(Date, Type)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
create unique nonclustered index IX_Data on dbo.Data(Type, Date);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Among other queries we often need a snapshot of data per each &lt;code&gt;Type&lt;/code&gt; for
a latest &lt;code&gt;Date&lt;/code&gt; available:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; select&lt;br /&gt;
max(Date) Date,&lt;br /&gt;
Type&lt;br /&gt;
from&lt;br /&gt;
dbo.Data&lt;br /&gt;
group by&lt;br /&gt;
Type&lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
We have found that the above select does not run well on our data set. In fact &lt;code&gt;dbo.Data&lt;/code&gt; grows
with time, while snapshot we need stays more or less of the same size. The best solution
to such query is to precalculate it. One way would be to create an indexed view, but
SQL Server does not support &lt;code&gt;max()&lt;/code&gt; aggregate in indexed views. 
&lt;/p&gt;
&lt;p&gt;
So, we have decided to add additional &lt;code&gt;bit&lt;/code&gt; field &lt;code&gt;dbo.Data.Last&lt;/code&gt; indicating
that a row belongs to a last date snapshot, and to create filtered index to access
that snapshot:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;create table dbo.Data&lt;br /&gt;
(&lt;br /&gt;
Date date not null,&lt;br /&gt;
Type int not null,&lt;br /&gt;
Value nvarchar(50) null,&lt;br /&gt;
&lt;strong&gt;Last bit not null default 0,&lt;br /&gt;
&lt;/strong&gt; primary key clustered(Date, Type)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
create unique nonclustered index IX_Data on dbo.Data(Type, Date);&lt;br /&gt;
&lt;br /&gt;
create unique nonclustered index IX_Data_Last on dbo.Data(Type) 
&lt;br /&gt;
include(Date) 
&lt;br /&gt;
where Last = 1;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
One way to support &lt;code&gt;Last&lt;/code&gt; indicator is to create a trigger that will adjust &lt;code&gt;Last&lt;/code&gt; value:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;create trigger dbo.Data_Update on dbo.Data&lt;br /&gt;
after insert,delete,update&lt;br /&gt;
as 
&lt;br /&gt;
begin&lt;br /&gt;&lt; 2)&lt;br /&gt;
if (trigger_nestlevel(@@procid) begin&lt;br /&gt;
set nocount on;&lt;br /&gt;
&lt;br /&gt;
with D as&lt;br /&gt;
(&lt;br /&gt;
select Date, Type from deleted&lt;br /&gt;
union&lt;br /&gt;
select Date, Type from inserted&lt;br /&gt;
),&lt;br /&gt;
U as&lt;br /&gt;
(&lt;br /&gt;
select&lt;br /&gt;
V.Date, V.Type&lt;br /&gt;
from&lt;br /&gt;
D&lt;br /&gt;
inner join&lt;br /&gt;
dbo.Data V&lt;br /&gt;
on&lt;br /&gt;
(V.Last = 1) and&lt;br /&gt;
(V.Type = D.Type)&lt;br /&gt;
union&lt;br /&gt;
select&lt;br /&gt;
max(V.Date) Date,&lt;br /&gt;
V.Type&lt;br /&gt;
from&lt;br /&gt;
D&lt;br /&gt;
inner join&lt;br /&gt;
dbo.Data V&lt;br /&gt;
on&lt;br /&gt;
V.Type = D.Type&lt;br /&gt;
group by&lt;br /&gt;
V.Type&lt;br /&gt;
),&lt;br /&gt;
V as&lt;br /&gt;
(&lt;br /&gt;
select&lt;br /&gt;
rank() over(partition by V.Type order by V.Date desc) Row,&lt;br /&gt;
V.*&lt;br /&gt;
from&lt;br /&gt;
dbo.Data V&lt;br /&gt;
inner join&lt;br /&gt;
U&lt;br /&gt;
on&lt;br /&gt;
(V.Date = U.Date) and&lt;br /&gt;
(V.Type = U.Type)&lt;br /&gt;
)&lt;br /&gt;
update V set Last = 1 - cast(Row - 1 as bit);&lt;br /&gt;
end;&lt;br /&gt;
end; &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
With &lt;code&gt;Last&lt;/code&gt; indicator in action, our original query has been transformed
to:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; select Date, Type from dbo.Data where Last = 1&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Execution plan shows that a new filtered index &lt;code&gt;IX_Data_Last&lt;/code&gt; is used.
Execution speed has increased considerably. As our actual table contains other bit
fields, so &lt;code&gt;Last&lt;/code&gt; indicator did not increase the table size, as SQL Server
packs each 8 bit fields in one byte.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=8fcf8ab3-2bc8-4ef5-b27e-dee6bac94e3a" /&gt;</content>
  </entry>
  <entry>
    <title>Stream xslt transformation through WCF</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/08/03/StreamXsltTransformationThroughWCF.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,cad54317-1876-4b98-a8d2-7e44e5dda1f7.aspx</id>
    <published>2012-08-03T15:32:49.727-07:00</published>
    <updated>2012-08-04T12:21:03.7489353-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="ASP.NET" label="ASP.NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ASPNET.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <category term="xslt" label="xslt" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,xslt.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Streaming xslt transformation with ForwardXPathNavigator</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/07/26/StreamingXsltTransformationWithForwardXPathNavigator.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,24094700-ca7e-4e73-8b30-fbdc9d72072a.aspx</id>
    <published>2012-07-26T11:49:51.577-07:00</published>
    <updated>2012-07-26T11:55:53.6665889-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <category term="xslt" label="xslt" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,xslt.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Dictionary extension</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/07/25/DictionaryExtension.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,382cb645-ac89-4cbb-b2f9-14913fb1f743.aspx</id>
    <published>2012-07-24T22:54:33.366-07:00</published>
    <updated>2012-07-24T22:57:31.6157242-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Streaming entity data</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/07/22/StreamingEntityData.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,60b3cb37-5da9-4f28-a35f-38d1ef3cccce.aspx</id>
    <published>2012-07-22T13:38:29.597-07:00</published>
    <updated>2012-07-26T11:54:00.2624849-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <category term="xslt" label="xslt" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,xslt.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title> KendoUI Tips</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/06/24/KendoUITips.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,5e4d06ce-7ca2-4145-ae50-274a4d0231f3.aspx</id>
    <published>2012-06-24T12:59:30.935-07:00</published>
    <updated>2012-06-24T13:03:32.8406338-07:00</updated>
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
For some reason KendoUI DataSource does not allow to access current ajax request.
Indeed, it seems quite natural to have a way to cancel running request.
</p>
        <p>
To achieve a desired effect we have made a small set of changes in the <code>RemoteTransport</code> class:
</p>
        <p>
          <code> var RemoteTransport_setup = kendo.RemoteTransport.fn.setup;<br /></code>
          <br />
          <code> kendo.RemoteTransport.fn.setup = function()<br />
{<br />
var that = this,<br />
options = RemoteTransport_setup.apply(that, arguments),<br />
beforeSend = options.beforeSend;<br /><br />
options.beforeSend = functions(request, options)<br />
{<br />
that.abort();<br /><br />
that._request = request;<br /><br />
if (beforeSend &amp;&amp; (beforeSend.apply(this, arguments) === false))<br />
{<br />
that._request = null;<br /><br />
return false;<br />
}<br /><br />
request.always(function() { that._request = null; });<br />
} 
<br /><br />
return options;<br />
}<br /><br />
kendo.RemoteTransport.fn.request = function()<br />
{<br />
return this._request;<br />
}<br /><br />
kendo.RemoteTransport.fn.abort = function()<br />
{<br />
var request = this._request;<br /><br />
if (request)<br />
{<br />
this._request = null;<br />
request.abort();<br />
}<br />
}</code>
        </p>
        <p>
These changes allow to get an ajax request instance: <code>grid.dataSource.request()</code>,
or to cancel a request <code>grid.dataSource.abort()</code>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=5e4d06ce-7ca2-4145-ae50-274a4d0231f3" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Wish list for the Kendo UI</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/06/17/WishListForTheKendoUI.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,1eeefbd4-00f5-47e4-9748-4179fe7a7a09.aspx</id>
    <published>2012-06-17T13:03:37.1105876-07:00</published>
    <updated>2012-06-17T13:03:37.1105876-07:00</updated>
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
We're pleased to work with Kendo UI. Its design is good, however we find here
and there things we would wish be done better. Here is a list of problems in a no
particular order we would like to be addressed in the next release:
</p>
        <ul>
          <li>
RTL is not supported (including correct scroll bar position see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=1eeefbd4-00f5-47e4-9748-4179fe7a7a09&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2012%2f06%2f11%2fTunningKendoUI.aspx">Tunning
KendoUI</a>).</li>
          <li>
Templates and binding should support a context information along with the data source.
(Why do they use <code>with</code> statement?)</li>
          <li>
attr binding should use jquery.attr() method; there should be prop binding which is
analogous to attr binding.</li>
          <li>
There should be custom binding that allows any json object to bind to different aspects
of a widget or an element.</li>
          <li>
One should be able to use format/parse functions during binding. (Allow binding to
express as a triple json object?)</li>
          <li>
            <code>parseExact(value, format, culture)</code> method should be rewritten, as it
has nothing in common with parsing data string according to exact format.</li>
          <li>
Type inference during binding is poor (<code>parseOption()</code> method). It works
neither for string "1,2", nor json " { x: 0 } ", nor for date.</li>
          <li>
Binding is not implemented for many components: splitter, grid.</li>
          <li>
Splitter's pane should support size="auto".</li>
          <li>
Drid does not support totals in group headers, nor it supports header selection.</li>
          <li>
DataSource does not works after remote error, neither it allows to cancel request.</li>
          <li>
innerHtml is used all over the code, thus one cannot rely on jquery.data().</li>
          <li>
Grid does not support customization (localization) of a column filter.</li>
          <li>
Grid should support data binding of its content.</li>
          <li>
One should be able to destroy any widget.</li>
        </ul>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=1eeefbd4-00f5-47e4-9748-4179fe7a7a09" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Tunning KendoUI</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/06/11/TunningKendoUI.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,563d1d96-9bb4-4bcb-bf00-8702fa2139c2.aspx</id>
    <published>2012-06-11T14:09:44.337-07:00</published>
    <updated>2012-06-11T14:14:04.11333-07:00</updated>
    <category term="javascript" label="javascript" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,javascript.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Trying to make KendoUI to work with Hebrew or more generally in RTL environment we
had to find a way to guess the position of scroll bar when direction is rtl.
</p>
        <p>
The problem exists due to the fact that some browsers (Chrome one of them) always
put scroll bars to the right. That's utterly wrong. Consider a label and a listbox:
</p>
        <table>
          <tr>
            <th>
Chrome 
</th>
            <th>
IE 
</th>
          </tr>
          <tr>
            <td>
              <img alt="List in chrome" style="border: 1px solid red" src="content/binary/list-in-chrome.png" />
            </td>
            <td>
              <img alt="List in chrome" style="border: 1px solid red" src="content/binary/list-in-ie.png" />
            </td>
          </tr>
        </table>
        <p>
You can see that the scroll bar appears between the label (on the right) and the data
in the list box (on the left) in Chrome, and on the left side of the list box in the
IE.
</p>
        <p>
We came up with the following test that calculates a scroll bar position in rtl mode:
</p>
        <p>
          <code>&lt;script type="text/javascript"&gt;<br />
var _scrollbar;<br /><br />
function scrollbar()<br />
{<br />
if (!_scrollbar)<br />
{<br />
var div = document.createElement("div");<br /><br />
div.style.cssText = "overflow:scroll;zoom:1;clear:both;direction:rtl";<br />
div.innerHTML = "&lt;div&gt;&amp;nbsp;&lt;/div&gt;";<br />
document.body.appendChild(div);<br /><br />
_scrollbar =<br />
{<br />
size: div.offsetWidth - div.scrollWidth,<br />
rtlPosition: div.offsetLeft &lt; div.firstChild.offsetLeft ? "left" : "right"<br />
};<br /><br />
document.body.removeChild(div);<br />
}<br /><br />
return _scrollbar;<br />
}<br />
&lt;/script&gt; </code>
        </p>
        <p>
In conjuction with an approach described in <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=563d1d96-9bb4-4bcb-bf00-8702fa2139c2&amp;url=http%3a%2f%2fstackoverflow.com%2fquestions%2f524696%2fhow-to-create-a-style-tag-with-javascript"> How
to create a &lt;style&gt; tag with Javascript</a> we were able to define rtl css classes
for kendo controls and in particular for the grid, combobox, dropdownlist, and datepicker.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=563d1d96-9bb4-4bcb-bf00-8702fa2139c2" />
      </div>
    </content>
  </entry>
  <entry>
    <title>xslt/xquery</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/05/08/xsltxquery.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,558eebcd-e6b3-4238-b8dc-21d0eccaf510.aspx</id>
    <published>2012-05-08T13:28:51.713-07:00</published>
    <updated>2012-05-08T20:38:23.3295008-07:00</updated>
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="xslt" label="xslt" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,xslt.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Some time ago we were taking a part in a project where 95% of all sources are xslt
2.0. It was a great experience for us. 
</p>
        <p>
The interesting part is that we used xslt in areas we would never expect it in early
2000s. It crunched gigabytes of data in offline, while earlier we generally sought
xslt application in a browser or on a server as an engine to render the data.
</p>
        <p>
Web applications (both .NET and java) are in our focus today, and it became hard to
find application for xslt or xquery.
</p>
        <p>
Indeed, client side now have a very strong APIs: jquery, jqueryui, jsview, jqgrid,
kendoui, and so on. These libraries, and today's browsers cover developer's needs
in building managable applications. In contrast, a native support of xslt (at least
v2) does not exist in browsers.
</p>
        <p>
Server side at present is seen as a set of web services. These services support both
xml and json formats, and implement a business logic only. It would be a torture to
try to write such a frontend in xslt/xquery. A server logic itself is often dealing
with a diversity of data sources like databases, files (including xml files) and other.
</p>
        <p>
As for a database (we primarily work with SQL Server 2008 R2), we think that all communication
should go through stored procedures, which implement all data logic. Clearly, this
place is not for xslt. However, those who know sql beyond its basics can confirm that
sql is very similar to xquery. More than that SQL Server (and other databases) integrate
xquery to work with xml data, and we do use it extensively.
</p>
        <p>
Server logic itself uses API like LINQ to manipulate with different data sources.
In fact, we think that one can build a compiler from xquery 3.0 to C# with LINQ. Other
way round compiler would be a whole different story.
</p>
        <p>
The net result is that we see little place for xslt and xquery. Well, after all it's
only a personal perspective on the subject. The similar type of thing has happened
to us with C++. As with xslt/xquery we love C++ very much, and we fond of C++11, but
at present we have no place in our current projects for C++. That's pitty.
</p>
        <p>
P.S. Among other things that play against xslt/xquery is that there is a shortage
of people who know these languages, thus who can support such projects.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=558eebcd-e6b3-4238-b8dc-21d0eccaf510" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Weekend C# puzzle</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/04/08/WeekendCPuzzle.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,f6004aca-9fa1-4c23-b987-9de640d86baf.aspx</id>
    <published>2012-04-08T02:18:29.951-07:00</published>
    <updated>2012-04-08T06:24:59.9919193-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Negative SP execution time</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/03/24/NegativeSPExecutionTime.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,96e3bc8b-f844-4d4c-b8bc-4b304424652e.aspx</id>
    <published>2012-03-24T07:44:04.061-07:00</published>
    <updated>2012-03-25T02:55:13.9038909-07:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Arthur Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Let's start from a distance.
</p>
        <p>
We support a busy database for a customer. Customer's requirement (in fact, state's
requirement) is that the database should have audit logs. This means that all important
requests should be logged. These logs help both for the offline security analysis,
and for the database health monitoring.
</p>
        <p>
Before the end of the last year we used SQL Server 2005, and then customer has upgraded
to SQL Server 2008 R2.
</p>
        <p>
As by design the database is accessed through Stored Procedures only, so the logging
was done using a small SP that traced input parameters and execution time. The call
to that SP was inserted throughout the code of other SPs.
</p>
        <p>
We expected SQL Server 2008 R2 to simplify the task, and to allow us to switch the
audit on and off on a fine grained level without the need to change a SP in the production
(see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=96e3bc8b-f844-4d4c-b8bc-4b304424652e&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fcc280386.aspx">Understanding
SQL Server Audit</a> for details).
</p>
        <p>
Unfortunatelly, we have almost immediately found that the current audit implementation
traces SP calls but does not store parameter values. This way, you can see that there
was a call "<code>execute X @param1, @param2</code>", but you have no idea what values
were passed. Internet search shows that this a known problem (see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=96e3bc8b-f844-4d4c-b8bc-4b304424652e&amp;url=http%3a%2f%2fconnect.microsoft.com%2fSQLServer%2ffeedback%2fdetails%2f624935%2fsql-server-2008-database-audit-on-insert-update-and-delete-actual-sql-and-not-parameter-values"> SQL
Server 2008 Database Audit on INSERT UPDATE and DELETE actual SQL and not parameter
values</a>), which renders SQL Server Audit useless.
</p>
        <p>
But nevertheless, looking at how can we simplify our hand-made audit we have found
a brilliant solution: "<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=96e3bc8b-f844-4d4c-b8bc-4b304424652e&amp;url=http%3a%2f%2fblogs.msdn.com%2fb%2fjenss%2farchive%2f2010%2f07%2f10%2flight-weight-sql-server-procedure-auditing-without-using-sql-server-auditing-version-2.aspx">Light
weight SQL Server procedure auditing without using SQL Server auditing</a>". It's
so simple, that it's a shame that we did not invent it ourselves! The approach is
to insert or remove tracing code automatically. Indeed, there is nothing but data
in the database, even the text of SP is only a data.
</p>
        <p>
To automate it even more, we have defined a small table with names of procedures and
their log levels, and have defined a procedure "<code>Log.SetLevel @level</code>"
to configure all logging in one go. In addition we have simplified logging procedures
and tables, and started to store parameters in xml columns rather than in a pipe-concatenated
strings.
</p>
        <p>
        </p>
        <p>
Now, to the negative SP execution times.
</p>
        <p>
The logging code among other things measures <code>current_timestamp</code> at the
begin and at the end of the execution of SP. This helps us (as developers) to monitor
how database performs on a day to day basis, and to build many useful statistics.
</p>
        <p>
For example we can see that the duration of about 10% of untrivial selects is 0ms
(execution time is under 1ms). This means that SQL Server is good at data caching.
But what is most interesting is that about 0.1% of requests have negative duration!
</p>
        <p>
You could speculate on parallel or on out of order execution, but the paradox is resolved
when you look closely on a value of duration. It's always around of -7,200,000ms.
No one will assume that execution has ended two hours before it has started. So, what
does it mean -2 hours? Well, we live in (UTC+02:00) Jerusalem time zone. We think
that UTC offset crawls somehow into the result. To prove our hypothesis we would like
to change time zone on sql servers, but customer won't agree on such an experiment.
:-)
</p>
        <p>
This effect probably means that there is some hidden bug in SQL Server 2008 R2 that
we cannot reliably reproduce, but we can see that the <code>datediff(ms, start_timestamp,
end_timestamp)</code> may return negative value when it's known that <code>start_timestamp</code> is
acquired before <code>end_timestamp</code>.
</p>
        <p>
          <span style="font-weight: bold">Update:</span> What a shame. During tunning of the
original logging procedures we have changed type from <code>datetime</code> to <code>datetime2</code>,
and calls from <code>GETUTCDATE()</code> to <code>current_timestamp</code>, except
one place (default value in the table definition) where it remained with <code>GETUTCDATE()</code>.
</p>
        <p>
So, negative durations meant operation timeout (in our case duration is greater than
30 secs). 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=96e3bc8b-f844-4d4c-b8bc-4b304424652e" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Languages XOM update</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/03/23/LanguagesXOMUpdate.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,19c9c7e3-4b24-4407-806a-b118460873e3.aspx</id>
    <published>2012-03-22T17:07:35.334-07:00</published>
    <updated>2012-03-22T17:09:59.2183994-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="Announce" label="Announce" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,Announce.aspx" />
    <category term="xslt" label="xslt" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,xslt.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>New season in C++ WG</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/03/16/NewSeasonInCWG.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,a9c55d27-d353-4bcf-b923-2b7f00768d22.aspx</id>
    <published>2012-03-16T12:21:58.172-07:00</published>
    <updated>2012-03-16T12:33:43.3985148-07:00</updated>
    <category term="C++" label="C++" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,C.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
After C++11 revision has been approved a new cycle of C++ design has begun:
</p>
        <p style="padding-left: 1em; font-style: italic">
          <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22&amp;url=http%3a%2f%2fwww.open-std.org%2fjtc1%2fsc22%2fwg21%2fdocs%2fpapers%2f2012%2fn3370.html">N3370</a>:
The C++ standards committee is soliciting proposals for additional library components.
Such proposals can range from small (addition of a single signature to an existing
library) to large (something bigger than any current standard library component). 
</p>
        <p>
At this stage it's interesting to read papers, as authors try to express ideas
rather than to formulate sentences that should go into spec as it lately was.
</p>
        <p>
These are just several papers that we've found interesting:
</p>
        <table border="1">
          <tr>
            <td>
              <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22&amp;url=http%3a%2f%2fwww.open-std.org%2fjtc1%2fsc22%2fwg21%2fdocs%2fpapers%2f2012%2fn3322.pdf">N3322</a>
            </td>
            <td>
12-0012 
</td>
            <td>
A Preliminary Proposal for a Static if 
</td>
            <td>
Walter E. Brown 
</td>
          </tr>
          <tr>
            <td>
              <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22&amp;url=http%3a%2f%2fwww.open-std.org%2fjtc1%2fsc22%2fwg21%2fdocs%2fpapers%2f2012%2fn3329.pdf">N3329</a>
            </td>
            <td>
12-0019 
</td>
            <td>
Proposal: static if declaration 
</td>
            <td>
H. Sutter, W. Bright, A. Alexandrescu 
</td>
          </tr>
        </table>
        <p>
Those proposals argue about compile time "if statement". The feature can
replace <code>#if</code> preprocessor directive, a SFINAE or in some cases template
specializations.
</p>
        <p>
A <code>static if</code> declaration can appear wherever a declaration or a statement
is legal. Authors also propose to add <code>static if</code> clause to a class and
a function declarations to conditionally exclude them from the scope.
</p>
        <p>
Examples:
</p>
        <p style="padding-left: 1em">
          <code>// Compile time factorial.<br />
template &lt;unsigned n&gt;<br />
struct factorial<br />
{<br />
static if (n &lt;= 1)<br />
{<br />
enum : unsigned { value = 1 };<br />
} 
<br />
else 
<br />
{<br />
enum : unsigned { value = factorial&lt;n - 1&gt;::value * n };<br />
}<br />
};<br /><br />
// Declare class provided a condition is true.<br />
class Internals if (sizeof(void*) == sizeof(int));</code>
        </p>
        <p>
Paper presents strong rationale why this addition helps to build better programs,
however the questions arise about relations between <code>static if</code> and concepts, <code>static
if</code> clause and an error diagnostics.
</p>
        <p>
        </p>
        <table border="1">
          <tr>
            <td>
              <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22&amp;url=http%3a%2f%2fwww.open-std.org%2fjtc1%2fsc22%2fwg21%2fdocs%2fpapers%2f2012%2fn3327.pdf"> N3327</a>
            </td>
            <td>
12-0017</td>
            <td>
A Standard Programmatic Interface for Asynchronous Operations</td>
            <td>
N. Gustafsson, A. Laksberg</td>
          </tr>
          <tr>
            <td>
              <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22&amp;url=http%3a%2f%2fwww.open-std.org%2fjtc1%2fsc22%2fwg21%2fdocs%2fpapers%2f2012%2fn3328.pdf"> N3328</a>
            </td>
            <td>
12-0018</td>
            <td>
Resumable Functions</td>
            <td>
Niklas Gustafsson</td>
          </tr>
        </table>
        <p>
That's our favorite.
</p>
        <p>
Authors propose an API and a language extensions to make asynchronous programs simpler. 
</p>
        <p>
In fact, asynchronous function will look very mush as a regular one but with small
additions. It's similar to <code>yield return</code> in C# (<span style="font-style: italic">a
construct that has been available in C# for many years and is well vetted</span>),
and to <code>async</code> expression in C# 4.5. Compiler will rewrite such a function
into a state machine, thus function can suspend its execution, wait for the data and
to resume when data is available.
</p>
        <p>
Example:
</p>
        <p style="padding-left: 1em">
          <code>// read data asynchronously from an input and write it into an output.<br />
int cnt = 0;<br /><br />
do<br />
{<br />
cnt = await streamR.read(512, buf);<br /><br />
if (cnt == 0) 
<br />
{<br />
break;<br />
}<br /><br />
cnt = await streamW.write(cnt, buf);<br />
}<br />
while(cnt &gt; 0);</code>
        </p>
        <p>
It's iteresting to see how authors will address <code>yield return</code>: either
with aditional keyword, or in terms of resumable functions.
</p>
        <p>
        </p>
        <p>
          <table border="1">
            <tr>
              <td>
                <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22&amp;url=http%3a%2f%2fwww.open-std.org%2fjtc1%2fsc22%2fwg21%2fdocs%2fpapers%2f2012%2fn3340.pdf"> N3340</a>
              </td>
              <td>
12-0030</td>
              <td>
Rich Pointers</td>
              <td>
D. M. Berris, M. Austern, L. Crowl</td>
            </tr>
          </table>
        </p>
        <p>
Here authors try to justify rich type-info but mask it under the name "rich pointers".
To make things even more obscure they argue about dynamic code generation.
</p>
        <p>
If you want a rich type-info then you should talk about it and not about thousand
of other things.
</p>
        <p>
We would better appealed to create a standard API to access post-compile object model,
which could be used to produce different type-infos or other source derivatives.
</p>
        <p>
This paper is our outsider. :-)
</p>
        <p>
        </p>
        <p>
          <table border="1">
            <tr>
              <td>
                <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22&amp;url=http%3a%2f%2fwww.open-std.org%2fjtc1%2fsc22%2fwg21%2fdocs%2fpapers%2f2012%2fn3341.pdf"> N3341</a>
              </td>
              <td>
12-0031</td>
              <td>
Transactional Language Constructs for C++</td>
              <td>
M. Wong, H. Boehm, J. Gottschlich, T. Shpeisman, et al.</td>
            </tr>
          </table>
        </p>
        <p>
Here people try to generalize (put you away from) locking, and replace it with other
word "transaction". 
</p>
        <p>
Seems it's not viable proposition. It's better to teach on functional style
of programming with its immutable objects.
</p>
        <p>
        </p>
        <table border="1">
          <tr>
            <td>
              <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22&amp;url=http%3a%2f%2fwww.open-std.org%2fjtc1%2fsc22%2fwg21%2fdocs%2fpapers%2f2012%2fn3347.pdf"> N3347</a>
            </td>
            <td>
12-0037</td>
            <td>
Modules in C++ (Revision 6)</td>
            <td>
Daveed Vandevoorde</td>
          </tr>
        </table>
        <p>
Author argues against C style source composition with <code>#include</code> directive,
and propose alternative called "modules".
</p>
        <p>
We think that many C++ developers would agree that C pre-processor is a legacy that
would never have existed, but for the same reason (for the legacy, and compatibility)
it should stay.
</p>
        <p>
In out opinion the current proposition is just immature, at least it's not intuitive.
Or in other words there should be something to replace the C pre-processor (and <code>#include</code> as
its part), but we don't like this paper from aestetic perspective.
</p>
        <p>
        </p>
        <table border="1">
          <tr>
            <td>
              <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22&amp;url=http%3a%2f%2fwww.open-std.org%2fjtc1%2fsc22%2fwg21%2fdocs%2fpapers%2f2012%2fn3365.html"> N3365</a>
            </td>
            <td>
12-0055</td>
            <td>
Filesystem Library Proposal (Revision 2)</td>
          </tr>
        </table>
        <p>
This proposal says no a word about asynchronous nature of file access, while it should
be designed around it.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Shifting to client side web applications</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/03/08/ShiftingToClientSideWebApplications.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</id>
    <published>2012-03-08T13:56:19.79-08:00</published>
    <updated>2012-03-08T14:05:09.7142088-08:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="ASP.NET" label="ASP.NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ASPNET.aspx" />
    <category term="Java" label="Java" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,Java.aspx" />
    <category term="JSF and Facelets" label="JSF and Facelets" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,JSFAndFacelets.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>.NET Dictionary nuisance</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/02/29/NETDictionaryNuisance.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7fb6be18-2165-4027-8554-94d7aa5e2683.aspx</id>
    <published>2012-02-29T12:42:46.31-08:00</published>
    <updated>2012-02-29T21:30:12.1658655-08:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>SQL Server, grouping ranges</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/02/01/SQLServerGroupingRanges.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,4deebb89-c120-4a3d-99f7-6fb4759471ca.aspx</id>
    <published>2012-02-01T12:34:09.848-08:00</published>
    <updated>2012-02-02T22:06:56.5062306-08:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
A customer have a table with data stored by dates, and asked us to present data from
this table by sequential date ranges.
</p>
        <p>
This query sounded trivial but took us half a day to create such a select.
</p>
        <p>
For simplicity consider a table of integer numbers, and try to build a select that
returns pairs of continuous ranges of values.
</p>
        <p>
So, for an input like this:
</p>
        <p style="padding-left: 1em">
          <code>declare @values table<br />
(<br />
value int not null primary key<br />
);<br /><br />
insert into @values(value)<br />
select 1 union all select 2 union all select 3 union all<br />
select 5 union all select 6 union all<br />
select 8 union all<br />
select 10 union all<br />
select 12 union all select 13 union all select 14; </code>
        </p>
        <p>
You will have a following output:
</p>
        <pre style="padding-left: 1em">
low  high
---- ----
1    3
5    6
8    8
10   10
12   14</pre>
        <p>
Logic of the algorithms is like this:
</p>
        <ol style="list-style-type: lower-alpha">
          <li>
get a low bound of each range (a value without value - 1 in the source);</li>
          <li>
get a high bound of each range (a value without value + 1 in the source);</li>
          <li>
combine low and high bounds.</li>
        </ol>
        <p>
Following this logic we have built at least three different queries, where the shortest
one is:
</p>
        <p style="padding-left: 1em">
          <code> with source as<br />
(<br />
select * from @values<br />
)<br />
select 
<br />
l.value low,<br />
min(h.value) high<br />
from 
<br />
source l<br />
inner join<br />
source h<br />
on<br />
(l.value - 1 not in (select value from source)) and<br />
(h.value + 1 not in (select value from source)) and<br />
(h.value &gt;= l.value)<br />
group by<br />
l.value;</code>
        </p>
        <p>
          <img alt="execution plan" style="width: 100%; border: 1px solid black" src="content/binary/number-ranges-plan.jpg" />
        </p>
        <p>
Looking at this query it's hard to understand why it took so long to write so
simple code...
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=4deebb89-c120-4a3d-99f7-6fb4759471ca" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Again about cinematography.</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/01/25/AgainAboutCinematography.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,a23cc7e7-d76b-4f52-9471-abd9beeaa151.aspx</id>
    <published>2012-01-25T12:57:35.811-08:00</published>
    <updated>2012-01-25T13:07:07.727763-08:00</updated>
    <category term="Announce" label="Announce" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,Announce.aspx" />
    <author>
      <name>Arthur Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Some time ago our younger brother Aleksander had started studying of <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a23cc7e7-d76b-4f52-9471-abd9beeaa151&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f05%2f05%2fCinematography.aspx">cinematography</a>.
</p>
        <p>
Few days ago he started his own <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a23cc7e7-d76b-4f52-9471-abd9beeaa151&amp;url=http%3a%2f%2fwww.wix.com%2faleksan86%2fnesta" target="_blank">"multimedia"
blog</a> (you'll better understand me when you'll see it), where you can see his portfolio.
Aleksander's latest work was made with cooperation with Ilan Lahov, see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a23cc7e7-d76b-4f52-9471-abd9beeaa151&amp;url=http%3a%2f%2fwww.wix.com%2faleksan86%2fnesta%23!__bar-mitzvah" target="_blank">"Bar
mitzvah"</a>. This work demonstrates Aleksander's progress in this field.
</p>
        <p>
Our congratulations to <a href="mailto:aleksander@nesterovsky-bros.com">Aleksander</a>!
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=a23cc7e7-d76b-4f52-9471-abd9beeaa151" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Be aware, an antivirus is working here!</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/01/20/BeAwareAnAntivirusIsWorkingHere.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,0ede73c9-1649-4252-9078-1a4f27d21838.aspx</id>
    <published>2012-01-20T08:47:58.865-08:00</published>
    <updated>2012-01-20T08:49:49.899597-08:00</updated>
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Arthur Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you're writing an application that deals with files in file system on Windows,
be sure that sooner or later you run into problems with antivirus software. 
</p>
        <p>
Our latest program that handles a lot of huge files and works as a Windows service,
it reports time to time about some strange errors. These errors look like the file
system disappeared on the fly, or, files were stolen by somebody else (after they
have been opened in exclusive mode by our application). 
</p>
We spent about two weeks in order to diagnose the cause of such behaviour, and then
came to conclusion that is a secret work of our antivirus. All such errors disappeared
as fog when the antivirus was configurated to skip folders with our files. 
<p></p><p>
Thus, keep in mind our experience and don't allow an ativirus to became an evil.
</p><img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=0ede73c9-1649-4252-9078-1a4f27d21838" /></div>
    </content>
  </entry>
  <entry>
    <title>Analizing SQL</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/01/19/AnalizingSQL.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,1f01d63b-7ec8-4ca2-a761-687d8c183bd3.aspx</id>
    <published>2012-01-19T13:12:11.08-08:00</published>
    <updated>2012-01-19T13:34:24.020859-08:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
While looking at some SQL we have realized that it can be considerably optimized.
</p>
        <p>
Consider a table source like this:
</p>
        <p style="padding-left: 1em">
          <code>with Data(ID, Type, SubType) 
<br />
(<br />
select 1, 'A', 'X'<br />
union all<br />
select 2, 'A', 'Y'<br />
union all<br />
select 3, 'A', 'Y'<br />
union all<br />
select 4, 'B', 'Z'<br />
union all<br />
select 5, 'B', 'Z'<br />
union all<br />
select 6, 'C', 'X'<br />
union all<br />
select 7, 'C', 'X'<br />
union all<br />
select 8, 'C', 'Z'<br />
union all<br />
select 9, 'C', 'X'<br />
union all<br />
select 10, 'C', 'X'<br />
)</code>
        </p>
        <p>
Suppose you want to group data by type, to calculate number of elements in each group
and to display sub type if all rows in a group are of the same sub type.
</p>
        <p>
Earlier we have written the code like this:
</p>
        <p style="padding-left: 1em">
          <code>select<br />
Type, 
<br />
case when count(distinct SubType) = 1 then min(SubType) end SubType,<br />
count(*) C<br />
from<br />
Data<br />
group by<br />
Type; </code>
        </p>
        <p>
Namely, we select <code>min(SybType)</code> provided that there is a single distinct <code>SubType</code>,
otherwise <code>null</code> is shown. That works perfectly, but algorithmically <code>count(distinct
SubType) = 1</code> needs to build a set of distinct values for each group just to
ask the size of this set. That is expensive!
</p>
        <p>
What we wanted can be expressed differently: if <code>min(SybType)</code> and <code> max(SybType)</code> are
the same then we want to display it, otherwise to show <code>null</code>. 
</p>
        <p>
That's the new version:
</p>
        <p style="padding-left: 1em">
          <code>select<br />
Type, 
<br />
case when min(SubType) = max(SubType) then min(SubType) end SubType,<br />
count(*) C<br />
from<br />
Data<br />
group by<br />
Type; </code>
        </p>
        <p>
Such a simple rewrite has cardinally simplified the execution plan:
</p>
        <p>
          <img alt="Execution plans" src="content/binary/group-by-plan.jpg" style="width: 100%;&#xA;      border: 1px solid black" />
        </p>
        <p>
Another bizarre problem we have discovered is that SQL Server 2008 R2 just does not
support the following:
</p>
        <p style="padding-left: 1em">
          <code>select<br />
count(distinct SubType) over(partition by Type)<br />
from<br />
Data </code>
        </p>
        <p>
That's really strange, but it's known bug (see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=1f01d63b-7ec8-4ca2-a761-687d8c183bd3&amp;url=http%3a%2f%2fconnect.microsoft.com%2fSQLServer%2ffeedback%2fdetails%2f254393%2fover-clause-enhancement-request-distinct-clause-for-aggregate-functions"> Microsoft
Connect</a>). 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=1f01d63b-7ec8-4ca2-a761-687d8c183bd3" />
      </div>
    </content>
  </entry>
  <entry>
    <title>READ_COMMITTED_SNAPSHOT in SQL Server 2008 R2</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2012/01/13/READCOMMITTEDSNAPSHOTInSQLServer2008R2.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,c712a85b-5b6d-4f56-a053-78aa1b7dff4f.aspx</id>
    <published>2012-01-13T05:43:56.952075-08:00</published>
    <updated>2012-01-13T05:43:56.952075-08:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
A database we support for a client contains multi-billion row tables. Many users query
the data from that database, and it's permanently populated with a new data.
</p>
        <p>
Every day we load several millions rows of a new data. Such loads can lock tables
for a considerable time, so our loading procedures collect new data into intermediate
tables and insert it into a final destination by chunks, and usually after work hours.
</p>
        <p>
SQL Server 2008 R2 introduced <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c712a85b-5b6d-4f56-a053-78aa1b7dff4f&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fms188277.aspx"> READ_COMMITTED_SNAPSHOT
database option</a>. This feature trades locks for an increased tempdb size (to store
row versions) and possible performance degradation during a transaction.
</p>
        <p>
When we have switched the database to that option we did not notice any considerable
performance change. Encouraged, we've decided to increase size of chunks of data
we insert at once. 
</p>
        <p>
Earlier we have found that when we insert no more than 1000 rows at once, users don't
notice impact, but for a bigger chunk sizes users start to complain on performance
degradation. This has probably happened due to locks escalations.
</p>
        <p>
Now, with chunks of 10000 or even 100000 rows we have found that no queries became
slower. But load process became several times faster.
</p>
        <p>
We were ready to pay for increased tempdb and transaction log size to increase performance,
but in our case we didn't approach limits assigned by the DBA. Another gain is
that we can easily load data at any time. This makes data we store more up to date.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=c712a85b-5b6d-4f56-a053-78aa1b7dff4f" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Fast Property Access in V8 JavaScript Engine</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/12/17/FastPropertyAccessInV8JavaScriptEngine.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,bef403f0-158f-477b-b322-47d66251cb6a.aspx</id>
    <published>2011-12-17T02:18:14.81-08:00</published>
    <updated>2011-12-17T03:08:01.1241355-08:00</updated>
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Yesterday, by accident, we've seen an article about some design principles of <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=bef403f0-158f-477b-b322-47d66251cb6a&amp;url=http%3a%2f%2fcode.google.com%2fapis%2fv8%2fdesign.html">V8
JavaScript Engine</a>. It made clearer what techniques are used in today's script
implementations.
</p>
        <p>
In particular V8 engine optimizes property access using "<span style="font-style: italic">dynamically
created hidden classes</span>". These are structures to store object's layout,
they are derived when ש new property is created (deleted) on the object. When code
accesses a property, and if a cached object's dynamic hidden class is available
at the code point then access time is comparable to one of native fields.
</p>
        <p>
In our opinion this tactics might lead to a proliferation of such dynamic hidden classes,
which requires a considerable housekeeping, which also slows property write access,
especially when it's written for the first time. 
</p>
        <p>
We would like to suggest a slightly different strategy, which exploits the cache matches,
and does not require a dynamic hidden classes. 
</p>
        <p>
Consider an implementation data type with following characteristics:
</p>
        <ul>
          <li>
object is implemented as a hash map of property id to property value: Map&lt;ID, Value&gt;;</li>
          <li>
it stores data as an array of pairs and can be accessed directly: Pair&lt;ID, Value&gt;
values[];</li>
          <li>
property index can be acquired with a method: int index(ID);</li>
        </ul>
        <p>
A pseudo code for the property access looks like this:
</p>
        <p style="padding-left: 1em">
          <code> pair = object.values[cachedIndex];<br /><br />
if (pair.ID == propertyID)<br />
{<br />
value = pair.Value;<br />
}<br />
else<br />
{<br />
// Cache miss.<br />
cachedIndex = object.index(propertyID);<br />
value = objec.values[cachedIndex].Value;<br />
}</code>
        </p>
        <p>
This approach brings us back to dictionary like implementation but with important
optimization of array speed access when property index is cached, and with no dynamic
hidden classes.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=bef403f0-158f-477b-b322-47d66251cb6a" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Saxon 9.4 is out</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/12/10/Saxon94IsOut.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,08d97135-32ca-4586-bdf4-410ef43f8ebe.aspx</id>
    <published>2011-12-10T04:16:28.754-08:00</published>
    <updated>2011-12-10T04:19:04.8086725-08:00</updated>
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="xslt" label="xslt" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,xslt.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p style="padding-left: 1em; font-style: italic">
          <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=08d97135-32ca-4586-bdf4-410ef43f8ebe&amp;url=http%3a%2f%2ftwitter.com%2f%23!%2fmichaelhkay%2fstatuses%2f145259939055673344">@michaelhkay
Saxon 9.4 is out.</a>
        </p>
        <p>
But why author does not state that HE version is still xslt/xpath 2.0, as neither
xslt maps, nor function items are supported.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=08d97135-32ca-4586-bdf4-410ef43f8ebe" />
      </div>
    </content>
  </entry>
  <entry>
    <title>SQL Server 2008 with(recompile) Next</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/12/03/SQLServer2008WithrecompileNext.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,49f851e0-8141-4827-852c-98161e14234e.aspx</id>
    <published>2011-12-03T07:06:44.313-08:00</published>
    <updated>2011-12-03T07:08:35.6601086-08:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Recently, we have found and reported the bug in the SQL Server 2008 (see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=49f851e0-8141-4827-852c-98161e14234e&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f11%2f18%2fSQLServer2008Withrecompile.aspx">SQL
Server 2008 with(recompile)</a>, and also <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=49f851e0-8141-4827-852c-98161e14234e&amp;url=http%3a%2f%2fconnect.microsoft.com%2fSQLServer%2ffeedback%2fdetails%2f705658%2fwith-recompile-does-not-work-for-select-var">Microsoft
Connect</a>).
</p>
        <p>
Persons, who's responsible for the bug evaluation has closed it, as if "By Design".
This strange resolution, in our opinion, says about those persons only.
</p>
        <p>
Well, we shall try once more (see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=49f851e0-8141-4827-852c-98161e14234e&amp;url=http%3a%2f%2fconnect.microsoft.com%2fSQLServer%2ffeedback%2fdetails%2f705658%2fwith-recompile-does-not-work-for-select-var">Microsoft
Connect</a>). We have posted another trivial demonstartion of the bug, where we show
that option(recompile) is not used, which leads to table scan (nothing worse can happen
for a huge table).
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=49f851e0-8141-4827-852c-98161e14234e" />
      </div>
    </content>
  </entry>
  <entry>
    <title>SQL Server 2008 with(recompile)</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/11/18/SQLServer2008Withrecompile.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,abae0d0b-73a8-4531-8aa6-147b0ba5586a.aspx</id>
    <published>2011-11-18T06:49:50.595-08:00</published>
    <updated>2011-11-18T07:29:10.6475906-08:00</updated>
    <category term="SQL Server puzzle" label="SQL Server puzzle" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,SQLServerPuzzle.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="html">  &lt;p&gt;
&lt;p&gt;
Recently we have introduced some stored procedure in the production and have found
that it performs incredibly slow.
&lt;/p&gt;
&lt;p&gt;
Our reasoning and tests in the development environment did not manifest any problem
at all.
&lt;/p&gt;
&lt;p&gt;
In essence that procedure executes some SELECT and returns a status as a signle output
variable. Procedure recieves several input parameters, and the SELECT statement uses &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=abae0d0b-73a8-4531-8aa6-147b0ba5586a&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fms181714.aspx"&gt; with(recompile)
execution hint&lt;/a&gt; to optimize the performance for a specific parameters.
&lt;/p&gt;
&lt;p&gt;
We have analyzed the execution plan of that procedure and have found that it works
as if with(recompile) hint was not specified. Without that hint SELECT failed to use
index seek but rather used index scan.
&lt;/p&gt;
&lt;p&gt;
What we have lately found is that the same SELECT that produces result set instead
of reading result into a variable performs very well.
&lt;/p&gt;
&lt;p&gt;
We think that this is a bug in SQL Server 2008 R2 (and in SQL Server 2008).
&lt;/p&gt;
&lt;p&gt;
To demonstrate the problem you can run this test:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; -- Setup&lt;br /&gt;
create table dbo.Items&lt;br /&gt;
(&lt;br /&gt;
Item int not null primary key&lt;br /&gt;
);&lt;br /&gt;
go&lt;br /&gt;
&lt;br /&gt;
insert into dbo.Items&lt;br /&gt;
select 1&lt;br /&gt;
union all&lt;br /&gt;
select 2&lt;br /&gt;
union all&lt;br /&gt;
select 3&lt;br /&gt;
union all&lt;br /&gt;
select 4&lt;br /&gt;
union all&lt;br /&gt;
select 5&lt;br /&gt;
go&lt;br /&gt;
&lt;br /&gt;
create procedure dbo.GetMaxItem&lt;br /&gt;
(&lt;br /&gt;
@odd bit = null,&lt;br /&gt;
@result int output&lt;br /&gt;
)&lt;br /&gt;
as&lt;br /&gt;
begin&lt;br /&gt;
set nocount on;&lt;br /&gt;
&lt;br /&gt;
with Items as&lt;br /&gt;
(&lt;br /&gt;
select * from dbo.Items where @odd is null&lt;br /&gt;
union all&lt;br /&gt;
select * from dbo.Items where (@odd = 1) and ((Item &amp; 1) = 1)&lt;br /&gt;
union all&lt;br /&gt;
select * from dbo.Items where (@odd = 0) and ((Item &amp; 1) = 0)&lt;br /&gt;
)&lt;br /&gt;
select @result = max(Item) from Items&lt;br /&gt;
option(recompile);&lt;br /&gt;
end;&lt;br /&gt;
go&lt;br /&gt;
&lt;br /&gt;
create procedure dbo.GetMaxItem2&lt;br /&gt;
(&lt;br /&gt;
@odd bit = null,&lt;br /&gt;
@result int output&lt;br /&gt;
)&lt;br /&gt;
as&lt;br /&gt;
begin&lt;br /&gt;
set nocount on;&lt;br /&gt;
&lt;br /&gt;
declare @results table&lt;br /&gt;
(&lt;br /&gt;
Item int&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
with Items as&lt;br /&gt;
(&lt;br /&gt;
select * from dbo.Items where @odd is null&lt;br /&gt;
union all&lt;br /&gt;
select * from dbo.Items where (@odd = 1) and ((Item &amp; 1) = 1)&lt;br /&gt;
union all&lt;br /&gt;
select * from dbo.Items where (@odd = 0) and ((Item &amp; 1) = 0)&lt;br /&gt;
)&lt;br /&gt;
insert into @results&lt;br /&gt;
select max(Item) from Items&lt;br /&gt;
option(recompile);&lt;br /&gt;
&lt;br /&gt;
select @result = Item from @results;&lt;br /&gt;
end;&lt;br /&gt;
go&lt;br /&gt;
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Test with output into a variable:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;declare @result1 int;&lt;br /&gt;
&lt;br /&gt;
execute dbo.GetMaxItem @odd = null, @result = @result1 output &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img alt="Execution plan of dbo.GetMaxItem" src="content/binary/GetMaxItemSQL.JPG" style="width: 40em" /&gt;
&lt;/p&gt;
&lt;p&gt;
Test without output directly into a variable:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;declare @result2 int;&lt;br /&gt;
&lt;br /&gt;
execute dbo.GetMaxItem2 @odd = null, @result = @result2 output &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img alt="Execution plan of dbo.GetMaxItem2" src="content/binary/GetMaxItem2SQL.JPG" style="width: 40em" /&gt;
&lt;/p&gt;
&lt;p&gt;
Now, you can see the difference: the first execution plan uses startup expressions,
while the second optimizes execution branches, which are not really used. In our case
it was crucial, as the execition time difference was minutes (and more in future)
vs a split of second.
&lt;/p&gt;
&lt;p&gt;
See also &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=abae0d0b-73a8-4531-8aa6-147b0ba5586a&amp;amp;url=https%3a%2f%2fconnect.microsoft.com%2fSQLServer%2ffeedback%2fdetails%2f705658%2fwith-recompile-does-not-work-for-select-var"&gt;Microsoft
Connect Entry&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=abae0d0b-73a8-4531-8aa6-147b0ba5586a" /&gt;</content>
  </entry>
  <entry>
    <title>eXperanto project actual yet...</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/11/10/eXperantoProjectActualYet.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</id>
    <published>2011-11-10T14:14:09.467-08:00</published>
    <updated>2011-11-10T21:11:03.9398052-08:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="Java" label="Java" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,Java.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Arthur Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>jQuery</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/10/28/jQuery.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,ad1c5ce2-4806-4de7-8c58-6af54cb9df06.aspx</id>
    <published>2011-10-28T15:59:23.607-07:00</published>
    <updated>2011-10-28T16:05:03.3640122-07:00</updated>
    <category term="ASP.NET" label="ASP.NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ASPNET.aspx" />
    <category term="JSF and Facelets" label="JSF and Facelets" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,JSFAndFacelets.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <category term="xslt" label="xslt" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,xslt.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
It has happened so, that we have never worked with jQuery, however were aware of it.
</p>
        <p>
In early 2000 we have developed a web application that contained rich javascript APIs,
including UI components. Later, we were actively practicing in ASP.NET, and later
in JSF.
</p>
        <p>
At present, looking at jQuery more closely we regret that we have failed to start
using it earlier.
</p>
        <p>
Separation of business logic and presentation is remarkable when one uses JSON web
services. In fact server part can be seen as a set of web services representing a
business logic and a set of resources: html, styles, scripts, others. Nor ASP.NET
or JSF approach such a consistent separation.
</p>
        <p>
The only trouble, in our opinion, is that jQuery has no standard data binding: a way
to bind JSON data to (and from) html controls. The technique that will probably be
standardized is called <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=ad1c5ce2-4806-4de7-8c58-6af54cb9df06&amp;url=https%3a%2f%2fgithub.com%2fBorisMoore%2fjsviews">jQuery
Templates or JsViews </a> .
</p>
        <p>
Unfortunatelly after reading about this <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=ad1c5ce2-4806-4de7-8c58-6af54cb9df06&amp;url=http%3a%2f%2fwiki.jqueryui.com%2fw%2fpage%2f37898666%2fTemplate">binding
API</a>, and being in love with Xslt and XQuery we just want to cry. We don't know
what would be the best solution for the task, but what we see looks uncomfortable
to us.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=ad1c5ce2-4806-4de7-8c58-6af54cb9df06" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Funny, about yield return in java</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/10/16/FunnyAboutYieldReturnInJava.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,582c91ec-61fc-4bf4-a65c-bf410715a420.aspx</id>
    <published>2011-10-16T06:49:33.089-07:00</published>
    <updated>2011-10-18T12:28:03.5626746-07:00</updated>
    <category term="Java" label="Java" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,Java.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Incidentally, we have found one new implementation of yield return in java that is
in the development stage. Sources can be found at <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;url=https%3a%2f%2fgithub.com%2fpeichhorn%2flombok-pg%2fzipball%2fmaster"> https://github.com/peichhorn/lombok-pg/zipball/master</a>.
Just to be sure we have copied those sources at other place <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fplagiarism%2fpeichhorn-lombok-pg-0.10.0-39-g384fb7b.zip"> peichhorn-lombok-pg-0.10.0-39-g384fb7b.zip</a> (you
may search "yield" in the archive).
</p>
        <p>
It's broken according to source tracker, but the funny thing is that sources, however
different, still resemble our yield return implementation (<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar">Yield.jar</a>, <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.3.7.jar">Yield.3.7.jar</a> -
Indigo, <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip">Yield.zip</a> -
sources) very much: variable names, error messages, algorithmic structure.
</p>
        <p>
Those programmers probably have forgotten good manners: to reference a base work,
at least.
</p>
        <p>
Well, we generously forgive them this blunder.
</p>
        <p>
P.S. our implementation, in contrast, works without bugs.
</p>
        <p>
P.P.S. misunderstanding is resolved. See comments.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420" />
      </div>
    </content>
  </entry>
  <entry>
    <title>An XPath enumerator function</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/09/29/AnXPathEnumeratorFunction.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9c7823a7-6ac4-41ca-a75a-de10204879cd.aspx</id>
    <published>2011-09-29T04:56:05.05-07:00</published>
    <updated>2011-09-29T04:57:04.0072027-07:00</updated>
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="xslt" label="xslt" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,xslt.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
A couple of weeks ago, we have suggested to introduce a enumerator function into the
XPath (see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=9c7823a7-6ac4-41ca-a75a-de10204879cd&amp;url=http%3a%2f%2fwww.w3.org%2fBugs%2fPublic%2fshow_bug.cgi%3fid%3d14152"> [F+O30]
A enumerator function</a>):
</p>
        <p style="font-style: italic; padding-left: 1em">
I would like the WG to consider an addition of a function that turns a sequence into
a enumeration of values.<br /><br />
Consider a function like this:  fn:enumerator($items as item()*) as function()
as item()<span style="font-weight: bold">?</span>;<br /><br />
alternatively, signature could be:<br /><br />
 fn:enumerator($items as function() as item()*) as function() as item()<span style="font-weight: bold">?</span>;<br /><br />
This function receives a sequence, and returns a function item, which upon N's
call shall return N's element of the original sequence. This way, a sequence of
items is turned into a function providing a enumeration of items of the sequence. 
<br /><br />
As an example consider two functions:<br /><br />
a) t:rand($seed as xs:double) as xs:double* - a function producing a random number
sequence;<br />
b) t:work($input as element()) as element() - a function that generates output from
it's input, and that needs random numbers in the course of the execution. 
<br /><br />
t:work() may contain a code like this:<br />
  let $rand := fn:enumerator(t:rand($seed)),<br /><br />
and later it can call $rand() to get a random numbers. 
<br /><br />
Enumerators will help to compose algorithms where one algorithm communicate with other
independant algorithms, thus making code simpler. The most obvious class of enumerators
are generators: ordered numbers, unique identifiers, random numbers. 
<br /><br />
Technically, <span style="font-weight: bold">function returned from</span> fn:enumerator()
is nondetermenistic, but its "side effect" is similar to a "side effect"
of a function generate-id() from a newly created node (see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=9c7823a7-6ac4-41ca-a75a-de10204879cd&amp;url=http%3a%2f%2fwww.w3.org%2fBugs%2fPublic%2fshow_bug.cgi%3fid%3d13747" title="RESOLVED LATER - [XPath 3.0] Determinism of expressions returning constructed nodes">bug
#13747</a>, and <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=9c7823a7-6ac4-41ca-a75a-de10204879cd&amp;url=http%3a%2f%2fwww.w3.org%2fBugs%2fPublic%2fshow_bug.cgi%3fid%3d13494" title="NEW - Node uniqueness returned from XSLT function.">bug
#13494</a>). 
</p>
        <p>
The idea is inspired by a generator function, which returns a new value upon each
call. 
</p>
        <p>
Such function can be seen as a stateful object. But our goal is to look at it in a
more functional way. So, we look at the algorithm as a function that produces a sequence
of output, which is pure functional; and an enumerator that allows to iterate over
algorithm's output.
</p>
        <p>
This way, we see the function that implements an algorithm and the function that uses
it can be seen as two thread of functional programs that use messaging to communicate
to each other.
</p>
        <p>
Honestly, we doubt that WG will accept it, but it's interesting to watch the discussion.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9c7823a7-6ac4-41ca-a75a-de10204879cd" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Resolution of the Saxon optimizer bug</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/09/14/ResolutionOfTheSaxonOptimizerBug.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,d0be8e77-04d9-47a0-840e-d62b1936b3ac.aspx</id>
    <published>2011-09-13T22:54:56.464-07:00</published>
    <updated>2011-09-13T23:14:52.9775176-07:00</updated>
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="xslt" label="xslt" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,xslt.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
More than month has passed since we have reported a problem to the saxon forum (see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=d0be8e77-04d9-47a0-840e-d62b1936b3ac&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f07%2f27%2fSaxonOptimizerBug.aspx"> Saxon
optimizer bug</a> and <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=d0be8e77-04d9-47a0-840e-d62b1936b3ac&amp;url=http%3a%2f%2fsourceforge.net%2fprojects%2fsaxon%2fforums%2fforum%2f94027%2ftopic%2f4621860"> Saxon
9.2 generate-id() bug)</a>. 
</p>
        <p>
The essence of the problem is that we have constructed argumentless function to return
a unique identifiers each time function is called. To achieve the effect we have created
a temporary node and returned its <code>generate-id()</code> value.
</p>
        <p>
Such a function is nondetermenistic, as we cannot state that its result depends on
arguments only. This means that engine's optimizer is not free to reorder calls to
such a function. That's what happens in Saxon 9.2, and Saxon 9.3 where engine
elevates function call out of cycle thus producing invalid results.
</p>
        <p>
Michael Kay, the author of the Saxon engine, argued that this is "a gray area
of the xslt spec":
</p>
        <p style="padding-left: 1em; font-style: italic">
If the spec were stricter about defining exactly when you can rely on identity-dependent
operations then I would be obliged to follow it, but I think it's probably deliberate
that it currently allows implementations some latitude, effectively signalling to
users that they should avoid depending on this aspect of the behaviour. 
</p>
        <p>
He adviced to raise a bug in the w3c bugzilla to resolve the issue. In the end two
related bugs have been raised:
</p>
        <ul>
          <li>
            <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=d0be8e77-04d9-47a0-840e-d62b1936b3ac&amp;url=http%3a%2f%2fwww.w3.org%2fBugs%2fPublic%2fshow_bug.cgi%3fid%3d13494">Bug
13494</a> - Node uniqueness returned from XSLT function;</li>
          <li>
            <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=d0be8e77-04d9-47a0-840e-d62b1936b3ac&amp;url=http%3a%2f%2fwww.w3.org%2fBugs%2fPublic%2fshow_bug.cgi%3fid%3d13747">Bug
13747</a> - [XPath 3.0] Determinism of expressions returning constructed nodes.</li>
        </ul>
        <p>
Yesterday, the WG has resolved the issue:
</p>
        <p style="padding: 1em; font-style: italic">
The Working Group agreed that default behavior should continue to require these nodes
to be constructed with unique IDs. We believe that this is the kind of thing implementations
can do with annotations or declaration options, and it would be best to get implementation
experience with this before standardizing.
</p>
        <p>
This means that the technique we used to generate unique identifiers is correct and
the behaviour is well defined.
</p>
        <p>
The only problem is to wait when Saxon will fix its behaviour accordingly.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=d0be8e77-04d9-47a0-840e-d62b1936b3ac" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Entity Framework and char parameters</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/09/06/EntityFrameworkAndCharParameters.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,243e0e15-c07d-4a6b-87f9-c0e89f52b781.aspx</id>
    <published>2011-09-06T14:11:38.155-07:00</published>
    <updated>2011-09-06T14:12:10.2735928-07:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,NET.aspx" />
    <category term="Thinking aloud" label="Thinking aloud" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,ThinkingAloud.aspx" />
    <category term="Tips and tricks" label="Tips and tricks" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,TipsAndTricks.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Masha</title>
    <link rel="alternate" type="text/html" href="http://www.nesterovsky-bros.com/weblog/2011/08/29/Masha.aspx" />
    <id>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9533804c-0be1-4e57-8cbc-764a1b66f53a.aspx</id>
    <published>2011-08-29T06:54:22.4889081-07:00</published>
    <updated>2011-08-29T06:54:22.4889081-07:00</updated>
    <category term="Announce" label="Announce" scheme="http://www.nesterovsky-bros.com/weblog/CategoryView,category,Announce.aspx" />
    <author>
      <name>Vladimir Nesterovsky</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Please welcome a new human being Masha Vladimirovna Nesterovsky!
</p>
        <p>
          <img alt="Masha" src="http://www.nesterovsky-bros.com/weblog/images/Masha.jpg" style="padding: 2px; border: 1px solid red" />
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9533804c-0be1-4e57-8cbc-764a1b66f53a" />
      </div>
    </content>
  </entry>
</feed>