<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:georss="http://www.georss.org/georss" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
  <channel>
    <title>Nesterovsky bros - .NET</title>
    <link>http://www.nesterovsky-bros.com/weblog/</link>
    <description />
    <language>en-us</language>
    <copyright>Nesterovsky bros</copyright>
    <lastBuildDate>Fri, 26 Jan 2024 13:27:16 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.12105.0</generator>
    <managingEditor>contact@nesterovsky-bros.com</managingEditor>
    <webMaster>contact@nesterovsky-bros.com</webMaster>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=371a8329-d392-4d20-bcbc-db9503816f26</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,371a8329-d392-4d20-bcbc-db9503816f26.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=371a8329-d392-4d20-bcbc-db9503816f26</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h4>Introduction
</h4>
        <p>
We migrate code out of MF to Azure. Tool we use produces plain good functionally equivalent
C# code. But it turns it's not enough!
</p>
        <p>
So, what's the problem?
</p>
        <p>
Converted code is very slow, especially for batch processing, where MF completes job,
say in 30 minutes, while converted code finishes in 8 hours.
</p>
        <p>
At this point usually someone appears and whispers in the ear:
</p>
        <blockquote>
          <p>
Look, those old technologies are proven by the time. It worth to stick to old good
Cobol, or better to Assembler if you want to do the real thing.
</p>
        </blockquote>
        <p>
We're curious though: why is there a difference?
</p>
        <p>
Turns out the issue lies in differences of network topology between MF and Azure solutions.
On MF all programs, database and file storage virtually sit in a single box, thus
network latency is negligible. 
</p>
        <p>
It's rather usual to see chatty SQL programs on MF that are doing a lot of small
SQL queries.
</p>
        <p>
In Azure - programs, database, file storage are different services most certainly
sitting in different phisical boxes. You should be thankfull if they are co-located
in a single datacenter. So, network latency immediately becomes a factor. Even if
it just adds 1 millisecond per SQL roundtrip, it adds up in loops, and turns in the
showstopper.
</p>
        <p>
There is no easy workaround on the hardware level.
</p>
        <p>
People advice to write programs differently: "<a href="https://learn.microsoft.com/en-us/azure/azure-sql/database/performance-guidance">Tune
applications and databases for performance in Azure SQL Database</a>".
</p>
        <p>
That's a good advice for a new development but discouraging for migration done
by a tool.
</p>
        <p>
So, what is the way forward?
</p>
        <p>
Well, there is one. While accepting weak sides of Azure we can exploit its strong
sides.
</p>
        <h4>Parallel refactoring
</h4>
        <p>
Before continuing let's consider a <a href="https://github.com/nesterovsky-bros/parallel-refactoring/tree/master/Services/SerialProcessor.cs#L5-L29">code</a> demoing
the problem:
</p>
        <pre>
          <code class="lang-C#">
            <span class="hljs-keyword">public</span>
            <span class="hljs-literal">void</span> CreateReport(StringWriter
writer) { <span class="hljs-built_in">var</span> index = <span class="hljs-number">0</span>;
foreach(<span class="hljs-built_in">var</span> transaction <span class="hljs-keyword">in</span> dataService.
GetTransactions(). OrderBy(item =&gt; (item.At, item.SourceAccountId))) { <span class="hljs-built_in">var</span> sourceAccount
= dataService.GetAccount(transaction.SourceAccountId); <span class="hljs-built_in">var</span> targetAccount
= transaction.TargetAccountId != <span class="hljs-built_in">null</span> ? dataService.GetAccount(transaction.TargetAccountId)
: <span class="hljs-built_in">null</span>; ++index; <span class="hljs-keyword">if</span> (index
% <span class="hljs-number">100</span> == <span class="hljs-number">0</span>) { Console.WriteLine(index);
} writer.WriteLine($<span class="hljs-string">"{index},{transaction.Id},{ transaction.At},{transaction.Type},{transaction.Amount},{
transaction.SourceAccountId},{sourceAccount?.Name},{ transaction.TargetAccountId},{targetAccount?.Name}"</span>);
} } </code>
        </pre>
        <p>
This cycle queries transactions, along with two more small queries to get source and
target accounts for each transaction. Results are printed into a report.
</p>
        <p>
If we assume query latency just 1 millisecond, and try to run such code for 100K transactions
we easily come to 200+ seconds of execution. 
</p>
        <p>
Reality turns to be much worse. Program spends most of its lifecycle waiting for database
results, and iterations don't advance until all work of previous iterations is
complete.
</p>
        <p>
We could do better even without trying to rewrite all code! Let's articulate our
goals:
</p>
        <ol>
          <li>
To make code fast.</li>
          <li>
To leave code recognizable.</li>
        </ol>
        <p>
The idea is to form two processing pipelines:
</p>
        <ul>
          <li>
(a) one that processes data in parallel out of order;</li>
          <li>
(b) other that processes data serially, in original order;</li>
        </ul>
        <p>
Each pipeline may post sub-tasks to the other, so (a) runs its tasks in parallel unordered,
while (b) runs its tasks as if everything was running serially.
</p>
        <p>
So, parallel plan would be like this:
</p>
        <ol>
          <li>
Queue parallel sub-tasks (a) for each transaction.</li>
          <li>
Parallel sub-task in (a) reads source and target accounts, and queues serial sub-task
(b) passing transaction and accounts.</li>
          <li>
Serial sub-task (b) increments index, and writes report record.</li>
          <li>
Wait for all tasks to complete.</li>
        </ol>
        <p>
To reduce burden of task piplelines we use <a href="https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/dataflow-task-parallel-library">Dataflow
(Task Parallel Library)</a>, and encapsulate everything in a small wrapper.
</p>
        <p>
Consider refactored <a href="https://github.com/nesterovsky-bros/parallel-refactoring/tree/master/Services/ParallelProcessor.cs#L14-L48">code</a>:
</p>
        <pre>
          <code class="lang-C#">
            <span class="hljs-keyword">public</span>
            <span class="hljs-literal">void</span> CreateReport(StringWriter
writer) { using <span class="hljs-built_in">var</span> parallel = <span class="hljs-literal">new</span> Parallel(options.Value.Parallelism); <span class="hljs-comment">//
⬅️ 1</span><span class="hljs-built_in">var</span> index = <span class="hljs-number">0</span>;
parallel.ForEachAsync( <span class="hljs-comment">// ⬅️ 2</span> dataService. GetTransactions().
OrderBy(item =&gt; (item.At, item.SourceAccountId)), transaction =&gt; <span class="hljs-comment">//
⬅️ 3</span> { <span class="hljs-built_in">var</span> sourceAccount = dataService.GetAccount(transaction.SourceAccountId); <span class="hljs-built_in">var</span> targetAccount
= transaction.TargetAccountId != <span class="hljs-built_in">null</span> ? dataService.GetAccount(transaction.TargetAccountId)
: <span class="hljs-built_in">null</span>; parallel.PostSync( <span class="hljs-comment">//
⬅️ 4</span> (transaction, sourceAccount, targetAccount), <span class="hljs-comment">//
⬅️ 5</span><span class="hljs-built_in">data</span> =&gt; { <span class="hljs-built_in">var</span> (transaction,
sourceAccount, targetAccount) = <span class="hljs-built_in">data</span>; <span class="hljs-comment">//
⬅️ 6</span> ++index; <span class="hljs-keyword">if</span> (index % <span class="hljs-number">100</span> == <span class="hljs-number">0</span>)
{ Console.WriteLine(index); } writer.WriteLine($<span class="hljs-string">"{index},{transaction.Id},{
transaction.At},{transaction.Type},{transaction.Amount},{ transaction.SourceAccountId},{sourceAccount?.Name},{
transaction.TargetAccountId},{targetAccount?.Name}"</span>); }); }); } </code>
        </pre>
        <p>
Consider ⬅️ points:
</p>
        <ol>
          <li>
We create <code>Parallel</code> utility class passing degree of parallelism requested.</li>
          <li>
We iterate transactions using <code>parallel.ForEachAsync()</code> that queues parallel
sub-tasks for each transaction, and then waits until all tasks are complete.</li>
          <li>
Each parallel sub-task recieves a transaction. It may be called from a different thread.</li>
          <li>
Having recieved required accounts we queue a sub-task for synchronous execution using <code>parallel.PostSync()</code>,
and</li>
          <li>
Pass there data collected in parallel sub-task: transaction and accounts.</li>
          <li>
We deconstruct data passed into variables, and then proceed with serial logic.</li>
        </ol>
        <p>
What we achieve with this refactoring:
</p>
        <ol>
          <li>
Top level query that brings transactions is done and iterated serially.</li>
          <li>
But each iteration body is run in parallel. By default we set it up to allow up to
100 parallel executions. All those parallel sub-task do not wait on each other so
their waitings do not add up.</li>
          <li>
Sync sub-tasks are queued and executed in order of their serial appearance, so increments
and report records are not a subject of race conditions, nor a subject of reordering
of output records.</li>
        </ol>
        <p>
We think that such refactored code is still recognizible.
</p>
        <p>
As for performance this is what log shows:
</p>
        <pre>
          <code class="lang-log">
            <span class="hljs-selector-tag">Serial</span>
            <span class="hljs-selector-tag">test</span> 100
... <span class="hljs-selector-tag">Execution</span><span class="hljs-selector-tag">time</span>:
00<span class="hljs-selector-pseudo">:01</span><span class="hljs-selector-pseudo">:33.8152540</span><span class="hljs-selector-tag">Parallel</span><span class="hljs-selector-tag">test</span> 100
... <span class="hljs-selector-tag">Execution</span><span class="hljs-selector-tag">time</span>:
00<span class="hljs-selector-pseudo">:00</span><span class="hljs-selector-pseudo">:05.8705468</span></code>
        </pre>
        <h4>Reference
</h4>
        <p>
Please take a look at project to understand implementation details, and in particular <a href="https://github.com/nesterovsky-bros/parallel-refactoring/tree/master/Services/Parallel.cs"><code>Parallel</code></a> class
implementing API to post parallel and serial tasks, run cycles and some more.
</p>
        <p>
Please continue reading on <a href="https://github.com/nesterovsky-bros/parallel-refactoring/tree/master?tab=readme-ov-file#parallel-refactoring">GitHub</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=371a8329-d392-4d20-bcbc-db9503816f26" />
      </body>
      <title>Experience of parallel refactoring</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,371a8329-d392-4d20-bcbc-db9503816f26.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2024/01/26/ExperienceOfParallelRefactoring.aspx</link>
      <pubDate>Fri, 26 Jan 2024 13:27:16 GMT</pubDate>
      <description>&lt;h4&gt;Introduction
&lt;/h4&gt;
&lt;p&gt;
We migrate code out of MF to Azure. Tool we use produces plain good functionally equivalent
C# code. But it turns it&amp;#39;s not enough!
&lt;/p&gt;
&lt;p&gt;
So, what&amp;#39;s the problem?
&lt;/p&gt;
&lt;p&gt;
Converted code is very slow, especially for batch processing, where MF completes job,
say in 30 minutes, while converted code finishes in 8 hours.
&lt;/p&gt;
&lt;p&gt;
At this point usually someone appears and whispers in the ear:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Look, those old technologies are proven by the time. It worth to stick to old good
Cobol, or better to Assembler if you want to do the real thing.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
We&amp;#39;re curious though: why is there a difference?
&lt;/p&gt;
&lt;p&gt;
Turns out the issue lies in differences of network topology between MF and Azure solutions.
On MF all programs, database and file storage virtually sit in a single box, thus
network latency is negligible. 
&lt;/p&gt;
&lt;p&gt;
It&amp;#39;s rather usual to see chatty SQL programs on MF that are doing a lot of small
SQL queries.
&lt;/p&gt;
&lt;p&gt;
In Azure - programs, database, file storage are different services most certainly
sitting in different phisical boxes. You should be thankfull if they are co-located
in a single datacenter. So, network latency immediately becomes a factor. Even if
it just adds 1 millisecond per SQL roundtrip, it adds up in loops, and turns in the
showstopper.
&lt;/p&gt;
&lt;p&gt;
There is no easy workaround on the hardware level.
&lt;/p&gt;
&lt;p&gt;
People advice to write programs differently: &amp;quot;&lt;a href="https://learn.microsoft.com/en-us/azure/azure-sql/database/performance-guidance"&gt;Tune
applications and databases for performance in Azure SQL Database&lt;/a&gt;&amp;quot;.
&lt;/p&gt;
&lt;p&gt;
That&amp;#39;s a good advice for a new development but discouraging for migration done
by a tool.
&lt;/p&gt;
&lt;p&gt;
So, what is the way forward?
&lt;/p&gt;
&lt;p&gt;
Well, there is one. While accepting weak sides of Azure we can exploit its strong
sides.
&lt;/p&gt;
&lt;h4&gt;Parallel refactoring
&lt;/h4&gt;
&lt;p&gt;
Before continuing let&amp;#39;s consider a &lt;a href="https://github.com/nesterovsky-bros/parallel-refactoring/tree/master/Services/SerialProcessor.cs#L5-L29"&gt;code&lt;/a&gt; demoing
the problem:
&lt;/p&gt;
&lt;pre&gt;&lt;code class="lang-C#"&gt; &lt;span class="hljs-keyword"&gt;public&lt;/span&gt; &lt;span class="hljs-literal"&gt;void&lt;/span&gt; CreateReport(StringWriter
writer) { &lt;span class="hljs-built_in"&gt;var&lt;/span&gt; index = &lt;span class="hljs-number"&gt;0&lt;/span&gt;;
foreach(&lt;span class="hljs-built_in"&gt;var&lt;/span&gt; transaction &lt;span class="hljs-keyword"&gt;in&lt;/span&gt; dataService.
GetTransactions(). OrderBy(item =&amp;gt; (item.At, item.SourceAccountId))) { &lt;span class="hljs-built_in"&gt;var&lt;/span&gt; sourceAccount
= dataService.GetAccount(transaction.SourceAccountId); &lt;span class="hljs-built_in"&gt;var&lt;/span&gt; targetAccount
= transaction.TargetAccountId != &lt;span class="hljs-built_in"&gt;null&lt;/span&gt; ? dataService.GetAccount(transaction.TargetAccountId)
: &lt;span class="hljs-built_in"&gt;null&lt;/span&gt;; ++index; &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (index
% &lt;span class="hljs-number"&gt;100&lt;/span&gt; == &lt;span class="hljs-number"&gt;0&lt;/span&gt;) { Console.WriteLine(index);
} writer.WriteLine($&lt;span class="hljs-string"&gt;"{index},{transaction.Id},{ transaction.At},{transaction.Type},{transaction.Amount},{
transaction.SourceAccountId},{sourceAccount?.Name},{ transaction.TargetAccountId},{targetAccount?.Name}"&lt;/span&gt;);
} } &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
This cycle queries transactions, along with two more small queries to get source and
target accounts for each transaction. Results are printed into a report.
&lt;/p&gt;
&lt;p&gt;
If we assume query latency just 1 millisecond, and try to run such code for 100K transactions
we easily come to 200+ seconds of execution. 
&lt;/p&gt;
&lt;p&gt;
Reality turns to be much worse. Program spends most of its lifecycle waiting for database
results, and iterations don&amp;#39;t advance until all work of previous iterations is
complete.
&lt;/p&gt;
&lt;p&gt;
We could do better even without trying to rewrite all code! Let&amp;#39;s articulate our
goals:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
To make code fast.&lt;/li&gt;
&lt;li&gt;
To leave code recognizable.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The idea is to form two processing pipelines:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
(a) one that processes data in parallel out of order;&lt;/li&gt;
&lt;li&gt;
(b) other that processes data serially, in original order;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Each pipeline may post sub-tasks to the other, so (a) runs its tasks in parallel unordered,
while (b) runs its tasks as if everything was running serially.
&lt;/p&gt;
&lt;p&gt;
So, parallel plan would be like this:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Queue parallel sub-tasks (a) for each transaction.&lt;/li&gt;
&lt;li&gt;
Parallel sub-task in (a) reads source and target accounts, and queues serial sub-task
(b) passing transaction and accounts.&lt;/li&gt;
&lt;li&gt;
Serial sub-task (b) increments index, and writes report record.&lt;/li&gt;
&lt;li&gt;
Wait for all tasks to complete.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
To reduce burden of task piplelines we use &lt;a href="https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/dataflow-task-parallel-library"&gt;Dataflow
(Task Parallel Library)&lt;/a&gt;, and encapsulate everything in a small wrapper.
&lt;/p&gt;
&lt;p&gt;
Consider refactored &lt;a href="https://github.com/nesterovsky-bros/parallel-refactoring/tree/master/Services/ParallelProcessor.cs#L14-L48"&gt;code&lt;/a&gt;:
&lt;/p&gt;
&lt;pre&gt;&lt;code class="lang-C#"&gt; &lt;span class="hljs-keyword"&gt;public&lt;/span&gt; &lt;span class="hljs-literal"&gt;void&lt;/span&gt; CreateReport(StringWriter
writer) { using &lt;span class="hljs-built_in"&gt;var&lt;/span&gt; parallel = &lt;span class="hljs-literal"&gt;new&lt;/span&gt; Parallel(options.Value.Parallelism); &lt;span class="hljs-comment"&gt;//
⬅️ 1&lt;/span&gt; &lt;span class="hljs-built_in"&gt;var&lt;/span&gt; index = &lt;span class="hljs-number"&gt;0&lt;/span&gt;;
parallel.ForEachAsync( &lt;span class="hljs-comment"&gt;// ⬅️ 2&lt;/span&gt; dataService. GetTransactions().
OrderBy(item =&amp;gt; (item.At, item.SourceAccountId)), transaction =&amp;gt; &lt;span class="hljs-comment"&gt;//
⬅️ 3&lt;/span&gt; { &lt;span class="hljs-built_in"&gt;var&lt;/span&gt; sourceAccount = dataService.GetAccount(transaction.SourceAccountId); &lt;span class="hljs-built_in"&gt;var&lt;/span&gt; targetAccount
= transaction.TargetAccountId != &lt;span class="hljs-built_in"&gt;null&lt;/span&gt; ? dataService.GetAccount(transaction.TargetAccountId)
: &lt;span class="hljs-built_in"&gt;null&lt;/span&gt;; parallel.PostSync( &lt;span class="hljs-comment"&gt;//
⬅️ 4&lt;/span&gt; (transaction, sourceAccount, targetAccount), &lt;span class="hljs-comment"&gt;//
⬅️ 5&lt;/span&gt; &lt;span class="hljs-built_in"&gt;data&lt;/span&gt; =&amp;gt; { &lt;span class="hljs-built_in"&gt;var&lt;/span&gt; (transaction,
sourceAccount, targetAccount) = &lt;span class="hljs-built_in"&gt;data&lt;/span&gt;; &lt;span class="hljs-comment"&gt;//
⬅️ 6&lt;/span&gt; ++index; &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (index % &lt;span class="hljs-number"&gt;100&lt;/span&gt; == &lt;span class="hljs-number"&gt;0&lt;/span&gt;)
{ Console.WriteLine(index); } writer.WriteLine($&lt;span class="hljs-string"&gt;"{index},{transaction.Id},{
transaction.At},{transaction.Type},{transaction.Amount},{ transaction.SourceAccountId},{sourceAccount?.Name},{
transaction.TargetAccountId},{targetAccount?.Name}"&lt;/span&gt;); }); }); } &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Consider ⬅️ points:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
We create &lt;code&gt;Parallel&lt;/code&gt; utility class passing degree of parallelism requested.&lt;/li&gt;
&lt;li&gt;
We iterate transactions using &lt;code&gt;parallel.ForEachAsync()&lt;/code&gt; that queues parallel
sub-tasks for each transaction, and then waits until all tasks are complete.&lt;/li&gt;
&lt;li&gt;
Each parallel sub-task recieves a transaction. It may be called from a different thread.&lt;/li&gt;
&lt;li&gt;
Having recieved required accounts we queue a sub-task for synchronous execution using &lt;code&gt;parallel.PostSync()&lt;/code&gt;,
and&lt;/li&gt;
&lt;li&gt;
Pass there data collected in parallel sub-task: transaction and accounts.&lt;/li&gt;
&lt;li&gt;
We deconstruct data passed into variables, and then proceed with serial logic.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
What we achieve with this refactoring:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Top level query that brings transactions is done and iterated serially.&lt;/li&gt;
&lt;li&gt;
But each iteration body is run in parallel. By default we set it up to allow up to
100 parallel executions. All those parallel sub-task do not wait on each other so
their waitings do not add up.&lt;/li&gt;
&lt;li&gt;
Sync sub-tasks are queued and executed in order of their serial appearance, so increments
and report records are not a subject of race conditions, nor a subject of reordering
of output records.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
We think that such refactored code is still recognizible.
&lt;/p&gt;
&lt;p&gt;
As for performance this is what log shows:
&lt;/p&gt;
&lt;pre&gt;&lt;code class="lang-log"&gt;&lt;span class="hljs-selector-tag"&gt;Serial&lt;/span&gt; &lt;span class="hljs-selector-tag"&gt;test&lt;/span&gt; 100
... &lt;span class="hljs-selector-tag"&gt;Execution&lt;/span&gt; &lt;span class="hljs-selector-tag"&gt;time&lt;/span&gt;:
00&lt;span class="hljs-selector-pseudo"&gt;:01&lt;/span&gt;&lt;span class="hljs-selector-pseudo"&gt;:33.8152540&lt;/span&gt; &lt;span class="hljs-selector-tag"&gt;Parallel&lt;/span&gt; &lt;span class="hljs-selector-tag"&gt;test&lt;/span&gt; 100
... &lt;span class="hljs-selector-tag"&gt;Execution&lt;/span&gt; &lt;span class="hljs-selector-tag"&gt;time&lt;/span&gt;:
00&lt;span class="hljs-selector-pseudo"&gt;:00&lt;/span&gt;&lt;span class="hljs-selector-pseudo"&gt;:05.8705468&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Reference
&lt;/h4&gt;
&lt;p&gt;
Please take a look at project to understand implementation details, and in particular &lt;a href="https://github.com/nesterovsky-bros/parallel-refactoring/tree/master/Services/Parallel.cs"&gt;&lt;code&gt;Parallel&lt;/code&gt;&lt;/a&gt; class
implementing API to post parallel and serial tasks, run cycles and some more.
&lt;/p&gt;
&lt;p&gt;
Please continue reading on &lt;a href="https://github.com/nesterovsky-bros/parallel-refactoring/tree/master?tab=readme-ov-file#parallel-refactoring"&gt;GitHub&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=371a8329-d392-4d20-bcbc-db9503816f26" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,371a8329-d392-4d20-bcbc-db9503816f26.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=486afbcd-3e16-4240-97c4-3c985d6c6754</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,486afbcd-3e16-4240-97c4-3c985d6c6754.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=486afbcd-3e16-4240-97c4-3c985d6c6754</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
While doing a migration of some big xslt 3 project into plain C# we run into a case
that was not obvious to resolve.
</p>
        <p>
Documents we process can be from a tiny to a moderate size. Being stored in xml they
might take from virtually zero to, say, 10-20 MB.
</p>
        <p>
In C# we may rewrite Xslt code virtually in one-to-one manner using standard features
like XDocument, LINQ, regular classes, built-in collections, and so on. Clearly C#
has a reacher repertoire, so task is easily solved unless you run into multiple opportunities
to solve it.
</p>
        <p>
The simplest solution is to use XDocument API to represent data at runtime, and use
LINQ to query it. All features like xslt keys, templates, functions, xpath sequences,
arrays and maps and primitive types are natuarally mapped into C# language and its
APIs.
</p>
        <p>
Taking several xslt transformations we could see that xslt to C# rewrite is rather
straightforward and produces recognizable functional programs that have close C# source
code size to their original Xslt. As a bonus C# lets you write code in asynchronous
way, so C# wins in a runtime scalability, and in a design-time support.
</p>
        <p>
But can you do it better in C#, especially when some data has well defined xml schemas?
</p>
        <p>
The natural step, in our opinion, would be to produce C# plain object model from xml
schema and use it for runtime processing. Fortunately .NET has xml serialization attributes
and tools to produce classes from xml schemas. With small efforts we have created
a relevant class hierarchy for a rather big xml schema. XmlSerializer is used to convert
object model to and from xml through XmlReader and XmlWriter. So, we get typed replacement
of generic XDocument that still supports the same LINQ API over collections of objects,
and takes less memory at runtime.
</p>
        <p>
The next step would be to commit a simple test like: 
</p>
        <ul>
          <li>
            <p>
read object model;
</p>
          </li>
          <li>
            <p>
transform it;
</p>
          </li>
          <li>
            <p>
write it back.
</p>
          </li>
        </ul>
        <p>
We have created such tests both for XDocument and for object model cases, and compared
results from different perspectives.
</p>
        <p>
Both solution produce very similar code, which is also similar to original xslt both
in style and size.
</p>
        <p>
Object model has static typing, which is much better to support.
</p>
        <p>
But the most unexpected outcome is that object model was up to 20% slower due to serialization
and deserialization even with pregenerated xmlserializer assemblies. Difference of
transformation performance and memory consumption was so unnoticable that it can be
neglected. These results were confirmed with multiple tests, with multiple cycles
including heating up cycles.
</p>
        <p>
Here we run into a case where static typing harms more than helps. Because of the
nature of our processing pipeline, which is offline batch, this difference can be
mapped into 10th of minutes or even more.
</p>
        <p>
Thus in this particular case we decided to stay with runtime typing as a more performant
way of processing in C#. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=486afbcd-3e16-4240-97c4-3c985d6c6754" />
      </body>
      <title>XDocument vs Plain Object Model</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,486afbcd-3e16-4240-97c4-3c985d6c6754.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2023/01/08/XDocumentVsPlainObjectModel.aspx</link>
      <pubDate>Sun, 08 Jan 2023 13:28:14 GMT</pubDate>
      <description>  &lt;p&gt;
While doing a migration of some big xslt 3 project into plain C# we run into a case
that was not obvious to resolve.
&lt;/p&gt;
&lt;p&gt;
Documents we process can be from a tiny to a moderate size. Being stored in xml they
might take from virtually zero to, say, 10-20 MB.
&lt;/p&gt;
&lt;p&gt;
In C# we may rewrite Xslt code virtually in one-to-one manner using standard features
like XDocument, LINQ, regular classes, built-in collections, and so on. Clearly C#
has a reacher repertoire, so task is easily solved unless you run into multiple opportunities
to solve it.
&lt;/p&gt;
&lt;p&gt;
The simplest solution is to use XDocument API to represent data at runtime, and use
LINQ to query it. All features like xslt keys, templates, functions, xpath sequences,
arrays and maps and primitive types are natuarally mapped into C# language and its
APIs.
&lt;/p&gt;
&lt;p&gt;
Taking several xslt transformations we could see that xslt to C# rewrite is rather
straightforward and produces recognizable functional programs that have close C# source
code size to their original Xslt. As a bonus C# lets you write code in asynchronous
way, so C# wins in a runtime scalability, and in a design-time support.
&lt;/p&gt;
&lt;p&gt;
But can you do it better in C#, especially when some data has well defined xml schemas?
&lt;/p&gt;
&lt;p&gt;
The natural step, in our opinion, would be to produce C# plain object model from xml
schema and use it for runtime processing. Fortunately .NET has xml serialization attributes
and tools to produce classes from xml schemas. With small efforts we have created
a relevant class hierarchy for a rather big xml schema. XmlSerializer is used to convert
object model to and from xml through XmlReader and XmlWriter. So, we get typed replacement
of generic XDocument that still supports the same LINQ API over collections of objects,
and takes less memory at runtime.
&lt;/p&gt;
&lt;p&gt;
The next step would be to commit a simple test like: 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;
read object model;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
transform it;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
write it back.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
We have created such tests both for XDocument and for object model cases, and compared
results from different perspectives.
&lt;/p&gt;
&lt;p&gt;
Both solution produce very similar code, which is also similar to original xslt both
in style and size.
&lt;/p&gt;
&lt;p&gt;
Object model has static typing, which is much better to support.
&lt;/p&gt;
&lt;p&gt;
But the most unexpected outcome is that object model was up to 20% slower due to serialization
and deserialization even with pregenerated xmlserializer assemblies. Difference of
transformation performance and memory consumption was so unnoticable that it can be
neglected. These results were confirmed with multiple tests, with multiple cycles
including heating up cycles.
&lt;/p&gt;
&lt;p&gt;
Here we run into a case where static typing harms more than helps. Because of the
nature of our processing pipeline, which is offline batch, this difference can be
mapped into 10th of minutes or even more.
&lt;/p&gt;
&lt;p&gt;
Thus in this particular case we decided to stay with runtime typing as a more performant
way of processing in C#. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=486afbcd-3e16-4240-97c4-3c985d6c6754" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,486afbcd-3e16-4240-97c4-3c985d6c6754.aspx</comments>
      <category>.NET</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=5440bb74-6b87-4ec5-9119-715b3e16b9a7</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,5440bb74-6b87-4ec5-9119-715b3e16b9a7.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=5440bb74-6b87-4ec5-9119-715b3e16b9a7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Couple of days ago, while integrating with
someones C# library, we had to debug it, as something went wrong. The code is big
and obscure but for the integration purposes it's rather simple: you just create
and call a class, that's all. Yet, something just did not work. We had to prove
that it's not our fault, as the other side is uncooperative and would not run
common debug session to resolve the problem. 
<p></p><p>
To simplify the matter as much as possible here is the case: 
</p><blockquote><pre>var input = ...
var x = new X();
var output = x.Execute(input);
</pre></blockquote><p>
You pass correct input, and get correct output. Simple, right? But it did not work!
So, we delved into the foreign code, and this is what we have seen: 
</p><blockquote><pre>class X: Y
{
  public Output Execute(Input input)
  {
    return Perform(input);
  }

  protected override Output Run(Input input)
  { 
    ...

     return output;
  }
}

class Y: Z
{
  ...
}

class Z
{
  protected Output Perform(Input input)
  {
    return Run(Input);
  }
        
  protected virtual Output Run(Input input)
  {
    return null;
  }
}</pre></blockquote><p></p><p>
Do you see, still flow is simple, right? We call <code>X.Execute()</code>, it calls <code>Z.Perform()</code>,
which in turn calls overriden <code>X.Run()</code> that returns the result. 
</p><p>
But to our puzzlement we got <code>null</code> on output, as if <code>Z.Run()</code> was
called! 
</p><p>
We stepped through the code in debugger and confirmed that <code>Z.Perform()</code> calls <code>Z.Run()</code>,
even though "<code>this</code>" instance is of type <code>X</code>. 
</p><p>
How can it be? It's a nonsence! Yet, no overriden method was ever called. 
</p><p>
No matter how much scrunity we applied to sources <code>X</code> and <code>Z</code> it
just did not work. 
</p><p></p><p>
We verified that the signature of <code>X.Run()</code> matches the signature of <code>Z.Run()</code>,
so it overrides the method. 
</p><p>
Then what do we see here? 
</p><p>
And then enlightenment come! Yes, <code>X.Run()</code> overrides the method, but what
method? 
</p><p>
We looked closely at class <code>Y</code>, and bingo, we can see there following: 
</p><blockquote><pre>class Y: Z
{
  ...

  protected virtual Output Run(Input input)
  {
    return null;
  }
      
  ...
}</pre></blockquote><p>
So, <code>X.Run()</code> overrides <code>Y.Run()</code> and not <code>Z.Run()</code>! 
</p><p>
Per .NET <code>Y.Run()</code> and <code>Z.Run()</code> are two independant virtual
methods, where <code>Y.Run()</code> in addition hides <code>Z.Run()</code>. 
</p><p>
IDE even issued a warning that it's better declare <code>Y.Run()</code> as: 
</p><blockquote><pre>protected new virtual Output Run(Input input)
{
  return null;
}</pre></blockquote><p>
So, someones code was plainly wrong: <code>Y.Run()</code> had to use <code><b>override</b></code> rather
than <code><b>virtual</b></code>. 
</p><p>
We won, right? 
</p><p>
Well, it's hard to call it a win. 
</p><p>
We spent a hour looking at someones ugly code just to prove we're still sane. 
</p><p></p><p>
So, what is conclusion of this story? 
</p><p>
We think here it is: 
</p><ul><li>
be cautious while looking at someones code;</li><li>
look at IDE warnings, don't disregard them, and try to resolve all of them in
your code base;</li><li>
try to write simple code.</li></ul><img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=5440bb74-6b87-4ec5-9119-715b3e16b9a7" /></body>
      <title>Bad code</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,5440bb74-6b87-4ec5-9119-715b3e16b9a7.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2022/01/15/BadCode.aspx</link>
      <pubDate>Sat, 15 Jan 2022 13:55:08 GMT</pubDate>
      <description>Couple of days ago, while integrating with someones C# library, we had to debug it, as something went wrong.
        The code is big and obscure but for the integration purposes it&amp;#39;s rather simple: you just create and call a class, that&amp;#39;s all.
        Yet, something just did not work. We had to prove that it&amp;#39;s not our fault, as the other side is uncooperative and would not run common debug session to resolve the problem.
    &lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
To simplify the matter as much as possible here is the case: 
&lt;/p&gt;
&lt;blockquote&gt; &lt;pre&gt;var input = ...
var x = new X();
var output = x.Execute(input);
&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
You pass correct input, and get correct output. Simple, right? But it did not work!
So, we delved into the foreign code, and this is what we have seen: 
&lt;/p&gt;
&lt;blockquote&gt; &lt;pre&gt;class X: Y
{
  public Output Execute(Input input)
  {
    return Perform(input);
  }

  protected override Output Run(Input input)
  { 
    ...

     return output;
  }
}

class Y: Z
{
  ...
}

class Z
{
  protected Output Perform(Input input)
  {
    return Run(Input);
  }
        
  protected virtual Output Run(Input input)
  {
    return null;
  }
}&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Do you see, still flow is simple, right? We call &lt;code&gt;X.Execute()&lt;/code&gt;, it calls &lt;code&gt;Z.Perform()&lt;/code&gt;,
which in turn calls overriden &lt;code&gt;X.Run()&lt;/code&gt; that returns the result. 
&lt;/p&gt;
&lt;p&gt;
But to our puzzlement we got &lt;code&gt;null&lt;/code&gt; on output, as if &lt;code&gt;Z.Run()&lt;/code&gt; was
called! 
&lt;/p&gt;
&lt;p&gt;
We stepped through the code in debugger and confirmed that &lt;code&gt;Z.Perform()&lt;/code&gt; calls &lt;code&gt;Z.Run()&lt;/code&gt;,
even though &amp;quot;&lt;code&gt;this&lt;/code&gt;&amp;quot; instance is of type &lt;code&gt;X&lt;/code&gt;. 
&lt;/p&gt;
&lt;p&gt;
How can it be? It&amp;#39;s a nonsence! Yet, no overriden method was ever called. 
&lt;/p&gt;
&lt;p&gt;
No matter how much scrunity we applied to sources &lt;code&gt;X&lt;/code&gt; and &lt;code&gt;Z&lt;/code&gt; it
just did not work. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
We verified that the signature of &lt;code&gt;X.Run()&lt;/code&gt; matches the signature of &lt;code&gt;Z.Run()&lt;/code&gt;,
so it overrides the method. 
&lt;/p&gt;
&lt;p&gt;
Then what do we see here? 
&lt;/p&gt;
&lt;p&gt;
And then enlightenment come! Yes, &lt;code&gt;X.Run()&lt;/code&gt; overrides the method, but what
method? 
&lt;/p&gt;
&lt;p&gt;
We looked closely at class &lt;code&gt;Y&lt;/code&gt;, and bingo, we can see there following: 
&lt;/p&gt;
&lt;blockquote&gt; &lt;pre&gt;class Y: Z
{
  ...

  protected virtual Output Run(Input input)
  {
    return null;
  }
      
  ...
}&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
So, &lt;code&gt;X.Run()&lt;/code&gt; overrides &lt;code&gt;Y.Run()&lt;/code&gt; and not &lt;code&gt;Z.Run()&lt;/code&gt;! 
&lt;/p&gt;
&lt;p&gt;
Per .NET &lt;code&gt;Y.Run()&lt;/code&gt; and &lt;code&gt;Z.Run()&lt;/code&gt; are two independant virtual
methods, where &lt;code&gt;Y.Run()&lt;/code&gt; in addition hides &lt;code&gt;Z.Run()&lt;/code&gt;. 
&lt;/p&gt;
&lt;p&gt;
IDE even issued a warning that it&amp;#39;s better declare &lt;code&gt;Y.Run()&lt;/code&gt; as: 
&lt;/p&gt;
&lt;blockquote&gt; &lt;pre&gt;protected new virtual Output Run(Input input)
{
  return null;
}&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
So, someones code was plainly wrong: &lt;code&gt;Y.Run()&lt;/code&gt; had to use &lt;code&gt;&lt;b&gt;override&lt;/b&gt;&lt;/code&gt; rather
than &lt;code&gt;&lt;b&gt;virtual&lt;/b&gt;&lt;/code&gt;. 
&lt;/p&gt;
&lt;p&gt;
We won, right? 
&lt;/p&gt;
&lt;p&gt;
Well, it&amp;#39;s hard to call it a win. 
&lt;/p&gt;
&lt;p&gt;
We spent a hour looking at someones ugly code just to prove we&amp;#39;re still sane. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
So, what is conclusion of this story? 
&lt;/p&gt;
&lt;p&gt;
We think here it is: 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
be cautious while looking at someones code;&lt;/li&gt;
&lt;li&gt;
look at IDE warnings, don&amp;#39;t disregard them, and try to resolve all of them in
your code base;&lt;/li&gt;
&lt;li&gt;
try to write simple code.&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=5440bb74-6b87-4ec5-9119-715b3e16b9a7" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,5440bb74-6b87-4ec5-9119-715b3e16b9a7.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=9121c6f9-e622-4d71-b25b-58763fa5d88e</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9121c6f9-e622-4d71-b25b-58763fa5d88e.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=9121c6f9-e622-4d71-b25b-58763fa5d88e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Recently we have found that <a href="https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter.serialize?view=net-5.0">BinaryFormatter.Serialize</a> and <a href="https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter.deserialize?view=net-5.0">BinaryFormatter.Deserialize</a> methods
are marked as obsolete in .NET 5.0, and are declared dangerous:
</p>
        <blockquote>
          <i>The <strong>BinaryFormatter</strong> type is dangerous and is <strong>not</strong> recommended
for data processing. Applications should stop using BinaryFormatter as soon as possible,
even if they believe the data they're processing to be trustworthy. BinaryFormatter
is insecure and can't be made secure.</i>
        </blockquote>
        <p>
See <a href="https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide">BinaryFormatter
security guide</a> for more details.
</p>
        <p>
That guide along with its links go and expand on what problems BinaryFormatter poses.
The schema of dangeous use cases, we have seen so far, is like that:
</p>
        <ul>
          <li>
two different sides communicate to each other;</li>
          <li>
one side supplies input in BinaryFormatter's format;</li>
          <li>
other side reads input using BinaryFormatter and instantiates classes.</li>
        </ul>
        <p>
A threat arises when two sides cannot trust to each other or cannot establish trusted
communication chanel. In these cases malign input can be supplied to a side reading
the data, which might lead to unexpected code execution, deny of service, data exposure
and to other bad consequences.
</p>
        <p>
Arguing like this, today's maintainers of .NET concluded that it's better to tear
down BinaryFormatter and similar APIs out of the framework.
</p>
        <p>
Note that they don't claim BinaryFormatter itself, or Reflection API that it uses,
as a core of the problem. They blame on communication.
</p>
        <p>
Spelling out clearly what are concerns could help to everyone to better understand
how to address it. In the area of security of communication there are a lot of ready
solutions like:
</p>
        <ul>
          <li>
use signature to avoid tampering the data;</li>
          <li>
use encription to avoid spying the data;</li>
          <li>
use access rights to avoid even access to the data;</li>
          <li>
use secure communication channels.</li>
        </ul>
        <p>
We can surely state that without applying these solutions no other serialization format
is reliable and is subject of the same vulnerabilities.
</p>
        <p>
 
</p>
        <p>
After all it looked like an attempt to throw out the baby with the bath water. The
good news is that thankfully to now modular structure of .NET runtime we're able
to access binary serialization library, which are (and will be) available on nugets
repositories. So, it's futile effort to erase this usefull API. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9121c6f9-e622-4d71-b25b-58763fa5d88e" />
      </body>
      <title>Binary serialization in .NET 5.0</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9121c6f9-e622-4d71-b25b-58763fa5d88e.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2021/02/02/BinarySerializationInNET50.aspx</link>
      <pubDate>Tue, 02 Feb 2021 12:39:37 GMT</pubDate>
      <description>&lt;p&gt;
Recently we have found that &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter.serialize?view=net-5.0"&gt;BinaryFormatter.Serialize&lt;/a&gt; and &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter.deserialize?view=net-5.0"&gt;BinaryFormatter.Deserialize&lt;/a&gt; methods
are marked as obsolete in .NET 5.0, and are declared dangerous:
&lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;The &lt;strong&gt;BinaryFormatter&lt;/strong&gt; type is dangerous and is &lt;strong&gt;not&lt;/strong&gt; recommended
for data processing. Applications should stop using BinaryFormatter as soon as possible,
even if they believe the data they're processing to be trustworthy. BinaryFormatter
is insecure and can't be made secure.&lt;/i&gt;&lt;/blockquote&gt; 
&lt;p&gt;
See &lt;a href="https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide"&gt;BinaryFormatter
security guide&lt;/a&gt; for more details.
&lt;/p&gt;
&lt;p&gt;
That guide along with its links go and expand on what problems BinaryFormatter poses.
The schema of dangeous use cases, we have seen so far, is like that:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
two different sides communicate to each other;&lt;/li&gt;
&lt;li&gt;
one side supplies input in BinaryFormatter&amp;#39;s format;&lt;/li&gt;
&lt;li&gt;
other side reads input using BinaryFormatter and instantiates classes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
A threat arises when two sides cannot trust to each other or cannot establish trusted
communication chanel. In these cases malign input can be supplied to a side reading
the data, which might lead to unexpected code execution, deny of service, data exposure
and to other bad consequences.
&lt;/p&gt;
&lt;p&gt;
Arguing like this, today's maintainers of .NET concluded that it's better to tear
down BinaryFormatter and similar APIs out of the framework.
&lt;/p&gt;
&lt;p&gt;
Note that they don&amp;#39;t claim BinaryFormatter itself, or Reflection API that it uses,
as a core of the problem. They blame on communication.
&lt;/p&gt;
&lt;p&gt;
Spelling out clearly what are concerns could help to everyone to better understand
how to address it. In the area of security of communication there are a lot of ready
solutions like:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
use signature to avoid tampering the data;&lt;/li&gt;
&lt;li&gt;
use encription to avoid spying the data;&lt;/li&gt;
&lt;li&gt;
use access rights to avoid even access to the data;&lt;/li&gt;
&lt;li&gt;
use secure communication channels.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
We can surely state that without applying these solutions no other serialization format
is reliable and is subject of the same vulnerabilities.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
After all it looked like an attempt to throw out the baby with the bath water. The
good news is that thankfully to now modular structure of .NET runtime we&amp;#39;re able
to access binary serialization library, which are (and will be) available on nugets
repositories. So, it&amp;#39;s futile effort to erase this usefull API. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9121c6f9-e622-4d71-b25b-58763fa5d88e" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,9121c6f9-e622-4d71-b25b-58763fa5d88e.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=638aefac-fd7a-431a-9d7f-0aeef885d9a5</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,638aefac-fd7a-431a-9d7f-0aeef885d9a5.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=638aefac-fd7a-431a-9d7f-0aeef885d9a5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img src="https://i.ytimg.com/vi/VJTpbZrBAeU/hqdefault.jpg" width="200" style="float: left; margin-right: 5px;" />Eventually
we've started to deal with tasks that required machine learning. Thus, the good tutorial
for ML.NET was required and we had <a href="https://youtu.be/VJTpbZrBAeU" target="_blank">found
this one</a> that goes along with <a href="https://github.com/jeffprosise/ML.NET" target="_blank">good
simple codesamples</a>. Thanks to Jeff Prosise. Hope this may be helpfull to you too.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=638aefac-fd7a-431a-9d7f-0aeef885d9a5" />
      </body>
      <title>ML.NET tutorial</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,638aefac-fd7a-431a-9d7f-0aeef885d9a5.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2020/12/16/MLNETTutorial.aspx</link>
      <pubDate>Wed, 16 Dec 2020 11:38:57 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img src="https://i.ytimg.com/vi/VJTpbZrBAeU/hqdefault.jpg" width="200" style="float: left; margin-right: 5px;" /&gt;Eventually
we've started to deal with tasks that required machine learning. Thus, the good tutorial
for ML.NET was required and we had &lt;a href="https://youtu.be/VJTpbZrBAeU" target="_blank"&gt;found
this one&lt;/a&gt; that goes along with &lt;a href="https://github.com/jeffprosise/ML.NET" target="_blank"&gt;good
simple codesamples&lt;/a&gt;. Thanks to Jeff Prosise. Hope this may be helpfull to you too.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=638aefac-fd7a-431a-9d7f-0aeef885d9a5" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,638aefac-fd7a-431a-9d7f-0aeef885d9a5.aspx</comments>
      <category>.NET</category>
      <category>ML.NET</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=71417941-17bf-4a83-8249-5e980b6370fe</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,71417941-17bf-4a83-8249-5e980b6370fe.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=71417941-17bf-4a83-8249-5e980b6370fe</wfw:commentRss>
      <title>File selection and uploading AngularJS directive for IE8.</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,71417941-17bf-4a83-8249-5e980b6370fe.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2017/01/04/FileSelectionAndUploadingAngularJSDirectiveForIE8.aspx</link>
      <pubDate>Wed, 04 Jan 2017 10:52:41 GMT</pubDate>
      <description>    &lt;p&gt;
Although the golden age of IE8 has already passed and Microsoft already has stopped
its support, this browser still occupies about &lt;a href="https://www.netmarketshare.com/browser-market-share.aspx?qprid=2&amp;qpcustomd=0"&gt; 3%
of the of the world market desktop browsers&lt;/a&gt;. Despite this, many big organisations
still use this browser for enterprise web applications. We may confirm this, since
we deal with such organisations around the world. Companies try to get rid of IE8,
but this often requires Windows upgrade and resources to re-test all their web applications.
If such company has many web terminals&amp;nbsp; with Windows 7 or even with XP, this
task becames rather expensive. So, this process advances rather slowly. Meanwhile,
these organizations don&amp;#39;t stop development of new web applications that must work
on new HTML5 browsers and on old IE8. 
&lt;/p&gt;
&lt;p&gt;
A year ago we had developed an &lt;a href="http://www.nesterovsky-bros.com/weblog/2015/11/08/AngularJSFileUpload.aspx"&gt;UIUpload
AngularJS directive and service&lt;/a&gt; that simplifies file uploading in web applications
with AngularJS client. It works as expected on all HTML5 browsers. But few days ago,
we were asked to help with file uploading from AngularJS web application that will
work in IE8. We&amp;#39;ve spent few hours in order to investigate existing third-party
AngularJS directives and components. Here are few of them:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/nervgh/angular-file-upload"&gt;angular-file-upload&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/danialfarid/ng-file-upload"&gt;ng-file-upload&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://fineuploader.com/"&gt;fine-uploader&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
All of these directives for IE8 degrade down to &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt; and
then track the uploading progress. These solutions don&amp;#39;t allow to select files
for old browsers. At the same time, our aim was to implement an AngularJS directive
that allows selecting a file and perform uploading, which will work for IE8 and for
new browsers too.
&lt;/p&gt;
&lt;p class="auto-style1"&gt;
Since IE8 neither supports &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects"&gt;FormData&lt;/a&gt; nor &lt;a href="https://developer.mozilla.org/en/docs/Web/API/File"&gt;File
API&lt;/a&gt;, thus, the directive must work with DOM elements directly. In order to open
file selection dialog we need to hide&amp;nbsp; &lt;code&gt;&amp;lt;input type=&amp;quot;file&amp;quot;/&amp;gt;&lt;/code&gt; element
and then to route client-side event to it. When a file is selected it is sent to a
server as &lt;code&gt;multipart/form-data&lt;/code&gt; message. The server&amp;#39;s result will be
caught by hidden &lt;code&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt; element and passed to the directive&amp;#39;s
controller. 
&lt;/p&gt;
&lt;p&gt;
After few attempts we&amp;#39;ve implemented the &lt;a href="https://github.com/nesterovsky-bros/FileUpload/blob/master/FileUpload/Scripts/file-upload.js"&gt;desired
directive&lt;/a&gt;. The small VS2015 solution that demonstrates this directive and server-side
file handler you may &lt;a href="https://github.com/nesterovsky-bros/FileUpload"&gt;download
here&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
The key feature of &lt;a href="https://github.com/nesterovsky-bros/FileUpload/blob/master/FileUpload/Scripts/file-upload.js"&gt;this
directive&lt;/a&gt; is emulation of &lt;code&gt;replace&lt;/code&gt; and &lt;code&gt;template&lt;/code&gt; &lt;a href="https://code.angularjs.org/1.2.16/docs/api/ng/service/$compile#-restrict-"&gt;directive&amp;#39;s
definition properties&lt;/a&gt;:
&lt;/p&gt;
&lt;pre style="background-color: #f5f5f5;"&gt;
       var directive =
       {
          restrict: "AE",
          scope:
          {
            id: "@",
            serverUrl: "@",
            accept: "@?",
            onSuccess: "&amp;",
            onError: "&amp;?",
          },
          link: function (scope, element, attrs, controller)
          {
            var id = scope.id || ("fileUpload" + scope.$id);

            var template = "&amp;lt;iframe name='%id%$iframe' id='%id%$iframe' style='display: none;'&amp;gt;" +
              "&amp;lt;/iframe&amp;gt;&amp;lt;form name='%id%$form' enctype='multipart/form-data' " +
              "method='post' action='%action%' target='%id%$iframe'&amp;gt;" +
              "&amp;lt;span style='position:relative;display:inline-block;overflow:hidden;padding:0;'&amp;gt;" +
              "%html%&amp;lt;input type='file' name='%id%$file' id='%id%$file' " +
              "style='position:absolute;height:100%;width:100%;left:-10px;top:-1px;z-index:100;" +
              "font-size:50px;opacity:0;filter:alpha(opacity=0);'/&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/form&amp;gt;".
                replace("%action%", scope.serverUrl).
                  replace("%html%", element.html()).
                    replace(/%id%/g, id);

            element.replaceWith(template);
            ...
          }
       }
    &lt;/pre&gt;
&lt;p&gt;
We used such emulation, since each directive instance (an element) must have unique
name and ID in order to work properly. On the one hand template that returned by function
should have a root element when you use replace. On the other hand, IE8 doesn&amp;#39;t
like such root element (e.g. we&amp;#39;ve not succeeded to dispatch the click javascript
event to the &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; element).&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
The usage of the directive looks like as our previous example (see &lt;a href="https://github.com/nesterovsky-bros/UIUpload"&gt;UIUpload&lt;/a&gt;):
&lt;/p&gt;
&lt;pre style="background-color: #f5f5f5;" class="auto-style1"&gt;
    &amp;lt;a file-upload=&amp;quot;&amp;quot;
      class="btn btn-primary"        
      accept=".*"
      server-url="api/upload"
      on-success="controller.uploadSucceed(data, fileName)"
      on-error="controller.uploadFailed(e)"&amp;gt;Click here to upload file&amp;lt;/a&amp;gt;    &lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Where:
&lt;/p&gt;
&lt;dl&gt;
&lt;dt style="font-weight: bold"&gt;accept&lt;/dt&gt;
&lt;dd&gt;is a comma separated list of acceptable file extensions.&lt;/dd&gt;
&lt;dt style="font-weight: bold"&gt;server-url&lt;/dt&gt; 
&lt;dd&gt;
is server URL where to upload the selected file. In case when there is no "server-url"
attribute the content of selected file will be passed to success handler as a data
URI.&lt;/dd&gt;
&lt;dt style="font-weight: bold"&gt;on-success&lt;/dt&gt; 
&lt;dd&gt;
A "success" handler, which is called when upload is finished successfully.&lt;/dd&gt;
&lt;dt style="font-weight: bold"&gt;on-error&lt;/dt&gt; 
&lt;dd&gt;
An "error" handler, which is called when upload is failed.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;
We hope this simple directive may help to keep calm for those of you who is forced
to deal with IE8 and more advanced browsers at the same time.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=71417941-17bf-4a83-8249-5e980b6370fe" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,71417941-17bf-4a83-8249-5e980b6370fe.aspx</comments>
      <category>.NET</category>
      <category>AngularJS</category>
      <category>ASP.NET</category>
      <category>javascript</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=41fb6e80-8184-404b-aefe-2123225a72a0</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,41fb6e80-8184-404b-aefe-2123225a72a0.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=41fb6e80-8184-404b-aefe-2123225a72a0</wfw:commentRss>
      <title>Dependency Injection</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,41fb6e80-8184-404b-aefe-2123225a72a0.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2016/10/06/DependencyInjection.aspx</link>
      <pubDate>Thu, 06 Oct 2016 11:27:42 GMT</pubDate>
      <description>  &lt;p&gt;
Our genuine love is C++. Unfortunately clients don&amp;#39;t always share our favors,
so we mostly occupied in the C#, java and javascript. Nevertheless, we&amp;#39;re closely
watching the evolution of the C++. It became more mature in the latest specs.
&lt;/p&gt;
&lt;p&gt;
Recently, we wondered how would we deal with dependency injection in C++. What we
found is only strengthened our commitment to C++.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://en.cppreference.com/w/cpp/language/parameter_pack"&gt;Parameter packs&lt;/a&gt; introduced
in C++ 11 allow trivial implementation of constructor injection, while &lt;a href="http://en.cppreference.com/w/cpp/types/type_index"&gt;std::type_index&lt;/a&gt;, &lt;a href="http://en.cppreference.com/w/cpp/types/type_info"&gt;std::type_info&lt;/a&gt; and &lt;a href="http://en.cppreference.com/w/cpp/utility/any"&gt;std:any&lt;/a&gt; give
service containers. 
&lt;/p&gt;
&lt;p&gt;
In fact there are many DI implementations out there. The one we refer here is &lt;a href="http://boost-experimental.github.io/di/index.html"&gt;Boost.DI&lt;/a&gt;.
It&amp;#39;s not standard nor we can claim it&amp;#39;s the best but it&amp;#39;s good example
of how this concept can be implemented.
&lt;/p&gt;
&lt;p&gt;
So, consider &lt;a href="http://boost-experimental.github.io/di/tutorial/index.html"&gt;their
example&lt;/a&gt; seen in Java with CDI, in C# in .NET Core injection, and in C++:
&lt;/p&gt;
&lt;p&gt;
Java:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre style="white-space: pre-wrap"&gt;
@Dependent
public class Renderer 
{
  @Inject @Device
  private int device;
};

@Dependent
public class View 
{
  @Inject @Title
  private String title;
  @Inject
  private Renderer renderer;
};

@Dependent
public class Model {};

@Dependent
public class Controller 
{
  @Inject
  private Model model;
  @Inject
  private View view;
};

@Dependent
public class User {};

@Dependent
public class App 
{
  @Inject
  private Controller controller;
  @Inject
  private User user;
};

...
  Privider&amp;lt;App&gt; provider = ...

  App app = provider.get();
&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
C#:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre style="white-space: pre-wrap"&gt;public class RenderedOptions
{
  public int Device { get; set; }
}
    
public class ViewOptions
{
  public int Title { get; set; }
}
    
public class Renderer 
{
  public Renderer(IOptions&amp;lt;RendererOptions&gt; options)
  {
    Device = options.Device;
  }

  public int Device { get; set; }
}

public class View 
{
  public View(IOptions&amp;lt;ViewOptions&gt; options, Renderer renderer)
  {
    Title = options.Title;
    Renderer = renderer;
  }

  public string Title { get; set; }
  public Renderer Renderer { get; set; }
}

public class Model {}

public class Controller 
{
  public Controller(Model model, View view) 
  {
    Model = model;
    View = view;
  }

  public Model Model { get; set; }
  public View View { get; set; }
};

public class User {};

public class App 
{
  public App(Controller controller, User user) 
  {
    Controller = controller;
    User = user;
  }

  public Controller Controller { get; set; }
  public User User { get; set; }
};

...
  IServiceProvider serviceProvider = ...

  serviceProvider.GetService&amp;lt;App&gt;();
&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
C++:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre style="white-space: pre-wrap"&gt;#include &amp;lt;boost/di.hpp&gt;

namespace di = boost::di;

struct renderer 
{
  int device;
};

class view 
{
public:
  view(std::string title, const renderer&amp;) {}
};

class model {};

class controller 
{
public:
  controller(model&amp;, view&amp;) {}
};

class user {};

class app 
{
public:
  app(controller&amp;, user&amp;) {}
};

int main()
{
  /**
   * renderer renderer_;
   * view view_{"", renderer_};
   * model model_;
   * controller controller_{model_, view_};
   * user user_;
   * app app_{controller_, user_};
   */

  auto injector = di::make_injector();
  injector.create&amp;lt;app&gt;();
}
&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
What is different between these DI flavors?
&lt;/p&gt;
&lt;p&gt;
Not too much from the perspective of the final task achieved.
&lt;/p&gt;
&lt;p&gt;
In java we used &lt;a href="http://docs.jboss.org/weld/reference/2.4.0.CR1/en-US/html/injection.html"&gt;member
injection&lt;/a&gt;, with &lt;a href="http://docs.jboss.org/weld/reference/2.4.0.CR1/en-US/html/injection.html#_qualifier_annotations"&gt;qualifiers&lt;/a&gt; to
inject scalars.
&lt;/p&gt;
&lt;p&gt;
In C# we used &lt;a href="https://docs.asp.net/en/latest/fundamentals/dependency-injection.html"&gt;constructor
injection&lt;/a&gt; with &lt;a href="https://docs.asp.net/en/latest/fundamentals/configuration.html#options-config-objects"&gt;Options
pattern&lt;/a&gt; to inject scalars.
&lt;/p&gt;
&lt;p&gt;
In C++ we used constructor injection with direct constants injected.
&lt;/p&gt;
&lt;p&gt;
All technologies have their API to initialize DI container, but, again, while API
is different, the idea is the same.
&lt;/p&gt;
&lt;p&gt;
So, expressiveness of C++ matches to those of java and C#.
&lt;/p&gt;
&lt;p&gt;
Deeper analysis shows that java&amp;#39;s CDI is more feature rich than DI of C# and C++,
but, personally, we consider it&amp;#39;s advantage of C# and C++ that they have such
a light DI.
&lt;/p&gt;
&lt;p&gt;
At the same time there is an important difference between C++ vs java and C#.
&lt;/p&gt;
&lt;p&gt;
While both java and C# are deemed to use reflection (C# in theory could use code generation
on the fly to avoid reflection), C++&amp;#39;s DI natively constructs and injects services.
&lt;/p&gt;
&lt;p&gt;
What does it mean for the user?
&lt;/p&gt;
&lt;p&gt;
Well, a lot! Both in java and in C# you would not want to use DI in a performance
critical part of code (e.g. in a tight loop), while it&amp;#39;s Ok in C++ due to near
to zero performance impact from DI. This may result in more modular and performant
code in C++.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=41fb6e80-8184-404b-aefe-2123225a72a0" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,41fb6e80-8184-404b-aefe-2123225a72a0.aspx</comments>
      <category>.NET</category>
      <category>C++</category>
      <category>Java</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=2f514933-13db-476b-90c8-9cc9724b708d</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,2f514933-13db-476b-90c8-9cc9724b708d.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=2f514933-13db-476b-90c8-9cc9724b708d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
While reading on ASP.NET Core Session, and analyzing the difference with previous
version of ASP.NET we bumped into a problem...
</p>
        <p>
At <a href="https://docs.asp.net/en/latest/fundamentals/app-state.html#managing-application-state">Managing
Application State</a> they note:
</p>
        <blockquote>
          <i>Session is non-locking, so if two requests both attempt to modify the
contents of session, the last one will win. Further, Session is implemented as a coherent
session, which means that all of the contents are stored together. This means that
if two requests are modifying different parts of the session (different keys), they
may still impact each other.</i>
        </blockquote>
        <p>
This is different from previous versions of ASP.NET where session was blocking, which
meant that if you had multiple concurrent requests to the session, then all requests
were synchronized. So, you could keep consistent state.
</p>
        <p>
In ASP.NET Core you have no built-in means to keep a consistent state of the session.
Even assurances that the session is coherent does not help in any way.
</p>
        <p>
You options are:
</p>
        <ul>
          <li>
build your own synchronization to deal with this problem (e.g. around the database);</li>
          <li>
decree that your application cannot handle concurrent requests to the same session,
so client should not attempt it, otherwise behaviour is undefined.</li>
        </ul>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=2f514933-13db-476b-90c8-9cc9724b708d" />
      </body>
      <title>ASP.NET Core Session</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,2f514933-13db-476b-90c8-9cc9724b708d.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2016/09/28/ASPNETCoreSession.aspx</link>
      <pubDate>Wed, 28 Sep 2016 19:22:15 GMT</pubDate>
      <description>  &lt;p&gt;
While reading on ASP.NET Core Session, and analyzing the difference with previous
version of ASP.NET we bumped into a problem...
&lt;/p&gt;
&lt;p&gt;
At &lt;a href="https://docs.asp.net/en/latest/fundamentals/app-state.html#managing-application-state"&gt;Managing
Application State&lt;/a&gt; they note:
&lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;Session is non-locking, so if two requests both attempt to modify the
contents of session, the last one will win. Further, Session is implemented as a coherent
session, which means that all of the contents are stored together. This means that
if two requests are modifying different parts of the session (different keys), they
may still impact each other.&lt;/i&gt; &lt;/blockquote&gt; 
&lt;p&gt;
This is different from previous versions of ASP.NET where session was blocking, which
meant that if you had multiple concurrent requests to the session, then all requests
were synchronized. So, you could keep consistent state.
&lt;/p&gt;
&lt;p&gt;
In ASP.NET Core you have no built-in means to keep a consistent state of the session.
Even assurances that the session is coherent does not help in any way.
&lt;/p&gt;
&lt;p&gt;
You options are:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
build your own synchronization to deal with this problem (e.g. around the database);&lt;/li&gt;
&lt;li&gt;
decree that your application cannot handle concurrent requests to the same session,
so client should not attempt it, otherwise behaviour is undefined.&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=2f514933-13db-476b-90c8-9cc9724b708d" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,2f514933-13db-476b-90c8-9cc9724b708d.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=2c8f3cc7-725b-4c89-abc4-dc785e14058e</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,2c8f3cc7-725b-4c89-abc4-dc785e14058e.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=2c8f3cc7-725b-4c89-abc4-dc785e14058e</wfw:commentRss>
      <title>Angular1 ES 2015 with decorators</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,2c8f3cc7-725b-4c89-abc4-dc785e14058e.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2016/09/11/Angular1ES2015WithDecorators.aspx</link>
      <pubDate>Sun, 11 Sep 2016 08:32:54 GMT</pubDate>
      <description>  &lt;p&gt;
Angular 2 is already available though there are a lot of code and libraries that are
still in Angular 1.x. Here we outline how to write AngularJS 1.x in the modern javascript. 
&lt;/p&gt;
&lt;p&gt;
Prerequisites: &lt;a href="https://github.com/lukehoban/es6features"&gt;EcmaScript 2015&lt;/a&gt;, &lt;a href="https://github.com/wycats/javascript-decorators"&gt;javascript
decorators&lt;/a&gt;, &lt;a href="https://docs.angularjs.org/guide"&gt;AngularJS 1.x&lt;/a&gt;. &lt;b&gt;No
knowledge of &lt;a href="https://angular.io/"&gt;Angular 2.0&lt;/a&gt; is required.&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
Please note that decorators we have introduced, while resemble those from Angular
2, do not match them exactly.
&lt;/p&gt;
&lt;p&gt;
A sample uses &lt;a href="https://nodejs.org/"&gt;nodejs&lt;/a&gt;, &lt;a href="https://www.npmjs.com/"&gt;npm&lt;/a&gt; and &lt;a href="http://gulpjs.com/"&gt;gulp&lt;/a&gt; as
a building pipeline. In addition we have added &lt;a href="https://www.visualstudio.com/en-us/features/node-js-vs.aspx"&gt;Visual
Studio Nodejs project&lt;/a&gt;, and &lt;a href="https://maven.apache.org/"&gt;maven project&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Build pipeline uses &lt;a href="http://babeljs.io/"&gt;Babel&lt;/a&gt; with ES 2015 and decorator
plugins to transpile sources into javascript that today's browsers do support. Babel
can be replaced or augmented with Typescript compiler to support Microsoft's javascript
extensions. Sources are combinded and optionally minified into one or more javascript
bundles. In addition html template files are transformed into javascript modules that
export a content of html body as a string literals. In general all sources are in
src folder and the build's output is assembled in the dist folder. Details of build
process are in &lt;a href="gulpfile.js"&gt;gulpfile.js&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
So, let's introduce an API we have defined in &lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/angular-decorators.js"&gt;angular-decorators.js&lt;/a&gt; module:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Class decorators: 
&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Component(name, options?)&lt;/code&gt; - a decorator to register angular component.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Controller(name)&lt;/code&gt; - a decorator to register angular controller.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Directive(name, options?)&lt;/code&gt; - a decorator to register angular directive.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Injectable(name)&lt;/code&gt; - a decorator to register angular service.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Module(name, ...require)&lt;/code&gt; - a decorator to declare an angular module;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Pipe(name, pure?)&lt;/code&gt; - a decorator to register angular filter.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Component's and Directive's options is the same object passed into &lt;code&gt;Module.component(),
Module.directive()&lt;/code&gt; calls with difference that no &lt;code&gt;options.bindings&lt;/code&gt;, &lt;code&gt;options.scope&lt;/code&gt;, &lt;code&gt;options.require&lt;/code&gt; is
specified. Instead &lt;code&gt;@Attribute(), @Input(), @Output(), @TwoWay(), @Collection(),
@Optional()&lt;/code&gt; are used to describe &lt;code&gt;options.bindings&lt;/code&gt;, and &lt;code&gt;@Host(),
Self(), SkipSelf(), @Optional()&lt;/code&gt; are used to describe &lt;code&gt;options.require&lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
Every decorated class can use &lt;code&gt;@Inject()&lt;/code&gt; member decorator to inject a
service.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Member decorators: 
&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Attribute(name?)&lt;/code&gt; - a decorator that binds attribute to the property.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;BindThis()&lt;/code&gt; - a decorator that binds "&lt;code&gt;this&lt;/code&gt;" of the function
to the class instance.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Collection()&lt;/code&gt; - a decorator that binds a collection property to an expression
in attribute in two directions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Host(name?)&lt;/code&gt; - a decorator that binds a property to a host controller
of a directive found on the element or its ancestors.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HostListener(name?)&lt;/code&gt; - a decorator that binds method to a host event.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Inject(name?)&lt;/code&gt; - an injection member decorator.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Input(name?)&lt;/code&gt; - a decorator that binds a property to an expression in
attribute.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Optional()&lt;/code&gt; - a decorator that optionally binds a property.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Output(name?)&lt;/code&gt; - a decorator that provides a way to execute an expression
in the context of the parent scope.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Self(name?)&lt;/code&gt; - a decorator that binds a property to a host controller
of a directive found on the element.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SkipSelf(name?)&lt;/code&gt; - a decorator that binds a property to a host controller
of a directive found on the ancestors of the element.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TwoWay()&lt;/code&gt; - a decorator that binds a property to an expression in attribute
in two directions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
If optional name is omitted in the member decorator then property name is used as
a name parameter. &lt;code&gt;@Host(), @Self(), @SkipSelf()&lt;/code&gt; accept class decorated
with &lt;code&gt;@Component()&lt;/code&gt; or &lt;code&gt;@Directive()&lt;/code&gt; as a name parameter. 
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;@Inject()&lt;/code&gt; accepts class decorated with &lt;code&gt;@Injectable()&lt;/code&gt; or &lt;code&gt;@Pipe()&lt;/code&gt; as
a name parameter.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Other: 
&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;modules(...require)&lt;/code&gt; - converts an array of modules, possibly referred
by module classes, to an array of module names.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Now we can start with samples. Please note that we used samples scattered here and
there on the Anuglar site.
&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;@Component(), @SkipSelf(), @Attribute()&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;
&lt;br /&gt;
In the &lt;a href="https://docs.angularjs.org/guide/component"&gt;Angular's component development
guide&lt;/a&gt; there is a sample &lt;code&gt;myTabs&lt;/code&gt; and &lt;code&gt;myPane&lt;/code&gt; components.&lt;br /&gt;
Here its rewritten form &lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/components/myTabs.js"&gt;components/myTabs.js&lt;/a&gt;: &lt;pre style="white-space: pre-wrap"&gt;import { Component } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/angular-decorators.js"&gt;../angular-decorators&lt;/a&gt;";
// Import decorators import template from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/templates/my-tabs.html"&gt;../templates/my-tabs.html&lt;/a&gt;";
// Import template for my-tabs component @Component("myTabs", { template, transclude:
true }) // Decorate class as a component export class MyTabs // Controller class for
the component { panes = []; // List of active panes select(pane) // Selects an active
pane { this.panes.forEach(function(pane) { pane.selected = false; }); pane.selected
= true; } addPane(pane) // Adds a new pane { if (this.panes.length === 0) { this.select(pane);
} this.panes.push(pane); } }&lt;/pre&gt;
&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/components/myTabs.js"&gt;components/myPane.js&lt;/a&gt;: &lt;pre style="white-space: pre-wrap"&gt;import { Component, Attribute, SkipSelf } "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/angular-decorators.js"&gt;../angular-decorators&lt;/a&gt;";
// Import decorators import { MyTabs } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/components/myTabs.js"&gt;./myTabs&lt;/a&gt;";
// Import container&amp;#39;s directive. import template from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/templates/my-pane.html"&gt;../templates/my-pane.html&lt;/a&gt;";
// Import template. @Component("myPane", { template, transclude: true }) // Decorate
class as a component export class MyPane // Controller class for the component { @SkipSelf(MyTabs)
tabsCtrl; //Inject ancestor MyTabs controller. @Attribute() title; // Attribute &amp;quot;@&amp;quot;
binding. $onInit() // Angular&amp;#39;s $onInit life-cycle hook. { this.tabsCtrl.addPane(this);
console.log(this); }; } &lt;/pre&gt;
&lt;/dd&gt;
&lt;dt&gt;@Component(), @Input(), @Output()&lt;/dt&gt; 
&lt;dd&gt;
In the &lt;a href="https://docs.angularjs.org/guide/component"&gt;Angular's component development
guide&lt;/a&gt; there is a sample &lt;code&gt;myTabs&lt;/code&gt; component.&lt;br /&gt;
Here its rewritten form &lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/components/heroDetail.js"&gt;components/heroDetail.js&lt;/a&gt;:&lt;br /&gt;
&lt;pre style="white-space: pre-wrap"&gt;import { Component, Input, Output } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/angular-decorators.js"&gt;../angular-decorators&lt;/a&gt;";
import template from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/templates/heroDetail.html"&gt;../templates/heroDetail.html&lt;/a&gt;";
@Component("heroDetail", { template }) // Decorate class as a component export class
HeroDetail // Controller class for the component { @Input() hero; // One way binding
&amp;quot;&amp;lt;&amp;quot; @Output() onDelete; // Bind expression in the context of the parent
scope &amp;quot;&amp;amp;&amp;quot; @Output() onUpdate; // Bind expression in the context of the
parent scope &amp;quot;&amp;amp;&amp;quot; delete() { this.onDelete({ hero: this.hero }); }; update(prop,
value) { this.onUpdate({ hero: this.hero, prop, value }); }; }&lt;/pre&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;@Directive(), @Inject(), @Input(), @BindThis()&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;
&lt;div&gt;
&lt;br /&gt;
In the &lt;a href="https://docs.angularjs.org/guide/directive"&gt;Angular's directive development
guide&lt;/a&gt; there is a sample &lt;code&gt;myCurrentTime&lt;/code&gt; directive.&lt;br /&gt;
Here its rewritten form &lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/directives/myCurrentTime.js"&gt;directives/myCurrentTime.js&lt;/a&gt;: 
&lt;/div&gt;
&lt;pre style="white-space: pre-wrap"&gt;import { Directive, Inject, Input, BindThis } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/angular-decorators.js"&gt;../angular-decorators&lt;/a&gt;";
// Import decorators @Directive("myCurrentTime") // Decorate MyCurrentTime class as
a directive export class MyCurrentTime // Controller class for the directive { @Inject()
$interval; // &amp;quot;$interval&amp;quot; service is injected into $interval property @Inject()
dateFilter; // &amp;quot;date&amp;quot; filter service is injected into dateFilter property
@Inject() $element; // &amp;quot;$element&amp;quot; instance is injected into $element property.
@Input() myCurrentTime; // Input one way &amp;quot;&amp;lt;&amp;quot; property. timeoutId; //
updateTime is adapted as following in the constructor: // this.updateTime = this.updateTime.bind(this);
@BindThis() updateTime() { this.$element.text(this.dateFilter(new Date(), this.myCurrentTime));
} $onInit() // Angular&amp;#39;s $onInit life-cycle hook. { this.timeoutId = this.$interval(this.updateTime,
1000); } $onDestroy() // Angular&amp;#39;s $onDestroys life-cycle hook. { this.$interval.cancel(this.timeoutId);
} $onChanges(changes) // Angular&amp;#39;s $onChanges life-cycle hook. { this.updateTime();
} } &lt;/pre&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;@Directive(), @Inject(), @HostListener(), @BindThis()&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;
&lt;br /&gt;
In the &lt;a href="https://docs.angularjs.org/guide/directive"&gt;Angular's directive development
guide&lt;/a&gt; there is a sample &lt;code&gt;myDraggable&lt;/code&gt; directive.&lt;br /&gt;
Here its rewritten form. &lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/directives/myDraggable.js"&gt;directives/myDraggable.js&lt;/a&gt;: &lt;pre style="white-space: pre-wrap"&gt;import { Directive, Inject, HostListener, BindThis } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/angular-decorators.js"&gt;../angular-decorators&lt;/a&gt;";
// Import decorators @Directive("myDraggable") // Decorate class as a directive export
class MyDraggable // Controller class for the directive { @Inject() $document; //
&amp;quot;$document&amp;quot; instance is injected into $document property. @Inject() $element;//
&amp;quot;$element&amp;quot; instance is injected into $element property. startX = 0; startY
= 0; x = 0; y = 0; // Listen mousedown event over $element. @HostListener() mousedown(event)
{ // Prevent default dragging of selected content event.preventDefault(); this.startX
= event.pageX - this.x; this.startY = event.pageY - this.y; this.$document.on('mousemove',
this.mousemove); this.$document.on('mouseup', this.mouseup); } @BindThis() mousemove(event)
// bind mousemove() function to &amp;quot;this&amp;quot; instance. { this.y = event.pageY
- this.startY; this.x = event.pageX - this.startX; this.$element.css({ top: this.y
+ 'px', left: this.x + 'px' }); } @BindThis() mouseup() // bind mouseup() function
to &amp;quot;this&amp;quot; instance. { this.$document.off('mousemove', this.mousemove); this.$document.off('mouseup',
this.mouseup); } $onInit() // Angular&amp;#39;s $onInit life-cycle hook. { this.$element.css(
{ position: 'relative', border: '1px solid red', backgroundColor: 'lightgrey', cursor:
'pointer' }); } }&lt;/pre&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;@Injectable(), @Inject()&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;
&lt;br /&gt;
In the &lt;a href="https://docs.angularjs.org/guide/providers"&gt;Angular's providers development
guide&lt;/a&gt; there is a sample &lt;code&gt;notifier&lt;/code&gt; service.&lt;br /&gt;
Here its rewritten form. &lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/services/notify.js"&gt;services/notify.js&lt;/a&gt;: &lt;pre style="white-space: pre-wrap"&gt;import { Inject, Injectable } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/angular-decorators.js"&gt;../angular-decorators&lt;/a&gt;";
// Import decorators @Injectable("notifier") // Decorate class as a service export
class NotifierService { @Inject() $window; // Inject &amp;quot;$window&amp;quot; instance
into the property msgs = []; notify(msg) { this.msgs.push(msg); if (this.msgs.length
=== 3) { this.$window.alert(this.msgs.join('\n')); this.msgs = []; } } }&lt;/pre&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;@Pipe()&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;
&lt;br /&gt;
In the &lt;a href="https://docs.angularjs.org/guide/filter"&gt;Angular's filters development
guide&lt;/a&gt; there is a sample &lt;code&gt;reverse&lt;/code&gt; custom filter.&lt;br /&gt;
Here its rewritten form. &lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/filters/reverse.js"&gt;filters/reverse.js&lt;/a&gt;: &lt;pre style="white-space: pre-wrap"&gt;import { Pipe } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/angular-decorators.js"&gt;../angular-decorators&lt;/a&gt;&lt; input.length; i++)
    {
      out = input.charAt(i) + out;
    }
    // conditional based on optional argument
    if (uppercase)
    {
      out = out.toUpperCase();
    }
    return out;
  }
}&lt;/pre&gt;";
// Import decorators @Pipe("reverse") // Decorate class as a filter export class ReverseFilter
{ transform(input, uppercase) // filter function. { input = input || ''; var out =
''; for(var i = 0; i 
&lt;/dd&gt;
&lt;dt&gt;Module(), modules(), angular.bootstrap()&lt;/dt&gt; 
&lt;dd&gt;
Here are an examples of a class representing angular module, and manual angular bootstrap: &lt;pre style="white-space: pre-wrap"&gt;import { angular, modules, Module } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/angular-decorators.js"&gt;../angular-decorators&lt;/a&gt;";
// Import decorators import { MyController } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/controllers/myController.js"&gt;./controllers/myController&lt;/a&gt;";
// Import components. import { HeroList } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/components/heroList.js"&gt;./components/heroList&lt;/a&gt;";
import { HeroDetail } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/components/heroDetail.js"&gt;./components/heroDetail&lt;/a&gt;";
import { EditableField } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/components/editableField.js"&gt;./components/editableField&lt;/a&gt;";
import { NotifierService } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/services/notify.js"&gt;./services/notify&lt;/a&gt;";
import { MyTabs } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/components/myTabs.js"&gt;./components/myTabs&lt;/a&gt;";
import { MyPane } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/components/myPane.js"&gt;./components/myPane&lt;/a&gt;";
import { ReverseFilter } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/filters/reverse.js"&gt;./filters/reverse&lt;/a&gt;";
import { MyCurrentTime } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/directives/myCurrentTime.js"&gt;./directives/myCurrentTime&lt;/a&gt;";
import { MyDraggable } from "&lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/directives/myDraggable.js"&gt;./directives/myDraggable&lt;/a&gt;";
@Module( // Decorator to register angular module, and refer to other modules or module
components. "my-app", [ MyController, NotifierService, HeroList, HeroDetail, EditableField,
MyTabs, MyPane, ReverseFilter, MyCurrentTime, MyDraggable ]) class MyApp { } // Manual
bootstrap, with modules() converting module classes into an array of module names.
angular.bootstrap(document, modules(MyApp));&lt;/pre&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;
Please see &lt;a href="https://github.com/nesterovsky-bros/angular1-es6-with-decorators/tree/master/src/angular-decorators.js"&gt;angular-decorators.js&lt;/a&gt; to
get detailed help on decorators.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=2c8f3cc7-725b-4c89-abc4-dc785e14058e" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,2c8f3cc7-725b-4c89-abc4-dc785e14058e.aspx</comments>
      <category>.NET</category>
      <category>AngularJS</category>
      <category>Announce</category>
      <category>Java</category>
      <category>javascript</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=362e155c-8943-4212-86af-7eef166208ab</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,362e155c-8943-4212-86af-7eef166208ab.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,362e155c-8943-4212-86af-7eef166208ab.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=362e155c-8943-4212-86af-7eef166208ab</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It's very old theme...
</p>
        <p>
Many years ago we have defined a .NET wrapper around Windows Uniscribe API.
</p>
        <p>
Uniscribe API is used to render bidirectional languages like Hebrew, so it's important
mainly here in Israel.
</p>
        <p>
Once in a while we get request from people to give that API, so we published it on
GitHub at <a href="https://github.com/nesterovsky-bros/BidiVisualConverter">https://github.com/nesterovsky-bros/BidiVisualConverter</a>.
</p>
        <p>
You're welcome to use it!
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=362e155c-8943-4212-86af-7eef166208ab" />
      </body>
      <title>BidiVisualConverter</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,362e155c-8943-4212-86af-7eef166208ab.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2016/01/14/BidiVisualConverter.aspx</link>
      <pubDate>Thu, 14 Jan 2016 14:19:54 GMT</pubDate>
      <description>&lt;p&gt;
It's very old theme...
&lt;/p&gt;
&lt;p&gt;
Many years ago we have defined a .NET wrapper around Windows Uniscribe API.
&lt;/p&gt;
&lt;p&gt;
Uniscribe API is used to render bidirectional languages like Hebrew, so it's important
mainly here in Israel.
&lt;/p&gt;
&lt;p&gt;
Once in a while we get request from people to give that API, so we published it on
GitHub at &lt;a href="https://github.com/nesterovsky-bros/BidiVisualConverter"&gt;https://github.com/nesterovsky-bros/BidiVisualConverter&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
You're welcome to use it!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=362e155c-8943-4212-86af-7eef166208ab" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,362e155c-8943-4212-86af-7eef166208ab.aspx</comments>
      <category>.NET</category>
      <category>Announce</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=fa5d6281-2d3e-430a-8583-40c6b30b9536</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,fa5d6281-2d3e-430a-8583-40c6b30b9536.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,fa5d6281-2d3e-430a-8583-40c6b30b9536.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=fa5d6281-2d3e-430a-8583-40c6b30b9536</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It's time to align csharpxom to the latest version of C#. The article <a href="https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C#-6">New
Language Features in C# 6</a> sums up what's being added.
</p>
        <p>
Sources can be found at <a href="https://github.com/nesterovsky-bros/languages-xom">nesterovsky-bros/languages-xom</a>,
and C# model is at <a href="https://github.com/nesterovsky-bros/languages-xom/tree/master/csharpxom">csharp
folder</a>.
</p>
        <p>
In general we feel hostile to any new features until they prove they bring an added
value. So, here our list of new features from most to least useless:
</p>
        <ol>
          <li>
            <p>
String interpolation
</p>
            <p>
              <code>var s = $"{p.Name} is {p.Age} year{{s}} old";</code>
            </p>
            <p>
This is useless, as it does not account resource localization.
</p>
          </li>
          <li>
            <p>
Null-conditional operators
</p>
            <p>
              <code>int? first = customers?[0].Orders?.Count();</code>
            </p>
            <p>
They claim to reduce cluttering from null checks, but in our opinion it looks opposite.
It's better to get NullReferenceException if arguments are wrong.
</p>
          </li>
          <li>
            <p>
Exception filters
</p>
            <p>
              <code>private static bool Log(Exception e) { /* log it */ ; return false; }<br />
…<br />
try { … } catch (Exception e) when (Log(e)) {}</code>
            </p>
            <p>
              <i>"It is also a common and accepted form of “abuse” to use exception filters
for side effects; e.g. logging."</i>
            </p>
            <p>
Design a feature for abuse just does not tastes good.
</p>
          </li>
          <li>
            <p>
Expression-bodied function and property members.
</p>
            <p>
              <code>public Point Move(int dx, int dy) =&gt; new Point(x + dx, y + dy);<br />
public string Name =&gt; First + " " + Last;</code>
            </p>
            <p>
Not sure it's that usefull.
</p>
          </li>
        </ol>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=fa5d6281-2d3e-430a-8583-40c6b30b9536" />
      </body>
      <title>Support C# 6.0 in languages xom</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,fa5d6281-2d3e-430a-8583-40c6b30b9536.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2015/08/24/SupportC60InLanguagesXom.aspx</link>
      <pubDate>Mon, 24 Aug 2015 10:52:07 GMT</pubDate>
      <description>  &lt;p&gt;
It&amp;#39;s time to align csharpxom to the latest version of C#. The article &lt;a href="https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C#-6"&gt;New
Language Features in C# 6&lt;/a&gt; sums up what&amp;#39;s being added.
&lt;/p&gt;
&lt;p&gt;
Sources can be found at &lt;a href="https://github.com/nesterovsky-bros/languages-xom"&gt;nesterovsky-bros/languages-xom&lt;/a&gt;,
and C# model is at &lt;a href="https://github.com/nesterovsky-bros/languages-xom/tree/master/csharpxom"&gt;csharp
folder&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
In general we feel hostile to any new features until they prove they bring an added
value. So, here our list of new features from most to least useless:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;
String interpolation
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;var s = $"{p.Name} is {p.Age} year{{s}} old";&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
This is useless, as it does not account resource localization.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
Null-conditional operators
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;int? first = customers?[0].Orders?.Count();&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
They claim to reduce cluttering from null checks, but in our opinion it looks opposite.
It's better to get NullReferenceException if arguments are wrong.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
Exception filters
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;private static bool Log(Exception e) { /* log it */ ; return false; }&lt;br /&gt;
…&lt;br /&gt;
try { … } catch (Exception e) when (Log(e)) {}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;i&gt;&amp;quot;It is also a common and accepted form of “abuse” to use exception filters
for side effects; e.g. logging.&amp;quot;&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
Design a feature for abuse just does not tastes good.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
Expression-bodied function and property members.
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;public Point Move(int dx, int dy) =&gt; new Point(x + dx, y + dy);&lt;br /&gt;
public string Name =&gt; First + " " + Last;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Not sure it&amp;#39;s that usefull.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=fa5d6281-2d3e-430a-8583-40c6b30b9536" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,fa5d6281-2d3e-430a-8583-40c6b30b9536.aspx</comments>
      <category>.NET</category>
      <category>Announce</category>
      <category>Java</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=9ab00999-60e6-4106-ad73-9e5359ee93c5</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9ab00999-60e6-4106-ad73-9e5359ee93c5.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,9ab00999-60e6-4106-ad73-9e5359ee93c5.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=9ab00999-60e6-4106-ad73-9e5359ee93c5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Though <a href="https://msdn.microsoft.com/en-us/library/e80y5yhx(v=vs.110).aspx">ADO.NET</a> and
other ORM framworks like <a href="https://msdn.microsoft.com/en-us/data/ef.aspx">EntityFramework</a> and <a href="https://github.com/StackExchange/dapper-dot-net">Dapper</a> support
async pattern, you should remember that database drivers (at least all we know about)
do not support concurrent db commands running against a single connection.
</p>
        <p>
To see what we mean consider a bug we have recently identified. Consider a code:
</p>
        <blockquote>
          <pre>await Task.WhenAll(
  newImages.
    Select(
      async image =&gt;
      {
        // Load data from url.
        image.Content = await HttpUtils.ReadData(image.Url);

        // Insert image into the database.
        image.ImageID = await context.InsertImage(image);
      }));
</pre>
        </blockquote>
        <p>
The code runs multiple tasks to read images, and to write them into a database. 
</p>
        <p>
Framework decides to run all these tasks in parallel. <code>HttpUtils.ReadData()</code> has
no problem with parallel execution, while <code>context.InsertImage()</code> does
not run well in parallel, and is a subject of race conditions.
</p>
        <p>
To workaround the problem we had to use async variant of a critical section. So the
fixed code looks like this:
</p>
        <blockquote>
          <pre>
            <b>using(var semaphore = new SemaphoreSlim(1))</b> { await Task.WhenAll(
newImages. Select( async image =&gt; { // Load data from url. image.Content = await HttpUtils.ReadData(image.Url); <b>await
semaphore.WaitAsync();</b> try { // Insert image into the database. image.ImageID
= await context.InsertImage(image); } finally { <b>semaphore.Release();</b> } }));
} </pre>
        </blockquote>
        <p>
So, in the async world we still should care about race conditions.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9ab00999-60e6-4106-ad73-9e5359ee93c5" />
      </body>
      <title>Database async trap</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9ab00999-60e6-4106-ad73-9e5359ee93c5.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2015/07/27/DatabaseAsyncTrap.aspx</link>
      <pubDate>Mon, 27 Jul 2015 06:44:45 GMT</pubDate>
      <description>  &lt;p&gt;
Though &lt;a href="https://msdn.microsoft.com/en-us/library/e80y5yhx(v=vs.110).aspx"&gt;ADO.NET&lt;/a&gt; and
other ORM framworks like &lt;a href="https://msdn.microsoft.com/en-us/data/ef.aspx"&gt;EntityFramework&lt;/a&gt; and &lt;a href="https://github.com/StackExchange/dapper-dot-net"&gt;Dapper&lt;/a&gt; support
async pattern, you should remember that database drivers (at least all we know about)
do not support concurrent db commands running against a single connection.
&lt;/p&gt;
&lt;p&gt;
To see what we mean consider a bug we have recently identified. Consider a code:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;await Task.WhenAll(
  newImages.
    Select(
      async image =&gt;
      {
        // Load data from url.
        image.Content = await HttpUtils.ReadData(image.Url);

        // Insert image into the database.
        image.ImageID = await context.InsertImage(image);
      }));
&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
The code runs multiple tasks to read images, and to write them into a database. 
&lt;/p&gt;
&lt;p&gt;
Framework decides to run all these tasks in parallel. &lt;code&gt;HttpUtils.ReadData()&lt;/code&gt; has
no problem with parallel execution, while &lt;code&gt;context.InsertImage()&lt;/code&gt; does
not run well in parallel, and is a subject of race conditions.
&lt;/p&gt;
&lt;p&gt;
To workaround the problem we had to use async variant of a critical section. So the
fixed code looks like this:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;b&gt;using(var semaphore = new SemaphoreSlim(1))&lt;/b&gt; { await Task.WhenAll(
newImages. Select( async image =&gt; { // Load data from url. image.Content = await HttpUtils.ReadData(image.Url); &lt;b&gt;await
semaphore.WaitAsync();&lt;/b&gt; try { // Insert image into the database. image.ImageID
= await context.InsertImage(image); } finally { &lt;b&gt;semaphore.Release();&lt;/b&gt; } }));
} &lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
So, in the async world we still should care about race conditions.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9ab00999-60e6-4106-ad73-9e5359ee93c5" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,9ab00999-60e6-4106-ad73-9e5359ee93c5.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=31ddd536-ff6e-4add-87b1-2b791238b528</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,31ddd536-ff6e-4add-87b1-2b791238b528.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=31ddd536-ff6e-4add-87b1-2b791238b528</wfw:commentRss>
      <title>Audio conversion</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,31ddd536-ff6e-4add-87b1-2b791238b528.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2015/05/31/AudioConversion.aspx</link>
      <pubDate>Sun, 31 May 2015 10:32:21 GMT</pubDate>
      <description>  &lt;p style="direction: ltr"&gt;
In one of our last projects we were dealing with audio: capture audio in browser,
store it on server and then return it by a request and replay in browser.
&lt;/p&gt;
&lt;p class="auto-style1"&gt;
Though an audio capturing is by itself rather interesting and challenging task, it&amp;#39;s
addressed by HTML5, so for example take a look at &lt;a href="http://www.html5rocks.com/en/tutorials/getusermedia/intro/" target="_blank"&gt;this
article&lt;/a&gt;. Here we share our findings about other problem, namely an audio conversion.
&lt;/p&gt;
&lt;p&gt;
You might thought that if you have already captured an audio in browser then you will
be able to play back it. Thus no additional audio conversion is required. 
&lt;/p&gt;
&lt;p&gt;
In practice we are limited by support of various audio formats in browsers. Browsers
can capture audio in WAV format, but this format is rather heavy for storing and streaming
back. Moreover, not all browsers support this format for playback. See &lt;a href="http://en.wikipedia.org/wiki/HTML5_Audio#Supported_audio_coding_formats" target="_blank"&gt;wikipedia&lt;/a&gt; for
details. There are only two audio formats that more or less widely supported by mainstream
browsers: MP3 and AAC. So, you have either convert WAV to MP3, or to AAC.
&lt;/p&gt;
&lt;p&gt;
The obvious choice is to select WAV to MP3 conversion, the benefit that there are
many libraries and APIs for such conversion. But in this case you risk falling into
a trap with &lt;a href="http://mp3licensing.com/royalty/software.html" target="_blank"&gt;MP3
licensing&lt;/a&gt;, especially if you deal with &lt;a href="http://mp3licensing.com/royalty/games.html" target="_blank"&gt;iteractive
software products&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
Eventually, you will come to the only possible solution (at least for moment of writting)
- conversion WAV to AAC.
&lt;/p&gt;
&lt;p&gt;
The native solution is to use &lt;a href="http://naudio.codeplex.com/" target="_blank"&gt;NAudio
library&lt;/a&gt;, which behind the scene uses &lt;a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms703138(v=vs.85).aspx" target="_blank"&gt;Media
Foundation Transforms&lt;/a&gt;. You&amp;#39;ll shortly get a working example. Actually the
core of solution will contain few lines only:
&lt;/p&gt;
&lt;pre style="padding-left: 1em"&gt;var source = Path.Combine(root, "audio.wav");
var target = Path.Combine(root, "audio.m4a");

using(var reader = new NAudio.Wave.WaveFileReader(source))
{
  MediaFoundationEncoder.EncodeToAac(reader, target);
}&lt;/pre&gt;
&lt;p&gt;
Everything is great. You&amp;#39;ll deploy your code on server (by the way &lt;a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd742785(v=vs.85).aspx"&gt;server
must be Windows Server 2008R2 or higher&lt;/a&gt;) and at this point you may find that your
code fails. The problem is that Media Foundation API is not preinstalled as a part
of Windows Server installation, and must be installed separately. If you own this
server then everything is all right, but in case you use a public web hosting server
then you won&amp;#39;t have ability to install Media Foundation API and your application
will never work properly. That&amp;#39;s what happened to us...
&lt;/p&gt;
&lt;p&gt;
After some research we came to conclusion that another possible solution is a wrapper
around an open source video/audio converter - &lt;a href="https://ffmpeg.org/ffmpeg.html#Description" target="_blank"&gt;FFPEG&lt;/a&gt;.
There were two issues with this solution:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
how to execute ffmpeg.exe on server asynchronously;&lt;/li&gt;
&lt;li&gt;
how to limit maximum parallel requests to conversion service.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
All these issues were successfully resolved in our prototype conversion service that
you may see &lt;a href="http://www.nesterovsky-bros.com/AudioConverter/"&gt;here&lt;/a&gt;, with
source published on &lt;a href="https://github.com/nesterovsky-bros/FFMpegWrapper"&gt;github&lt;/a&gt;.
The solution is Web API based REST service with simple client that uploads audio files
using AJAX requests to server and plays it back. As a bonus this solution allows us
perform not only WAV to AAC conversions, but from many others format to AAC without
additional efforts.
&lt;/p&gt;
&lt;p&gt;
Let&amp;#39;s take a close look at crucial details of this solution. The core is FFMpegWrapper
class that allows to run ffmpeg.exe asynchronously:
&lt;/p&gt;
&lt;pre style="padding-left: 1em"&gt;/// &amp;lt;summary&gt;
/// A ffmpeg.exe open source utility wrapper.
/// &amp;lt;/summary&gt;
public class FFMpegWrapper
{
  /// &amp;lt;summary&gt;
  /// Creates a wrapper for ffmpeg utility.
  /// &amp;lt;/summary&gt;
  /// &amp;lt;param name="ffmpegexe"&gt;a real path to ffmpeg.exe&amp;lt;/param&gt;
  public FFMpegWrapper(string ffmpegexe)
  {
    if (!string.IsNullOrEmpty(ffmpegexe) &amp;&amp; File.Exists(ffmpegexe))
    {
      this.ffmpegexe = ffmpegexe;
    }
  }

  /// &amp;lt;summary&gt;
  /// Runs ffmpeg asynchronously.
  /// &amp;lt;/summary&gt;
  /// &amp;lt;param name="args"&gt;determines command line arguments for ffmpeg.exe&amp;lt;/param&gt;
  /// &amp;lt;returns&gt;
  /// asynchronous result with ProcessResults instance that contains 
  /// stdout, stderr and process exit code.
  /// &amp;lt;/returns&gt;
  public Task&amp;lt;ProcessResults&gt; Run(string args)
  {
    if (string.IsNullOrEmpty(ffmpegexe))
    {
      throw new InvalidOperationException("Cannot find FFMPEG.exe");
    }

    //create a process info object so we can run our app
    var info = new ProcessStartInfo 
    { 
      FileName = ffmpegexe,
      Arguments = args,
      CreateNoWindow = true
    };

    return ProcessEx.RunAsync(info);
  }

  private string ffmpegexe;
}&lt;/pre&gt;
&lt;p style="direction: ltr"&gt;
It became possible to run&amp;nbsp; a process asynchronously thanks to James Manning and
his &lt;a href="https://github.com/jamesmanning/RunProcessAsTask/tree/master/src/RunProcessAsTask" target="_blank"&gt;ProcessEx&lt;/a&gt; class.&amp;nbsp;&amp;nbsp;
&lt;/p&gt;
&lt;p style="direction: ltr"&gt;
Another useful part is a semaphore declaration in Global.asax.cs:
&lt;/p&gt;
&lt;pre&gt;   public class WebApiApplication : HttpApplication
  {
    protected void Application_Start()
    {
      GlobalConfiguration.Configure(WebApiConfig.Register);
    }

    /// &amp;lt;summary&gt;
    /// Gets application level semaphore that controls number of running 
    /// in parallel FFMPEG utilities.
    /// &amp;lt;/summary&gt;
    public static SemaphoreSlim Semaphore
    {
      get { return semaphore; }
    }

    private static SemaphoreSlim semaphore;

    static WebApiApplication()
    {
      var value =
        ConfigurationManager.AppSettings["NumberOfConcurentFFMpegProcesses"];

      int intValue = 10;

      if (!string.IsNullOrEmpty(value))
      {
        try
        {
          intValue = System.Convert.ToInt32(value);
        }
        catch
        {
          // use the default value
        }
      }

      semaphore = new SemaphoreSlim(intValue, intValue);
    }
  }&lt;/pre&gt;
&lt;p&gt;
And the last piece is the entry point, which was implemented as a REST controller:
&lt;/p&gt;
&lt;pre&gt;/// &amp;lt;summary&gt;
/// A controller to convert audio.
/// &amp;lt;/summary&gt;
public class AudioConverterController : ApiController
{
  /// &amp;lt;summary&gt;
  /// Gets ffmpeg utility wrapper.
  /// &amp;lt;/summary&gt;
  public FFMpegWrapper FFMpeg
  {
    get
    {
      if (ffmpeg == null)
      {
        ffmpeg = new FFMpegWrapper(
          HttpContext.Current.Server.MapPath("~/lib/ffmpeg.exe"));
      }

      return ffmpeg;
    }
  }

  /// &amp;lt;summary&gt;
  /// Converts an audio in WAV, OGG, MP3 or other formats 
  /// to AAC format (MP4 audio).
  /// &amp;lt;/summary&gt;
  /// &amp;lt;returns&gt;A data URI as a  string.&amp;lt;/returns&gt;
  [HttpPost]
  public async Task&amp;lt;string&gt; ConvertAudio([FromBody]string audio)
  {
    if (string.IsNullOrEmpty(audio))
    {
      throw new ArgumentException(
        "Invalid audio stream (probably the input audio is too big).");
    }

    var tmp = Path.GetTempFileName();
    var root = tmp + ".dir";

    Directory.CreateDirectory(root);
    File.Delete(tmp);

    try
    {
      var start = audio.IndexOf(':');
      var end = audio.IndexOf(';');
      var mimeType = audio.Substring(start + 1, end - start - 1);
      var ext = mimeType.Substring(mimeType.IndexOf('/') + 1);
      var source = Path.Combine(root, "audio." + ext);
      var target = Path.Combine(root, "audio.m4a");

      await WriteToFileAsync(audio, source);

      switch (ext)
      {
        case "mpeg":
        case "mp3":
        case "wav":
        case "wma":
        case "ogg":
        case "3gp":
        case "amr":
        case "aif":
        case "mid":
        case "au":
        {
          await WebApiApplication.Semaphore.WaitAsync();

          var result = await FFMpeg.Run(
            string.Format(
              "-i {0} -c:a libvo_aacenc -b:a 96k {1}",
              source,
              target));

          WebApiApplication.Semaphore.Release();

          if (result.Process.ExitCode != 0)
          {
            throw new InvalidDataException(
              "Cannot convert this audio file to audio/mp4.");
          }

          break;
        }
        default:
        {
          throw new InvalidDataException(
            "Mime type: '" + mimeType + "' is not supported.");
        }
      }

      var buffer = await ReadAllBytes(target);
      var response = "data:audio/mp4;base64," + System.Convert.ToBase64String(buffer);

      return response;
    }
    finally
    {
      Directory.Delete(root, true);
    }
  }&lt;/pre&gt;
&lt;p&gt;
For those who&amp;#39;d like to read more about audio conversion, we may suggest to read &lt;a href="http://www.codeproject.com/Articles/501521/How-to-convert-between-most-audio-formats-in-NET" target="_blank"&gt;this
article&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=31ddd536-ff6e-4add-87b1-2b791238b528" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,31ddd536-ff6e-4add-87b1-2b791238b528.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=47730c42-3135-46c2-8545-42f95e345f73</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,47730c42-3135-46c2-8545-42f95e345f73.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,47730c42-3135-46c2-8545-42f95e345f73.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=47730c42-3135-46c2-8545-42f95e345f73</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A year ago we had published out attempt at creating <a href="http://www.nesterovsky-bros.com/weblog/2014/01/08/WeakTable.aspx">WeakTable
in .NET</a>. This is not too often used API but at rare case it's just irreplaceable.
</p>
        <p>
But yesterday a user <a href="http://chat.stackoverflow.com/users/298609/paya">@Paya</a> has
finally found our class to be useful, performed the code review, and identified some
bugs. See discussion at <a href="http://chat.stackoverflow.com/rooms/73496/discussion-between-vladimir-nesterovsky-and-paya">StackOverflow</a>.
</p>
        <p>
Bug fixes are available at: <a href="https://github.com/nesterovsky-bros/WeakTable/">https://github.com/nesterovsky-bros/WeakTable</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=47730c42-3135-46c2-8545-42f95e345f73" />
      </body>
      <title>WeakTable update</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,47730c42-3135-46c2-8545-42f95e345f73.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2015/03/22/WeakTableUpdate.aspx</link>
      <pubDate>Sun, 22 Mar 2015 08:05:29 GMT</pubDate>
      <description>  &lt;p&gt;
A year ago we had published out attempt at creating &lt;a href="http://www.nesterovsky-bros.com/weblog/2014/01/08/WeakTable.aspx"&gt;WeakTable
in .NET&lt;/a&gt;. This is not too often used API but at rare case it&amp;#39;s just irreplaceable.
&lt;/p&gt;
&lt;p&gt;
But yesterday a user &lt;a href="http://chat.stackoverflow.com/users/298609/paya"&gt;@Paya&lt;/a&gt; has
finally found our class to be useful, performed the code review, and identified some
bugs. See discussion at &lt;a href="http://chat.stackoverflow.com/rooms/73496/discussion-between-vladimir-nesterovsky-and-paya"&gt;StackOverflow&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Bug fixes are available at: &lt;a href="https://github.com/nesterovsky-bros/WeakTable/"&gt;https://github.com/nesterovsky-bros/WeakTable&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=47730c42-3135-46c2-8545-42f95e345f73" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,47730c42-3135-46c2-8545-42f95e345f73.aspx</comments>
      <category>.NET</category>
      <category>Announce</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=b3fe14e3-9656-4ccc-bb03-16dc4a4e7374</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b3fe14e3-9656-4ccc-bb03-16dc4a4e7374.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=b3fe14e3-9656-4ccc-bb03-16dc4a4e7374</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Earlier this year <a href="http://www.asp.net/web-api/overview/advanced/dependency-injection#author-36870">Mike
Wasson</a> has published a post: "<a href="http://www.asp.net/web-api/overview/advanced/dependency-injection">Dependency
Injection in ASP.NET Web API 2</a>" that describes Web API's approach to
the <a href="http://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection
design pattern</a>.
</p>
        <p>
In short it goes like this:
</p>
        <ul>
          <li>
Web API provides a primary integration point through <code>HttpConfiguration.DependencyResolver</code> property,
and tries to obtain many services through this resolver;</li>
          <li>
Web API suggests to use your favorite Dependecy Injection library through the integration
point. Author lists following libraries: <span><a href="http://msdn.microsoft.com/en-us/library/ff647202.aspx">Unity</a> (by
Microsoft), <a href="http://www.castleproject.org/">Castle Windsor</a>, <a href="http://www.springframework.net/">Spring.Net</a>, <a href="http://code.google.com/p/autofac/">Autofac</a>, <a href="http://www.ninject.org/">Ninject</a>,
and <a href="http://docs.structuremap.net/">StructureMap</a>.</span></li>
        </ul>
        <p>
The Unity Container (Unity) is a lightweight, extensible dependency injection container.
There are Nugets both for Unity library and for Web API integration.
</p>
        <p>
Now to the point of this post.
</p>
        <p>
Unity defines a hierarchy of injection scopes. In Web API they are usually mapped
to application and request scopes. This way a developer can inject application singletons,
create request level, or transient objects.
</p>
        <p>
Everything looks reasonable. The only problem we have found is that there is no way
you to inject Web API objects like <code>HttpConfiguration</code>, <code>HttpControllerContext</code> or
request's <code>CancellationToken</code>, as they are never registered for injection.
</p>
        <p>
To workaround this we have created a small class called <code>UnityControllerActivator</code> that
perfroms required registration:
</p>
        <blockquote>
          <code> using System;<br />
using System.Net.Http;<br />
using System.Threading;<br />
using System.Threading.Tasks;<br />
using System.Web.Http.Controllers;<br />
using System.Web.Http.Dispatcher;<br /><br />
using Microsoft.Practices.Unity;<br /><br />
/// &lt;summary&gt;<br />
/// Unity controller activator.<br />
/// &lt;/summary&gt;<br />
public class UnityControllerActivator: IHttpControllerActivator<br />
{<br />
  /// &lt;summary&gt;<br />
  /// Creates an UnityControllerActivator instance.<br />
  /// &lt;/summary&gt;<br />
  /// &lt;param name="activator"&gt;Base activator.&lt;/param&gt;<br />
  public UnityControllerActivator(IHttpControllerActivator activator)<br />
  {<br />
    if (activator == null)<br />
    {<br />
      throw new ArgumentException("activator");<br />
    }<br /><br />
    this.activator = activator;<br />
  }<br /><br />
  /// &lt;summary&gt;<br />
  /// Creates a controller wrapper.<br />
  /// &lt;/summary&gt;<br />
  /// &lt;param name="request"&gt;A http request.&lt;/param&gt;<br />
  /// &lt;param name="controllerDescriptor"&gt;Controller descriptor.&lt;/param&gt;<br />
  /// &lt;param name="controllerType"&gt;Controller type.&lt;/param&gt;<br />
  /// &lt;returns&gt;A controller wrapper.&lt;/returns&gt;<br />
  public IHttpController Create(<br />
    HttpRequestMessage request, 
<br />
    HttpControllerDescriptor controllerDescriptor, 
<br />
    Type controllerType)<br />
  {<br />
    return new Controller 
<br />
    { 
<br />
      activator = activator,<br />
      controllerType = controllerType 
<br />
    };<br />
  }<br /><br />
  /// &lt;summary&gt;<br />
  /// Base controller activator.<br />
  /// &lt;/summary&gt;<br />
  private readonly IHttpControllerActivator activator;<br /><br />
  /// &lt;summary&gt;<br />
  /// A controller wrapper.<br />
  /// &lt;/summary&gt;<br />
  private class Controller: IHttpController, IDisposable<br />
  {<br />
    /// &lt;summary&gt;<br />
    /// Base controller activator.<br />
    /// &lt;/summary&gt;<br />
    public IHttpControllerActivator activator;<br /><br />
    /// &lt;summary&gt;<br />
    /// Controller type.<br />
    /// &lt;/summary&gt;<br />
    public Type controllerType;<br /><br />
    /// &lt;summary&gt;<br />
    /// A controller instance.<br />
    /// &lt;/summary&gt;<br />
    public IHttpController controller;<br /><br />
    /// &lt;summary&gt;<br />
    /// Disposes controller.<br />
    /// &lt;/summary&gt;<br />
    public void Dispose()<br />
    {<br />
      var disposable = controller as IDisposable;<br /><br />
      if (disposable != null)<br />
      {<br />
        disposable.Dispose();<br />
      }<br />
    }<br /><br />
    /// &lt;summary&gt;<br />
    /// Executes an action.<br />
    /// &lt;/summary&gt;<br />
    /// &lt;param name="controllerContext"&gt;Controller context.&lt;/param&gt;<br />
    /// &lt;param name="cancellationToken"&gt;Cancellation token.&lt;/param&gt;<br />
    /// &lt;returns&gt;Response message.&lt;/returns&gt;<br />
    public Task&lt;HttpResponseMessage&gt; ExecuteAsync(<br />
      HttpControllerContext controllerContext, 
<br />
      CancellationToken cancellationToken)<br />
    {<br />
      if (controller == null)<br />
      {<br />
        var request = controllerContext.Request;<br />
        <b>var container = request.GetDependencyScope().<br />
          GetService(typeof(IUnityContainer))
as IUnityContainer;<br /><br />
        if (container != null)<br />
        {<br />
          container.RegisterInstance&lt;HttpControllerContext&gt;(controllerContext);<br />
          container.RegisterInstance&lt;HttpRequestMessage&gt;(request);<br />
          container.RegisterInstance&lt;CancellationToken&gt;(cancellationToken);<br />
        }<br /></b><br />
        controller = activator.Create(<br />
          request, 
<br />
          controllerContext.ControllerDescriptor, 
<br />
          controllerType);<br />
      }<br /><br />
      controllerContext.Controller = controller;<br /><br />
      return controller.ExecuteAsync(controllerContext, cancellationToken);<br />
    }<br />
  }<br />
} </code>
        </blockquote>
        <p>
          <b>Note on how it works.</b>
        </p>
        <blockquote>
          <ul>
            <li>
              <code>IHttpControllerActivator</code> is a controller factory, which Web API uses
to create new controller instances using <code>IHttpControllerActivator.Create()</code>.
Later controller's <code>IHttpController.ExecuteAsync()</code> is called to run the
logic.</li>
            <li>
              <code>UnityControllerActivator</code> replaces original controller activator with
a wrapper that delays creation (injection) of real controller untill request objects
are registered in the scope</li>
          </ul>
        </blockquote>
        <p>
To register this class one need to update code in the <code>UnityWebApiActivator.cs</code> (file
added with nuget <code>Unity.AspNet.WebApi</code>)
</p>
        <blockquote>
          <code> public static class UnityWebApiActivator<br />
{<br />
  /// &lt;summary&gt;Integrates Unity when the application starts.&lt;summary&gt;<br />
  public static void Start() 
<br />
  {<br />
    var config = GlobalConfiguration.Configuration;<br />
    var container = UnityConfig.GetConfiguredContainer();<br /><br />
    container.RegisterInstance&lt;HttpConfiguration&gt;(config);<br />
    container.RegisterInstance&lt;IHttpControllerActivator&gt;(<br />
      new UnityControllerActivator(config.Services.GetHttpControllerActivator()));<br /><br />
    config.DependencyResolver = UnityHierarchicalDependencyResolver(container);<br />
  }<br />
  ...<br />
} </code>
        </blockquote>
        <p>
With this addition we have simplified the boring problem with passing of <code>CancellationToken</code> all
around the code, as controller (and other classes) just declared a property to inject:
</p>
        <blockquote>
          <code> public class MyController: ApiController<br />
{<br />
  [Dependency]<br />
  public CancellationToken CancellationToken { get; set; }<br /><br />
  [Dependency]<br />
  public IModelContext Model { get; set; }<br /><br />
  public async Task&lt;IEnumerable&lt;Products&gt;&gt; GetProducts(...)<br />
  {<br />
    ...<br />
  }<br /><br />
  public async Task&lt;IEnumerable&lt;Customer&gt;&gt; GetCustomer(...)<br />
  {<br />
    ...<br />
  }<br /><br />
  ...<br />
}<br /><br />
...<br /><br />
public class ModelContext: IModelContext<br />
{<br />
  [Dependency]<br />
  public CancellationToken CancellationToken { get; set; }<br />
  ...<br />
} </code>
        </blockquote>
        <p>
And finally to perform unit tests for controllers with Depenency Injection you can
use a code like this:
</p>
        <blockquote>
          <code> using System.Threading;<br />
using System.Threading.Tasks;<br />
using System.Web.Http;<br />
using System.Web.Http.Controllers;<br />
using System.Web.Http.Dependencies;<br />
using System.Net.Http;<br /><br />
using Microsoft.Practices.Unity;<br />
using Microsoft.Practices.Unity.WebApi;<br /><br />
using Microsoft.VisualStudio.TestTools.UnitTesting;<br /><br />
[TestClass]<br />
public class MyControllerTest<br />
{<br />
  [ClassInitialize]<br />
  public static void Initialize(TestContext context)<br />
  {<br />
    config = new HttpConfiguration();<br /><br />
    Register(config);<br />
  }<br /><br />
  [ClassCleanup]<br />
  public static void Cleanup()<br />
  {<br />
    config.Dispose();<br />
  }<br /><br />
  [TestMethod]<br />
  public async Task GetProducts()<br />
  {<br />
    var controller = CreateController&lt;MyController&gt;();<br /><br />
    //...<br />
  }<br /><br />
  public static T CreateController&lt;T&gt;(HttpRequestMessage request = null)<br />
    where T: ApiController<br />
  {<br />
    if (request == null)<br />
    {<br />
      request = new HttpRequestMessage();<br />
    }<br /><br />
    request.SetConfiguration(config);<br /><br />
    var controllerContext = new HttpControllerContext()<br />
    {<br />
      Configuration = config,<br />
      Request = request<br />
    };<br /><br />
    var scope = request.GetDependencyScope();<br />
    var container = scope.GetService(typeof(IUnityContainer)) 
<br />
      as IUnityContainer;<br /><br />
    if (container != null)<br />
    {<br />
      container.RegisterInstance&lt;HttpControllerContext&gt;(controllerContext);<br />
      container.RegisterInstance&lt;HttpRequestMessage&gt;(request);<br />
      container.RegisterInstance&lt;CancellationToken&gt;(CancellationToken.None);<br />
    }<br /><br />
    T controller = scope.GetService(typeof(T)) as T;<br /><br />
    controller.Configuration = config;<br />
    controller.Request = request;<br />
    controller.ControllerContext = controllerContext;<br /><br />
    return controller;<br />
  }<br /><br />
  public static void Register(HttpConfiguration config)<br />
  {<br />
    config.DependencyResolver = CreateDependencyResolver(config);<br />
  }<br /><br />
  public static IDependencyResolver CreateDependencyResolver(HttpConfiguration
config)<br />
  {<br />
    var container = new UnityContainer();<br /><br />
    container.RegisterInstance&lt;HttpConfiguration&gt;(config);<br /><br />
    // TODO: configure Unity contaiener.<br /><br />
    return new UnityHierarchicalDependencyResolver(container);<br />
  }<br /><br />
  public static HttpConfiguration config;<br />
} </code>
        </blockquote>
        <p>
P.S. To those who think Dependency Injection is an universal tool, please read the
article: <a href="http://www.tonymarston.net/php-mysql/dependency-injection-is-evil.html">Dependency
Injection is Evil</a>. <img alt=":-)" src="http://www.nesterovsky-bros.com/weblog/smilies/happy.gif" /></p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=b3fe14e3-9656-4ccc-bb03-16dc4a4e7374" />
      </body>
      <title>Dependency Injection in ASP.NET Web API 2</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b3fe14e3-9656-4ccc-bb03-16dc4a4e7374.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/12/15/DependencyInjectionInASPNETWebAPI2.aspx</link>
      <pubDate>Mon, 15 Dec 2014 20:24:49 GMT</pubDate>
      <description>  &lt;p&gt;
Earlier this year &lt;a href="http://www.asp.net/web-api/overview/advanced/dependency-injection#author-36870" &gt;Mike
Wasson&lt;/a&gt; has published a post: "&lt;a href="http://www.asp.net/web-api/overview/advanced/dependency-injection"&gt;Dependency
Injection in ASP.NET Web API 2&lt;/a&gt;&amp;quot; that describes Web API&amp;#39;s approach to
the &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;Dependency Injection
design pattern&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
In short it goes like this:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Web API provides a primary integration point through &lt;code&gt;HttpConfiguration.DependencyResolver&lt;/code&gt; property,
and tries to obtain many services through this resolver;&lt;/li&gt;
&lt;li&gt;
Web API suggests to use your favorite Dependecy Injection library through the integration
point. Author lists following libraries: &lt;span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ff647202.aspx"&gt;Unity&lt;/a&gt; (by
Microsoft), &lt;a href="http://www.castleproject.org/"&gt;Castle Windsor&lt;/a&gt;, &lt;a href="http://www.springframework.net/"&gt;Spring.Net&lt;/a&gt;, &lt;a href="http://code.google.com/p/autofac/"&gt;Autofac&lt;/a&gt;, &lt;a href="http://www.ninject.org/"&gt;Ninject&lt;/a&gt;,
and &lt;a href="http://docs.structuremap.net/"&gt;StructureMap&lt;/a&gt;.&lt;/span&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The Unity Container (Unity) is a lightweight, extensible dependency injection container.
There are Nugets both for Unity library and for Web API integration.
&lt;/p&gt;
&lt;p&gt;
Now to the point of this post.
&lt;/p&gt;
&lt;p&gt;
Unity defines a hierarchy of injection scopes. In Web API they are usually mapped
to application and request scopes. This way a developer can inject application singletons,
create request level, or transient objects.
&lt;/p&gt;
&lt;p&gt;
Everything looks reasonable. The only problem we have found is that there is no way
you to inject Web API objects like &lt;code&gt;HttpConfiguration&lt;/code&gt;, &lt;code&gt;HttpControllerContext&lt;/code&gt; or
request's &lt;code&gt;CancellationToken&lt;/code&gt;, as they are never registered for injection.
&lt;/p&gt;
&lt;p&gt;
To workaround this we have created a small class called &lt;code&gt;UnityControllerActivator&lt;/code&gt; that
perfroms required registration:
&lt;/p&gt;
&lt;blockquote&gt;&lt;code&gt; using System;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Web.Http.Controllers;&lt;br /&gt;
using System.Web.Http.Dispatcher;&lt;br /&gt;
&lt;br /&gt;
using Microsoft.Practices.Unity;&lt;br /&gt;
&lt;br /&gt;
/// &amp;lt;summary&gt;&lt;br /&gt;
/// Unity controller activator.&lt;br /&gt;
/// &amp;lt;/summary&gt;&lt;br /&gt;
public class UnityControllerActivator: IHttpControllerActivator&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp; /// Creates an UnityControllerActivator instance.&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="activator"&gt;Base activator.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp; public UnityControllerActivator(IHttpControllerActivator activator)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (activator == null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ArgumentException("activator");&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; this.activator = activator;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp; /// Creates a controller wrapper.&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="request"&gt;A http request.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="controllerDescriptor"&gt;Controller descriptor.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="controllerType"&gt;Controller type.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp; /// &amp;lt;returns&gt;A controller wrapper.&amp;lt;/returns&gt;&lt;br /&gt;
&amp;nbsp; public IHttpController Create(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpRequestMessage request, 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpControllerDescriptor controllerDescriptor, 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Type controllerType)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return new Controller 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; activator = activator,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; controllerType = controllerType 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp; /// Base controller activator.&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp; private readonly IHttpControllerActivator activator;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp; /// A controller wrapper.&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp; private class Controller: IHttpController, IDisposable&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Base controller activator.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public IHttpControllerActivator activator;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Controller type.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Type controllerType;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// A controller instance.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public IHttpController controller;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Disposes controller.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Dispose()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var disposable = controller as IDisposable;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (disposable != null)&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; disposable.Dispose();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Executes an action.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="controllerContext"&gt;Controller context.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="cancellationToken"&gt;Cancellation token.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;returns&gt;Response message.&amp;lt;/returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Task&amp;lt;HttpResponseMessage&gt; ExecuteAsync(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpControllerContext controllerContext, 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CancellationToken cancellationToken)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (controller == null)&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; var request = controllerContext.Request;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;var container = request.GetDependencyScope().&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetService(typeof(IUnityContainer))
as IUnityContainer;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (container != null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp; container.RegisterInstance&amp;lt;HttpControllerContext&gt;(controllerContext);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.RegisterInstance&amp;lt;HttpRequestMessage&gt;(request);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.RegisterInstance&amp;lt;CancellationToken&gt;(cancellationToken);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;/b&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; controller = activator.Create(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; request, 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; controllerContext.ControllerDescriptor, 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; controllerType);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; controllerContext.Controller = controller;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return controller.ExecuteAsync(controllerContext, cancellationToken);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
} &lt;/code&gt;&lt;/blockquote&gt; 
&lt;p&gt;
&lt;b&gt;Note on how it works.&lt;/b&gt;
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;IHttpControllerActivator&lt;/code&gt; is a controller factory, which Web API uses
to create new controller instances using &lt;code&gt;IHttpControllerActivator.Create()&lt;/code&gt;.
Later controller's &lt;code&gt;IHttpController.ExecuteAsync()&lt;/code&gt; is called to run the
logic.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UnityControllerActivator&lt;/code&gt; replaces original controller activator with
a wrapper that delays creation (injection) of real controller untill request objects
are registered in the scope&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
To register this class one need to update code in the &lt;code&gt;UnityWebApiActivator.cs&lt;/code&gt; (file
added with nuget &lt;code&gt;Unity.AspNet.WebApi&lt;/code&gt;)
&lt;/p&gt;
&lt;blockquote&gt; &lt;code&gt; public static class UnityWebApiActivator&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt;Integrates Unity when the application starts.&amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp; public static void Start() 
&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var config = GlobalConfiguration.Configuration;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var container = UnityConfig.GetConfiguredContainer();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; container.RegisterInstance&amp;lt;HttpConfiguration&gt;(config);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; container.RegisterInstance&amp;lt;IHttpControllerActivator&gt;(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new UnityControllerActivator(config.Services.GetHttpControllerActivator()));&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; config.DependencyResolver = UnityHierarchicalDependencyResolver(container);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; ...&lt;br /&gt;
} &lt;/code&gt; &lt;/blockquote&gt; 
&lt;p&gt;
With this addition we have simplified the boring problem with passing of &lt;code&gt;CancellationToken&lt;/code&gt; all
around the code, as controller (and other classes) just declared a property to inject:
&lt;/p&gt;
&lt;blockquote&gt; &lt;code&gt; public class MyController: ApiController&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; [Dependency]&lt;br /&gt;
&amp;nbsp; public CancellationToken CancellationToken { get; set; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; [Dependency]&lt;br /&gt;
&amp;nbsp; public IModelContext Model { get; set; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public async Task&amp;lt;IEnumerable&amp;lt;Products&gt;&gt; GetProducts(...)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public async Task&amp;lt;IEnumerable&amp;lt;Customer&gt;&gt; GetCustomer(...)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
public class ModelContext: IModelContext&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; [Dependency]&lt;br /&gt;
&amp;nbsp; public CancellationToken CancellationToken { get; set; }&lt;br /&gt;
&amp;nbsp; ...&lt;br /&gt;
} &lt;/code&gt; &lt;/blockquote&gt; 
&lt;p&gt;
And finally to perform unit tests for controllers with Depenency Injection you can
use a code like this:
&lt;/p&gt;
&lt;blockquote&gt;&lt;code&gt; using System.Threading;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Web.Http;&lt;br /&gt;
using System.Web.Http.Controllers;&lt;br /&gt;
using System.Web.Http.Dependencies;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
&lt;br /&gt;
using Microsoft.Practices.Unity;&lt;br /&gt;
using Microsoft.Practices.Unity.WebApi;&lt;br /&gt;
&lt;br /&gt;
using Microsoft.VisualStudio.TestTools.UnitTesting;&lt;br /&gt;
&lt;br /&gt;
[TestClass]&lt;br /&gt;
public class MyControllerTest&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; [ClassInitialize]&lt;br /&gt;
&amp;nbsp; public static void Initialize(TestContext context)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; config = new HttpConfiguration();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Register(config);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; [ClassCleanup]&lt;br /&gt;
&amp;nbsp; public static void Cleanup()&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; config.Dispose();&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; [TestMethod]&lt;br /&gt;
&amp;nbsp; public async Task GetProducts()&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var controller = CreateController&amp;lt;MyController&gt;();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; //...&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public static T CreateController&amp;lt;T&gt;(HttpRequestMessage request = null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; where T: ApiController&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (request == null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; request = new HttpRequestMessage();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; request.SetConfiguration(config);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var controllerContext = new HttpControllerContext()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Configuration = config,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Request = request&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var scope = request.GetDependencyScope();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var container = scope.GetService(typeof(IUnityContainer)) 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; as IUnityContainer;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (container != null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.RegisterInstance&amp;lt;HttpControllerContext&gt;(controllerContext);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.RegisterInstance&amp;lt;HttpRequestMessage&gt;(request);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.RegisterInstance&amp;lt;CancellationToken&gt;(CancellationToken.None);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; T controller = scope.GetService(typeof(T)) as T;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; controller.Configuration = config;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; controller.Request = request;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; controller.ControllerContext = controllerContext;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return controller;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public static void Register(HttpConfiguration config)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; config.DependencyResolver = CreateDependencyResolver(config);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public static IDependencyResolver CreateDependencyResolver(HttpConfiguration
config)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var container = new UnityContainer();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; container.RegisterInstance&amp;lt;HttpConfiguration&gt;(config);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // TODO: configure Unity contaiener.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return new UnityHierarchicalDependencyResolver(container);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public static HttpConfiguration config;&lt;br /&gt;
} &lt;/code&gt;&lt;/blockquote&gt; 
&lt;p&gt;
P.S. To those who think Dependency Injection is an universal tool, please read the
article: &lt;a href="http://www.tonymarston.net/php-mysql/dependency-injection-is-evil.html"&gt;Dependency
Injection is Evil&lt;/a&gt;. &lt;img alt=":-)" src="http://www.nesterovsky-bros.com/weblog/smilies/happy.gif"&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=b3fe14e3-9656-4ccc-bb03-16dc4a4e7374" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,b3fe14e3-9656-4ccc-bb03-16dc4a4e7374.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=41875b64-8d10-43bb-9893-d1c124af9f31</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,41875b64-8d10-43bb-9893-d1c124af9f31.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,41875b64-8d10-43bb-9893-d1c124af9f31.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=41875b64-8d10-43bb-9893-d1c124af9f31</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Farewell Entity Framework and hello <a href="https://github.com/StackExchange/dapper-dot-net">Dapper</a>!
</p>
        <p>
For many years we were using <a href="http://msdn.microsoft.com/en-us/data/ef.aspx">Entity
Framework</a>. It's still very popular and Microsoft's primary Object-Relational
Mapper library.
</p>
        <p>
Clearly, the decision is subjective but here are our arguments.
</p>
        <p>
We know and love SQL, and think that in its domain it occupies strong positions. What
SQL leaves out of scope is a bridge between itself and other languages. That's
where ORM should help.
</p>
        <p>
We strongly beleive that no ORM library should try to hide SQL behind the Object's
language itself. We beleive in a separation of roles in development. Database design
and Data Access Layer should be separated from client's logic. Thus, we strive,
if possible, to encapulate data access through SQL functions and stored procedures.
</p>
        <p>
Entity Framework, in contrast, tries to factor out SQL, giving a perspective of object
graph to a client. Initially, it looks promising but at the end a developer should
remember that any object query is mapped back to SQL. Without keeping this in mind
either query won't compile, or performance will be poor. 
<br /><br />
E.g. This query will probably fail to build SQL, as no Regex can be mapped to SQL:
</p>
        <p style="padding-left: 1em">
          <i>
            <code> var result = context.Content.<br />
  Where(data =&gt; Regex.IsMatch(data.Content, pattern)).<br />
  ToArray();</code>
          </i>
        </p>
        <p>
This query might be slow, if no suitble SQL index is defined:
</p>
        <p style="padding-left: 1em">
          <i>
            <code> var result = context.Content.<br />
  Where(data =&gt; data.Field == value).<br />
  ToArray();</code>
          </i>
        </p>
        <p>
Thus no EF's goal is achieved completely, SQL power is limitted, and Data Access
Layer is often fused into other client's logic.
</p>
        <p>
We think that Entity Framework is over-engineered library, which tries to be more
than ORM. Its generality often bumps into limits of SQL support in EF: SQL dialects,
types, operators, functions, and so on. One can observe that people for years appeal
to introduce support of xml, hierarchyid, geometry/geography types, full text search,
and so on. This state cannot be different, as EF will never be able and does not aim
to support all SQL features.
</p>
        <p>
EF has both design-time and runtime. Each database vendor should implement their EF
adapter for EF to play well with that database. This cooperation is not always smooth.
E.g see <a href="http://stackoverflow.com/questions/25089346/database-first-create-entity-framework-6-1-1-model-using-system-data-sqlite-1-0">Database
first create entity framework 6.1.1 model using system.data.sqlite 1.0.93</a>.
</p>
        <p>
At some point the cost of dealing with EF has became too high for us, so we started
to look into an alternatives: from plain ADO.NET to lighter ORM library.
</p>
        <p>
To our delight we have immediately found: <a href="https://github.com/StackExchange/dapper-dot-net#dapper---a-simple-object-mapper-for-net"></a><a href="https://github.com/StackExchange/dapper-dot-net">Dapper
- a simple object mapper for .NET</a>. It provides a simple extensions to <code>IDBConnection</code> interface
to deal with mapping of query parameters to object properties, and of query results
to plain types. Here are some examples:
</p>
        <p style="padding-left: 1em">
          <i>
            <code> // Get Customer<br />
var customer = connection.<br />
  Query&lt;Customer&gt;("select * from Customers where CustomerId = @id", new {
id = customerID }).<br />
  ToSingle(); 
<br /><br />
// Insert a value<br />
connection.Execute("insert into MyTable(A, B) values(@a, @b)", new { a =
2, b = 3 });</code>
          </i>
        </p>
        <p>
So, Dapper leaves you with plain SQL, which we consider as advantage.
</p>
        <p>
Except beeing minimalistic compared to EF, Dapper claims performance close to pure
hand written ADO.NET. Indeed, they build dynamic methods to populate parameters and
to create rows instances, so reflection is used during warm up period only.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=41875b64-8d10-43bb-9893-d1c124af9f31" />
      </body>
      <title>Farewell Entity Framework</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,41875b64-8d10-43bb-9893-d1c124af9f31.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/11/30/FarewellEntityFramework.aspx</link>
      <pubDate>Sun, 30 Nov 2014 12:47:46 GMT</pubDate>
      <description>  &lt;p&gt;
Farewell Entity Framework and hello &lt;a href="https://github.com/StackExchange/dapper-dot-net"&gt;Dapper&lt;/a&gt;!
&lt;/p&gt;
&lt;p&gt;
For many years we were using &lt;a href="http://msdn.microsoft.com/en-us/data/ef.aspx"&gt;Entity
Framework&lt;/a&gt;. It&amp;#39;s still very popular and Microsoft&amp;#39;s primary Object-Relational
Mapper library.
&lt;/p&gt;
&lt;p&gt;
Clearly, the decision is subjective but here are our arguments.
&lt;/p&gt;
&lt;p&gt;
We know and love SQL, and think that in its domain it occupies strong positions. What
SQL leaves out of scope is a bridge between itself and other languages. That&amp;#39;s
where ORM should help.
&lt;/p&gt;
&lt;p&gt;
We strongly beleive that no ORM library should try to hide SQL behind the Object&amp;#39;s
language itself. We beleive in a separation of roles in development. Database design
and Data Access Layer should be separated from client&amp;#39;s logic. Thus, we strive,
if possible, to encapulate data access through SQL functions and stored procedures.
&lt;/p&gt;
&lt;p&gt;
Entity Framework, in contrast, tries to factor out SQL, giving a perspective of object
graph to a client. Initially, it looks promising but at the end a developer should
remember that any object query is mapped back to SQL. Without keeping this in mind
either query won&amp;#39;t compile, or performance will be poor. 
&lt;br /&gt;
&lt;br /&gt;
E.g. This query will probably fail to build SQL, as no Regex can be mapped to SQL:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;i&gt;&lt;code&gt; var result = context.Content.&lt;br /&gt;
&amp;nbsp; Where(data =&amp;gt; Regex.IsMatch(data.Content, pattern)).&lt;br /&gt;
&amp;nbsp; ToArray();&lt;/code&gt;&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
This query might be slow, if no suitble SQL index is defined:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;i&gt;&lt;code&gt; var result = context.Content.&lt;br /&gt;
&amp;nbsp; Where(data =&amp;gt; data.Field == value).&lt;br /&gt;
&amp;nbsp; ToArray();&lt;/code&gt;&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
Thus no EF&amp;#39;s goal is achieved completely, SQL power is limitted, and Data Access
Layer is often fused into other client&amp;#39;s logic.
&lt;/p&gt;
&lt;p&gt;
We think that Entity Framework is over-engineered library, which tries to be more
than ORM. Its generality often bumps into limits of SQL support in EF: SQL dialects,
types, operators, functions, and so on. One can observe that people for years appeal
to introduce support of xml, hierarchyid, geometry/geography types, full text search,
and so on. This state cannot be different, as EF will never be able and does not aim
to support all SQL features.
&lt;/p&gt;
&lt;p&gt;
EF has both design-time and runtime. Each database vendor should implement their EF
adapter for EF to play well with that database. This cooperation is not always smooth.
E.g see &lt;a href="http://stackoverflow.com/questions/25089346/database-first-create-entity-framework-6-1-1-model-using-system-data-sqlite-1-0"&gt;Database
first create entity framework 6.1.1 model using system.data.sqlite 1.0.93&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
At some point the cost of dealing with EF has became too high for us, so we started
to look into an alternatives: from plain ADO.NET to lighter ORM library.
&lt;/p&gt;
&lt;p&gt;
To our delight we have immediately found: &lt;a href="https://github.com/StackExchange/dapper-dot-net#dapper---a-simple-object-mapper-for-net"&gt;&lt;/a&gt;&lt;a href="https://github.com/StackExchange/dapper-dot-net"&gt;Dapper
- a simple object mapper for .NET&lt;/a&gt;. It provides a simple extensions to &lt;code&gt;IDBConnection&lt;/code&gt; interface
to deal with mapping of query parameters to object properties, and of query results
to plain types. Here are some examples:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;i&gt; &lt;code&gt; // Get Customer&lt;br /&gt;
var customer = connection.&lt;br /&gt;
&amp;nbsp; Query&amp;lt;Customer&gt;("select * from Customers where CustomerId = @id", new {
id = customerID }).&lt;br /&gt;
&amp;nbsp; ToSingle(); 
&lt;br /&gt;
&lt;br /&gt;
// Insert a value&lt;br /&gt;
connection.Execute(&amp;quot;insert into MyTable(A, B) values(@a, @b)&amp;quot;, new { a =
2, b = 3 });&lt;/code&gt;&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
So, Dapper leaves you with plain SQL, which we consider as advantage.
&lt;/p&gt;
&lt;p&gt;
Except beeing minimalistic compared to EF, Dapper claims performance close to pure
hand written ADO.NET. Indeed, they build dynamic methods to populate parameters and
to create rows instances, so reflection is used during warm up period only.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=41875b64-8d10-43bb-9893-d1c124af9f31" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,41875b64-8d10-43bb-9893-d1c124af9f31.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=9ca4f9a5-1de8-4ed7-8175-6755a1bd14f6</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9ca4f9a5-1de8-4ed7-8175-6755a1bd14f6.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,9ca4f9a5-1de8-4ed7-8175-6755a1bd14f6.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=9ca4f9a5-1de8-4ed7-8175-6755a1bd14f6</wfw:commentRss>
      <title>.NET Cache API</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9ca4f9a5-1de8-4ed7-8175-6755a1bd14f6.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/07/28/NETCacheAPI.aspx</link>
      <pubDate>Mon, 28 Jul 2014 05:36:06 GMT</pubDate>
      <description>  &lt;p&gt;
Looking at &lt;a href="http://code.google.com/p/guava-libraries/wiki/CachesExplained"&gt;Guava
Cache&lt;/a&gt; we think its API is more convenient than &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx"&gt;.NET&amp;#39;s
Cache API&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
Just consider: 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;
.NET has getters, and setters of &lt;code&gt;object&lt;/code&gt;s by &lt;code&gt;string&lt;/code&gt; keys. 
&lt;br /&gt;
You should provide caching policy with each setter. 
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
Guava cache operates with typed storage of &lt;code&gt;Key&lt;/code&gt; to &lt;code&gt;Value.&lt;/code&gt;
&lt;br /&gt;
Provides a value factory and a caching policy in advance at cache construction. 
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Guava&amp;#39;s advantange is based on an idea that homogenous storage assumes a uniform
way of creation of values, and uniform caching policy. Thus a great part of logic
is factored out into a cache initialization. 
&lt;/p&gt;
&lt;p&gt;
We have decided to create a simple adapter of the &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx"&gt;MemoryCache&lt;/a&gt; to
achieve the same goal. Here is a result of such an experiment: 
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; public class Cache&amp;lt;K, V&gt; 
&lt;br /&gt;
&amp;nbsp; where V: class 
&lt;br /&gt;
{ 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// A cache builder. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; public struct Builder 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// A memory cache. If not specified then MemoryCache.Default is
used. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public MemoryCache MemoryCache; 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// An expiration value. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Alternatively CachePolicyFunc can be used. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public TimeSpan Expiration; 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Indicates whether to use sliding (true), or absolute (false) 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// expiration. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Alternatively CachePolicyFunc can be used. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public bool Sliding; 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Optional function to get caching policy. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Alternatively Expiration and Sliding property can be used. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Func&amp;lt;V, CacheItemPolicy&gt; CachePolicyFunc; 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Optional value validator. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Func&amp;lt;V, bool&gt; Validator; 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// A value factory. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Alternatively FactoryAsync can be used. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Func&amp;lt;K, V&gt; Factory; 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Async value factory. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Alternatively Factory can be used. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Func&amp;lt;K, Task&amp;lt;V&gt;&gt; FactoryAsync; 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// A key to string converter. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Func&amp;lt;K, string&gt; KeyFunc; 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Converts builder to a Cache&amp;lt;K, V&gt; instance. 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="builder"&gt;A builder to convert.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;returns&gt;A Cache&amp;lt;K, V&gt; instance.&amp;lt;/returns&gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static implicit operator Cache&amp;lt;K, V&gt;(Builder builder) 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return new Cache&amp;lt;K, V&gt;(builder); 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Creates a cache from a cache builder. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="builder"&gt;A cache builder instance.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; public Cache(Builder builder) 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((builder.Factory == null) &amp;&amp; (builder.FactoryAsync == null)) 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ArgumentException("builder.Factory"); 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (builder.MemoryCache == null) 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.MemoryCache = MemoryCache.Default; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; this.builder = builder; 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Cached value by key. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="key"&gt;A key.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;returns&gt;A cached value.&amp;lt;/returns&gt; 
&lt;br /&gt;
&amp;nbsp; public V this[K key] 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; get { return Get(key); } 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; set { Set(key, value); } 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Sets a value for a key. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="key"&gt;A key to set.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="value"&gt;A value to set.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; public void Set(K key, V value) 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SetImpl(GetKey(key), IsValid(value) ? value : null); 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Gets a value for a key. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="key"&gt;A key to get value for.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;returns&gt;A value instance.&amp;lt;/returns&gt; 
&lt;br /&gt;
&amp;nbsp; public V Get(K key) 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var keyValue = GetKey(key); 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var value = builder.MemoryCache.Get(keyValue) as V; 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!IsValid(value)) 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = CreateValue(key); 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetImpl(keyValue, value); 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return value; 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Gets a task to return an async value. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="key"&gt;A key.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;returns&gt;A cached value.&amp;lt;/returns&gt; 
&lt;br /&gt;
&amp;nbsp; public async Task&amp;lt;V&gt; GetAsync(K key) 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var keyValue = GetKey(key); 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var value = builder.MemoryCache.Get(keyValue) as V; 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!IsValid(value)) 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = await CreateValueAsync(key); 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetImpl(keyValue, value); 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return value; 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Gets string key value for a key. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="key"&gt;A key.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;returns&gt;A string key value.&amp;lt;/returns&gt; 
&lt;br /&gt;
&amp;nbsp; protected string GetKey(K key) 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return builder.KeyFunc != null ? builder.KeyFunc(key) : 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; key == null ? null : key.ToString(); 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Creates a value for a key. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="key"&gt;A key to create value for.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;returns&gt;A value instance.&amp;lt;/returns&gt; 
&lt;br /&gt;
&amp;nbsp; protected V CreateValue(K key) 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return builder.Factory != null ? builder.Factory(key) : 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.FactoryAsync(key).Result; 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Creates a task for value for a key. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="key"&gt;A key to create value for.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp; /// &amp;lt;returns&gt;A task for a value instance.&amp;lt;/returns&gt; 
&lt;br /&gt;
&amp;nbsp; protected Task&amp;lt;V&gt; CreateValueAsync(K key) 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return builder.FactoryAsync != null ? builder.FactoryAsync(key)
: 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task.FromResult(builder.Factory(key)); 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Validates the value. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="value"&gt;A value to validate.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;returns&gt; 
&lt;br /&gt;
&amp;nbsp; /// true if value is valid for a cache, and false otherise. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/returns&gt; 
&lt;br /&gt;
&amp;nbsp; protected bool IsValid(V value) 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return (value != null) &amp;&amp; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((builder.Validator == null) || builder.Validator(value)); 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Set implementation. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="key"&gt;A key to set value for.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;param name="value"&gt;A value to set.&amp;lt;/param&gt; 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;returns&gt;A set value.&amp;lt;/returns&gt; 
&lt;br /&gt;
&amp;nbsp; private V SetImpl(string key, V value) 
&lt;br /&gt;
&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (value == null) 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.MemoryCache.Remove(key); 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; else 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.MemoryCache.Set( 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; key, 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value, 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.CachePolicyFunc != null ? builder.CachePolicyFunc(value)
: 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.Sliding ? 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new CacheItemPolicy { SlidingExpiration
= builder.Expiration } : 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new CacheItemPolicy 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AbsoluteExpiration
= DateTime.Now + builder.Expiration 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }); 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return value; 
&lt;br /&gt;
&amp;nbsp; } 
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt; 
&lt;br /&gt;
&amp;nbsp; /// Cache builder. 
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt; 
&lt;br /&gt;
&amp;nbsp; private Builder builder; 
&lt;br /&gt;
} 
&lt;br /&gt;
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The use consists of initialization:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; Cache&amp;lt;MyKey, MyValue&gt; MyValues = new Cache&amp;lt;MyKey, MyValue&gt;.Builder 
&lt;br /&gt;
{ 
&lt;br /&gt;
&amp;nbsp; KeyFunc = key =&gt; ...key to string value..., 
&lt;br /&gt;
&amp;nbsp; Factory = key =&gt; ...create a value for a key..., 
&lt;br /&gt;
&amp;nbsp; Expiration = new TimeSpan(0, 3, 0), 
&lt;br /&gt;
&amp;nbsp; Sliding = true 
&lt;br /&gt;
}; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
and a trivial cache access:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;var value = MyValues[key];&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
This contrasts with MemoryCache coding pattern:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; MemoryCache cache = MemoryCache.Default;&lt;br /&gt;
... &lt;/code&gt; 
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; var keyAsString = ...key to string value...&lt;br /&gt;
var value = cache.Get(keyAsString) as MyValue;&lt;br /&gt;
&lt;br /&gt;
if (value == null)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; value = ...create a value for a key...&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; cache.Set(keyAsString, value, ...caching policy...);&lt;br /&gt;
} &lt;/code&gt; 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9ca4f9a5-1de8-4ed7-8175-6755a1bd14f6" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,9ca4f9a5-1de8-4ed7-8175-6755a1bd14f6.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=8e33a257-619e-412a-b3ac-1947b068fd77</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8e33a257-619e-412a-b3ac-1947b068fd77.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=8e33a257-619e-412a-b3ac-1947b068fd77</wfw:commentRss>
      <title>IEqualityComparer in LINQ</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8e33a257-619e-412a-b3ac-1947b068fd77.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/07/10/IEqualityComparerInLINQ.aspx</link>
      <pubDate>Thu, 10 Jul 2014 20:31:42 GMT</pubDate>
      <description>  &lt;p&gt;
&lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable"&gt;Enumerable
class&lt;/a&gt;&lt;/code&gt; contains many overloads with &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms132151"&gt;IEqualityComparable&amp;lt;T&amp;gt;&lt;/a&gt;&lt;/code&gt; argument.
Most notable methods are:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Contains;&lt;/li&gt;
&lt;li&gt;
Distinct;&lt;/li&gt;
&lt;li&gt;
Except;&lt;/li&gt;
&lt;li&gt;
GroupBy;&lt;/li&gt;
&lt;li&gt;
Intersect;&lt;/li&gt;
&lt;li&gt;
Join;&lt;/li&gt;
&lt;li&gt;
ToDictionary;&lt;/li&gt;
&lt;li&gt;
ToLookup;&lt;/li&gt;
&lt;li&gt;
Union.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Recently we dealt with simple case:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; source.&lt;br /&gt;
&amp;nbsp; Select(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; item =&amp;gt; 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new Word 
&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; Text = ..., 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LangID = ..., 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Properties = ...&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }).&lt;br /&gt;
&amp;nbsp; Distinct(&lt;span style="color: red"&gt;&lt;b&gt;equality comparer by Text and LangID&lt;/b&gt;&lt;/span&gt;);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
In other words how do you produce a enumeration of distinct words from a enumeration
of words, where two words are qualified equal if their &lt;code&gt;Text&lt;/code&gt; and &lt;code&gt;LangID&lt;/code&gt; are
equal?
&lt;/p&gt;
&lt;p&gt;
It turns out it&amp;#39;s cumbersome to implement &lt;code&gt;IEqualityComparer&amp;lt;T&amp;gt;&lt;/code&gt; interface
(and any other interface in C#), at least it&amp;#39;s nothing close to a conciseness
of lambda functions.
&lt;/p&gt;
&lt;p&gt;
Here we&amp;#39;ve decided to step in into framework space and to introduce an API to
define simple equality comparers for a class.
&lt;/p&gt;
&lt;p&gt;
We start from the use case:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; var &lt;span style="color: red"&gt;&lt;b&gt;wordComparer&lt;/b&gt;&lt;/span&gt; = KeyEqualityComparer.Null&amp;lt;Word&gt;().&lt;br /&gt;
&amp;nbsp; ThenBy(item =&gt; item.Text).&lt;br /&gt;
&amp;nbsp; ThenBy(item =&gt; item.LangID);&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
source.Select(...).Distinct(&lt;span style="color: red"&gt;&lt;b&gt;wordComparer&lt;/b&gt;&lt;/span&gt;);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
And then proceed to the API:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;namespace NesterovskyBros.Linq&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; using System;&lt;br /&gt;
&amp;nbsp; using System.Collections;&lt;br /&gt;
&amp;nbsp; using System.Collections.Generic;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp; /// A equality comparer extensions.&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp; public static class KeyEqualityComparer&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Gets null as equality comparer for a type.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;typeparam name="T"&gt;A type.&amp;lt;/typeparam&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// null as equality comparer for a type.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static IEqualityComparer&amp;lt;T&gt; Null&amp;lt;T&gt;()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return null;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Creates an equality comparer for a enumeration item.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;typeparam name="T"&gt;A type.&amp;lt;/typeparam&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="source"&gt;A source items.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="keyFunc"&gt;A key function.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// null as equality comparer for a type.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static IEqualityComparer&amp;lt;T&gt; EqualityComparerBy&amp;lt;T,
K&gt;(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this IEnumerable&amp;lt;T&gt; source,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Func&amp;lt;T, K&gt; keyFunc)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return new KeyEqualityComparer&amp;lt;T, K&gt;(keyFunc);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Creates an equality comparer that uses this comparer as a base.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;typeparam name="T"&gt;A type.&amp;lt;/typeparam&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;typeparam name="K"&gt;A key type.&amp;lt;/typeparam&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="equalityComparer"&gt;A base equality comparer.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="keyFunc"&gt;A key function.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// An equality comparer that uses this comparer as a base.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static KeyEqualityComparer&amp;lt;T, K&gt; ThenBy&amp;lt;T, K&gt;(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this IEqualityComparer&amp;lt;T&gt; equalityComparer, 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Func&amp;lt;T, K&gt; keyFunc)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return new KeyEqualityComparer&amp;lt;T, K&gt;(keyFunc, equalityComparer);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp; /// Equality comparer that uses a function to extract a comparision key.&lt;br /&gt;
&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp; /// &amp;lt;typeparam name="T"&gt;A type.&amp;lt;/typeparam&gt;&lt;br /&gt;
&amp;nbsp; /// &amp;lt;typeparam name="K"&gt;A key type.&amp;lt;/typeparam&gt;&lt;br /&gt;
&amp;nbsp; public struct KeyEqualityComparer&amp;lt;T, K&gt;: IEqualityComparer&amp;lt;T&gt;&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Creates an equality comparer.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="keyFunc"&gt;A key function.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="equalityComparer"&gt;A base equality comparer.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public KeyEqualityComparer(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Func&amp;lt;T, K&gt; keyFunc, 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IEqualityComparer&amp;lt;T&gt; equalityComparer = null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; KeyFunc = keyFunc;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EqualityComparer = equalityComparer;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="x"&gt;The first object of type T to compare.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="y"&gt;The second object of type T to compare.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// true if the specified objects are equal; otherwise, false.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public bool Equals(T x, T y)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return ((EqualityComparer == null) || EqualityComparer.Equals(x,
y)) &amp;&amp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EqualityComparer&amp;lt;K&gt;.Default.Equals(KeyFunc(x),
KeyFunc(y));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Returns a hash code for the specified object.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="obj"&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// The value for which a hash code is to be returned.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;returns&gt;A hash code for the specified object.&amp;lt;/returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int GetHashCode(T obj)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var hash = EqualityComparer&amp;lt;K&gt;.Default.GetHashCode(KeyFunc(obj));&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (EqualityComparer != null)&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; var hash2 = EqualityComparer.GetHashCode(obj);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hash ^= (hash2 &amp;lt;&amp;lt; 5) + hash2;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return hash;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// A key function.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public readonly Func&amp;lt;T, K&gt; KeyFunc;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Optional base equality comparer.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public readonly IEqualityComparer&amp;lt;T&gt; EqualityComparer;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
So, now you can easily build simple equality comparers to cache them or instantiate
on the fly. This comparers are usually related to property values or their function
of source values.
&lt;/p&gt;
&lt;p&gt;
See also &lt;a href="http://www.nesterovsky-bros.com/weblog/2014/03/07/LINQExtensions.aspx"&gt;LINQ
extensions&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=8e33a257-619e-412a-b3ac-1947b068fd77" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,8e33a257-619e-412a-b3ac-1947b068fd77.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=56c7dc56-7d3c-4263-b4a9-48cc1fee2aee</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,56c7dc56-7d3c-4263-b4a9-48cc1fee2aee.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,56c7dc56-7d3c-4263-b4a9-48cc1fee2aee.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=56c7dc56-7d3c-4263-b4a9-48cc1fee2aee</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is a small post about refactoring <code>lock</code> statements in <code>async</code> methods.
</p>
        <p>
Before refactoring we had a code like this:
</p>
        <p style="padding-left: 1em">
          <code>lock(sync)<br />
{<br />
  result = methodToRefactorIntoAsync();<br />
}<br /><br />
...<br /><br />
private object sync = new object();</code>
        </p>
        <p>
Lock is bound to a thread, thus no way you to use it in <code>async</code> code. As
an alternative you may use <a href="http://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim(v=vs.110).aspx">SemaphoreSlim</a> class:
</p>
        <p style="padding-left: 1em">
          <code> await sync.WaitAsync(cancellationToken);<br /><br />
try<br />
{<br />
  result = await methodAsync(cancellationToken);<br />
}<br />
finally<br />
{<br />
  sync.Release();<br />
}<br /><br />
...<br /><br />
private SemaphoreSlim sync = new SemaphoreSlim(1, 1);<br /></code>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=56c7dc56-7d3c-4263-b4a9-48cc1fee2aee" />
      </body>
      <title>Async and scarce resources in C#</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,56c7dc56-7d3c-4263-b4a9-48cc1fee2aee.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/06/28/AsyncAndScarceResourcesInC.aspx</link>
      <pubDate>Sat, 28 Jun 2014 11:56:32 GMT</pubDate>
      <description>  &lt;p&gt;
This is a small post about refactoring &lt;code&gt;lock&lt;/code&gt; statements in &lt;code&gt;async&lt;/code&gt; methods.
&lt;/p&gt;
&lt;p&gt;
Before refactoring we had a code like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;lock(sync)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; result = methodToRefactorIntoAsync();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
private object sync = new object();&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Lock is bound to a thread, thus no way you to use it in &lt;code&gt;async&lt;/code&gt; code. As
an alternative you may use &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim(v=vs.110).aspx"&gt;SemaphoreSlim&lt;/a&gt; class:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; await sync.WaitAsync(cancellationToken);&lt;br /&gt;
&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; result = await methodAsync(cancellationToken);&lt;br /&gt;
}&lt;br /&gt;
finally&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; sync.Release();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
private SemaphoreSlim sync = new SemaphoreSlim(1, 1);&lt;br /&gt;
&lt;/code&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=56c7dc56-7d3c-4263-b4a9-48cc1fee2aee" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,56c7dc56-7d3c-4263-b4a9-48cc1fee2aee.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=4d14fa41-ded2-40c1-9a0d-cf1af923566b</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,4d14fa41-ded2-40c1-9a0d-cf1af923566b.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=4d14fa41-ded2-40c1-9a0d-cf1af923566b</wfw:commentRss>
      <title>Client disconnects in ASP.NET Web API</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,4d14fa41-ded2-40c1-9a0d-cf1af923566b.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/06/27/ClientDisconnectsInASPNETWebAPI.aspx</link>
      <pubDate>Fri, 27 Jun 2014 14:42:44 GMT</pubDate>
      <description>  &lt;p&gt;
What will you do if you have &lt;code&gt;async&lt;/code&gt; Web API method that runs on server
for a some time but your client is dropped?
&lt;/p&gt;
&lt;p&gt;
There are two solutions:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Run method to the end and allow to a framework to deal with disconnect;&lt;/li&gt;
&lt;li&gt;
Try to be notified about client&amp;#39;s drop and to break early.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The first approach is simplest but might result in some overconsumption of server
resources. The other method requires you to check client status from time to time.
&lt;/p&gt;
&lt;p&gt;
Fortunatelly, ASP.NET provides a &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.httpresponse.clientdisconnectedtoken(v=vs.110).aspx"&gt;HttpResponse.ClientDisconnectedToken&lt;/a&gt; property,
which is limited to IIS 7.5+ in integrated mode, but still fits our needs. So, you
should request &lt;code&gt;ClientDisconnectedToken&lt;/code&gt;, if any, and implement your &lt;code&gt;async&lt;/code&gt; code
using that token. 
&lt;/p&gt;
&lt;p&gt;
The following extension function gets that token:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; using System.Linq;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
using System.Web;&lt;br /&gt;
&lt;br /&gt;
public static class HttpApiExtensions&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; public static CancellationToken GetCancellationToken(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; this HttpRequestMessage request)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CancellationToken cancellationToken;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; object value;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var key = typeof(HttpApiExtensions).Namespace + ":CancellationToken";&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (request.Properties.TryGetValue(key, out value))&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (CancellationToken)value;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var httpContext = HttpContext.Current;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (httpContext != null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var httpResponse = httpContext.Response;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (httpResponse != null)&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; try&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp; cancellationToken = httpResponse.ClientDisconnectedToken;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&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; catch&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp; // Do not support cancellation.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; request.Properties[key] = cancellationToken;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return cancellationToken;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
And here is a Web API &lt;a href="http://www.nesterovsky-bros.com/weblog/2014/06/25/ParallelVsAsyncInC.aspx"&gt;WordCount
service described in the previous post&lt;/a&gt;:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
&lt;br /&gt;
public class ValuesController: ApiController&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; public async Task&amp;lt;int&gt; GetWordCount([FromUri(Name = "url")] string[] urls)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var cancellationToken = &lt;b&gt;Request.GetCancellationToken();&lt;/b&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; using(var client = new HttpClient())&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (await Task.WhenAll(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; urls.Select(url =&gt; WordCountAsync(client,
url, cancellationToken)))).Sum();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public static async Task&amp;lt;int&gt; WordCountAsync(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpClient client,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; string url, 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CancellationToken cancellationToken)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; string content = await (await client.GetAsync(url, cancellationToken)).&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Content.ReadAsStringAsync();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return WordCount(content);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; private static int WordCount(string text)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var count = 0;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var space = true;&lt;br /&gt;
&lt;br /&gt;&lt; text.Length; ++i)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for (var i = 0; i &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (space != char.IsWhiteSpace(text[i]))&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; space = !space;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!space)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp; ++count;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return count;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
} &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
Though is simple there is a nuisance. You should pass cancellation token here and
there, which adds to a &lt;a href="http://www.nesterovsky-bros.com/weblog/2014/06/23/AsyncCodeInC.aspx"&gt;pollution&lt;/a&gt; from &lt;code&gt;async&lt;/code&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=4d14fa41-ded2-40c1-9a0d-cf1af923566b" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,4d14fa41-ded2-40c1-9a0d-cf1af923566b.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=b90dd55e-e3eb-4a62-9a3c-696b2caa1d0e</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b90dd55e-e3eb-4a62-9a3c-696b2caa1d0e.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=b90dd55e-e3eb-4a62-9a3c-696b2caa1d0e</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <title>Parallel vs Async in C#</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b90dd55e-e3eb-4a62-9a3c-696b2caa1d0e.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/06/25/ParallelVsAsyncInC.aspx</link>
      <pubDate>Wed, 25 Jun 2014 12:51:28 GMT</pubDate>
      <description>  &lt;p&gt;
Though parallel and async algorithms solve different tasks, they converge in some
cases. And it&amp;#39;s not always immediately clear what&amp;#39;s the best.
&lt;/p&gt;
&lt;p&gt;
Consider the following task: get a total word count contained in a given a set of
urls.
&lt;/p&gt;
&lt;p&gt;
At first we&amp;#39;ve solved it as a parallel task: indeed this fits to &lt;a href="http://en.wikipedia.org/wiki/MapReduce"&gt;MapReduce&lt;/a&gt; pattern
when you get urls&amp;#39; contents to count the number of words in&amp;nbsp; parallel (Map),
and then sum word counts per each url to get final result (Reduce). But then we decided
that the very same MapReduce algorithm can be implemented with &lt;code&gt;async&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
This is a parallel word count:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; public static int ParallelWordCount(IEnumerable&amp;lt;string&gt; urls)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; var result = 0;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; Parallel.ForEach(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; urls,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; url =&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string content;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using(var client = new WebClient())&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; content = client.DownloadString(url);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var count = WordCount(content);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Interlocked.Add(ref result, count);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; return result;&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Here is &lt;code&gt;async&lt;/code&gt; word count:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; public static async Task&amp;lt;int&gt; WordCountAsync(IEnumerable&amp;lt;string&gt; urls)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; return (await Task.WhenAll(urls.Select(url =&gt; WordCountAsync(url)))).Sum();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static async Task&amp;lt;int&gt; WordCountAsync(string url)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; string content;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; using(var client = new WebClient())&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; content = await client.DownloadStringTaskAsync(url);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; return WordCount(content);&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
And this is an implementation of word count for a text (it&amp;#39;s less important for
this discussion):
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; public static int WordCount(string text)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; var count = 0;&lt;br /&gt;
&amp;nbsp; var space = true;&lt;br /&gt;
&lt;br /&gt;&lt; text.Length; ++i)&lt;br /&gt;
&amp;nbsp; for(var i = 0; i &amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (space != char.IsWhiteSpace(text[i]))&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; space = !space;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!space)&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; ++count;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; return count;&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Our impressions are:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;
The parallel version is contained in one method, while the &lt;code&gt;async&lt;/code&gt; one
is implemeneted with two methods.
&lt;/p&gt;
&lt;p&gt;
This is due to the fact that C# compiler fails to generate &lt;code&gt;async&lt;/code&gt; labmda
function. We attribute this to Microsoft who leads and implements C# spec. Features
should be composable. If one can implement a method as a lambda function, and one
can implement a method as &lt;code&gt;async&lt;/code&gt; then one should be able to implement
a method as an &lt;code&gt;async&lt;/code&gt; lambda function.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
Both parallel and &lt;code&gt;async&lt;/code&gt; versions are using thread pool to run their logic.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
While both implementations follow MapReduce pattern, we can see that &lt;code&gt;async&lt;/code&gt; version
is much more scaleable. It&amp;#39;s because of parallel threads stay blocked while waiting
for an http response. On the other hand &lt;code&gt;async&lt;/code&gt; tasks are not bound to
any thread and are just not running while waiting for I/O.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
This sample helped us to answer the question as to when to use parallel and when &lt;code&gt;async&lt;/code&gt;.
The &lt;b&gt;simple&lt;/b&gt; answer goes like this:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
if your logic is only CPU bound then use parallel API;&lt;/li&gt;
&lt;li&gt;
otherwise use &lt;code&gt;async&lt;/code&gt; API (this accounts I/O waits).&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=b90dd55e-e3eb-4a62-9a3c-696b2caa1d0e" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,b90dd55e-e3eb-4a62-9a3c-696b2caa1d0e.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=695e151a-af98-4cc0-afd8-1a14dcba21ed</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,695e151a-af98-4cc0-afd8-1a14dcba21ed.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,695e151a-af98-4cc0-afd8-1a14dcba21ed.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=695e151a-af98-4cc0-afd8-1a14dcba21ed</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Not a long ago C# has introduced special language constructs to simplify asynchronous
programming. It seems <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3858.pdf">C++1x
will follow async trend</a>. But only recently when frameworks like ASP.NET Web API
and Entity Framework started to catch up we've felt how it's to program with <code><a href="http://msdn.microsoft.com/en-us/library/hh156513.aspx">async</a></code> and <code><a href="http://msdn.microsoft.com/en-us/library/hh156528.aspx">await</a></code> keywords.
</p>
        <p>
At first glance it seems it's a pure pleasure to write async methods:
</p>
        <p style="padding-left: 1em">
          <code>private async Task SumPageSizesAsync()<br />
{<br />
  // To use the HttpClient type in desktop apps, you must include a using directive
and add a 
<br />
  // reference for the System.Net.Http namespace.<br />
  HttpClient client = new HttpClient();<br />
  // . . .<br /><br />
  byte[] urlContents = await client.GetByteArrayAsync(url);<br />
  // . . .<br />
}</code>
        </p>
        <p>
To dereference a <code>Task&lt;T&gt;</code> into <code>T</code> you just write <code>await
task_t_expression</code>, mark your method with <code>async</code> specifier, and
adjust output type (if not <code>void</code>) to <code>Task</code> or <code>Task&lt;Result&gt;</code>.
Compiler applies its magic to convert your code into an asynchronous state machine.
</p>
        <p>
We liked this feature and immediately have started to use it. But, as we said, <code>async</code>/<code>await</code> has
shined in full when frameworks made it a core element, and at that point we have started
to see that while  <code>async</code>/<code>await</code> solve the task, they
does not abstract the developer from implementation details, as a code gets considerably
polluted.
</p>
        <p>
Consider a method with pollution marked:
</p>
        <p style="padding-left: 1em">
          <code>public static <b>async</b><b>Task&lt;</b>UserAuthorization<b>&gt;</b> GetAuthorization<b>Async</b>(string
accessToken)<br />
{<br />
  var key = "oauth2:" + accessToken;<br />
  var authorization = cache.Get&lt;UserAuthorization&gt;(key);<br /><br />
  if (authorization != null)<br />
  {<br />
    return authorization;<br />
  }<br /><br />
  using(var model = new ModelContainer())<br />
  {<br />
    authorization =<br /><b>      (await </b>model.UserAuthorizations.<br />
        Where(item =&gt; item.AccessToken == accessToken).<br /><b>        ToListAsync()).</b><br />
      FirstOrDefault();<br />
  }<br /><br />
  if (authorization == null)<br />
  {<br />
    authorization = <b>await </b>Validate<b>Async</b>(accessToken);<br />
  }<br /><br />
  cache.Set(key, cache.ShortDelay, authorization);<br /><br />
  return authorization;<br />
} </code>
        </p>
        <p>
The more you use <code>async</code>, the more pollution you will see in your code.
Ideally we would like to see the method quoted without any marked parts.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=695e151a-af98-4cc0-afd8-1a14dcba21ed" />
      </body>
      <title>Async code in C#</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,695e151a-af98-4cc0-afd8-1a14dcba21ed.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/06/23/AsyncCodeInC.aspx</link>
      <pubDate>Mon, 23 Jun 2014 06:15:55 GMT</pubDate>
      <description>  &lt;p&gt;
Not a long ago C# has introduced special language constructs to simplify asynchronous
programming. It seems &lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3858.pdf"&gt;C++1x
will follow async trend&lt;/a&gt;. But only recently when frameworks like ASP.NET Web API
and Entity Framework started to catch up we&amp;#39;ve felt how it&amp;#39;s to program with &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/hh156513.aspx"&gt;async&lt;/a&gt;&lt;/code&gt; and &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/hh156528.aspx"&gt;await&lt;/a&gt;&lt;/code&gt; keywords.
&lt;/p&gt;
&lt;p&gt;
At first glance it seems it&amp;#39;s a pure pleasure to write async methods:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;private async Task SumPageSizesAsync()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; // To use the HttpClient type in desktop apps, you must include a using directive
and add a 
&lt;br /&gt;
&amp;nbsp; // reference for the System.Net.Http namespace.&lt;br /&gt;
&amp;nbsp; HttpClient client = new HttpClient();&lt;br /&gt;
&amp;nbsp; // . . .&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; byte[] urlContents = await client.GetByteArrayAsync(url);&lt;br /&gt;
&amp;nbsp; // . . .&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
To dereference a &lt;code&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt; into &lt;code&gt;T&lt;/code&gt; you just write &lt;code&gt;await
task_t_expression&lt;/code&gt;, mark your method with &lt;code&gt;async&lt;/code&gt; specifier, and
adjust output type (if not &lt;code&gt;void&lt;/code&gt;) to &lt;code&gt;Task&lt;/code&gt; or &lt;code&gt;Task&amp;lt;Result&gt;&lt;/code&gt;.
Compiler applies its magic to convert your code into an asynchronous state machine.
&lt;/p&gt;
&lt;p&gt;
We liked this feature and immediately have started to use it. But, as we said, &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; has
shined in full when frameworks made it a core element, and at that point we have started
to see that while&amp;nbsp; &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; solve the task, they
does not abstract the developer from implementation details, as a code gets considerably
polluted.
&lt;/p&gt;
&lt;p&gt;
Consider a method with pollution marked:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;public static &lt;b&gt;async&lt;/b&gt; &lt;b&gt;Task&amp;lt;&lt;/b&gt;UserAuthorization&lt;b&gt;&gt;&lt;/b&gt; GetAuthorization&lt;b&gt;Async&lt;/b&gt;(string
accessToken)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; var key = "oauth2:" + accessToken;&lt;br /&gt;
&amp;nbsp; var authorization = cache.Get&amp;lt;UserAuthorization&gt;(key);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; if (authorization != null)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return authorization;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; using(var model = new ModelContainer())&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; authorization =&lt;br /&gt;
&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (await &lt;/b&gt;model.UserAuthorizations.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Where(item =&gt; item.AccessToken == accessToken).&lt;br /&gt;
&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ToListAsync()).&lt;/b&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FirstOrDefault();&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; if (authorization == null)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; authorization = &lt;b&gt;await &lt;/b&gt;Validate&lt;b&gt;Async&lt;/b&gt;(accessToken);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; cache.Set(key, cache.ShortDelay, authorization);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; return authorization;&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The more you use &lt;code&gt;async&lt;/code&gt;, the more pollution you will see in your code.
Ideally we would like to see the method quoted without any marked parts.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=695e151a-af98-4cc0-afd8-1a14dcba21ed" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,695e151a-af98-4cc0-afd8-1a14dcba21ed.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=a892f73a-77bf-4def-89e4-8b753f7d6552</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,a892f73a-77bf-4def-89e4-8b753f7d6552.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=a892f73a-77bf-4def-89e4-8b753f7d6552</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We needed to have oauth2 authorization in angularjs project.
</p>
        <p>
Internet search on the subject immediately brings large amout of solutions like:
</p>
        <ul>
          <li>
oauth2 provider specific javascript libraries;</li>
          <li>
            <a href="https://github.com/enginous/angular-oauth">angular-oauth</a> project;</li>
          <li>
            <a href="https://oauth.io/">oauth.io</a> aggregator;</li>
        </ul>
        <p>
But unfortunatelly:
</p>
        <ul>
          <li>
provider specific libraries have too different set of APIs, which requires another
umbrella library to allow for application to accept several providers;</li>
          <li>
            <a href="https://github.com/enginous/angular-oauth">angular-oauth</a> - supports Google
only, and does not work in IE 11 with default security settings;</li>
          <li>
            <a href="https://oauth.io/">oauth.io</a> looks attractive but adds additional level
of indirection server, and is free for a basic plan only.</li>
        </ul>
        <p>
However there is a problem with all those approaches.
</p>
        <p>
Let's assume that you have properly implemented client side authorization, and
finally have gotten an <code>access_token</code>.
</p>
        <p>
Now, you access your server with that <code>access_token</code>. What is your first
step on the server? 
</p>
        <p>
Right! Your should validate it against oauth2 provider.
</p>
        <p>
So, while client side authorization, among other things, included a validation of
your token, you have to perform the validation on the server once more.
</p>
        <p>
At this point we felt that we need to implement our oauth2 API. <img alt=":-)" src="http://www.nesterovsky-bros.com/weblog/smilies/happy.gif" /></p>
        <p>
It can be found at <a href="https://github.com/nesterovsky-bros/oauth2">nesterovsky-bros/oauth2</a>.
</p>
        <p>
This is the readme from that project:
</p>
        <div style="padding-left: 1em">
          <p>
Here we implement oauth2 authorization within angularjs.
</p>
          <p>
Authentication is done as follows:
</p>
          <ol>
            <li>
Open oauth2 provider login/grant screen.</li>
            <li>
Redirect to the oauth2 callback screen with access token.</li>
            <li>
Verify of the access token against provider.</li>
            <li>
Get some basic profile.</li>
          </ol>
          <p>
A base javascript class <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2.js">OAuth2</a> implements
these steps.
</p>
          <p>
There are following implementations that authorize against specific providers:
</p>
          <ul>
            <li>
              <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2-facebook.js">OAuth2Facebook</a>;</li>
            <li>
              <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2-google.js">OAuth2Google</a>;</li>
            <li>
              <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2-server.js">OAuth2Server</a>;</li>
          </ul>
          <p>
            <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2-server.js">OAuth2Server</a> -
implements authorization through known providers, but calls server side to validate
access token. This way, the server side can establish a user's session.
</p>
          <p>
The file <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/Config.json">Config.json</a> contains
endpoints and request parameters per supported provider.
</p>
          <p>
            <strong>Note:</strong> You should register a client_id for each provider.
</p>
          <p>
            <strong>Note:</strong> user_id and access_tokens are unique only in a scope of access
provider, thus a session is identified by Provider + access_token, and a user is identified
by Provider + user_id.
</p>
          <p>
The use case can be found in <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/test/test.js">test.js</a> E.g.
authorization against <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2-server.js">OAuth2Server</a> is
done like this:
</p>
          <div style="padding-left: 1em">
            <code>var login = new OAuth2Server(provider);<br /><br />
token = login.authorize();<br /><br />
token.$promise.then(<br />
  function()<br />
  {<br />
    // token contains populated data.<br />
  },<br />
  function(error)<br />
  {<br />
    if (error)<br />
    {<br />
      // handle an error<br />
    }<br />
  }); </code>
          </div>
          <p>
Authorization token contains:
</p>
          <ul>
            <li>
a promise to handle authorization outcome.</li>
            <li>
cancelToken (a Deferred object) to cancel authorization in progress.</li>
          </ul>
          <p>
Whole sample is implemented as VS project. All scripts are build with <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app.tt">app.tt</a>,
that combines content of <a href="https://github.com/nesterovsky-bros/oauth2/tree/master/OAuth2/Scripts/app">Scripts/app</a> int <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app.js">app.js</a>.
</p>
          <p>
Server side is implemented with ASP.NET Web API. Authorization controllers are:
</p>
          <ul>
            <li>
              <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Controllers/OAuth2/GoogleController.cs">GoogleController</a>;</li>
            <li>
              <a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Controllers/OAuth2/FacebookController.cs">FacebookController</a>
            </li>
          </ul>
        </div>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=a892f73a-77bf-4def-89e4-8b753f7d6552" />
      </body>
      <title>oauth2 and angularjs</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,a892f73a-77bf-4def-89e4-8b753f7d6552.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/06/12/oauth2AndAngularjs.aspx</link>
      <pubDate>Thu, 12 Jun 2014 14:59:42 GMT</pubDate>
      <description>  &lt;p&gt;
We needed to have oauth2 authorization in angularjs project.
&lt;/p&gt;
&lt;p&gt;
Internet search on the subject immediately brings large amout of solutions like:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
oauth2 provider specific javascript libraries;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/enginous/angular-oauth"&gt;angular-oauth&lt;/a&gt; project;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://oauth.io/"&gt;oauth.io&lt;/a&gt; aggregator;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
But unfortunatelly:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
provider specific libraries have too different set of APIs, which requires another
umbrella library to allow for application to accept several providers;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/enginous/angular-oauth"&gt;angular-oauth&lt;/a&gt; - supports Google
only, and does not work in IE 11 with default security settings;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://oauth.io/"&gt;oauth.io&lt;/a&gt; looks attractive but adds additional level
of indirection server, and is free for a basic plan only.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
However there is a problem with all those approaches.
&lt;/p&gt;
&lt;p&gt;
Let&amp;#39;s assume that you have properly implemented client side authorization, and
finally have gotten an &lt;code&gt;access_token&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
Now, you access your server with that &lt;code&gt;access_token&lt;/code&gt;. What is your first
step on the server? 
&lt;/p&gt;
&lt;p&gt;
Right! Your should validate it against oauth2 provider.
&lt;/p&gt;
&lt;p&gt;
So, while client side authorization, among other things, included a validation of
your token, you have to perform the validation on the server once more.
&lt;/p&gt;
&lt;p&gt;
At this point we felt that we need to implement our oauth2 API. &lt;img alt=":-)" src="http://www.nesterovsky-bros.com/weblog/smilies/happy.gif"&gt;
&lt;/p&gt;
&lt;p&gt;
It can be found at &lt;a href="https://github.com/nesterovsky-bros/oauth2"&gt;nesterovsky-bros/oauth2&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
This is the readme from that project:
&lt;/p&gt;
&lt;div style="padding-left: 1em"&gt;
&lt;p&gt;
Here we implement oauth2 authorization within angularjs.
&lt;/p&gt;
&lt;p&gt;
Authentication is done as follows:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Open oauth2 provider login/grant screen.&lt;/li&gt;
&lt;li&gt;
Redirect to the oauth2 callback screen with access token.&lt;/li&gt;
&lt;li&gt;
Verify of the access token against provider.&lt;/li&gt;
&lt;li&gt;
Get some basic profile.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
A base javascript class &lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2.js"&gt;OAuth2&lt;/a&gt; implements
these steps.
&lt;/p&gt;
&lt;p&gt;
There are following implementations that authorize against specific providers:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2-facebook.js"&gt;OAuth2Facebook&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2-google.js"&gt;OAuth2Google&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2-server.js"&gt;OAuth2Server&lt;/a&gt;;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2-server.js"&gt;OAuth2Server&lt;/a&gt; -
implements authorization through known providers, but calls server side to validate
access token. This way, the server side can establish a user&amp;#39;s session.
&lt;/p&gt;
&lt;p&gt;
The file &lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/Config.json"&gt;Config.json&lt;/a&gt; contains
endpoints and request parameters per supported provider.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Note:&lt;/strong&gt; You should register a client_id for each provider.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Note:&lt;/strong&gt; user_id and access_tokens are unique only in a scope of access
provider, thus a session is identified by Provider + access_token, and a user is identified
by Provider + user_id.
&lt;/p&gt;
&lt;p&gt;
The use case can be found in &lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/test/test.js"&gt;test.js&lt;/a&gt; E.g.
authorization against &lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app/oauth2/oauth2-server.js"&gt;OAuth2Server&lt;/a&gt; is
done like this:
&lt;/p&gt;
&lt;div style="padding-left: 1em"&gt;
&lt;code&gt;var login = new OAuth2Server(provider);&lt;br /&gt;
&lt;br /&gt;
token = login.authorize();&lt;br /&gt;
&lt;br /&gt;
token.$promise.then(&lt;br /&gt;
&amp;nbsp; function()&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // token contains populated data.&lt;br /&gt;
&amp;nbsp; },&lt;br /&gt;
&amp;nbsp; function(error)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (error)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // handle an error&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; }); &lt;/code&gt;
&lt;/div&gt;
&lt;p&gt;
Authorization token contains:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
a promise to handle authorization outcome.&lt;/li&gt;
&lt;li&gt;
cancelToken (a Deferred object) to cancel authorization in progress.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Whole sample is implemented as VS project. All scripts are build with &lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app.tt"&gt;app.tt&lt;/a&gt;,
that combines content of &lt;a href="https://github.com/nesterovsky-bros/oauth2/tree/master/OAuth2/Scripts/app"&gt;Scripts/app&lt;/a&gt; int &lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Scripts/app.js"&gt;app.js&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Server side is implemented with ASP.NET Web API. Authorization controllers are:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Controllers/OAuth2/GoogleController.cs"&gt;GoogleController&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/nesterovsky-bros/oauth2/blob/master/OAuth2/Controllers/OAuth2/FacebookController.cs"&gt;FacebookController&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=a892f73a-77bf-4def-89e4-8b753f7d6552" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,a892f73a-77bf-4def-89e4-8b753f7d6552.aspx</comments>
      <category>.NET</category>
      <category>AngularJS</category>
      <category>javascript</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=be57df20-9025-4f1b-849e-5b982df91f7a</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,be57df20-9025-4f1b-849e-5b982df91f7a.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=be57df20-9025-4f1b-849e-5b982df91f7a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the article <a href="http://www.nesterovsky-bros.com/weblog/2013/05/13/ErrorHandlingInWCFBasedWebApplications.aspx">"Error
handling in WCF based web applications"</a> we've shown a custom error handler
for RESTful service based on WCF. This time we shall do the same for Web API 2.1 service. 
</p>
        <p>
Web API 2.1 provides an elegant way to implementat custom error handlers/loggers,
see <a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling">the
following article.</a> Web API permits many error loggers followed by a single error
handler for all uncaught exceptions. A default error handler knows to output an error
both in XML and JSON formats depending on requested MIME type. 
</p>
        <p>
In our projects we use unique error reference IDs. This feature allows to an end-user
to refer to any error that has happened during the application life time and pass
such error ID to the technical support for further investigations. Thus, error details
passed to the client-side contain an ErrorID field. An error logger generates ErrorID
and passes it over to an error handler for serialization. 
</p>
        <p>
Let's look at our error handling implementation for a Web API application. 
</p>
        <p>
The first part is an implementation of <code>IExceptionLogger</code> interface. It
assigns ErrorID and logs all errors: 
</p>
        <p style="padding-left: 1em">
          <code> /// Defines a global logger for unhandled exceptions.<br />
public class GlobalExceptionLogger : ExceptionLogger<br />
{<br />
/// Writes log record to the database synchronously.<br />
public override void Log(ExceptionLoggerContext context)<br />
{<br />
try<br />
{<br />
var request = context.Request;<br />
var exception = context.Exception;<br /><br />
var id = LogError(<br />
request.RequestUri.ToString(),<br />
context.RequestContext == null ?<br />
null : context.RequestContext.Principal.Identity.Name,<br />
request.ToString(),<br />
exception.Message,<br />
exception.StackTrace);<br /><br />
// associates retrieved error ID with the current exception<br />
exception.Data["NesterovskyBros:id"] = id;<br />
}<br />
catch<br />
{<br />
// logger shouldn't throw an exception!!!<br />
}<br />
}<br /><br />
// in the real life this method may store all relevant info into a database.<br />
private long LogError(<br />
string address,<br />
string userid,<br />
string request,<br />
string message,<br />
string stackTrace)<br />
{<br />
...<br />
}<br />
} </code>
        </p>
        <p>
The second part is the implementation of <code>IExceptionHandler</code>:
</p>
        <p style="padding-left: 1em">
          <code> /// Defines a global handler for unhandled exceptions.<br />
public class GlobalExceptionHandler : ExceptionHandler<br />
{<br />
/// This core method should implement custom error handling, if any.<br />
/// It determines how an exception will be serialized for client-side processing.<br />
public override void Handle(ExceptionHandlerContext context)<br />
{<br />
var requestContext = context.RequestContext;<br />
var config = requestContext.Configuration;<br /><br />
context.Result = new ErrorResult(<br />
context.Exception,<br />
requestContext == null ? false : requestContext.IncludeErrorDetail,<br />
config.Services.GetContentNegotiator(),<br />
context.Request,<br />
config.Formatters);<br />
}<br /><br />
/// An implementation of IHttpActionResult interface.<br />
private class ErrorResult : ExceptionResult<br />
{<br />
public ErrorResult(<br />
Exception exception,<br />
bool includeErrorDetail,<br />
IContentNegotiator negotiator,<br />
HttpRequestMessage request,<br />
IEnumerable&lt;MediaTypeFormatter&gt; formatters) :<br />
base(exception, includeErrorDetail, negotiator, request, formatters)<br />
{<br />
}<br /><br />
/// Creates an HttpResponseMessage instance asynchronously.<br />
/// This method determines how a HttpResponseMessage content will look like.<br />
public override Task&lt;HttpResponseMessage&gt; ExecuteAsync(CancellationToken cancellationToken)<br />
{<br />
var content = new HttpError(Exception, IncludeErrorDetail);<br /><br />
// define an additional content field with name "ErrorID"<br />
content.Add("ErrorID", Exception.Data["NesterovskyBros:id"] as long?);<br /><br />
var result =<br />
ContentNegotiator.Negotiate(typeof(HttpError), Request, Formatters);<br /><br />
var message = new HttpResponseMessage<br />
{<br />
RequestMessage = Request,<br />
StatusCode = result == null ?<br />
HttpStatusCode.NotAcceptable : HttpStatusCode.InternalServerError<br />
};<br /><br />
if (result != null)<br />
{<br />
try<br />
{<br />
// serializes the HttpError instance either to JSON or to XML<br />
// depend on requested by the client MIME type.<br />
message.Content = new ObjectContent&lt;HttpError&gt;(<br />
content,<br />
result.Formatter,<br />
result.MediaType);<br />
}<br />
catch<br />
{<br />
message.Dispose();<br /><br />
throw;<br />
}<br />
}<br /><br />
return Task.FromResult(message);<br />
}<br />
}<br />
} </code>
        </p>
        <p>
Last, but not least part of this solution is registration and configuration of the
error logger/handler: 
</p>
        <p style="padding-left: 1em">
          <code> /// WebApi congiguation.<br />
public static class WebApiConfig<br />
{<br />
public static void Register(HttpConfiguration config)<br />
{<br />
...<br /><br />
// register the exception logger and handler<br />
config.Services.Add(typeof(IExceptionLogger), new GlobalExceptionLogger());<br />
config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());<br /><br />
// set error detail policy according with value from Web.config<br />
var customErrors =<br />
(CustomErrorsSection)ConfigurationManager.GetSection("system.web/customErrors");<br /><br />
if (customErrors != null)<br />
{<br />
switch (customErrors.Mode)<br />
{<br />
case CustomErrorsMode.RemoteOnly:<br />
{<br />
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;<br /><br />
break;<br />
}<br />
case CustomErrorsMode.On:<br />
{<br />
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Never;<br /><br />
break;<br />
}<br />
case CustomErrorsMode.Off:<br />
{<br />
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;<br /><br />
break;<br />
}<br />
default:<br />
{<br />
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Default;<br /><br />
break;<br />
}<br />
}<br />
}<br />
}<br />
} </code>
        </p>
        <p>
The client-side error handler remain almost untouched. The implementation details
you may find in <code>/Scripts/api/api.js</code> and <code>Scripts/controls/error.js</code> files. 
</p>
        <p>
You may <a href="http://www.nesterovsky-bros.com/download/GlobalErrorHandler.zip">download
the demo project here</a>. Feel free to use this solution in your .NET projects. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=be57df20-9025-4f1b-849e-5b982df91f7a" />
      </body>
      <title>Custom error handling with Web API</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,be57df20-9025-4f1b-849e-5b982df91f7a.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/03/10/CustomErrorHandlingWithWebAPI.aspx</link>
      <pubDate>Mon, 10 Mar 2014 10:56:24 GMT</pubDate>
      <description>    &lt;p&gt;
In the article &lt;a href="http://www.nesterovsky-bros.com/weblog/2013/05/13/ErrorHandlingInWCFBasedWebApplications.aspx"&gt;&amp;quot;Error
handling in WCF based web applications&amp;quot;&lt;/a&gt; we've shown a custom error handler
for RESTful service based on WCF. This time we shall do the same for Web API 2.1 service. 
&lt;/p&gt;
&lt;p&gt;
Web API 2.1 provides an elegant way to implementat custom error handlers/loggers,
see &lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling"&gt;the
following article.&lt;/a&gt; Web API permits many error loggers followed by a single error
handler for all uncaught exceptions. A default error handler knows to output an error
both in XML and JSON formats depending on requested MIME type. 
&lt;/p&gt;
&lt;p&gt;
In our projects we use unique error reference IDs. This feature allows to an end-user
to refer to any error that has happened during the application life time and pass
such error ID to the technical support for further investigations. Thus, error details
passed to the client-side contain an ErrorID field. An error logger generates ErrorID
and passes it over to an error handler for serialization. 
&lt;/p&gt;
&lt;p&gt;
Let's look at our error handling implementation for a Web API application. 
&lt;/p&gt;
&lt;p&gt;
The first part is an implementation of &lt;code&gt;IExceptionLogger&lt;/code&gt; interface. It
assigns ErrorID and logs all errors: 
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; /// Defines a global logger for unhandled exceptions.&lt;br /&gt;
public class GlobalExceptionLogger : ExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
/// Writes log record to the database synchronously.&lt;br /&gt;
public override void Log(ExceptionLoggerContext context)&lt;br /&gt;
{&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
var request = context.Request;&lt;br /&gt;
var exception = context.Exception;&lt;br /&gt;
&lt;br /&gt;
var id = LogError(&lt;br /&gt;
request.RequestUri.ToString(),&lt;br /&gt;
context.RequestContext == null ?&lt;br /&gt;
null : context.RequestContext.Principal.Identity.Name,&lt;br /&gt;
request.ToString(),&lt;br /&gt;
exception.Message,&lt;br /&gt;
exception.StackTrace);&lt;br /&gt;
&lt;br /&gt;
// associates retrieved error ID with the current exception&lt;br /&gt;
exception.Data["NesterovskyBros:id"] = id;&lt;br /&gt;
}&lt;br /&gt;
catch&lt;br /&gt;
{&lt;br /&gt;
// logger shouldn't throw an exception!!!&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// in the real life this method may store all relevant info into a database.&lt;br /&gt;
private long LogError(&lt;br /&gt;
string address,&lt;br /&gt;
string userid,&lt;br /&gt;
string request,&lt;br /&gt;
string message,&lt;br /&gt;
string stackTrace)&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
} &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
The second part is the implementation of &lt;code&gt;IExceptionHandler&lt;/code&gt;:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; /// Defines a global handler for unhandled exceptions.&lt;br /&gt;
public class GlobalExceptionHandler : ExceptionHandler&lt;br /&gt;
{&lt;br /&gt;
/// This core method should implement custom error handling, if any.&lt;br /&gt;
/// It determines how an exception will be serialized for client-side processing.&lt;br /&gt;
public override void Handle(ExceptionHandlerContext context)&lt;br /&gt;
{&lt;br /&gt;
var requestContext = context.RequestContext;&lt;br /&gt;
var config = requestContext.Configuration;&lt;br /&gt;
&lt;br /&gt;
context.Result = new ErrorResult(&lt;br /&gt;
context.Exception,&lt;br /&gt;
requestContext == null ? false : requestContext.IncludeErrorDetail,&lt;br /&gt;
config.Services.GetContentNegotiator(),&lt;br /&gt;
context.Request,&lt;br /&gt;
config.Formatters);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/// An implementation of IHttpActionResult interface.&lt;br /&gt;
private class ErrorResult : ExceptionResult&lt;br /&gt;
{&lt;br /&gt;
public ErrorResult(&lt;br /&gt;
Exception exception,&lt;br /&gt;
bool includeErrorDetail,&lt;br /&gt;
IContentNegotiator negotiator,&lt;br /&gt;
HttpRequestMessage request,&lt;br /&gt;
IEnumerable&amp;lt;MediaTypeFormatter&gt; formatters) :&lt;br /&gt;
base(exception, includeErrorDetail, negotiator, request, formatters)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/// Creates an HttpResponseMessage instance asynchronously.&lt;br /&gt;
/// This method determines how a HttpResponseMessage content will look like.&lt;br /&gt;
public override Task&amp;lt;HttpResponseMessage&gt; ExecuteAsync(CancellationToken cancellationToken)&lt;br /&gt;
{&lt;br /&gt;
var content = new HttpError(Exception, IncludeErrorDetail);&lt;br /&gt;
&lt;br /&gt;
// define an additional content field with name "ErrorID"&lt;br /&gt;
content.Add("ErrorID", Exception.Data["NesterovskyBros:id"] as long?);&lt;br /&gt;
&lt;br /&gt;
var result =&lt;br /&gt;
ContentNegotiator.Negotiate(typeof(HttpError), Request, Formatters);&lt;br /&gt;
&lt;br /&gt;
var message = new HttpResponseMessage&lt;br /&gt;
{&lt;br /&gt;
RequestMessage = Request,&lt;br /&gt;
StatusCode = result == null ?&lt;br /&gt;
HttpStatusCode.NotAcceptable : HttpStatusCode.InternalServerError&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
if (result != null)&lt;br /&gt;
{&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
// serializes the HttpError instance either to JSON or to XML&lt;br /&gt;
// depend on requested by the client MIME type.&lt;br /&gt;
message.Content = new ObjectContent&amp;lt;HttpError&gt;(&lt;br /&gt;
content,&lt;br /&gt;
result.Formatter,&lt;br /&gt;
result.MediaType);&lt;br /&gt;
}&lt;br /&gt;
catch&lt;br /&gt;
{&lt;br /&gt;
message.Dispose();&lt;br /&gt;
&lt;br /&gt;
throw;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return Task.FromResult(message);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
} &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
Last, but not least part of this solution is registration and configuration of the
error logger/handler: 
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; /// WebApi congiguation.&lt;br /&gt;
public static class WebApiConfig&lt;br /&gt;
{&lt;br /&gt;
public static void Register(HttpConfiguration config)&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
// register the exception logger and handler&lt;br /&gt;
config.Services.Add(typeof(IExceptionLogger), new GlobalExceptionLogger());&lt;br /&gt;
config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());&lt;br /&gt;
&lt;br /&gt;
// set error detail policy according with value from Web.config&lt;br /&gt;
var customErrors =&lt;br /&gt;
(CustomErrorsSection)ConfigurationManager.GetSection("system.web/customErrors");&lt;br /&gt;
&lt;br /&gt;
if (customErrors != null)&lt;br /&gt;
{&lt;br /&gt;
switch (customErrors.Mode)&lt;br /&gt;
{&lt;br /&gt;
case CustomErrorsMode.RemoteOnly:&lt;br /&gt;
{&lt;br /&gt;
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;&lt;br /&gt;
&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
case CustomErrorsMode.On:&lt;br /&gt;
{&lt;br /&gt;
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Never;&lt;br /&gt;
&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
case CustomErrorsMode.Off:&lt;br /&gt;
{&lt;br /&gt;
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;&lt;br /&gt;
&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
default:&lt;br /&gt;
{&lt;br /&gt;
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Default;&lt;br /&gt;
&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
} &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
The client-side error handler remain almost untouched. The implementation details
you may find in &lt;code&gt;/Scripts/api/api.js&lt;/code&gt; and &lt;code&gt;Scripts/controls/error.js&lt;/code&gt; files. 
&lt;/p&gt;
&lt;p&gt;
You may &lt;a href="http://www.nesterovsky-bros.com/download/GlobalErrorHandler.zip"&gt;download
the demo project here&lt;/a&gt;. Feel free to use this solution in your .NET projects. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=be57df20-9025-4f1b-849e-5b982df91f7a" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,be57df20-9025-4f1b-849e-5b982df91f7a.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>javascript</category>
      <category>kendoui</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=9d90a7c4-6361-41fd-bd71-dab52eedcad0</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9d90a7c4-6361-41fd-bd71-dab52eedcad0.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,9d90a7c4-6361-41fd-bd71-dab52eedcad0.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=9d90a7c4-6361-41fd-bd71-dab52eedcad0</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
From time to time we run into tasks that we would like to solve in LINQ style but
unfortunately it either cannot be done or a solution is not efficient.
</p>
        <p>
Note that by LINQ style we do not mean C# query expressions (we have a strong distaste
for that syntax) but extension methods defined in <code>System.Linq.Enumerable</code> and
other classes.
</p>
        <p>
Here we quote several extension methods that are good for a general use:
</p>
        <p>
1. Select with predicate. This is shorthand of <code>items.Where(...).Select(...)</code>:
</p>
        <p style="padding-left: 1em">
          <code> /// &lt;summary&gt;<br />
/// Projects each element of a sequence into a new form.<br />
/// &lt;/summary&gt;<br />
/// &lt;typeparam name="T"&gt;A type of elements of source sequence.&lt;/typeparam&gt;<br />
/// &lt;typeparam name="R"&gt;A type of elements of target sequence.&lt;/typeparam&gt;<br />
/// &lt;param name="source"&gt;A source sequence.&lt;/param&gt;<br />
/// &lt;param name="where"&gt;A predicate to filter elements.&lt;/param&gt;<br />
/// &lt;param name="selector"&gt;A result element selector.&lt;/param&gt;<br />
/// &lt;returns&gt;A target sequence.&lt;/returns&gt;<br />
public static IEnumerable&lt;R&gt; Select&lt;T, R&gt;(<br />
this IEnumerable&lt;T&gt; source, 
<br />
Func&lt;T, bool&gt; where, 
<br />
Func&lt;T, R&gt; selector)<br />
{<br />
return source.Where(where).Select(selector);<br />
} </code>
        </p>
        <p>
2. Select with predicate with source element index passed both into the predicate
and into the selector. This one you cannot trivially implement in LINQ:
</p>
        <p style="padding-left: 1em">
          <code> /// &lt;summary&gt;<br />
/// Projects each element of a sequence into a new form.<br />
/// &lt;/summary&gt;<br />
/// &lt;typeparam name="T"&gt;A type of elements of source sequence.&lt;/typeparam&gt;<br />
/// &lt;typeparam name="R"&gt;A type of elements of target sequence.&lt;/typeparam&gt;<br />
/// &lt;param name="source"&gt;A source sequence.&lt;/param&gt;<br />
/// &lt;param name="where"&gt;A predicate to filter elements.&lt;/param&gt;<br />
/// &lt;param name="selector"&gt;A result element selector.&lt;/param&gt;<br />
/// &lt;returns&gt;A target sequence.&lt;/returns&gt;<br />
public static IEnumerable&lt;R&gt; Select&lt;T, R&gt;(<br />
this IEnumerable&lt;T&gt; source,<br />
Func&lt;T, int, bool&gt; where,<br />
Func&lt;T, int, R&gt; selector)<br />
{<br />
var index = 0;<br /><br />
foreach(var value in source)<br />
{<br />
if (where(value, index))<br />
{<br />
yield return selector(value, index);<br />
}<br /><br />
++index;<br />
}<br />
} </code>
        </p>
        <p>
3. A function with output element as projection of a window of input elements. Such
function can be used to get finite difference (operation opposite to a cumulative
sum).
</p>
        <p style="padding-left: 1em">
          <code> /// &lt;summary&gt;<br />
/// Projects a window of source elements in a source sequence into target sequence.<br />
/// Thus<br />
/// target[i] = 
<br />
/// selector(source[i], source[i - 1], ... source[i - window + 1])<br />
/// &lt;/summary&gt;<br />
/// &lt;typeparam name="T"&gt;A type of elements of source sequence.&lt;/typeparam&gt;<br />
/// &lt;typeparam name="R"&gt;A type of elements of target sequence.&lt;/typeparam&gt;<br />
/// &lt;param name="source"&gt;A source sequence.&lt;/param&gt;<br />
/// &lt;param name="window"&gt;A size of window.&lt;/param&gt;<br />
/// &lt;param name="lookbehind"&gt;<br />
/// Indicate whether to produce target if the number of source elements 
<br />
/// preceeding the current is less than the window size.<br />
/// &lt;/param&gt;<br />
/// &lt;param name="lookahead"&gt;<br />
/// Indicate whether to produce target if the number of source elements 
<br />
/// following current is less than the window size.<br />
/// &lt;/param&gt;<br />
/// &lt;param name="selector"&gt;<br />
/// A selector that derives target element.<br />
/// On input it receives:<br />
/// an array of source elements stored in round-robing fashon;<br />
/// an index of the first element;<br />
/// a number of elements in the array to count.<br />
/// &lt;/param&gt;<br />
/// &lt;returns&gt;Returns a sequence of target elements.&lt;/returns&gt;<br />
public static IEnumerable&lt;R&gt; Window&lt;T, R&gt;(<br />
this IEnumerable&lt;T&gt; source,<br />
int window,<br />
bool lookbehind,<br />
bool lookahead,<br />
Func&lt;T[], int, int, R&gt; selector)<br />
{<br />
var buffer = new T[window];<br />
var index = 0;<br />
var count = 0;<br /><br />
foreach(var value in source)<br />
{<br />
if (count &lt; window)<br />
{<br />
buffer[count++] = value;<br /><br />
if (lookbehind || (count == window))<br />
{<br />
yield return selector(buffer, 0, count);<br />
}<br />
}<br />
else<br />
{<br />
buffer[index] = value;<br />
index = index + 1 == window ? 0 : index + 1;<br /><br />
yield return selector(buffer, index, count);<br />
}<br />
}<br /><br />
if (lookahead)<br />
{<br />
while(--count &gt; 0)<br />
{<br />
index = index + 1 == window ? 0 : index + 1;<br /><br />
yield return selector(buffer, index, count);<br />
}<br />
}<br />
} </code>
        </p>
        <p>
This way a finite difference looks like this:
</p>
        <p style="padding-left: 1em">
          <code> var diff = input.Window(<br />
2,<br />
false,<br />
false,<br />
(buffer, index, count) =&gt; buffer[index ^ 1] - buffer[index]); </code>
        </p>
        <p>
4. A specialization of <code>Window</code> method that returns a enumeration of windows:
</p>
        <p style="padding-left: 1em">
          <code> /// &lt;summary&gt;<br />
/// Projects a window of source elements in a source sequence into a<br />
/// sequence of window arrays.<br />
/// &lt;/summary&gt;<br />
/// &lt;typeparam name="T"&gt;A type of elements of source sequence.&lt;/typeparam&gt;<br />
/// &lt;typeparam name="R"&gt;A type of elements of target sequence.&lt;/typeparam&gt;<br />
/// &lt;param name="source"&gt;A source sequence.&lt;/param&gt;<br />
/// &lt;param name="window"&gt;A size of window.&lt;/param&gt;<br />
/// &lt;param name="lookbehind"&gt;<br />
/// Indicate whether to produce target if the number of source elements 
<br />
/// preceeding the current is less than the window size.<br />
/// &lt;/param&gt;<br />
/// &lt;param name="lookahead"&gt;<br />
/// Indicate whether to produce target if the number of source elements 
<br />
/// following current is less than the window size.<br />
/// &lt;/param&gt;<br />
/// &lt;returns&gt;Returns a sequence of windows.&lt;/returns&gt;<br />
public static IEnumerable&lt;T[]&gt; Window&lt;T&gt;(<br />
this IEnumerable&lt;T&gt; source,<br />
int window,<br />
bool lookbehind,<br />
bool lookahead)<br />
{<br />
return source.Window(<br />
window,<br />
lookbehind,<br />
lookahead,<br />
(buffer, index, count) =&gt;<br />
{<br />
var result = new T[count];<br /><br />
for(var i = 0; i &lt; count; ++i)<br />
{<br />
result[i] = buffer[index];<br />
index = index + 1 == buffer.Length ? 0 : index + 1;<br />
}<br /><br />
return result;<br />
});<br />
}</code>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9d90a7c4-6361-41fd-bd71-dab52eedcad0" />
      </body>
      <title>LINQ extensions</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,9d90a7c4-6361-41fd-bd71-dab52eedcad0.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/03/07/LINQExtensions.aspx</link>
      <pubDate>Fri, 07 Mar 2014 14:11:46 GMT</pubDate>
      <description>    &lt;p&gt;
From time to time we run into tasks that we would like to solve in LINQ style but
unfortunately it either cannot be done or a solution is not efficient.
&lt;/p&gt;
&lt;p&gt;
Note that by LINQ style we do not mean C# query expressions (we have a strong distaste
for that syntax) but extension methods defined in &lt;code&gt;System.Linq.Enumerable&lt;/code&gt; and
other classes.
&lt;/p&gt;
&lt;p&gt;
Here we quote several extension methods that are good for a general use:
&lt;/p&gt;
&lt;p&gt;
1. Select with predicate. This is shorthand of &lt;code&gt;items.Where(...).Select(...)&lt;/code&gt;:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; /// &amp;lt;summary&gt;&lt;br /&gt;
/// Projects each element of a sequence into a new form.&lt;br /&gt;
/// &amp;lt;/summary&gt;&lt;br /&gt;
/// &amp;lt;typeparam name="T"&gt;A type of elements of source sequence.&amp;lt;/typeparam&gt;&lt;br /&gt;
/// &amp;lt;typeparam name="R"&gt;A type of elements of target sequence.&amp;lt;/typeparam&gt;&lt;br /&gt;
/// &amp;lt;param name="source"&gt;A source sequence.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="where"&gt;A predicate to filter elements.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="selector"&gt;A result element selector.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;returns&gt;A target sequence.&amp;lt;/returns&gt;&lt;br /&gt;
public static IEnumerable&amp;lt;R&gt; Select&amp;lt;T, R&gt;(&lt;br /&gt;
this IEnumerable&amp;lt;T&gt; source, 
&lt;br /&gt;
Func&amp;lt;T, bool&gt; where, 
&lt;br /&gt;
Func&amp;lt;T, R&gt; selector)&lt;br /&gt;
{&lt;br /&gt;
return source.Where(where).Select(selector);&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
2. Select with predicate with source element index passed both into the predicate
and into the selector. This one you cannot trivially implement in LINQ:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; /// &amp;lt;summary&gt;&lt;br /&gt;
/// Projects each element of a sequence into a new form.&lt;br /&gt;
/// &amp;lt;/summary&gt;&lt;br /&gt;
/// &amp;lt;typeparam name="T"&gt;A type of elements of source sequence.&amp;lt;/typeparam&gt;&lt;br /&gt;
/// &amp;lt;typeparam name="R"&gt;A type of elements of target sequence.&amp;lt;/typeparam&gt;&lt;br /&gt;
/// &amp;lt;param name="source"&gt;A source sequence.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="where"&gt;A predicate to filter elements.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="selector"&gt;A result element selector.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;returns&gt;A target sequence.&amp;lt;/returns&gt;&lt;br /&gt;
public static IEnumerable&amp;lt;R&gt; Select&amp;lt;T, R&gt;(&lt;br /&gt;
this IEnumerable&amp;lt;T&gt; source,&lt;br /&gt;
Func&amp;lt;T, int, bool&gt; where,&lt;br /&gt;
Func&amp;lt;T, int, R&gt; selector)&lt;br /&gt;
{&lt;br /&gt;
var index = 0;&lt;br /&gt;
&lt;br /&gt;
foreach(var value in source)&lt;br /&gt;
{&lt;br /&gt;
if (where(value, index))&lt;br /&gt;
{&lt;br /&gt;
yield return selector(value, index);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
++index;&lt;br /&gt;
}&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
3. A function with output element as projection of a window of input elements. Such
function can be used to get finite difference (operation opposite to a cumulative
sum).
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; /// &amp;lt;summary&gt;&lt;br /&gt;
/// Projects a window of source elements in a source sequence into target sequence.&lt;br /&gt;
/// Thus&lt;br /&gt;
/// target[i] = 
&lt;br /&gt;
/// selector(source[i], source[i - 1], ... source[i - window + 1])&lt;br /&gt;
/// &amp;lt;/summary&gt;&lt;br /&gt;
/// &amp;lt;typeparam name="T"&gt;A type of elements of source sequence.&amp;lt;/typeparam&gt;&lt;br /&gt;
/// &amp;lt;typeparam name="R"&gt;A type of elements of target sequence.&amp;lt;/typeparam&gt;&lt;br /&gt;
/// &amp;lt;param name="source"&gt;A source sequence.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="window"&gt;A size of window.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="lookbehind"&gt;&lt;br /&gt;
/// Indicate whether to produce target if the number of source elements 
&lt;br /&gt;
/// preceeding the current is less than the window size.&lt;br /&gt;
/// &amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="lookahead"&gt;&lt;br /&gt;
/// Indicate whether to produce target if the number of source elements 
&lt;br /&gt;
/// following current is less than the window size.&lt;br /&gt;
/// &amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="selector"&gt;&lt;br /&gt;
/// A selector that derives target element.&lt;br /&gt;
/// On input it receives:&lt;br /&gt;
/// an array of source elements stored in round-robing fashon;&lt;br /&gt;
/// an index of the first element;&lt;br /&gt;
/// a number of elements in the array to count.&lt;br /&gt;
/// &amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;returns&gt;Returns a sequence of target elements.&amp;lt;/returns&gt;&lt;br /&gt;
public static IEnumerable&amp;lt;R&gt; Window&amp;lt;T, R&gt;(&lt;br /&gt;
this IEnumerable&amp;lt;T&gt; source,&lt;br /&gt;
int window,&lt;br /&gt;
bool lookbehind,&lt;br /&gt;
bool lookahead,&lt;br /&gt;
Func&amp;lt;T[], int, int, R&gt; selector)&lt;br /&gt;
{&lt;br /&gt;
var buffer = new T[window];&lt;br /&gt;
var index = 0;&lt;br /&gt;
var count = 0;&lt;br /&gt;
&lt;br /&gt;
foreach(var value in source)&lt;br /&gt;
{&lt;br /&gt;
if (count &amp;lt; window)&lt;br /&gt;
{&lt;br /&gt;
buffer[count++] = value;&lt;br /&gt;
&lt;br /&gt;
if (lookbehind || (count == window))&lt;br /&gt;
{&lt;br /&gt;
yield return selector(buffer, 0, count);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
buffer[index] = value;&lt;br /&gt;
index = index + 1 == window ? 0 : index + 1;&lt;br /&gt;
&lt;br /&gt;
yield return selector(buffer, index, count);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (lookahead)&lt;br /&gt;
{&lt;br /&gt;
while(--count &gt; 0)&lt;br /&gt;
{&lt;br /&gt;
index = index + 1 == window ? 0 : index + 1;&lt;br /&gt;
&lt;br /&gt;
yield return selector(buffer, index, count);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
This way a finite difference looks like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; var diff = input.Window(&lt;br /&gt;
2,&lt;br /&gt;
false,&lt;br /&gt;
false,&lt;br /&gt;
(buffer, index, count) =&gt; buffer[index ^ 1] - buffer[index]); &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
4. A specialization of &lt;code&gt;Window&lt;/code&gt; method that returns a enumeration of windows:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; /// &amp;lt;summary&gt;&lt;br /&gt;
/// Projects a window of source elements in a source sequence into a&lt;br /&gt;
/// sequence of window arrays.&lt;br /&gt;
/// &amp;lt;/summary&gt;&lt;br /&gt;
/// &amp;lt;typeparam name="T"&gt;A type of elements of source sequence.&amp;lt;/typeparam&gt;&lt;br /&gt;
/// &amp;lt;typeparam name="R"&gt;A type of elements of target sequence.&amp;lt;/typeparam&gt;&lt;br /&gt;
/// &amp;lt;param name="source"&gt;A source sequence.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="window"&gt;A size of window.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="lookbehind"&gt;&lt;br /&gt;
/// Indicate whether to produce target if the number of source elements 
&lt;br /&gt;
/// preceeding the current is less than the window size.&lt;br /&gt;
/// &amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;param name="lookahead"&gt;&lt;br /&gt;
/// Indicate whether to produce target if the number of source elements 
&lt;br /&gt;
/// following current is less than the window size.&lt;br /&gt;
/// &amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;returns&gt;Returns a sequence of windows.&amp;lt;/returns&gt;&lt;br /&gt;
public static IEnumerable&amp;lt;T[]&gt; Window&amp;lt;T&gt;(&lt;br /&gt;
this IEnumerable&amp;lt;T&gt; source,&lt;br /&gt;
int window,&lt;br /&gt;
bool lookbehind,&lt;br /&gt;
bool lookahead)&lt;br /&gt;
{&lt;br /&gt;
return source.Window(&lt;br /&gt;
window,&lt;br /&gt;
lookbehind,&lt;br /&gt;
lookahead,&lt;br /&gt;
(buffer, index, count) =&gt;&lt;br /&gt;
{&lt;br /&gt;
var result = new T[count];&lt;br /&gt;
&lt;br /&gt;
for(var i = 0; i &amp;lt; count; ++i)&lt;br /&gt;
{&lt;br /&gt;
result[i] = buffer[index];&lt;br /&gt;
index = index + 1 == buffer.Length ? 0 : index + 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return result;&lt;br /&gt;
});&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=9d90a7c4-6361-41fd-bd71-dab52eedcad0" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,9d90a7c4-6361-41fd-bd71-dab52eedcad0.aspx</comments>
      <category>.NET</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=ab2110fa-c136-4b6b-bfdc-aa0dce059a97</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,ab2110fa-c136-4b6b-bfdc-aa0dce059a97.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,ab2110fa-c136-4b6b-bfdc-aa0dce059a97.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=ab2110fa-c136-4b6b-bfdc-aa0dce059a97</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Consider how would you implement Style object in the HTML DOM?
</p>
        <p>
These are some characteristics of that object:
</p>
        <ul>
          <li>
It has a long list of properties, e.g. in IE 11 there are more than 300 properties
over a style object.</li>
          <li>
Any specific instance usually have only several properties assigned.</li>
          <li>
Reads of properties are much more frequent than writes. In fact style often stays
unchanged after initialization.</li>
          <li>
DOM contains many style instances (often thousands).</li>
          <li>
The number of distinct instances in terms of values of properties is moderate (usually
dozens).</li>
        </ul>
        <p>
Here is how would we approached to such an object.
</p>
        <p>
1. Styles are sparse objects, thus there is no point to implement plain class with
all those properties, as it's wasteful.
</p>
        <p>
We would rather use two techniques to keep style's state:
</p>
        <ul>
          <li>
A dictionary of properties with their values;</li>
          <li>
An aggregation of objects, where all properies are grouped into families, each group
is defined by a separate type, and a style's state is an aggregation of that groups.</li>
        </ul>
        <p>
A current style of an element is an aggregation of styles of ancestor element. It
can either by dynamic or be fused into a single style instance.
</p>
        <p>
2. Make style's state immutable, and share all these states among all style instances.
</p>
        <p>
In this implementation property write turns into a state transition operation: <code>state
= set(state, property, value)</code>. Thus no state is modified but replaced with
other state that corresponds to a required change.
</p>
        <p>
If state is seen as a dictionary then API may look like this :
</p>
        <p style="padding-left: 1em">
          <code> public class State&lt;K, V&gt;<br />
{<br />
// Gets shared dictionary for an input dictionary.<br />
public IDictionary&lt;K, V&gt; Get(IDictionary&lt;K, V&gt; dictionary);<br /><br />
// Gets a shared dictionary for an input dictionary with key set to a value. 
<br />
public IDictionary&lt;K, V&gt; Set(IDictionary&lt;K, V&gt; dictionary, K key, V value);<br /><br />
// Gets a shared dictionary for an input dictionary.<br />
public IDictionary&lt;K, V&gt; Remove(IDictionary&lt;K, V&gt; dictionary, K key);<br /><br />
// Gets typed value.<br />
public T Get&lt;T&gt;(IDictionary&lt;K, V&gt; dictionary, K key)<br />
where T: V<br />
{<br />
V value;<br /><br />
if ((dictionary == null) || !dictionary.TryGetValue(key, out value))<br />
{<br />
return default(T);<br />
}<br /><br />
return (T)value;<br />
}<br /><br />
// Sets or removes a typed value.<br />
// dictionary can be null.<br />
// null returned if output dictionary would be empty. 
<br />
public IDictionary&lt;K, V&gt; Set&lt;T&gt;(IDictionary&lt;K, V&gt; dictionary, K key, T value)<br />
where T : V<br />
{<br />
return value == null ? Remove(dictionary, key) : 
<br />
Set(dictionary, key, (V)value);<br />
}<br />
}</code>
        </p>
        <p>
States can be cached. Provided the cache keeps states in a weak way, no unsued state
will be stored for a long time. We may use weak table of dictionary to dictionary <code>WeakTable&lt;Dictionary&lt;K,
V&gt;, Dictionary&lt;K, V&gt;&gt;</code> as a storage for such a cache. All required
API is described in the <a href="http://www.nesterovsky-bros.com/weblog/2014/01/08/WeakTable.aspx">WeakTable</a> and <a href="http://www.nesterovsky-bros.com/weblog/2014/01/13/HashCodeOfDictionary.aspx">Hash
Code of Dictionary</a> posts.
</p>
        <p>
3. Style can be implemented as a structure with shared state as a storage. Here is
a scetch:
</p>
        <p style="padding-left: 1em">
          <code>[Serializable]<br />
public struct Style<br />
{<br />
// All properties.<br />
public enum Property<br />
{<br />
Background,<br />
BorderColor,<br />
BorderStyle,<br />
Color,<br />
FontFamily,<br />
FontSize,<br />
// ...<br />
}<br /><br />
public int? Background<br />
{<br />
get { return states.Get&lt;int?&gt;(state, Property.Background); }<br />
set { state = states.Set(state, Property.Background, value); }<br />
}<br /><br />
public int? BorderColor<br />
{<br />
get { return states.Get&lt;int?&gt;(state, Property.BorderColor); }<br />
set { state = states.Set(state, Property.BorderColor, value); }<br />
}<br /><br />
public string BorderStyle<br />
{<br />
get { return states.Get&lt;string&gt;(state, Property.BorderStyle); }<br />
set { state = states.Set(state, Property.BorderStyle, value); }<br />
}<br /><br />
public int? Color<br />
{<br />
get { return states.Get&lt;int?&gt;(state, Property.Color); }<br />
set { state = states.Set(state, Property.Color, value); }<br />
}<br /><br />
public string FontFamily<br />
{<br />
get { return states.Get&lt;string&gt;(state, Property.FontFamily); }<br />
set { state = states.Set(state, Property.FontFamily, value); }<br />
}<br /><br />
public double? FontSize<br />
{<br />
get { return states.Get&lt;double?&gt;(state, Property.FontSize); }<br />
set { state = states.Set(state, Property.FontSize, value); }<br />
}<br /><br />
// ...<br /><br />
[OnDeserialized]<br />
private void OnDeserialized(StreamingContext context)<br />
{<br />
state = states.Get(state);<br />
}<br /><br />
// A state.<br />
private IDictionary&lt;Property, object&gt; state;<br /><br />
// A states cache.<br />
private static readonly State&lt;Property, object&gt; states =<br />
new State&lt;Property, object&gt;();<br />
} </code>
        </p>
        <p>
Note that:
</p>
        <ul>
          <li>
default state is a <code>null</code> dictionary;</li>
          <li>
states are application wide shared.</li>
        </ul>
        <p>
The following link is our implementation of <code><a href="http://www.nesterovsky-bros.com/download/State.cs.txt">State&lt;K,
V&gt;</a></code> class: <a href="http://www.nesterovsky-bros.com/download/State.cs.txt">State.cs</a>.
</p>
        <p>
        </p>
        <p>
Here we have outlined the idea of shared state object, and how it can be applied to
sparse mostly immutable objects. We used HTML style as an example of such an object.
Shared state object may work in many other areas, but for it to shine its use case
should fit to the task.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=ab2110fa-c136-4b6b-bfdc-aa0dce059a97" />
      </body>
      <title>Shared state</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,ab2110fa-c136-4b6b-bfdc-aa0dce059a97.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/01/22/SharedState.aspx</link>
      <pubDate>Wed, 22 Jan 2014 19:43:25 GMT</pubDate>
      <description>    &lt;p&gt;
Consider how would you implement Style object in the HTML DOM?
&lt;/p&gt;
&lt;p&gt;
These are some characteristics of that object:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
It has a long list of properties, e.g. in IE 11 there are more than 300 properties
over a style object.&lt;/li&gt;
&lt;li&gt;
Any specific instance usually have only several properties assigned.&lt;/li&gt;
&lt;li&gt;
Reads of properties are much more frequent than writes. In fact style often stays
unchanged after initialization.&lt;/li&gt;
&lt;li&gt;
DOM contains many style instances (often thousands).&lt;/li&gt;
&lt;li&gt;
The number of distinct instances in terms of values of properties is moderate (usually
dozens).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Here is how would we approached to such an object.
&lt;/p&gt;
&lt;p&gt;
1. Styles are sparse objects, thus there is no point to implement plain class with
all those properties, as it&amp;#39;s wasteful.
&lt;/p&gt;
&lt;p&gt;
We would rather use two techniques to keep style&amp;#39;s state:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
A dictionary of properties with their values;&lt;/li&gt;
&lt;li&gt;
An aggregation of objects, where all properies are grouped into families, each group
is defined by a separate type, and a style&amp;#39;s state is an aggregation of that groups.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
A current style of an element is an aggregation of styles of ancestor element. It
can either by dynamic or be fused into a single style instance.
&lt;/p&gt;
&lt;p&gt;
2. Make style&amp;#39;s state immutable, and share all these states among all style instances.
&lt;/p&gt;
&lt;p&gt;
In this implementation property write turns into a state transition operation: &lt;code&gt;state
= set(state, property, value)&lt;/code&gt;. Thus no state is modified but replaced with
other state that corresponds to a required change.
&lt;/p&gt;
&lt;p&gt;
If state is seen as a dictionary then API may look like this :
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; public class State&amp;lt;K, V&gt;&lt;br /&gt;
{&lt;br /&gt;
// Gets shared dictionary for an input dictionary.&lt;br /&gt;
public IDictionary&amp;lt;K, V&gt; Get(IDictionary&amp;lt;K, V&gt; dictionary);&lt;br /&gt;
&lt;br /&gt;
// Gets a shared dictionary for an input dictionary with key set to a value. 
&lt;br /&gt;
public IDictionary&amp;lt;K, V&gt; Set(IDictionary&amp;lt;K, V&gt; dictionary, K key, V value);&lt;br /&gt;
&lt;br /&gt;
// Gets a shared dictionary for an input dictionary.&lt;br /&gt;
public IDictionary&amp;lt;K, V&gt; Remove(IDictionary&amp;lt;K, V&gt; dictionary, K key);&lt;br /&gt;
&lt;br /&gt;
// Gets typed value.&lt;br /&gt;
public T Get&amp;lt;T&gt;(IDictionary&amp;lt;K, V&gt; dictionary, K key)&lt;br /&gt;
where T: V&lt;br /&gt;
{&lt;br /&gt;
V value;&lt;br /&gt;
&lt;br /&gt;
if ((dictionary == null) || !dictionary.TryGetValue(key, out value))&lt;br /&gt;
{&lt;br /&gt;
return default(T);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return (T)value;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sets or removes a typed value.&lt;br /&gt;
// dictionary can be null.&lt;br /&gt;
// null returned if output dictionary would be empty. 
&lt;br /&gt;
public IDictionary&amp;lt;K, V&gt; Set&amp;lt;T&gt;(IDictionary&amp;lt;K, V&gt; dictionary, K key, T value)&lt;br /&gt;
where T : V&lt;br /&gt;
{&lt;br /&gt;
return value == null ? Remove(dictionary, key) : 
&lt;br /&gt;
Set(dictionary, key, (V)value);&lt;br /&gt;
}&lt;br /&gt;
}&lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
States can be cached. Provided the cache keeps states in a weak way, no unsued state
will be stored for a long time. We may use weak table of dictionary to dictionary &lt;code&gt;WeakTable&amp;lt;Dictionary&amp;lt;K,
V&amp;gt;, Dictionary&amp;lt;K, V&amp;gt;&amp;gt;&lt;/code&gt; as a storage for such a cache. All required
API is described in the &lt;a href="http://www.nesterovsky-bros.com/weblog/2014/01/08/WeakTable.aspx"&gt;WeakTable&lt;/a&gt; and &lt;a href="http://www.nesterovsky-bros.com/weblog/2014/01/13/HashCodeOfDictionary.aspx"&gt;Hash
Code of Dictionary&lt;/a&gt; posts.
&lt;/p&gt;
&lt;p&gt;
3. Style can be implemented as a structure with shared state as a storage. Here is
a scetch:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;[Serializable]&lt;br /&gt;
public struct Style&lt;br /&gt;
{&lt;br /&gt;
// All properties.&lt;br /&gt;
public enum Property&lt;br /&gt;
{&lt;br /&gt;
Background,&lt;br /&gt;
BorderColor,&lt;br /&gt;
BorderStyle,&lt;br /&gt;
Color,&lt;br /&gt;
FontFamily,&lt;br /&gt;
FontSize,&lt;br /&gt;
// ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public int? Background&lt;br /&gt;
{&lt;br /&gt;
get { return states.Get&amp;lt;int?&gt;(state, Property.Background); }&lt;br /&gt;
set { state = states.Set(state, Property.Background, value); }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public int? BorderColor&lt;br /&gt;
{&lt;br /&gt;
get { return states.Get&amp;lt;int?&gt;(state, Property.BorderColor); }&lt;br /&gt;
set { state = states.Set(state, Property.BorderColor, value); }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public string BorderStyle&lt;br /&gt;
{&lt;br /&gt;
get { return states.Get&amp;lt;string&gt;(state, Property.BorderStyle); }&lt;br /&gt;
set { state = states.Set(state, Property.BorderStyle, value); }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public int? Color&lt;br /&gt;
{&lt;br /&gt;
get { return states.Get&amp;lt;int?&gt;(state, Property.Color); }&lt;br /&gt;
set { state = states.Set(state, Property.Color, value); }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public string FontFamily&lt;br /&gt;
{&lt;br /&gt;
get { return states.Get&amp;lt;string&gt;(state, Property.FontFamily); }&lt;br /&gt;
set { state = states.Set(state, Property.FontFamily, value); }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public double? FontSize&lt;br /&gt;
{&lt;br /&gt;
get { return states.Get&amp;lt;double?&gt;(state, Property.FontSize); }&lt;br /&gt;
set { state = states.Set(state, Property.FontSize, value); }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// ...&lt;br /&gt;
&lt;br /&gt;
[OnDeserialized]&lt;br /&gt;
private void OnDeserialized(StreamingContext context)&lt;br /&gt;
{&lt;br /&gt;
state = states.Get(state);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// A state.&lt;br /&gt;
private IDictionary&amp;lt;Property, object&gt; state;&lt;br /&gt;
&lt;br /&gt;
// A states cache.&lt;br /&gt;
private static readonly State&amp;lt;Property, object&gt; states =&lt;br /&gt;
new State&amp;lt;Property, object&gt;();&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Note that:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
default state is a &lt;code&gt;null&lt;/code&gt; dictionary;&lt;/li&gt;
&lt;li&gt;
states are application wide shared.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The following link is our implementation of &lt;code&gt;&lt;a href="http://www.nesterovsky-bros.com/download/State.cs.txt"&gt;State&amp;lt;K,
V&amp;gt;&lt;/a&gt;&lt;/code&gt; class: &lt;a href="http://www.nesterovsky-bros.com/download/State.cs.txt"&gt;State.cs&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Here we have outlined the idea of shared state object, and how it can be applied to
sparse mostly immutable objects. We used HTML style as an example of such an object.
Shared state object may work in many other areas, but for it to shine its use case
should fit to the task.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=ab2110fa-c136-4b6b-bfdc-aa0dce059a97" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,ab2110fa-c136-4b6b-bfdc-aa0dce059a97.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=bc06427b-87e2-4e14-9ebb-1a186f875e3a</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,bc06427b-87e2-4e14-9ebb-1a186f875e3a.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,bc06427b-87e2-4e14-9ebb-1a186f875e3a.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=bc06427b-87e2-4e14-9ebb-1a186f875e3a</wfw:commentRss>
      <title>Hash Code of Dictionary</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,bc06427b-87e2-4e14-9ebb-1a186f875e3a.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/01/13/HashCodeOfDictionary.aspx</link>
      <pubDate>Mon, 13 Jan 2014 20:33:31 GMT</pubDate>
      <description>    &lt;p&gt;
Dealing recently with some task (the same that inspired us to implement &lt;a href="http://www.nesterovsky-bros.com/weblog/2014/01/08/WeakTable.aspx"&gt;WeakTable&lt;/a&gt;),
we were in a position to use a dictionary as a key in another dictionary.
&lt;/p&gt;
&lt;p&gt;
What are the rules for the class to be used as key:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
key should be immutable;&lt;/li&gt;
&lt;li&gt;
key should implement a &lt;code&gt;GetHashCode()&lt;/code&gt; method;&lt;/li&gt;
&lt;li&gt;
key should implement a &lt;code&gt;Equals()&lt;/code&gt; method.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The first requirement is usually implemented as a documentation contract like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em; font-style: italic"&gt;
As long as an object is used as a key in the &lt;code&gt;Dictionary&amp;lt;TKey, TValue&gt;&lt;/code&gt;,
it must not change in any way that affects its hash value.
&lt;/p&gt;
&lt;p&gt;
The third requirement about equals is trivially implemented as a method:
&lt;/p&gt;
&lt;p style="padding-left: 1em;"&gt;
&lt;code&gt; public bool Equals(IDictionary&amp;lt;K, V&gt; x, IDictionary&amp;lt;K, V&gt; y)&lt;br /&gt;
{&lt;br /&gt;
if (x == y)&lt;br /&gt;
{&lt;br /&gt;
return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if ((x == null) || (y == null) || (x.Count != y.Count))&lt;br /&gt;
{&lt;br /&gt;
return false;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
foreach(var entry in x)&lt;br /&gt;
{&lt;br /&gt;
V value;&lt;br /&gt;
&lt;br /&gt;
if (!y.TryGetValue(entry.Key, out value) ||&lt;br /&gt;
!valueComparer.Equals(entry.Value, value))&lt;br /&gt;
{&lt;br /&gt;
return false;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return true;&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
But how would you implement hash code?
&lt;/p&gt;
&lt;p&gt;
We argued like this.
&lt;/p&gt;
&lt;p&gt;
1. Let&amp;#39;s consider the dictionary as a sparse array of values with only populated
items that correspond to key hash codes.
&lt;/p&gt;
&lt;p&gt;
2. Hash code is constructed using some fair algorithm. E.g like that used in java
to calculate &lt;a href="http://en.wikipedia.org/wiki/Java_hashCode()"&gt;string&amp;#39;s hash
code&lt;/a&gt;:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; n-1&lt;br /&gt;
h(s) = SUM (s[i]*p^(n-1-i)) mod m, where m = 2^31&lt;br /&gt;
i=0&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
In our case:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt; can be arbitrary large &lt;code&gt;int&lt;/code&gt; value, so in fact it&amp;#39;s &lt;code&gt;2^32&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
items are enumerated in unknown order;&lt;/li&gt;
&lt;li&gt;
there is only limited set of items, so most &lt;code&gt;s[i]&lt;/code&gt; are zeros.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
As result we cannot use recurrent function to calculate a power &lt;code&gt;p^k mod m.&lt;/code&gt; Fortunately
one can build fast exponentiation arguing like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; 32/s - 1&lt;br /&gt;
p^k = p^ SUM 2^((s*i)*k[i]) mod m, where s some int: 1, 2, 4, 8, 16, or 32.&lt;br /&gt;
i=0&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Thus
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; 32/s - 1&lt;br /&gt;
p^k = PRODUCT (p^(2^(s*i)))^k[i] mod m&lt;br /&gt;
i=0&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
If &lt;code&gt;s = 1&lt;/code&gt; then &lt;code&gt;k[i]&lt;/code&gt; is either &lt;code&gt;1&lt;/code&gt; or &lt;code&gt;0&lt;/code&gt; (a
bit), and there is &lt;code&gt;32&lt;/code&gt; different &lt;code&gt;p^(2^i) mod m&lt;/code&gt; values, which
can be precalculated.
&lt;/p&gt;
&lt;p&gt;
On the other hand, if we select &lt;code&gt;s = 8&lt;/code&gt; we can write the formula as:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;p^k = p^k[0] * (p^(2^8))^k[1] * (p^(2^16))^k[2] * (p^(2^24))^k[3] mod m&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
where &lt;code&gt;k[i]&lt;/code&gt; is a &lt;code&gt;8&lt;/code&gt; bit value (byte). 
&lt;/p&gt;
&lt;p style="direction: ltr"&gt;
Precalculating all values &lt;code&gt;p^n, (p^(2^8))^n&lt;/code&gt;, &lt;code&gt;(p^(2^16))^n&lt;/code&gt;, &lt;code&gt;(p^(2^24))^n&lt;/code&gt; for &lt;code&gt;n&lt;/code&gt; in &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;255&lt;/code&gt; we
reach the formula with &lt;code&gt;4&lt;/code&gt; multiplications and with &lt;code&gt;1024&lt;/code&gt; precalculated
values.
&lt;/p&gt;
&lt;p&gt;
Here is the whole utility to calculate hash factors:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; /// &amp;lt;summary&gt;&lt;br /&gt;
/// Hash utilities.&lt;br /&gt;
/// &amp;lt;/summary&gt;&lt;br /&gt;
public class Hash&lt;br /&gt;
{ 
&lt;br /&gt;
/// &amp;lt;summary&gt;&lt;br /&gt;
/// Returns a P^value mod 2^31, where P is hash base.&lt;br /&gt;
/// &amp;lt;/summary&gt;&lt;br /&gt;
/// &amp;lt;param name="value"&gt;A value to get hash factor for.&amp;lt;/param&gt;&lt;br /&gt;
/// &amp;lt;returns&gt;A hash factor value.&amp;lt;/returns&gt;&lt;br /&gt;
public static int GetHashFactor(int value)&lt;br /&gt;
{&lt;br /&gt;
return factors[(uint)value &amp; 0xff] *&lt;br /&gt;
factors[(((uint)value &gt;&gt; 8) &amp; 0xff) | 0x100] *&lt;br /&gt;
factors[(((uint)value &gt;&gt; 16) &amp; 0xff) | 0x200] *&lt;br /&gt;
factors[(((uint)value &gt;&gt; 24) &amp; 0xff) | 0x300];&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/// &amp;lt;summary&gt;&lt;br /&gt;
/// Initializes hash factors.&lt;br /&gt;
/// &amp;lt;/summary&gt;&lt;br /&gt;
static Hash()&lt;br /&gt;
{&lt;br /&gt;
var values = new int[4 * 256];&lt;br /&gt;
var value = P;&lt;br /&gt;
var current = 1;&lt;br /&gt;
var i = 0;&lt;br /&gt;
&lt;br /&gt;
do&lt;br /&gt;
{&lt;br /&gt;
values[i++] = current;&lt;br /&gt;
current *= value;&lt;br /&gt;
}&lt;br /&gt;&lt; 256);&lt;br /&gt;
while(i 
&lt;br /&gt;
value = current;&lt;br /&gt;
current = 1;&lt;br /&gt;
&lt;br /&gt;
do&lt;br /&gt;
{&lt;br /&gt;
values[i++] = current;&lt;br /&gt;
current *= value;&lt;br /&gt;
}&lt;br /&gt;&lt; 512);&lt;br /&gt;
while(i 
&lt;br /&gt;
value = current;&lt;br /&gt;
current = 1;&lt;br /&gt;
&lt;br /&gt;
do&lt;br /&gt;
{&lt;br /&gt;
values[i++] = current;&lt;br /&gt;
current *= value;&lt;br /&gt;
}&lt;br /&gt;&lt; 768);&lt;br /&gt;
while(i 
&lt;br /&gt;
value = current;&lt;br /&gt;
current = 1;&lt;br /&gt;
&lt;br /&gt;
do&lt;br /&gt;
{&lt;br /&gt;
values[i++] = current;&lt;br /&gt;
current *= value;&lt;br /&gt;
}&lt;br /&gt;&lt; 1024);&lt;br /&gt;
while(i 
&lt;br /&gt;
factors = values;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/// &amp;lt;summary&gt;&lt;br /&gt;
/// A base to calculate hash factors.&lt;br /&gt;
/// &amp;lt;/summary&gt;&lt;br /&gt;
public const int P = 1103515245;&lt;br /&gt;
&lt;br /&gt;
/// &amp;lt;summary&gt;&lt;br /&gt;
/// Hash factors.&lt;br /&gt;
/// &amp;lt;/summary&gt;&lt;br /&gt;
private static readonly int[] factors;&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
With this API hash code for a dictionary is a trivial operation:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; public int GetHashCode(IDictionary&amp;lt;K, V&gt; dictionary)&lt;br /&gt;
{&lt;br /&gt;
if (dictionary == null)&lt;br /&gt;
{&lt;br /&gt;
return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var result = 0;&lt;br /&gt;
&lt;br /&gt;
foreach(var entry in dictionary)&lt;br /&gt;
{&lt;br /&gt;
if ((entry.Key == null) || (entry.Value == null))&lt;br /&gt;
{&lt;br /&gt;
continue;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
result += Hash.GetHashFactor(keyComparer.GetHashCode(entry.Key)) *&lt;br /&gt;
valueComparer.GetHashCode(entry.Value);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return result;&lt;br /&gt;
} &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
And finally, here is a reference to a class &lt;code&gt;&lt;a href="http://nesterovsky-bros.com/download/DictionaryEqualityComparer.cs.txt"&gt;DictionaryEqualityComparer&amp;lt;K,
V&gt;&lt;/a&gt;: IEqualityComparer&amp;lt;IDictionary&amp;lt;K, V&gt;&amp;gt;&lt;/code&gt; that allows a dictionary
to be a key in another dictionary.
&lt;/p&gt;
&lt;p style="font-weight: bold"&gt;
Update
&lt;/p&gt;
&lt;p&gt;
We have commited some tests, and have found that with suffiently &amp;quot;good&amp;quot;
implementation of GetHashCode() of key or value we achieve results almost of the same
quality, as the results of the algorithm we have outlined above with much simpler
and straightforward algorithm like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; public int GetHashCode(IDictionary&amp;lt;K, V&gt; dictionary)&lt;br /&gt;
{&lt;br /&gt;
if (dictionary == null)&lt;br /&gt;
{&lt;br /&gt;
return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var result = 0;&lt;br /&gt;
&lt;br /&gt;
foreach(var entry in dictionary)&lt;br /&gt;
{&lt;br /&gt;
if ((entry.Key == null) || (entry.Value == null))&lt;br /&gt;
{&lt;br /&gt;
continue;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var k = entry.Key.GetHashCode();&lt;br /&gt;
var v = entry.Value.GetHashCode();&lt;br /&gt;
&lt;br /&gt;&lt;&lt; 5) + k;&lt;br /&gt;&lt;&lt; (k &gt;
k = (k v = (v &gt; 3)) + v;&lt;br /&gt;
&lt;br /&gt;
result += k ^ v;&lt;br /&gt;
&lt;br /&gt;
//result += Hash.GetHashFactor(keyComparer.GetHashCode(entry.Key)) *&lt;br /&gt;
// valueComparer.GetHashCode(entry.Value);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return result;&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
It was worth to blog about this just to find out that we have outwitted ourselves,
and finally to reach to a trivial hash code implementation for the dictionary.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=bc06427b-87e2-4e14-9ebb-1a186f875e3a" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,bc06427b-87e2-4e14-9ebb-1a186f875e3a.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=4abc49ac-23b2-448e-9951-f94e2393d672</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,4abc49ac-23b2-448e-9951-f94e2393d672.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,4abc49ac-23b2-448e-9951-f94e2393d672.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=4abc49ac-23b2-448e-9951-f94e2393d672</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Dealing recently with some task, we were in a position to use a weak dictionary in
the .NET. Instinctively we assumed that it should exist somewhere in the standard
library. We definitely knew that there is a <code>WeakReference</code> class to for
a single instance. We also knew that there is <code>WeakHashMap</code> in java, and
that it's based on java's <code>WeakReference</code>.
</p>
        <p>
So, we were surprised to find that there is no such thing out of the box in .NET.
</p>
        <p>
We have found that java's and .NET's weak references are different. In java
weak references whose targets are GCed can be automatically put into a queue, which
can be used to build clean up logic to remove dead keys from weak hash map. There
is nothing similar in .NET, where weak reference just silently loses it's value.
</p>
        <p>
Internet is full with custom implementations of weak dictionaries in .NET.
</p>
        <p>
.NET 4.5 finally defines a class <a href="http://msdn.microsoft.com/en-us/library/dd287757(v=vs.110).aspx"><code>ConditionalWeakTable&lt;TKey,
TValue&gt;</code></a>, which solves the problem in case when you need to match keys
by instance identity.
</p>
        <p>
Unfortunately in our case we needed to match keys using key's <code>GetHashCode()</code> and <code>Equals()</code>.
So, <code>ConditionalWeakTable&lt;TKey, TValue&gt;</code> did not directly work, but
then we found a way to make it work for us.
</p>
        <p>
Here is a quote from the definition:
</p>
        <p style="padding-left: 1em; font-style: italic">
A <code>ConditionalWeakTable&lt;TKey, TValue&gt;</code> object is a dictionary that binds
a managed object, which is represented by a key, to its attached property, which is
represented by a value. The object's keys are the individual instances of the TKey
class to which the property is attached, and its values are the property values that
are assigned to the corresponding objects.<br /><br />
...in the <code>ConditionalWeakTable&lt;TKey, TValue&gt;</code> class, adding a key/value
pair to the table does not ensure that the key will persist, even if it can be reached
directly from a value stored in the table... Instead, ConditionalWeakTable&lt;TKey,
TValue&gt; automatically removes the key/value entry as soon as no other references to
a key exist outside the table. 
</p>
        <p>
This property of <code>ConditionalWeakTable&lt;TKey, TValue&gt;</code> has helped
us to build a way to get a notification when the key is being finalized, which is
the missed ingredient in .NET's weak references.
</p>
        <p>
Assume you have an instance <code>key</code> of type <code>Key</code>. To get a notification
you should define a class <code>Finalizer</code> that will call some handler when
it's finalized, and you should bind <code>key</code> and a finalizer instance
using weak table.
</p>
        <p>
The code looks like this:
</p>
        <p>
          <code>public class Finalizer&lt;K&gt;<br />
  where K: class<br />
{<br />
  public static void Bind(K key, Action&lt;K&gt; handler)<br />
  {<br />
    var finalizer = table.GetValue(key, k =&gt; new Finalizer&lt;K&gt;
{ key = k });<br /><br />
    finalizer.Handler += handler;<br />
  }<br /><br />
  public static void Unbind(K key, Action&lt;K&gt; handler)<br />
  {<br />
    Finalizer finalizer;<br /><br />
    if (table.TryGetValue(key, out finalizer))<br />
    {<br />
      finalizer.Handler -= handler;<br />
    }<br />
  }<br /><br />
  ~Finalizer()<br />
  {<br />
    var handler = Handler;<br /><br />
    if (handler != null)<br />
    {<br />
      handler(key);<br />
    }<br />
  }<br /><br />
  private event Action&lt;K&gt; Handler;<br />
  private K key;<br /><br />
  private static readonly ConditionalWeakTable&lt;K, Finalizer&gt; table =<br />
    new ConditionalWeakTable&lt;K, Finalizer&gt;();<br />
}<br /><br /><br />
Key key = ...<br /><br />
Finalizer.Bind(key, k =&gt; { /* clean up. */ });</code>
        </p>
        <p>
Using this approach we have created a class <a href="http://nesterovsky-bros.com/download/WeakTable.cs.txt">WeakTable&lt;K,
V&gt;</a> modeled after ConditionalWeakTable&lt;TKey, TValue&gt;.
</p>
        <p>
So, this is our take in the problem: <a href="http://nesterovsky-bros.com/download/WeakTable.cs.txt">WeakTable.cs</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=4abc49ac-23b2-448e-9951-f94e2393d672" />
      </body>
      <title>WeakTable</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,4abc49ac-23b2-448e-9951-f94e2393d672.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/01/08/WeakTable.aspx</link>
      <pubDate>Wed, 08 Jan 2014 21:57:16 GMT</pubDate>
      <description>    &lt;p&gt;
Dealing recently with some task, we were in a position to use a weak dictionary in
the .NET. Instinctively we assumed that it should exist somewhere in the standard
library. We definitely knew that there is a &lt;code&gt;WeakReference&lt;/code&gt; class to for
a single instance. We also knew that there is &lt;code&gt;WeakHashMap&lt;/code&gt; in java, and
that it&amp;#39;s based on java&amp;#39;s &lt;code&gt;WeakReference&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
So, we were surprised to find that there is no such thing out of the box in .NET.
&lt;/p&gt;
&lt;p&gt;
We have found that java&amp;#39;s and .NET&amp;#39;s weak references are different. In java
weak references whose targets are GCed can be automatically put into a queue, which
can be used to build clean up logic to remove dead keys from weak hash map. There
is nothing similar in .NET, where weak reference just silently loses it&amp;#39;s value.
&lt;/p&gt;
&lt;p&gt;
Internet is full with custom implementations of weak dictionaries in .NET.
&lt;/p&gt;
&lt;p&gt;
.NET 4.5 finally defines a class &lt;a href="http://msdn.microsoft.com/en-us/library/dd287757(v=vs.110).aspx"&gt;&lt;code&gt;ConditionalWeakTable&amp;lt;TKey,
TValue&amp;gt;&lt;/code&gt;&lt;/a&gt;, which solves the problem in case when you need to match keys
by instance identity.
&lt;/p&gt;
&lt;p&gt;
Unfortunately in our case we needed to match keys using key&amp;#39;s &lt;code&gt;GetHashCode()&lt;/code&gt; and &lt;code&gt;Equals()&lt;/code&gt;.
So, &lt;code&gt;ConditionalWeakTable&amp;lt;TKey, TValue&amp;gt;&lt;/code&gt; did not directly work, but
then we found a way to make it work for us.
&lt;/p&gt;
&lt;p&gt;
Here is a quote from the definition:
&lt;/p&gt;
&lt;p style="padding-left: 1em; font-style: italic"&gt;
A &lt;code&gt;ConditionalWeakTable&amp;lt;TKey, TValue&gt;&lt;/code&gt; object is a dictionary that binds
a managed object, which is represented by a key, to its attached property, which is
represented by a value. The object's keys are the individual instances of the TKey
class to which the property is attached, and its values are the property values that
are assigned to the corresponding objects.&lt;br /&gt;
&lt;br /&gt;
...in the &lt;code&gt;ConditionalWeakTable&amp;lt;TKey, TValue&gt;&lt;/code&gt; class, adding a key/value
pair to the table does not ensure that the key will persist, even if it can be reached
directly from a value stored in the table... Instead, ConditionalWeakTable&amp;lt;TKey,
TValue&gt; automatically removes the key/value entry as soon as no other references to
a key exist outside the table. 
&lt;/p&gt;
&lt;p&gt;
This property of &lt;code&gt;ConditionalWeakTable&amp;lt;TKey, TValue&amp;gt;&lt;/code&gt; has helped
us to build a way to get a notification when the key is being finalized, which is
the missed ingredient in .NET&amp;#39;s weak references.
&lt;/p&gt;
&lt;p&gt;
Assume you have an instance &lt;code&gt;key&lt;/code&gt; of type &lt;code&gt;Key&lt;/code&gt;. To get a notification
you should define a class &lt;code&gt;Finalizer&lt;/code&gt; that will call some handler when
it&amp;#39;s finalized, and you should bind &lt;code&gt;key&lt;/code&gt; and a finalizer instance
using weak table.
&lt;/p&gt;
&lt;p&gt;
The code looks like this:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;public class Finalizer&amp;lt;K&amp;gt;&lt;br /&gt;
&amp;nbsp; where K: class&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; public static void Bind(K key, Action&amp;lt;K&amp;gt; handler)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var finalizer = table.GetValue(key, k =&amp;gt; new Finalizer&amp;lt;K&amp;gt;
{ key = k });&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; finalizer.Handler += handler;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public static void Unbind(K key, Action&amp;lt;K&amp;gt; handler)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Finalizer finalizer;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (table.TryGetValue(key, out finalizer))&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalizer.Handler -= handler;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; ~Finalizer()&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var handler = Handler;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (handler != null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; handler(key);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; private event Action&amp;lt;K&amp;gt; Handler;&lt;br /&gt;
&amp;nbsp; private K key;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; private static readonly ConditionalWeakTable&amp;lt;K, Finalizer&amp;gt; table =&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; new ConditionalWeakTable&amp;lt;K, Finalizer&amp;gt;();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Key key = ...&lt;br /&gt;
&lt;br /&gt;
Finalizer.Bind(key, k =&amp;gt; { /* clean up. */ });&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Using this approach we have created a class &lt;a href="http://nesterovsky-bros.com/download/WeakTable.cs.txt"&gt;WeakTable&amp;lt;K,
V&amp;gt;&lt;/a&gt; modeled after ConditionalWeakTable&amp;lt;TKey, TValue&amp;gt;.
&lt;/p&gt;
&lt;p&gt;
So, this is our take in the problem: &lt;a href="http://nesterovsky-bros.com/download/WeakTable.cs.txt"&gt;WeakTable.cs&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=4abc49ac-23b2-448e-9951-f94e2393d672" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,4abc49ac-23b2-448e-9951-f94e2393d672.aspx</comments>
      <category>.NET</category>
      <category>Java</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=91ba6c08-d73c-416f-a3d7-6da67e5baa1b</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,91ba6c08-d73c-416f-a3d7-6da67e5baa1b.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,91ba6c08-d73c-416f-a3d7-6da67e5baa1b.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=91ba6c08-d73c-416f-a3d7-6da67e5baa1b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Oftentimes we deal with Hebrew in .NET.
</p>
        <p>
The task we face again and again is attempt to convert a Hebrew text from visual to
logical representation.
</p>
        <p>
The latest demand of such task was when we processed content extracted from PDF. It's
turned out that PDF stores content as graphic primitives, and as result text is stored
visually (often each letter is kept separately).
</p>
        <p>
We solved the task more than a decade ago, by calling <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd374091(v=vs.85).aspx">Uniscribe
API</a>.
</p>
        <p>
The function by itself is a small wrapper around that API, so in .NET 1.0 we were
using managed C++, several years later we have switched to C++/CLI.
</p>
        <p>
But now after many .NET releases, and with 32/64 versions we can see that C++ is only
a guest in .NET world.
</p>
        <p>
To run C++ in .NET you have to install VC runtime libraries adjusted to a specific
.NET version. This turns C++ support in .NET into not a trivial task.
</p>
        <p>
So, we have finally decided to define C# interop for the Uniscribe API, and recreate
that function in pure C#:
</p>
        <p style="padding-left: 1em">
          <code>namespace NesterovskyBros.Bidi<br />
{<br />
  /// &lt;summary&gt;<br />
  /// An utility to convert visual string to logical.<br />
  /// &lt;summary&gt;<br />
  public static class BidiConverter<br />
  {<br />
    /// &lt;summary&gt;<br />
    /// Converts visual string to logical.<br />
    /// &lt;/summary&gt;<br />
    /// &lt;param name="value"&gt;A value to convert.&lt;/param&gt;<br />
    /// &lt;param name="rtl"&gt;A base direction.&lt;/param&gt;<br />
    /// &lt;param name="direction"&gt;<br />
    /// true for visual to logical, and false for logical to visual.<br />
    /// &lt;/param&gt;<br />
    /// &lt;returns&gt;Converted string.&lt;/returns&gt;<br />
    public static string Convert(string value, bool rtl, bool direction);</code>
        </p>
        <p>
You can download this project from <a href="http://www.nesterovsky-bros.com/download/BidiVisualConverter.zip">BidiVisualConverter.zip</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=91ba6c08-d73c-416f-a3d7-6da67e5baa1b" />
      </body>
      <title>Bidi Visual Converter</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,91ba6c08-d73c-416f-a3d7-6da67e5baa1b.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2013/12/27/BidiVisualConverter.aspx</link>
      <pubDate>Fri, 27 Dec 2013 20:36:20 GMT</pubDate>
      <description>    &lt;p&gt;
Oftentimes we deal with Hebrew in .NET.
&lt;/p&gt;
&lt;p&gt;
The task we face again and again is attempt to convert a Hebrew text from visual to
logical representation.
&lt;/p&gt;
&lt;p&gt;
The latest demand of such task was when we processed content extracted from PDF. It&amp;#39;s
turned out that PDF stores content as graphic primitives, and as result text is stored
visually (often each letter is kept separately).
&lt;/p&gt;
&lt;p&gt;
We solved the task more than a decade ago, by calling &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd374091(v=vs.85).aspx"&gt;Uniscribe
API&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
The function by itself is a small wrapper around that API, so in .NET 1.0 we were
using managed C++, several years later we have switched to C++/CLI.
&lt;/p&gt;
&lt;p&gt;
But now after many .NET releases, and with 32/64 versions we can see that C++ is only
a guest in .NET world.
&lt;/p&gt;
&lt;p&gt;
To run C++ in .NET you have to install VC runtime libraries adjusted to a specific
.NET version. This turns C++ support in .NET into not a trivial task.
&lt;/p&gt;
&lt;p&gt;
So, we have finally decided to define C# interop for the Uniscribe API, and recreate
that function in pure C#:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;namespace NesterovskyBros.Bidi&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp; /// An utility to convert visual string to logical.&lt;br /&gt;
&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp; public static class BidiConverter&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Converts visual string to logical.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="value"&gt;A value to convert.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="rtl"&gt;A base direction.&amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="direction"&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// true for visual to logical, and false for logical to visual.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/param&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;returns&gt;Converted string.&amp;lt;/returns&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static string Convert(string value, bool rtl, bool direction);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
You can download this project from &lt;a href="http://www.nesterovsky-bros.com/download/BidiVisualConverter.zip"&gt;BidiVisualConverter.zip&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=91ba6c08-d73c-416f-a3d7-6da67e5baa1b" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,91ba6c08-d73c-416f-a3d7-6da67e5baa1b.aspx</comments>
      <category>.NET</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p style="direction: ltr">
Although WCF REST service + JSON is outdated comparing to Web API, there are yet a
lot of such solutions (and probably will appear new ones) that use such "old"
technology. 
</p>
        <p>
One of the crucial points of any web application is an error handler that allows gracefully
resolve server-side exceptions and routes them as JSON objects to the client for further
processing. There are dozen approachesin Internet that solve this issue  (e.g. <a href="http://blog.manglar.com/how-to-provide-custom-json-exceptions-from-as-wcf-service/">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/images/colapsed-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/images/expanded-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/download/ErrorHandling.zip">download
demo project here</a>. It contains three crucial parts:
</p>
        <ul>
          <li>
A server-side error handler that catches all exceptions and serializes them as JSON
objects (see <strong>/Code/JsonErrorHandler.cs</strong> and <strong>/Code/JsonWebHttpBehaviour.cs</strong>).</li>
          <li>
An error dialog that's based on user-control defined in previous articles (see <strong>/scripts/controls/error.js</strong>,<strong> /scripts/controls/error.resources.js</strong> and <strong>/scripts/templates/error.tmpl.html</strong>).</li>
          <li>
A client-side error handler that displays errors in user-friendly's manner (see <strong>/scripts/api/api.js</strong>,
method <strong>defaultErrorHandler()</strong>).</li>
        </ul>
        <p>
Of course this is only a draft solution, but it defines a direction for further customizations
in your web applications.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f" />
      </body>
      <title>Error handling in WCF based web applications</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2013/05/13/ErrorHandlingInWCFBasedWebApplications.aspx</link>
      <pubDate>Mon, 13 May 2013 23:09:02 GMT</pubDate>
      <description>   &lt;p style="direction: ltr"&gt;
Although WCF REST service + JSON is outdated comparing to Web API, there are yet a
lot of such solutions (and probably will appear new ones) that use such &amp;quot;old&amp;quot;
technology. 
&lt;/p&gt;
&lt;p&gt;
One of the crucial points of any web application is an error handler that allows gracefully
resolve server-side exceptions and routes them as JSON objects to the client for further
processing. There are dozen approachesin Internet that solve this issue&amp;nbsp; (e.g. &lt;a href="http://blog.manglar.com/how-to-provide-custom-json-exceptions-from-as-wcf-service/"&gt;http://blog.manglar.com/how-to-provide-custom-json-exceptions-from-as-wcf-service/&lt;/a&gt;),
but there is no one that demonstrates error handling ot the client-side. We realize
that it&amp;#39;s impossible to write something general that suits for every web application,
but we&amp;#39;d like to show a client-side error handler that utilizes JSON and KendoUI.
&lt;/p&gt;
&lt;p style="direction: ltr"&gt;
On our opinion, the successfull error handler must display an understandable error
message on one hand, and on the other hand it has to provide technical info for developers
in order to investigate the exception reason (and to fix it, if need):
&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tr&gt;
&lt;td style="vertical-align: top;"&gt;
&lt;a href="http://www.nesterovsky-bros.com/images/colapsed-error-dialog.png"&gt; &lt;img src="http://www.nesterovsky-bros.com/images/thumb-colapsed-error-dialog.png" alt="collapsed error dialog" /&gt; &lt;/a&gt; 
&lt;/td&gt;
&lt;td&gt;
&lt;a href="http://www.nesterovsky-bros.com/images/expanded-error-dialog.png"&gt; &lt;img src="http://www.nesterovsky-bros.com/images/thumb-expanded-error-dialog.png" alt="collapsed error dialog" /&gt; &lt;/a&gt; 
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;
You may &lt;a href="http://www.nesterovsky-bros.com/download/ErrorHandling.zip"&gt;download
demo project here&lt;/a&gt;. It contains three crucial parts:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
A server-side error handler that catches all exceptions and serializes them as JSON
objects (see &lt;strong&gt;/Code/JsonErrorHandler.cs&lt;/strong&gt; and &lt;strong&gt;/Code/JsonWebHttpBehaviour.cs&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;
An error dialog that&amp;#39;s based on user-control defined in previous articles (see &lt;strong&gt;/scripts/controls/error.js&lt;/strong&gt;,&lt;strong&gt; /scripts/controls/error.resources.js&lt;/strong&gt; and &lt;strong&gt;/scripts/templates/error.tmpl.html&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;
A client-side error handler that displays errors in user-friendly&amp;#39;s manner (see &lt;strong&gt;/scripts/api/api.js&lt;/strong&gt;,
method &lt;strong&gt;defaultErrorHandler()&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Of course this is only a draft solution, but it defines a direction for further customizations
in your web applications.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,3af8fe47-6f58-4e53-b4e8-6ee5a9203c2f.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>javascript</category>
      <category>kendoui</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=b17502e5-9edf-43d2-9fab-7a5a3124ddf5</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b17502e5-9edf-43d2-9fab-7a5a3124ddf5.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=b17502e5-9edf-43d2-9fab-7a5a3124ddf5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Kendo UI Docs contains an article "<a href="http://docs.kendoui.com/howto/load-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/download/WebTemplates.zip">WebTemplates.zip</a> contains
a web project demonstrating our technique. "<a href="http://www.nesterovsky-bros.com/download/KendoUI/scripts/templates.tt">templates.tt</a>"
is text template transformation used in the project.
</p>
        <p>
See also: <a href="http://www.nesterovsky-bros.com/weblog/2013/07/07/CompileKendoUITemplates.aspx">Compile
KendoUI templates</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=b17502e5-9edf-43d2-9fab-7a5a3124ddf5" />
      </body>
      <title>How To: Load KendoUI Templates from External Files</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b17502e5-9edf-43d2-9fab-7a5a3124ddf5.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2013/01/06/HowToLoadKendoUITemplatesFromExternalFiles.aspx</link>
      <pubDate>Sun, 06 Jan 2013 19:43:19 GMT</pubDate>
      <description>&lt;p&gt;
Kendo UI Docs contains an article &amp;quot;&lt;a href="http://docs.kendoui.com/howto/load-templates-external-files"&gt;How
To: Load Templates from External Files&lt;/a&gt;&amp;quot;, where authors review two way of
dealing with Kendo UI templates.
&lt;/p&gt;
&lt;p&gt;
While using Kendo UI we have found our own answer to: &lt;i&gt;where will the Kendo UI templates
be defined and maintained?&lt;/i&gt;
&lt;/p&gt;
&lt;p style="float: right"&gt;
&lt;img alt="solution tree" src="content/binary/templates.png" style="width: 320px" /&gt;
&lt;/p&gt;
&lt;p&gt;
In our .NET project we have decided to keep templates separately, and to store them
under the &amp;quot;templates&amp;quot; folder. Those templates are in fact include html,
head, and stylesheet links. This is to help us to present those tempates in the design
view. 
&lt;/p&gt;
&lt;p&gt;
In our scripts folder, we have defined a small text transformation template: &amp;quot;templates.tt&amp;quot;,
which produces &amp;quot;templates.js&amp;quot; file. This template takes body contents of
each &amp;quot;*.tmpl.html&amp;quot; file from &amp;quot;templates&amp;quot; folder and builds string
of the form:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;document.write('&amp;lt;script id="footer-template" type="text/x-kendo-template"&amp;gt;...&amp;lt;/script&amp;gt;&amp;lt;script
id="row-template" type="text/x-kendo-template"&amp;gt;...&amp;lt;/script&amp;gt;');&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
In our page that uses templates, we include &amp;quot;templates.js&amp;quot;:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; &amp;lt;!DOCTYPE html&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;script src=&amp;quot;scripts/templates.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt; 
&lt;br /&gt;
...&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Thus, we have:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
clean separation of templates and page content;&lt;/li&gt;
&lt;li&gt;
automatically generated templates include file.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;a href="http://www.nesterovsky-bros.com/download/WebTemplates.zip"&gt;WebTemplates.zip&lt;/a&gt; contains
a web project demonstrating our technique. &amp;quot;&lt;a href="http://www.nesterovsky-bros.com/download/KendoUI/scripts/templates.tt"&gt;templates.tt&lt;/a&gt;&amp;quot;
is text template transformation used in the project.
&lt;/p&gt;
&lt;p&gt;
See also: &lt;a href="http://www.nesterovsky-bros.com/weblog/2013/07/07/CompileKendoUITemplates.aspx"&gt;Compile
KendoUI templates&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=b17502e5-9edf-43d2-9fab-7a5a3124ddf5" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,b17502e5-9edf-43d2-9fab-7a5a3124ddf5.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>javascript</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=40a04a86-ce1f-44a2-8daa-f54306b84e20</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,40a04a86-ce1f-44a2-8daa-f54306b84e20.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,40a04a86-ce1f-44a2-8daa-f54306b84e20.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=40a04a86-ce1f-44a2-8daa-f54306b84e20</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Our goal is to generate reports in streaming mode.
</p>
        <p>
At some point we need to deal with data streams (e.g. xml streams for xslt transformations).
Often a nature of report demands several passes through the data. To increase performance
we have defined a class named <code>StreamResource</code>. This class encapsulates
input data, reads it once and caches it into a temp file; thus data can be traversed
many times. <code>StreamResource</code> can read data lazily or in a eager way thus
releasing resources early. This class can be used as a variation of <code>PipeStream</code>,
which never blocks, as if a size of a buffer is not limited, and which can be read
many times. 
</p>
        <p>
The API looks like this:
</p>
        <p style="padding-left: 1em">
          <code> public class StreamResource: IDisposable<br />
{<br />
/// &lt;summary&gt;<br />
/// Creates a StreamSource instance.<br />
/// &lt;/summary&gt;<br />
/// &lt;param name="source"&gt;<br />
/// A function that returns source as an input stream.<br />
/// &lt;/param&gt;<br />
/// &lt;param name="settings"&gt;Optional settings.&lt;/param&gt;<br />
public StreamResource(Func&lt;Stream&gt; source, Settings settings = null);<br /><br />
/// &lt;summary&gt;<br />
/// Creates a StreamSource instance.<br />
/// &lt;/summary&gt;<br />
/// &lt;param name="source"&gt;<br />
/// A function that writes source data into an output stream.<br />
/// &lt;/param&gt;<br />
/// &lt;param name="settings"&gt;Optional settings.&lt;/param&gt;<br />
public StreamResource(Action&lt;Stream&gt; source, Settings settings = null);<br /><br />
/// &lt;summary&gt;<br />
/// Gets an input stream.<br />
/// &lt;/summary&gt;<br />
/// &lt;param name="shared"&gt;<br />
/// Indicates that this StreamResouce should be disposed when returned<br />
/// stream is closed and there are no more currently opened cache streams.<br />
/// &lt;/param&gt;<br />
/// &lt;returns&gt;A input stream.&lt;/returns&gt;<br />
public Stream GetStream(bool shared = false);<br />
} </code>
        </p>
        <p>
The use pattern is following:
</p>
        <p style="padding-left: 1em">
          <code>// Acquire resource.<br />
using(var resource = new StreamResource(() =&gt; CallService(params...)))<br />
{<br />
// Read stream.<br />
using(var stream = resource.GetStream())<br />
{<br />
...<br />
}<br /><br />
...<br /><br />
// Read stream again.<br />
using(var stream = resource.GetStream())<br />
{<br />
...<br />
}<br />
}</code>
        </p>
        <p style="direction: ltr">
          <code>StreamResource</code> is efficient even if you need to process content only
once, as it monitors timings of reading of source data and compares it with timings
of data consumption. If the difference exceeds some threshold then <code>StreamResource</code> caches
source greedily, otherwise source is pooled lazily. Thus, input resources can be released
promptly. This is important, for example, when the source depends on a database connection.
</p>
        <p>
The use pattern is following:
</p>
        <p style="padding-left: 1em">
          <code>// Acquire resource and get shared stream.<br />
using(var stream = new StreamResource(() =&gt; CallService(params...)).GetStream(true))<br />
{<br />
...<br />
}</code>
        </p>
        <p>
Finally, <code>StreamResource</code> allows to process data in a pipe stream mode.
This is when you have a generator function <code>Action&lt;Stream&gt;</code> that
can write to a stream, and you want to read that data. The advantage of <code>StreamResource</code> over
real pipe stream is that it can work without blocking of generator, thus releasing
resources early.
</p>
        <p>
The use pattern is similar to the previous one:
</p>
        <p style="padding-left: 1em">
          <code>using(var stream = new StreamResource(output =&gt; Generate(output, params...)).GetStream(true))<br />
{<br />
...<br />
}</code>
        </p>
        <p>
The source of the class can be found at <a href="http://www.nesterovsky-bros.com/download/Streaming.zip">Streaming.zip</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=40a04a86-ce1f-44a2-8daa-f54306b84e20" />
      </body>
      <title>StreamResource</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,40a04a86-ce1f-44a2-8daa-f54306b84e20.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/11/20/StreamResource.aspx</link>
      <pubDate>Tue, 20 Nov 2012 07:01:57 GMT</pubDate>
      <description>&lt;p&gt;
Our goal is to generate reports in streaming mode.
&lt;/p&gt;
&lt;p&gt;
At some point we need to deal with data streams (e.g. xml streams for xslt transformations).
Often a nature of report demands several passes through the data. To increase performance
we have defined a class named &lt;code&gt;StreamResource&lt;/code&gt;. This class encapsulates
input data, reads it once and caches it into a temp file; thus data can be traversed
many times. &lt;code&gt;StreamResource&lt;/code&gt; can read data lazily or in a eager way thus
releasing resources early. This class can be used as a variation of &lt;code&gt;PipeStream&lt;/code&gt;,
which never blocks, as if a size of a buffer is not limited, and which can be read
many times. 
&lt;/p&gt;
&lt;p&gt;
The API looks like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; public class StreamResource: IDisposable&lt;br /&gt;
{&lt;br /&gt;
/// &amp;lt;summary&amp;gt;&lt;br /&gt;
/// Creates a StreamSource instance.&lt;br /&gt;
/// &amp;lt;/summary&amp;gt;&lt;br /&gt;
/// &amp;lt;param name="source"&amp;gt;&lt;br /&gt;
/// A function that returns source as an input stream.&lt;br /&gt;
/// &amp;lt;/param&amp;gt;&lt;br /&gt;
/// &amp;lt;param name="settings"&amp;gt;Optional settings.&amp;lt;/param&amp;gt;&lt;br /&gt;
public StreamResource(Func&amp;lt;Stream&amp;gt; source, Settings settings = null);&lt;br /&gt;
&lt;br /&gt;
/// &amp;lt;summary&amp;gt;&lt;br /&gt;
/// Creates a StreamSource instance.&lt;br /&gt;
/// &amp;lt;/summary&amp;gt;&lt;br /&gt;
/// &amp;lt;param name="source"&amp;gt;&lt;br /&gt;
/// A function that writes source data into an output stream.&lt;br /&gt;
/// &amp;lt;/param&amp;gt;&lt;br /&gt;
/// &amp;lt;param name="settings"&amp;gt;Optional settings.&amp;lt;/param&amp;gt;&lt;br /&gt;
public StreamResource(Action&amp;lt;Stream&amp;gt; source, Settings settings = null);&lt;br /&gt;
&lt;br /&gt;
/// &amp;lt;summary&amp;gt;&lt;br /&gt;
/// Gets an input stream.&lt;br /&gt;
/// &amp;lt;/summary&amp;gt;&lt;br /&gt;
/// &amp;lt;param name="shared"&amp;gt;&lt;br /&gt;
/// Indicates that this StreamResouce should be disposed when returned&lt;br /&gt;
/// stream is closed and there are no more currently opened cache streams.&lt;br /&gt;
/// &amp;lt;/param&amp;gt;&lt;br /&gt;
/// &amp;lt;returns&amp;gt;A input stream.&amp;lt;/returns&amp;gt;&lt;br /&gt;
public Stream GetStream(bool shared = false);&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The use pattern is following:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;// Acquire resource.&lt;br /&gt;
using(var resource = new StreamResource(() =&amp;gt; CallService(params...)))&lt;br /&gt;
{&lt;br /&gt;
// Read stream.&lt;br /&gt;
using(var stream = resource.GetStream())&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
// Read stream again.&lt;br /&gt;
using(var stream = resource.GetStream())&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p style="direction: ltr"&gt;
&lt;code&gt;StreamResource&lt;/code&gt; is efficient even if you need to process content only
once, as it monitors timings of reading of source data and compares it with timings
of data consumption. If the difference exceeds some threshold then &lt;code&gt;StreamResource&lt;/code&gt; caches
source greedily, otherwise source is pooled lazily. Thus, input resources can be released
promptly. This is important, for example, when the source depends on a database connection.
&lt;/p&gt;
&lt;p&gt;
The use pattern is following:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;// Acquire resource and get shared stream.&lt;br /&gt;
using(var stream = new StreamResource(() =&amp;gt; CallService(params...)).GetStream(true))&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Finally, &lt;code&gt;StreamResource&lt;/code&gt; allows to process data in a pipe stream mode.
This is when you have a generator function &lt;code&gt;Action&amp;lt;Stream&amp;gt;&lt;/code&gt; that
can write to a stream, and you want to read that data. The advantage of &lt;code&gt;StreamResource&lt;/code&gt; over
real pipe stream is that it can work without blocking of generator, thus releasing
resources early.
&lt;/p&gt;
&lt;p&gt;
The use pattern is similar to the previous one:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;using(var stream = new StreamResource(output =&amp;gt; Generate(output, params...)).GetStream(true))&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The source of the class can be found at &lt;a href="http://www.nesterovsky-bros.com/download/Streaming.zip"&gt;Streaming.zip&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=40a04a86-ce1f-44a2-8daa-f54306b84e20" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,40a04a86-ce1f-44a2-8daa-f54306b84e20.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=6d962206-6ca4-4ed5-828b-551082e660dc</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,6d962206-6ca4-4ed5-828b-551082e660dc.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,6d962206-6ca4-4ed5-828b-551082e660dc.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=6d962206-6ca4-4ed5-828b-551082e660dc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Two monthes ago we have started <a href="http://www.nesterovsky-bros.com/weblog/2012/09/07/ChangeColumnTypeInSQLServer2008R2.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://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getint16.aspx">SqlDataReader.GetInt16</a></code> to
access the column value. This method has a remark: "<i>No conversions are performed;
therefore, the data retrieved must already be a 16-bit signed integer.</i>"
</p>
        <p>
Fortunately, we have found that EF allows additional columns in the result set. This
helped us to formulate the solution. We have updated the procedure definition like
this:
</p>
        <p style="padding-left: 1em">
          <code>create procedure Data.GetReports(@type int) as<br />
begin<br />
select 
<br />
cast(Type as smallint) Type, -- deprecated<br />
Type TypeEx, ... 
<br />
from 
<br />
Data.Report 
<br />
where 
<br />
Type = @type;<br />
end;</code>
        </p>
        <p>
This way:
</p>
        <ul>
          <li>
result column <code>"Type"</code> is declared as deprecated;</li>
          <li>
old clients still work;</li>
          <li>
all clients should be updated to use <code>"TypeEx"</code> column;</li>
          <li>
after all clients will be updated we shall remove <code>"Type"</code> column from
the result set.</li>
        </ul>
        <p>
So there is a clear migration process.
</p>
        <p>
P.S. we don't understand why <code>SqlDataReader</code> doesn't support value
conversion. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=6d962206-6ca4-4ed5-828b-551082e660dc" />
      </body>
      <title>Change column type in SQL Server, Part 2</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,6d962206-6ca4-4ed5-828b-551082e660dc.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/11/09/ChangeColumnTypeInSQLServerPart2.aspx</link>
      <pubDate>Fri, 09 Nov 2012 16:41:27 GMT</pubDate>
      <description>  &lt;p&gt;
Two monthes ago we have started &lt;a href="http://www.nesterovsky-bros.com/weblog/2012/09/07/ChangeColumnTypeInSQLServer2008R2.aspx"&gt; a
process of changing column type from &lt;code&gt;smallint&lt;/code&gt; to &lt;code&gt;int&lt;/code&gt; in
a big database.&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
This was splitted in two phases:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Change tables and internal stored procedures and functions.&lt;/li&gt;
&lt;li&gt;
Change interface API and update all clients.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The first part took almost two monthes to complete. Please read earlier post about
the technique we have selected for the implementation. In total we have transferred
about 15 billion rows. During this time database was online.
&lt;/p&gt;
&lt;p&gt;
The second part was short but the problem was that we did not control all clients,
so could not arbitrary change types of parameters and of result columns. 
&lt;/p&gt;
&lt;p&gt;
All our clients use Entity Framework 4 to access the database. All access is done
though stored procedures. So suppose there was a procedure:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;create procedure Data.GetReports(@type smallint) as&lt;br /&gt;
begin&lt;br /&gt;
select Type, ... from Data.Report where Type = @type;&lt;br /&gt;
end;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
where column &lt;code&gt;&amp;quot;Type&amp;quot;&lt;/code&gt; was of type &lt;code&gt;smallint&lt;/code&gt;. Now
we were going to change it to:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;create procedure Data.GetReports(@type int) as&lt;br /&gt;
begin&lt;br /&gt;
select Type, ... from Data.Report where Type = @type;&lt;br /&gt;
end;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
where &lt;code&gt;&amp;quot;Type&amp;quot;&lt;/code&gt; column became of type &lt;code&gt;int&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
Our tests have shown that EF bears with change of types of input parameters, but throws
exceptions when column type has been changed, even when a value fits the range. The
reason is that EF uses method &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getint16.aspx"&gt;SqlDataReader.GetInt16&lt;/a&gt;&lt;/code&gt; to
access the column value. This method has a remark: &amp;quot;&lt;i&gt;No conversions are performed;
therefore, the data retrieved must already be a 16-bit signed integer.&lt;/i&gt;&amp;quot;
&lt;/p&gt;
&lt;p&gt;
Fortunately, we have found that EF allows additional columns in the result set. This
helped us to formulate the solution. We have updated the procedure definition like
this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;create procedure Data.GetReports(@type int) as&lt;br /&gt;
begin&lt;br /&gt;
select 
&lt;br /&gt;
cast(Type as smallint) Type, -- deprecated&lt;br /&gt;
Type TypeEx, ... 
&lt;br /&gt;
from 
&lt;br /&gt;
Data.Report 
&lt;br /&gt;
where 
&lt;br /&gt;
Type = @type;&lt;br /&gt;
end;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
This way:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
result column &lt;code&gt;&amp;quot;Type&amp;quot;&lt;/code&gt; is declared as deprecated;&lt;/li&gt;
&lt;li&gt;
old clients still work;&lt;/li&gt;
&lt;li&gt;
all clients should be updated to use &lt;code&gt;&amp;quot;TypeEx&amp;quot;&lt;/code&gt; column;&lt;/li&gt;
&lt;li&gt;
after all clients will be updated we shall remove &lt;code&gt;"Type"&lt;/code&gt; column from
the result set.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
So there is a clear migration process.
&lt;/p&gt;
&lt;p&gt;
P.S. we don&amp;#39;t understand why &lt;code&gt;SqlDataReader&lt;/code&gt; doesn&amp;#39;t support value
conversion. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=6d962206-6ca4-4ed5-828b-551082e660dc" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,6d962206-6ca4-4ed5-828b-551082e660dc.aspx</comments>
      <category>.NET</category>
      <category>SQL Server puzzle</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=98b58257-9069-4d13-9d10-a7061e14e834</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,98b58257-9069-4d13-9d10-a7061e14e834.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,98b58257-9069-4d13-9d10-a7061e14e834.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=98b58257-9069-4d13-9d10-a7061e14e834</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you deal with web applications you probably have already dealt with export data
to Excel. There are several options to prepare data for Excel:
</p>
        <ul>
          <li>
generate CSV;</li>
          <li>
generate HTML that excel understands;</li>
          <li>
generate XML in Spreadsheet 2003 format;</li>
          <li>
generate data using Open XML SDK or some other 3rd party libraries;</li>
          <li>
generate data in XLSX format, according to Open XML specification.</li>
        </ul>
        <p>
You may find a good article with pros and cons of each solution <a href="http://blogs.msdn.com/b/erikaehrli/archive/2009/01/30/how-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx" target="_blank">here</a>.
We, in our turn, would like to share our experience in this field. Let's start from
requirements:
</p>
        <ul>
          <li>
Often we have to export huge data-sets.</li>
          <li>
We should be able to format, parametrize and to apply different styles to the exported
data.</li>
          <li>
There are cases when exported data may contain more than one table per sheet or even
more than one sheet.</li>
          <li>
Some exported data have to be illustrated with charts.</li>
        </ul>
        <p>
All these requirements led us to a solution based on XSLT processing of streamed data.
The advantage of this solution is that the result is immediately forwarded to a client
as fast as XSLT starts to generate output. Such approach is much productive than generating
of XLSX using of Open XML SDK or any other third party library, since it avoids keeping
a huge data-sets in memory on the server side.
</p>
        <p style="direction: ltr">
Another advantage - is simple maintenance, as we achieve clear separation of data
and presentation layers. On each request to change formatting or apply another style
to a cell you just have to modify xslt file(s) that generate variable parts of XLSX.
</p>
        <p>
As result, our clients get XLSX files according with Open XML specifications. The
details of implementations of our solution see in our next posts.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=98b58257-9069-4d13-9d10-a7061e14e834" />
      </body>
      <title>Export data to Excel</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,98b58257-9069-4d13-9d10-a7061e14e834.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/10/29/ExportDataToExcel.aspx</link>
      <pubDate>Mon, 29 Oct 2012 15:34:38 GMT</pubDate>
      <description>&lt;p&gt;
If you deal with web applications you probably have already dealt with export data
to Excel. There are several options to prepare data for Excel:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
generate CSV;&lt;/li&gt;
&lt;li&gt;
generate HTML that excel understands;&lt;/li&gt;
&lt;li&gt;
generate XML in Spreadsheet 2003 format;&lt;/li&gt;
&lt;li&gt;
generate data using Open XML SDK or some other 3rd party libraries;&lt;/li&gt;
&lt;li&gt;
generate data in XLSX format, according to Open XML specification.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
You may find a good article with pros and cons of each solution &lt;a href="http://blogs.msdn.com/b/erikaehrli/archive/2009/01/30/how-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx" target="_blank"&gt;here&lt;/a&gt;.
We, in our turn, would like to share our experience in this field. Let's start from
requirements:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Often we have to export huge data-sets.&lt;/li&gt;
&lt;li&gt;
We should be able to format, parametrize and to apply different styles to the exported
data.&lt;/li&gt;
&lt;li&gt;
There are cases when exported data may contain more than one table per sheet or even
more than one sheet.&lt;/li&gt;
&lt;li&gt;
Some exported data have to be illustrated with charts.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
All these requirements led us to a solution based on XSLT processing of streamed data.
The advantage of this solution is that the result is immediately forwarded to a client
as fast as XSLT starts to generate output. Such approach is much productive than generating
of XLSX using of Open XML SDK or any other third party library, since it avoids keeping
a huge data-sets in memory on the server side.
&lt;/p&gt;
&lt;p style="direction: ltr"&gt;
Another advantage - is simple maintenance, as we achieve clear separation of data
and presentation layers. On each request to change formatting or apply another style
to a cell you just have to modify xslt file(s) that generate variable parts of XLSX.
&lt;/p&gt;
&lt;p&gt;
As result, our clients get XLSX files according with Open XML specifications. The
details of implementations of our solution see in our next posts.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=98b58257-9069-4d13-9d10-a7061e14e834" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,98b58257-9069-4d13-9d10-a7061e14e834.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>Thinking aloud</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=cad54317-1876-4b98-a8d2-7e44e5dda1f7</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,cad54317-1876-4b98-a8d2-7e44e5dda1f7.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,cad54317-1876-4b98-a8d2-7e44e5dda1f7.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=cad54317-1876-4b98-a8d2-7e44e5dda1f7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Earlier we have shown <a href="http://www.nesterovsky-bros.com/weblog/2012/07/22/StreamingEntityData.aspx"> how
to build streaming xml reader from business data</a> and have reminded about <a href="http://www.nesterovsky-bros.com/weblog/2012/07/26/StreamingXsltTransformationWithForwardXPathNavigator.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/download/Streaming.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/download/Streaming.zip"> Streaming.zip</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7" />
      </body>
      <title>Stream xslt transformation through WCF</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,cad54317-1876-4b98-a8d2-7e44e5dda1f7.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/08/03/StreamXsltTransformationThroughWCF.aspx</link>
      <pubDate>Fri, 03 Aug 2012 22:32:49 GMT</pubDate>
      <description>  &lt;p&gt;
Earlier we have shown &lt;a href="http://www.nesterovsky-bros.com/weblog/2012/07/22/StreamingEntityData.aspx"&gt; how
to build streaming xml reader from business data&lt;/a&gt; and have reminded about &lt;a href="http://www.nesterovsky-bros.com/weblog/2012/07/26/StreamingXsltTransformationWithForwardXPathNavigator.aspx"&gt;ForwardXPathNavigator
which helps to create a streaming xslt transformation&lt;/a&gt;. Now we want to show how
to stream content produced with xslt out of WCF service.
&lt;/p&gt;
&lt;p&gt;
To achieve streaming in WCF one needs: 
&lt;/p&gt;
&lt;p&gt;
1. To configure service to use streaming. Description on how to do this can be found
in the internet. See web.config of the sample &lt;a href="http://www.nesterovsky-bros.com/download/Streaming.zip"&gt; Streaming.zip&lt;/a&gt; for
the details.
&lt;/p&gt;
&lt;p&gt;
2. Create a service with a method returning &lt;code&gt;Stream&lt;/code&gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;[ServiceContract(Namespace = "http://www.nesterovsky-bros.com")]&lt;br /&gt;
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]&lt;br /&gt;
public class Service&lt;br /&gt;
{&lt;br /&gt;
[OperationContract]&lt;br /&gt;
[WebGet(RequestFormat = WebMessageFormat.Json)]&lt;br /&gt;
public Stream GetPeopleHtml(int count, int seed)&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
2. Return a &lt;code&gt;Stream&lt;/code&gt; from xsl transformation. 
&lt;/p&gt;
&lt;p&gt;
Unfortunately (we mentioned it already), &lt;code&gt;XslCompiledTransform&lt;/code&gt; generates
its output into &lt;code&gt;XmlWriter&lt;/code&gt; (or into output &lt;code&gt;Stream&lt;/code&gt;) rather
than exposes result as &lt;code&gt;XmlReader&lt;/code&gt;, while WCF gets input stream and passes
it to a client. 
&lt;/p&gt;
&lt;p&gt;
We could generate xslt output into a file or a memory &lt;code&gt;Stream&lt;/code&gt; and then
return that content as input &lt;code&gt;Stream&lt;/code&gt;, but this will defeat a goal of streaming,
as client would have started to get data no earlier that the xslt completed its work.
What we need instead is a pipe that form xslt output &lt;code&gt;Stream&lt;/code&gt; to an input &lt;code&gt;Stream&lt;/code&gt; returned
from WCF.
&lt;/p&gt;
&lt;p&gt;
.NET implements pipe streams, so our task is trivial. We have defined a utility method
that creates an input &lt;code&gt;Stream&lt;/code&gt; from a generator populating an output &lt;code&gt;Stream&lt;/code&gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; public static Stream GetPipedStream(Action&amp;lt;Stream&amp;gt; generator)&lt;br /&gt;
{&lt;br /&gt;
var output = new AnonymousPipeServerStream();&lt;br /&gt;
var input = new AnonymousPipeClientStream(&lt;br /&gt;
output.GetClientHandleAsString());&lt;br /&gt;
&lt;br /&gt;
Task.Factory.StartNew(&lt;br /&gt;
() =&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
using(output)&lt;br /&gt;
{&lt;br /&gt;
generator(output);&lt;br /&gt;
output.WaitForPipeDrain();&lt;br /&gt;
}&lt;br /&gt;
},&lt;br /&gt;
TaskCreationOptions.LongRunning);&lt;br /&gt;
&lt;br /&gt;
return input;&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
We wrapped xsl transformation as such a generator:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;[OperationContract]&lt;br /&gt;
[WebGet(RequestFormat = WebMessageFormat.Json)]&lt;br /&gt;
public Stream GetPeopleHtml(int count, int seed)&lt;br /&gt;
{&lt;br /&gt;
var context = WebOperationContext.Current;&lt;br /&gt;
&lt;br /&gt;
context.OutgoingResponse.ContentType = "text/html";&lt;br /&gt;
context.OutgoingResponse.Headers["Content-Disposition"] = 
&lt;br /&gt;
"attachment;filename=reports.html";&lt;br /&gt;
&lt;br /&gt;
var cache = HttpRuntime.Cache;&lt;br /&gt;
var path = HttpContext.Current.Server.MapPath("~/People.xslt");&lt;br /&gt;
var transform = cache[path] as XslCompiledTransform;&lt;br /&gt;
&lt;br /&gt;
if (transform == null)&lt;br /&gt;
{&lt;br /&gt;
transform = new XslCompiledTransform();&lt;br /&gt;
transform.Load(path);&lt;br /&gt;
cache.Insert(path, transform, new CacheDependency(path));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return Extensions.&lt;strong&gt;GetPipedStream&lt;/strong&gt;(&lt;br /&gt;
output =&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
// We have a streamed business data.&lt;br /&gt;
var people = Data.CreateRandomData(count, seed, 0, count);&lt;br /&gt;
&lt;br /&gt;
// We want to see it as streamed xml data.&lt;br /&gt;
using(var stream =&lt;br /&gt;
people.&lt;strong&gt;ToXmlStream&lt;/strong&gt;("people", "http://www.nesterovsky-bros.com"))&lt;br /&gt;
using(var reader = XmlReader.Create(stream))&lt;br /&gt;
{&lt;br /&gt;
// XPath forward navigator is used as an input source.&lt;br /&gt;
transform.Transform(&lt;br /&gt;
new &lt;strong&gt;ForwardXPathNavigator&lt;/strong&gt;(reader),&lt;br /&gt;
new XsltArgumentList(),&lt;br /&gt;
output);&lt;br /&gt;
}&lt;br /&gt;
});&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
This way we have build a code that streams data directly from business data to a client
in a form of report. A set of utility functions and classes helped us to overcome
.NET&amp;#39;s limitations and to build simple code that one can easily support.
&lt;/p&gt;
&lt;p&gt;
The sources can be found at &lt;a href="http://www.nesterovsky-bros.com/download/Streaming.zip"&gt; Streaming.zip&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=cad54317-1876-4b98-a8d2-7e44e5dda1f7" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,cad54317-1876-4b98-a8d2-7e44e5dda1f7.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=24094700-ca7e-4e73-8b30-fbdc9d72072a</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,24094700-ca7e-4e73-8b30-fbdc9d72072a.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,24094700-ca7e-4e73-8b30-fbdc9d72072a.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=24094700-ca7e-4e73-8b30-fbdc9d72072a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the previous <a href="http://www.nesterovsky-bros.com/weblog/2012/07/22/StreamingEntityData.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.w3.org/TR/xslt-30/#streaming-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.w3.org/1999/XSL/Transform">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">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/download/Streaming.zip"> Streaming.zip</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a" />
      </body>
      <title>Streaming xslt transformation with ForwardXPathNavigator</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,24094700-ca7e-4e73-8b30-fbdc9d72072a.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/07/26/StreamingXsltTransformationWithForwardXPathNavigator.aspx</link>
      <pubDate>Thu, 26 Jul 2012 18:49:51 GMT</pubDate>
      <description>&lt;p&gt;
In the previous &lt;a href="http://www.nesterovsky-bros.com/weblog/2012/07/22/StreamingEntityData.aspx"&gt; post
about streaming&lt;/a&gt; we have dropped at the point where we have &lt;code&gt;XmlReader&lt;/code&gt; in
hands, which continously gets data from &lt;code&gt;IEnumerable&amp;lt;Person&amp;gt;&lt;/code&gt; source.
Now we shall remind about &lt;code&gt;ForwardXPathNavigator&lt;/code&gt; - a class we have built
back in 2002, which adds streaming transformations to .NET&amp;#39;s xslt processor.
&lt;/p&gt;
&lt;p&gt;
While &lt;code&gt;XslCompiledTransform&lt;/code&gt; is desperately obsolete, and no upgrade will
possibly follow; still it&amp;#39;s among the fastest xslt 1.0 processors. With &lt;code&gt; ForwardXPathNavigator&lt;/code&gt; we
add ability to transform input data of arbitrary size to this processor.
&lt;/p&gt;
&lt;p&gt;
We find it interesting that &lt;a href="http://www.w3.org/TR/xslt-30/#streaming-concepts"&gt; xslt
3.0 Working Draft defines streaming processing&lt;/a&gt; in a way that closely matches rules
for &lt;code&gt;ForwardXPathNavigator&lt;/code&gt;:
&lt;/p&gt;
&lt;div style="padding-left: 1em; font-style: italic"&gt;
&lt;p&gt;
Streaming achieves two important objectives: it allows large documents to be transformed
without requiring correspondingly large amounts of memory; and it allows the processor
to start producing output before it has finished receiving its input, thus reducing
latency.
&lt;/p&gt;
&lt;p&gt;
The rules for streamability, which are defined in detail in &lt;i&gt;19.3 Streamability
Analysis&lt;/i&gt;, impose two main constraints:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;
The only nodes reachable from the node that is currently being processed are its attributes
and namespaces, its ancestors and their attributes and namespaces, and its descendants
and their attributes and namespaces. The siblings of the node, and the siblings of
its ancestors, are not reachable in the tree, and any attempt to use their values
is a static error. However, constructs (for example, simple forms of &lt;code&gt;xsl:number&lt;/code&gt;,
and simple positional patterns) that require knowledge of the number of preceding
elements by name are permitted.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
When processing a given node in the tree, each descendant node can only be visited
once. Essentially this allows two styles of processing: either visit each of the children
once, and then process that child with the same restrictions applied; or process all
the descendants in a single pass, in which case it is not possible while processing
a descendant to make any further downward selection.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;
The only significant difference between &lt;code&gt;ForwardXPathNavigator&lt;/code&gt; and xlst
3.0 streaming is in that we reported violations of rules for streamability at runtime,
while xslt 3.0 attempts to perform this analysis at compile time.
&lt;/p&gt;
&lt;p&gt;
Here the C# code for the xslt streamed transformation:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; var transform = new XslCompiledTransform();&lt;br /&gt;
&lt;br /&gt;
transform.Load("People.xslt");&lt;br /&gt;
&lt;br /&gt;
// We have a streamed business data.&lt;br /&gt;
var people = Data.CreateRandomData(10000, 0, 0, 10000);&lt;br /&gt;
&lt;br /&gt;
// We want to see it as streamed xml data.&lt;br /&gt;
using(var stream =&lt;br /&gt;
people.ToXmlStream("people", "http://www.nesterovsky-bros.com"))&lt;br /&gt;
using(var reader = XmlReader.Create(stream))&lt;br /&gt;
using(var output = File.Create("people.html"))&lt;br /&gt;
{&lt;br /&gt;
// XPath forward navigator is used as an input source.&lt;br /&gt;
transform.Transform(&lt;br /&gt;
&lt;strong&gt;new ForwardXPathNavigator(reader)&lt;/strong&gt;,&lt;br /&gt;
new XsltArgumentList(),&lt;br /&gt;
output);&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Notice how &lt;code&gt;XmlReader&lt;/code&gt; is wrapped into &lt;code&gt;ForwardXPathNavigator&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
To complete the picture we need xslt that follows the streaming rules:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;&amp;lt;xsl:stylesheet version="1.0" xmlns:xsl="&lt;a href="http://www.w3.org/1999/XSL/Transform"&gt;http://www.w3.org/1999/XSL/Transform&lt;/a&gt;"&lt;br /&gt;
xmlns:msxsl="urn:schemas-microsoft-com:xslt"&lt;br /&gt;
xmlns:d="&lt;a href="http://www.nesterovsky-bros.com"&gt;http://www.nesterovsky-bros.com&lt;/a&gt;"&lt;br /&gt;
exclude-result-prefixes="msxsl d"&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsl:output method="html" indent="yes"/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Root template processed in the streaming mode. --&amp;gt;&lt;br /&gt;
&amp;lt;xsl:template match="/d:people"&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;List of persons&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;style type="text/css"&amp;gt;&lt;br /&gt;
.even&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
.odd&lt;br /&gt;
{&lt;br /&gt;
background: #d0d0d0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/style&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;table border="1"&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;ID&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;First name&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Last name&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;City&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Title&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Age&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsl:for-each select="d:person"&amp;gt;&lt;br /&gt;
&amp;lt;!-- 
&lt;br /&gt;
Get element snapshot.&lt;br /&gt;
A snapshot allows arbitrary access to the element&amp;#39;s content.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;xsl:variable name="person"&amp;gt;&lt;br /&gt;
&amp;lt;xsl:copy-of select="."/&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:variable&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsl:variable name="position" select="position()"/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsl:apply-templates mode="snapshot" select="msxsl:node-set($person)/d:person"&amp;gt;&lt;br /&gt;
&amp;lt;xsl:with-param name="position" select="$position"/&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:apply-templates&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:for-each&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:template&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsl:template mode="snapshot" match="d:person"&amp;gt;&lt;br /&gt;
&amp;lt;xsl:param name="position"/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;xsl:attribute name="class"&amp;gt;&lt;br /&gt;
&amp;lt;xsl:choose&amp;gt;&lt;br /&gt;
&amp;lt;xsl:when test="$position mod 2 = 1"&amp;gt;&lt;br /&gt;
&amp;lt;xsl:text&amp;gt;odd&amp;lt;/xsl:text&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:when&amp;gt;&lt;br /&gt;
&amp;lt;xsl:otherwise&amp;gt;&lt;br /&gt;
&amp;lt;xsl:text&amp;gt;even&amp;lt;/xsl:text&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:otherwise&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:choose&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:attribute&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:Id"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:FirstName"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:LastName"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:City"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:Title"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;xsl:value-of select="d:Age"/&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/xsl:template&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsl:stylesheet&amp;gt;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
So, we have started with a streamed entity data, proceeded to the streamed XmlReader
and reached to the streamed xslt transformation.
&lt;/p&gt;
&lt;p&gt;
But at the final post about streaming we shall remind a simple way of building WCF
service returning html stream from our xslt transformation.
&lt;/p&gt;
&lt;p&gt;
The sources can be found at &lt;a href="http://www.nesterovsky-bros.com/download/Streaming.zip"&gt; Streaming.zip&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=24094700-ca7e-4e73-8b30-fbdc9d72072a" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,24094700-ca7e-4e73-8b30-fbdc9d72072a.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=382cb645-ac89-4cbb-b2f9-14913fb1f743</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,382cb645-ac89-4cbb-b2f9-14913fb1f743.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,382cb645-ac89-4cbb-b2f9-14913fb1f743.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=382cb645-ac89-4cbb-b2f9-14913fb1f743</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you're using .NET's <code>IDictionary&lt;K, V&gt;</code> you have probably
found its access API too boring. Indeed at each access point you have to write a code
like this:
</p>
        <p>
          <code> MyValueType value;<br />
var hasValue = dictionary.TryGetValue(key, out value);<br />
...</code>
        </p>
        <p>
In many, if not in most, cases the value is of a reference type, and you do not usually
store <code>null</code> values, so it would be fine if dictionary returned <code>null</code> when
value does not exist for the key.
</p>
        <p>
To deal with this small nuisance we have declared a couple of accessor extension methods:
</p>
        <p>
          <code>public static class Extensions<br />
{<br />
public static V Get&lt;K, V&gt;(this IDictionary&lt;K, V&gt; dictionary, K key)<br />
where V: class<br />
{<br />
V value;<br /><br />
if (key == null)<br />
{<br />
value = null;<br />
}<br />
else<br />
{<br />
dictionary.TryGetValue(key, out value);<br />
}<br /><br />
return value;<br />
}<br /><br />
public static V Get&lt;K, V&gt;(this IDictionary&lt;K, V&gt; dictionary, K? key)<br />
where V: class<br />
where K: struct<br />
{<br />
V value;<br /><br />
if (key == null)<br />
{<br />
value = null;<br />
}<br />
else<br />
{<br />
dictionary.TryGetValue(key.GetValueOrDefault(), out value);<br />
}<br /><br />
return value;<br />
}<br />
}</code>
        </p>
        <p>
These methods simplify dictionary access to:
</p>
        <p>
          <code>var value = dictionary.Get(key);<br />
...</code>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=382cb645-ac89-4cbb-b2f9-14913fb1f743" />
      </body>
      <title>Dictionary extension</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,382cb645-ac89-4cbb-b2f9-14913fb1f743.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/07/25/DictionaryExtension.aspx</link>
      <pubDate>Wed, 25 Jul 2012 05:54:33 GMT</pubDate>
      <description>  &lt;p&gt;
If you&amp;#39;re using .NET&amp;#39;s &lt;code&gt;IDictionary&amp;lt;K, V&amp;gt;&lt;/code&gt; you have probably
found its access API too boring. Indeed at each access point you have to write a code
like this:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; MyValueType value;&lt;br /&gt;
var hasValue = dictionary.TryGetValue(key, out value);&lt;br /&gt;
...&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
In many, if not in most, cases the value is of a reference type, and you do not usually
store &lt;code&gt;null&lt;/code&gt; values, so it would be fine if dictionary returned &lt;code&gt;null&lt;/code&gt; when
value does not exist for the key.
&lt;/p&gt;
&lt;p&gt;
To deal with this small nuisance we have declared a couple of accessor extension methods:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;public static class Extensions&lt;br /&gt;
{&lt;br /&gt;
public static V Get&amp;lt;K, V&amp;gt;(this IDictionary&amp;lt;K, V&amp;gt; dictionary, K key)&lt;br /&gt;
where V: class&lt;br /&gt;
{&lt;br /&gt;
V value;&lt;br /&gt;
&lt;br /&gt;
if (key == null)&lt;br /&gt;
{&lt;br /&gt;
value = null;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
dictionary.TryGetValue(key, out value);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return value;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static V Get&amp;lt;K, V&amp;gt;(this IDictionary&amp;lt;K, V&amp;gt; dictionary, K? key)&lt;br /&gt;
where V: class&lt;br /&gt;
where K: struct&lt;br /&gt;
{&lt;br /&gt;
V value;&lt;br /&gt;
&lt;br /&gt;
if (key == null)&lt;br /&gt;
{&lt;br /&gt;
value = null;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
dictionary.TryGetValue(key.GetValueOrDefault(), out value);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return value;&lt;br /&gt;
}&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
These methods simplify dictionary access to:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;var value = dictionary.Get(key);&lt;br /&gt;
...&lt;/code&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=382cb645-ac89-4cbb-b2f9-14913fb1f743" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,382cb645-ac89-4cbb-b2f9-14913fb1f743.aspx</comments>
      <category>.NET</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=60b3cb37-5da9-4f28-a35f-38d1ef3cccce</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,60b3cb37-5da9-4f28-a35f-38d1ef3cccce.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,60b3cb37-5da9-4f28-a35f-38d1ef3cccce.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=60b3cb37-5da9-4f28-a35f-38d1ef3cccce</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
For some reason neither .NET's <code>XmlSerializer</code> nor <code>DataContractSerializer</code> allow
reading data through an <code>XmlReader</code>. These APIs work other way round writing
data into an <code>XmlWriter</code>. To get data through <code>XmlReader</code> one
needs to write it to some destination like a file or memory stream, and then to read
it using <code>XmlReader</code>. This complicates streaming design considerably.
</p>
        <p>
In fact the very same happens with other .NET APIs.
</p>
        <p>
We think the reason of why .NET designers preferred <code>XmlWriter</code> to <code>XmlReader</code> in
those APIs is that <code>XmlReader</code>'s implementation is a state machine
like, while <code>XmlWriter</code>'s implementation looks like a regular procedure.
It's much harder to <strong>manually</strong> write and to support a correct state
machine logic than a procedure.
</p>
        <p>
If history would have gone slightly different way, and if yield return, lambda, and
Enumerator API appeared before <code>XmlReader</code>, and <code>XmlWriter</code> then,
we think, both these classes looked differently. Xml source would have been described
with a <code>IEnumerable&lt;XmlEvent&gt;</code> instead of <code>XmlReader</code>,
and <code>XmlWriter</code> must be looked like a function receiving <code>IEnumerable&lt;XmlEvent&gt;</code>.
Implementing <code>XmlReader</code> would have meant a creating a enumerator. Yield
return and Enumerable API would have helped to implement it in a procedural way.
</p>
        <p>
But in our present we have to deal with the fact that <code>DataContractSerializer</code> should
write the data into <code>XmlWriter</code>, so let's assume we have a project
that uses Entity Framework to access the database, and that you have a data class <code>Person</code>,
and data access method <code>GetPeople()</code>:
</p>
        <p>
          <code> [DataContract(Name = "person", Namespace = "http://www.nesterovsky-bros.com")]<br />
public class Person 
<br />
{ 
<br />
[DataMember] public int Id { get; set; } 
<br />
[DataMember] public string FirstName { get; set; } 
<br />
[DataMember] public string LastName { get; set; } 
<br />
[DataMember] public string City { get; set; } 
<br />
[DataMember] public string Title { get; set; } 
<br />
[DataMember] public DateTime BirthDate { get; set; } 
<br />
[DataMember] public int Age { get; set; } 
<br />
} 
<br /><br />
public static IEnumerable&lt;Person&gt; GetPeople() { ... }</code>
        </p>
        <p>
And your goal is to expose result of <code>GetPeople()</code> as <code>XmlReader</code>.
We achieve result with three simple steps:
</p>
        <ol>
          <li>
Define <code>JoinedStream</code> - an input <code>Stream</code> implementation that
reads data from a enumeration of streams (<code>IEnumerable&lt;Stream&gt;</code>).</li>
          <li>
Build xml parts in the form of <code>IEnumerable&lt;Stream&gt;</code>.</li>
          <li>
Combine parts into final xml stream.</li>
        </ol>
        <p>
The code is rather simple, so here we qoute its essential part:
</p>
        <p>
          <code> public static class Extensions<br />
{<br />
public static Stream JoinStreams(this IEnumerable&lt;Stream&gt; streams, bool closeStreams
= true)<br />
{<br />
return new JoinedStream(streams, closeStreams);<br />
}<br /><br />
public static Stream ToXmlStream&lt;T&gt;(<br />
this IEnumerable&lt;T&gt; items, 
<br />
string rootName = null, 
<br />
string rootNamespace = null)<br />
{<br />
return items.ToXmlStreamParts&lt;T&gt;(rootName, rootNamespace).<br />
JoinStreams(false);<br />
}<br /><br />
private static IEnumerable&lt;Stream&gt; ToXmlStreamParts&lt;T&gt;(<br />
this IEnumerable&lt;T&gt; items,<br />
string rootName = null,<br />
string rootNamespace = null)<br />
{<br />
if (rootName == null)<br />
{<br />
rootName = "ArrayOfItems";<br />
}<br /><br />
if (rootNamespace == null)<br />
{<br />
rootNamespace = "";<br />
}<br /><br />
var serializer = new DataContractSerializer(typeof(T));<br />
var stream = new MemoryStream();<br />
var writer = XmlDictionaryWriter.CreateTextWriter(stream);<br /><br />
writer.WriteStartDocument();<br />
writer.WriteStartElement(rootName, rootNamespace);<br />
writer.WriteXmlnsAttribute("s", XmlSchema.Namespace);<br />
writer.WriteXmlnsAttribute("i", XmlSchema.InstanceNamespace);<br /><br />
foreach(var item in items)<br />
{<br />
serializer.WriteObject(writer, item);<br />
writer.WriteString(" ");<br /><br />
writer.Flush();<br />
stream.Position = 0;<br /><br />
yield return stream;<br /><br />
stream.Position = 0;<br />
stream.SetLength(0);<br />
}<br /><br />
writer.WriteEndElement();<br />
writer.WriteEndDocument();<br /><br />
writer.Flush();<br />
stream.Position = 0;<br /><br />
yield return stream;<br />
}<br /><br />
private class JoinedStream: Stream<br />
{<br />
public JoinedStream(IEnumerable&lt;Stream&gt; streams, bool closeStreams = true)<br />
...<br />
}<br />
} </code>
        </p>
        <p>
The use is even more simple:
</p>
        <p>
          <code> // We have a streamed business data.<br />
var people = GetPeople();<br /><br />
// We want to see it as streamed xml data.<br />
using(var stream = people.ToXmlStream("persons", "http://www.nesterovsky-bros.com"))<br />
using(var reader = XmlReader.Create(stream))<br />
{<br />
...<br />
}</code>
        </p>
        <p>
We have packed the sample into the project <a href="http://www.nesterovsky-bros.com/download/Streaming.zip">Streaming.zip</a>.
</p>
        <p>
In the <a href="http://www.nesterovsky-bros.com/weblog/2012/07/26/StreamingXsltTransformationWithForwardXPathNavigator.aspx">next
post</a> we're going to remind about streaming processing in xslt. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=60b3cb37-5da9-4f28-a35f-38d1ef3cccce" />
      </body>
      <title>Streaming entity data</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,60b3cb37-5da9-4f28-a35f-38d1ef3cccce.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/07/22/StreamingEntityData.aspx</link>
      <pubDate>Sun, 22 Jul 2012 20:38:29 GMT</pubDate>
      <description>  &lt;p&gt;
For some reason neither .NET&amp;#39;s &lt;code&gt;XmlSerializer&lt;/code&gt; nor &lt;code&gt;DataContractSerializer&lt;/code&gt; allow
reading data through an &lt;code&gt;XmlReader&lt;/code&gt;. These APIs work other way round writing
data into an &lt;code&gt;XmlWriter&lt;/code&gt;. To get data through &lt;code&gt;XmlReader&lt;/code&gt; one
needs to write it to some destination like a file or memory stream, and then to read
it using &lt;code&gt;XmlReader&lt;/code&gt;. This complicates streaming design considerably.
&lt;/p&gt;
&lt;p&gt;
In fact the very same happens with other .NET APIs.
&lt;/p&gt;
&lt;p&gt;
We think the reason of why .NET designers preferred &lt;code&gt;XmlWriter&lt;/code&gt; to &lt;code&gt;XmlReader&lt;/code&gt; in
those APIs is that &lt;code&gt;XmlReader&lt;/code&gt;&amp;#39;s implementation is a state machine
like, while &lt;code&gt;XmlWriter&lt;/code&gt;&amp;#39;s implementation looks like a regular procedure.
It&amp;#39;s much harder to &lt;strong&gt;manually&lt;/strong&gt; write and to support a correct state
machine logic than a procedure.
&lt;/p&gt;
&lt;p&gt;
If history would have gone slightly different way, and if yield return, lambda, and
Enumerator API appeared before &lt;code&gt;XmlReader&lt;/code&gt;, and &lt;code&gt;XmlWriter&lt;/code&gt; then,
we think, both these classes looked differently. Xml source would have been described
with a &lt;code&gt;IEnumerable&amp;lt;XmlEvent&amp;gt;&lt;/code&gt; instead of &lt;code&gt;XmlReader&lt;/code&gt;,
and &lt;code&gt;XmlWriter&lt;/code&gt; must be looked like a function receiving &lt;code&gt;IEnumerable&amp;lt;XmlEvent&amp;gt;&lt;/code&gt;.
Implementing &lt;code&gt;XmlReader&lt;/code&gt; would have meant a creating a enumerator. Yield
return and Enumerable API would have helped to implement it in a procedural way.
&lt;/p&gt;
&lt;p&gt;
But in our present we have to deal with the fact that &lt;code&gt;DataContractSerializer&lt;/code&gt; should
write the data into &lt;code&gt;XmlWriter&lt;/code&gt;, so let&amp;#39;s assume we have a project
that uses Entity Framework to access the database, and that you have a data class &lt;code&gt;Person&lt;/code&gt;,
and data access method &lt;code&gt;GetPeople()&lt;/code&gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; [DataContract(Name = &amp;quot;person&amp;quot;, Namespace = &amp;quot;http://www.nesterovsky-bros.com&amp;quot;)]&lt;br /&gt;
public class Person 
&lt;br /&gt;
{ 
&lt;br /&gt;
[DataMember] public int Id { get; set; } 
&lt;br /&gt;
[DataMember] public string FirstName { get; set; } 
&lt;br /&gt;
[DataMember] public string LastName { get; set; } 
&lt;br /&gt;
[DataMember] public string City { get; set; } 
&lt;br /&gt;
[DataMember] public string Title { get; set; } 
&lt;br /&gt;
[DataMember] public DateTime BirthDate { get; set; } 
&lt;br /&gt;
[DataMember] public int Age { get; set; } 
&lt;br /&gt;
} 
&lt;br /&gt;
&lt;br /&gt;
public static IEnumerable&amp;lt;Person&amp;gt; GetPeople() { ... }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
And your goal is to expose result of &lt;code&gt;GetPeople()&lt;/code&gt; as &lt;code&gt;XmlReader&lt;/code&gt;.
We achieve result with three simple steps:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Define &lt;code&gt;JoinedStream&lt;/code&gt; - an input &lt;code&gt;Stream&lt;/code&gt; implementation that
reads data from a enumeration of streams (&lt;code&gt;IEnumerable&amp;lt;Stream&amp;gt;&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
Build xml parts in the form of &lt;code&gt;IEnumerable&amp;lt;Stream&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
Combine parts into final xml stream.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The code is rather simple, so here we qoute its essential part:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; public static class Extensions&lt;br /&gt;
{&lt;br /&gt;
public static Stream JoinStreams(this IEnumerable&amp;lt;Stream&amp;gt; streams, bool closeStreams
= true)&lt;br /&gt;
{&lt;br /&gt;
return new JoinedStream(streams, closeStreams);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static Stream ToXmlStream&amp;lt;T&amp;gt;(&lt;br /&gt;
this IEnumerable&amp;lt;T&amp;gt; items, 
&lt;br /&gt;
string rootName = null, 
&lt;br /&gt;
string rootNamespace = null)&lt;br /&gt;
{&lt;br /&gt;
return items.ToXmlStreamParts&amp;lt;T&amp;gt;(rootName, rootNamespace).&lt;br /&gt;
JoinStreams(false);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
private static IEnumerable&amp;lt;Stream&amp;gt; ToXmlStreamParts&amp;lt;T&amp;gt;(&lt;br /&gt;
this IEnumerable&amp;lt;T&amp;gt; items,&lt;br /&gt;
string rootName = null,&lt;br /&gt;
string rootNamespace = null)&lt;br /&gt;
{&lt;br /&gt;
if (rootName == null)&lt;br /&gt;
{&lt;br /&gt;
rootName = "ArrayOfItems";&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (rootNamespace == null)&lt;br /&gt;
{&lt;br /&gt;
rootNamespace = "";&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var serializer = new DataContractSerializer(typeof(T));&lt;br /&gt;
var stream = new MemoryStream();&lt;br /&gt;
var writer = XmlDictionaryWriter.CreateTextWriter(stream);&lt;br /&gt;
&lt;br /&gt;
writer.WriteStartDocument();&lt;br /&gt;
writer.WriteStartElement(rootName, rootNamespace);&lt;br /&gt;
writer.WriteXmlnsAttribute("s", XmlSchema.Namespace);&lt;br /&gt;
writer.WriteXmlnsAttribute("i", XmlSchema.InstanceNamespace);&lt;br /&gt;
&lt;br /&gt;
foreach(var item in items)&lt;br /&gt;
{&lt;br /&gt;
serializer.WriteObject(writer, item);&lt;br /&gt;
writer.WriteString(" ");&lt;br /&gt;
&lt;br /&gt;
writer.Flush();&lt;br /&gt;
stream.Position = 0;&lt;br /&gt;
&lt;br /&gt;
yield return stream;&lt;br /&gt;
&lt;br /&gt;
stream.Position = 0;&lt;br /&gt;
stream.SetLength(0);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
writer.WriteEndElement();&lt;br /&gt;
writer.WriteEndDocument();&lt;br /&gt;
&lt;br /&gt;
writer.Flush();&lt;br /&gt;
stream.Position = 0;&lt;br /&gt;
&lt;br /&gt;
yield return stream;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
private class JoinedStream: Stream&lt;br /&gt;
{&lt;br /&gt;
public JoinedStream(IEnumerable&amp;lt;Stream&amp;gt; streams, bool closeStreams = true)&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The use is even more simple:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt; // We have a streamed business data.&lt;br /&gt;
var people = GetPeople();&lt;br /&gt;
&lt;br /&gt;
// We want to see it as streamed xml data.&lt;br /&gt;
using(var stream = people.ToXmlStream("persons", "http://www.nesterovsky-bros.com"))&lt;br /&gt;
using(var reader = XmlReader.Create(stream))&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
We have packed the sample into the project &lt;a href="http://www.nesterovsky-bros.com/download/Streaming.zip"&gt;Streaming.zip&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
In the &lt;a href="http://www.nesterovsky-bros.com/weblog/2012/07/26/StreamingXsltTransformationWithForwardXPathNavigator.aspx"&gt;next
post&lt;/a&gt; we&amp;#39;re going to remind about streaming processing in xslt. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=60b3cb37-5da9-4f28-a35f-38d1ef3cccce" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,60b3cb37-5da9-4f28-a35f-38d1ef3cccce.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=f6004aca-9fa1-4c23-b987-9de640d86baf</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,f6004aca-9fa1-4c23-b987-9de640d86baf.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,f6004aca-9fa1-4c23-b987-9de640d86baf.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=f6004aca-9fa1-4c23-b987-9de640d86baf</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Several days ago we've arrived to the blog "<a href="http://blogs.msdn.com/b/madst/archive/2007/05/11/recursive-lambda-expressions.aspx">Recursive
lambda expressions</a>". There, author asks how to write a lambda expression
that calculates a factorial (only expression statements are allowed).
</p>
        <p>
The problem by itself is rather artificial, but at times you feel an intellectual
pleasure solving such tasks by yourself. So, putting original blog post aside we devised
our answers. The shortest one goes like this:
</p>
        <ol>
          <li>
As C# lambda expression cannot refer to itself, so it have to receive itself as a
parameter, so:<br /><div style="padding: 1em"><code>factorial(factorial, n) = n &lt;= 1 ? 1 : n * factorial(factorial, n - 1);</code></div></li>
          <li>
To define such lambda expression we have to declare a delegate type that receives
a delegate of the same type:<br /><div style="padding: 1em"><code>delegate int Impl(Impl impl, int n);</code></div>
Fortunately, C# allows this, but a workaround could be used even if it were not possible.<br /><br /></li>
          <li>
To simplify the reasoning we've defined a two-expression version:<br /><div style="padding: 1em"><code>Impl impl = (f, n) =&gt; n &lt;= 1 ? 1 : n * f(f, n - 1); 
<br />
Func&lt;int, int&gt; factorial = i =&gt; impl(impl, i);</code></div></li>
          <li>
Finally, we've written out a one-expression version:<br /><div style="padding: 1em"><code>Func&lt;int, int&gt; factorial = i =&gt; ((Func&lt;Impl, int&gt;)(f =&gt; f(f,
i)))((f, n) =&gt; n &lt;= 1 ? 1 : n * f(f, n - 1)); </code></div></li>
          <li>
The use is:<br /><div style="padding: 1em"><code>var f = factorial(10);</code></div></li>
        </ol>
        <p>
After that excercise we've returned back to original blog and compared solutions.
We can see that author appeals to a set theory but for some reason his answer is more
complex than nesessary, but comments contain variants that analogous to our answer.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=f6004aca-9fa1-4c23-b987-9de640d86baf" />
      </body>
      <title>Weekend C# puzzle</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,f6004aca-9fa1-4c23-b987-9de640d86baf.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/04/08/WeekendCPuzzle.aspx</link>
      <pubDate>Sun, 08 Apr 2012 09:18:29 GMT</pubDate>
      <description>  &lt;p&gt;
Several days ago we&amp;#39;ve arrived to the blog &amp;quot;&lt;a href="http://blogs.msdn.com/b/madst/archive/2007/05/11/recursive-lambda-expressions.aspx"&gt;Recursive
lambda expressions&lt;/a&gt;&amp;quot;. There, author asks how to write a lambda expression
that calculates a factorial (only expression statements are allowed).
&lt;/p&gt;
&lt;p&gt;
The problem by itself is rather artificial, but at times you feel an intellectual
pleasure solving such tasks by yourself. So, putting original blog post aside we devised
our answers. The shortest one goes like this:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
As C# lambda expression cannot refer to itself, so it have to receive itself as a
parameter, so:&lt;br /&gt;
&lt;div style="padding: 1em"&gt;
&lt;code&gt;factorial(factorial, n) = n &amp;lt;= 1 ? 1 : n * factorial(factorial, n - 1);&lt;/code&gt; 
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
To define such lambda expression we have to declare a delegate type that receives
a delegate of the same type:&lt;br /&gt;
&lt;div style="padding: 1em"&gt;
&lt;code&gt;delegate int Impl(Impl impl, int n);&lt;/code&gt;
&lt;/div&gt;
Fortunately, C# allows this, but a workaround could be used even if it were not possible.&lt;br /&gt;
&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;
To simplify the reasoning we&amp;#39;ve defined a two-expression version:&lt;br /&gt;
&lt;div style="padding: 1em"&gt;
&lt;code&gt;Impl impl = (f, n) =&amp;gt; n &amp;lt;= 1 ? 1 : n * f(f, n - 1); 
&lt;br /&gt;
Func&amp;lt;int, int&amp;gt; factorial = i =&amp;gt; impl(impl, i);&lt;/code&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
Finally, we&amp;#39;ve written out a one-expression version:&lt;br /&gt;
&lt;div style="padding: 1em"&gt;
&lt;code&gt;Func&amp;lt;int, int&amp;gt; factorial = i =&amp;gt; ((Func&amp;lt;Impl, int&amp;gt;)(f =&amp;gt; f(f,
i)))((f, n) =&amp;gt; n &amp;lt;= 1 ? 1 : n * f(f, n - 1)); &lt;/code&gt; 
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
The use is:&lt;br /&gt;
&lt;div style="padding: 1em"&gt;
&lt;code&gt;var f = factorial(10);&lt;/code&gt; 
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
After that excercise we&amp;#39;ve returned back to original blog and compared solutions.
We can see that author appeals to a set theory but for some reason his answer is more
complex than nesessary, but comments contain variants that analogous to our answer.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=f6004aca-9fa1-4c23-b987-9de640d86baf" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,f6004aca-9fa1-4c23-b987-9de640d86baf.aspx</comments>
      <category>.NET</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=19c9c7e3-4b24-4407-806a-b118460873e3</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,19c9c7e3-4b24-4407-806a-b118460873e3.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,19c9c7e3-4b24-4407-806a-b118460873e3.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=19c9c7e3-4b24-4407-806a-b118460873e3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This time we <a href="http://www.nesterovsky-bros.com/download/languages-xom.zip">update
csharpxom</a> to adjust it to C# 4.5. Additions are<a href="http://msdn.microsoft.com/en-us/library/hh156513(v=vs.110).aspx"><code>async</code> modifier</a> and <a href="http://msdn.microsoft.com/en-us/library/hh156528(v=vs.110).aspx"><code>await</code> operator</a>.
</p>
        <p>
They are used to simplify asynchronous programming.
</p>
        <p>
The following example from the msdn:
</p>
        <p style="padding-left: 1em">
          <code>private async Task&lt;byte[]&gt; GetURLContentsAsync(string url)<br />
{<br />
var content = new MemoryStream();<br />
var request = (HttpWebRequest)WebRequest.Create(url);<br /><br />
using(var response = await request.GetResponseAsync())<br />
using(var responseStream = response.GetResponseStream())<br />
{<br />
await responseStream.CopyToAsync(content);<br />
}<br /><br />
return content.ToArray();<br />
}</code>
        </p>
        <p>
looks like this in csharpxom:
</p>
        <p style="padding-left: 1em">
          <code>&lt;method name="GetURLContentsAsync" access="private" <span style="font-weight: bold">async="true"</span>&gt;<br />
&lt;returns&gt;<br />
&lt;type name="Task" namespace="System.Threading.Tasks"&gt;<br />
&lt;type-arguments&gt;<br />
&lt;type name="byte" rank="1"/&gt;<br />
&lt;/type-arguments&gt;<br />
&lt;/type&gt;<br />
&lt;/returns&gt;<br />
&lt;parameters&gt;<br />
&lt;parameter name="url"&gt;<br />
&lt;type name="string"/&gt;<br />
&lt;/parameter&gt;<br />
&lt;/parameters&gt;<br />
&lt;block&gt;<br />
&lt;var name="content"&gt;<br />
&lt;initialize&gt;<br />
&lt;new-object&gt;<br />
&lt;type name="MemoryStream" namespace="System.IO"/&gt;<br />
&lt;/new-object&gt;<br />
&lt;/initialize&gt;<br />
&lt;/var&gt;<br />
&lt;var name="request"&gt;<br />
&lt;initialize&gt;<br />
&lt;cast&gt;<br />
&lt;invoke&gt;<br />
&lt;static-method-ref name="Create"&gt;<br />
&lt;type name="WebRequest" namespace="System.Net"/&gt;<br />
&lt;/static-method-ref&gt;<br />
&lt;arguments&gt;<br />
&lt;var-ref name="url"/&gt;<br />
&lt;/arguments&gt;<br />
&lt;/invoke&gt;<br />
&lt;type name="HttpWebRequest" namespace="System.Net"/&gt;<br />
&lt;/cast&gt;<br />
&lt;/initialize&gt;<br />
&lt;/var&gt;<br /><br />
&lt;using&gt;<br />
&lt;resource&gt;<br />
&lt;var name="response"&gt;<br />
&lt;initialize&gt;<br /><span style="font-weight: bold">&lt;await&gt;</span><br />
&lt;invoke&gt;<br />
&lt;method-ref name="GetResponseAsync"&gt;<br />
&lt;var-ref name="request"/&gt;<br />
&lt;/method-ref&gt;<br />
&lt;/invoke&gt;<br /><span style="font-weight: bold">&lt;/await&gt;</span><br />
&lt;/initialize&gt;<br />
&lt;/var&gt;<br />
&lt;/resource&gt;<br />
&lt;using&gt;<br />
&lt;resource&gt;<br />
&lt;var name="responseStream"&gt;<br />
&lt;initialize&gt;<br />
&lt;invoke&gt;<br />
&lt;method-ref name="GetResponseStream"&gt;<br />
&lt;var-ref name="response"/&gt;<br />
&lt;/method-ref&gt;<br />
&lt;/invoke&gt;<br />
&lt;/initialize&gt;<br />
&lt;/var&gt;<br />
&lt;/resource&gt;<br />
&lt;expression&gt;<br /><span style="font-weight: bold">&lt;await&gt;</span><br />
&lt;invoke&gt;<br />
&lt;method-ref name="CopyToAsync"&gt;<br />
&lt;var-ref name="responseStream"/&gt;<br />
&lt;/method-ref&gt;<br />
&lt;arguments&gt;<br />
&lt;var-ref name="content"/&gt;<br />
&lt;/arguments&gt;<br />
&lt;/invoke&gt;<br /><span style="font-weight: bold">&lt;/await&gt;</span><br />
&lt;/expression&gt;<br />
&lt;/using&gt;<br />
&lt;/using&gt;<br /><br />
&lt;return&gt;<br />
&lt;invoke&gt;<br />
&lt;method-ref name="ToArray"&gt;<br />
&lt;var-ref name="content"/&gt;<br />
&lt;/method-ref&gt;<br />
&lt;/invoke&gt;<br />
&lt;/return&gt;<br />
&lt;/block&gt;<br />
&lt;/method&gt;</code>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=19c9c7e3-4b24-4407-806a-b118460873e3" />
      </body>
      <title>Languages XOM update</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,19c9c7e3-4b24-4407-806a-b118460873e3.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/03/23/LanguagesXOMUpdate.aspx</link>
      <pubDate>Fri, 23 Mar 2012 00:07:35 GMT</pubDate>
      <description>  &lt;p&gt;
This time we &lt;a href="http://www.nesterovsky-bros.com/download/languages-xom.zip"&gt;update
csharpxom&lt;/a&gt; to adjust it to C# 4.5. Additions are&lt;a href="http://msdn.microsoft.com/en-us/library/hh156513(v=vs.110).aspx"&gt; &lt;code&gt;async&lt;/code&gt; modifier&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/hh156528(v=vs.110).aspx"&gt; &lt;code&gt;await&lt;/code&gt; operator&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
They are used to simplify asynchronous programming.
&lt;/p&gt;
&lt;p&gt;
The following example from the msdn:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;private async Task&amp;lt;byte[]&amp;gt; GetURLContentsAsync(string url)&lt;br /&gt;
{&lt;br /&gt;
var content = new MemoryStream();&lt;br /&gt;
var request = (HttpWebRequest)WebRequest.Create(url);&lt;br /&gt;
&lt;br /&gt;
using(var response = await request.GetResponseAsync())&lt;br /&gt;
using(var responseStream = response.GetResponseStream())&lt;br /&gt;
{&lt;br /&gt;
await responseStream.CopyToAsync(content);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return content.ToArray();&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
looks like this in csharpxom:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;&amp;lt;method name="GetURLContentsAsync" access="private" &lt;span style="font-weight: bold"&gt;async="true"&lt;/span&gt;&amp;gt;&lt;br /&gt;
&amp;lt;returns&amp;gt;&lt;br /&gt;
&amp;lt;type name="Task" namespace="System.Threading.Tasks"&amp;gt;&lt;br /&gt;
&amp;lt;type-arguments&amp;gt;&lt;br /&gt;
&amp;lt;type name="byte" rank="1"/&amp;gt;&lt;br /&gt;
&amp;lt;/type-arguments&amp;gt;&lt;br /&gt;
&amp;lt;/type&amp;gt;&lt;br /&gt;
&amp;lt;/returns&amp;gt;&lt;br /&gt;
&amp;lt;parameters&amp;gt;&lt;br /&gt;
&amp;lt;parameter name="url"&amp;gt;&lt;br /&gt;
&amp;lt;type name="string"/&amp;gt;&lt;br /&gt;
&amp;lt;/parameter&amp;gt;&lt;br /&gt;
&amp;lt;/parameters&amp;gt;&lt;br /&gt;
&amp;lt;block&amp;gt;&lt;br /&gt;
&amp;lt;var name="content"&amp;gt;&lt;br /&gt;
&amp;lt;initialize&amp;gt;&lt;br /&gt;
&amp;lt;new-object&amp;gt;&lt;br /&gt;
&amp;lt;type name="MemoryStream" namespace="System.IO"/&amp;gt;&lt;br /&gt;
&amp;lt;/new-object&amp;gt;&lt;br /&gt;
&amp;lt;/initialize&amp;gt;&lt;br /&gt;
&amp;lt;/var&amp;gt;&lt;br /&gt;
&amp;lt;var name="request"&amp;gt;&lt;br /&gt;
&amp;lt;initialize&amp;gt;&lt;br /&gt;
&amp;lt;cast&amp;gt;&lt;br /&gt;
&amp;lt;invoke&amp;gt;&lt;br /&gt;
&amp;lt;static-method-ref name="Create"&amp;gt;&lt;br /&gt;
&amp;lt;type name="WebRequest" namespace="System.Net"/&amp;gt;&lt;br /&gt;
&amp;lt;/static-method-ref&amp;gt;&lt;br /&gt;
&amp;lt;arguments&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="url"/&amp;gt;&lt;br /&gt;
&amp;lt;/arguments&amp;gt;&lt;br /&gt;
&amp;lt;/invoke&amp;gt;&lt;br /&gt;
&amp;lt;type name="HttpWebRequest" namespace="System.Net"/&amp;gt;&lt;br /&gt;
&amp;lt;/cast&amp;gt;&lt;br /&gt;
&amp;lt;/initialize&amp;gt;&lt;br /&gt;
&amp;lt;/var&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;using&amp;gt;&lt;br /&gt;
&amp;lt;resource&amp;gt;&lt;br /&gt;
&amp;lt;var name="response"&amp;gt;&lt;br /&gt;
&amp;lt;initialize&amp;gt;&lt;br /&gt;
&lt;span style="font-weight: bold"&gt;&amp;lt;await&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&amp;lt;invoke&amp;gt;&lt;br /&gt;
&amp;lt;method-ref name="GetResponseAsync"&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="request"/&amp;gt;&lt;br /&gt;
&amp;lt;/method-ref&amp;gt;&lt;br /&gt;
&amp;lt;/invoke&amp;gt;&lt;br /&gt;
&lt;span style="font-weight: bold"&gt;&amp;lt;/await&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&amp;lt;/initialize&amp;gt;&lt;br /&gt;
&amp;lt;/var&amp;gt;&lt;br /&gt;
&amp;lt;/resource&amp;gt;&lt;br /&gt;
&amp;lt;using&amp;gt;&lt;br /&gt;
&amp;lt;resource&amp;gt;&lt;br /&gt;
&amp;lt;var name="responseStream"&amp;gt;&lt;br /&gt;
&amp;lt;initialize&amp;gt;&lt;br /&gt;
&amp;lt;invoke&amp;gt;&lt;br /&gt;
&amp;lt;method-ref name="GetResponseStream"&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="response"/&amp;gt;&lt;br /&gt;
&amp;lt;/method-ref&amp;gt;&lt;br /&gt;
&amp;lt;/invoke&amp;gt;&lt;br /&gt;
&amp;lt;/initialize&amp;gt;&lt;br /&gt;
&amp;lt;/var&amp;gt;&lt;br /&gt;
&amp;lt;/resource&amp;gt;&lt;br /&gt;
&amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;span style="font-weight: bold"&gt;&amp;lt;await&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&amp;lt;invoke&amp;gt;&lt;br /&gt;
&amp;lt;method-ref name="CopyToAsync"&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="responseStream"/&amp;gt;&lt;br /&gt;
&amp;lt;/method-ref&amp;gt;&lt;br /&gt;
&amp;lt;arguments&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="content"/&amp;gt;&lt;br /&gt;
&amp;lt;/arguments&amp;gt;&lt;br /&gt;
&amp;lt;/invoke&amp;gt;&lt;br /&gt;
&lt;span style="font-weight: bold"&gt;&amp;lt;/await&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&amp;lt;/expression&amp;gt;&lt;br /&gt;
&amp;lt;/using&amp;gt;&lt;br /&gt;
&amp;lt;/using&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;return&amp;gt;&lt;br /&gt;
&amp;lt;invoke&amp;gt;&lt;br /&gt;
&amp;lt;method-ref name="ToArray"&amp;gt;&lt;br /&gt;
&amp;lt;var-ref name="content"/&amp;gt;&lt;br /&gt;
&amp;lt;/method-ref&amp;gt;&lt;br /&gt;
&amp;lt;/invoke&amp;gt;&lt;br /&gt;
&amp;lt;/return&amp;gt;&lt;br /&gt;
&amp;lt;/block&amp;gt;&lt;br /&gt;
&amp;lt;/method&amp;gt;&lt;/code&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=19c9c7e3-4b24-4407-806a-b118460873e3" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,19c9c7e3-4b24-4407-806a-b118460873e3.aspx</comments>
      <category>.NET</category>
      <category>Announce</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=8383cfc7-10ac-4f2a-a249-4cc37ddbac70</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=8383cfc7-10ac-4f2a-a249-4cc37ddbac70</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
For a long time we were developing web applications with ASP.NET and JSF. At present
we prefer rich clients and a server with page templates and RESTful web services.
</p>
        <p>
This transition brings technical questions. Consider this one.
</p>
        <p>
Browsers allow to store session state entirely on the client, so should we maintain
a session on the server?
</p>
        <p>
Since the server is just a set of web services, so we may supply all required arguments
on each call. 
</p>
        <p>
At first glance we can assume that no session is required on the server. However,
looking further we see that we should deal with data validation (security) on the
server. 
</p>
        <p>
Think about a classic ASP.NET application, where a user can select a value from a
dropdown. Either ASP.NET itself or your program (against a list from a session) verifies
that the value received is valid for the user. That list of values and might be other
parameters constitute a user profile, which we stored in session. The user profile
played important role (often indirectly) in the validation of input data.
</p>
        <p>
When the server is just a set of web services then we have to validate all parameters
manually. There are two sources that we can rely to: (a) <a href="http://msdn.microsoft.com/en-us/library/system.web.services.webservice.session.aspx"> a
session</a>, (b) <a href="http://msdn.microsoft.com/en-us/library/system.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://msdn.microsoft.com/en-us/library/ydy4x04a.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://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx"> Cache</a>,
which is also similar to (a) but which might be lighter than (at least not heavier
than) the solution with the session.
</p>
        <p>
What we see is that the client session does not free us from server session (or its
alternative).
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70" />
      </body>
      <title>Shifting to client side web applications</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/03/08/ShiftingToClientSideWebApplications.aspx</link>
      <pubDate>Thu, 08 Mar 2012 21:56:19 GMT</pubDate>
      <description>  &lt;p&gt;
For a long time we were developing web applications with ASP.NET and JSF. At present
we prefer rich clients and a server with page templates and RESTful web services.
&lt;/p&gt;
&lt;p&gt;
This transition brings technical questions. Consider this one.
&lt;/p&gt;
&lt;p&gt;
Browsers allow to store session state entirely on the client, so should we maintain
a session on the server?
&lt;/p&gt;
&lt;p&gt;
Since the server is just a set of web services, so we may supply all required arguments
on each call. 
&lt;/p&gt;
&lt;p&gt;
At first glance we can assume that no session is required on the server. However,
looking further we see that we should deal with data validation (security) on the
server. 
&lt;/p&gt;
&lt;p&gt;
Think about a classic ASP.NET application, where a user can select a value from a
dropdown. Either ASP.NET itself or your program (against a list from a session) verifies
that the value received is valid for the user. That list of values and might be other
parameters constitute a user profile, which we stored in session. The user profile
played important role (often indirectly) in the validation of input data.
&lt;/p&gt;
&lt;p&gt;
When the server is just a set of web services then we have to validate all parameters
manually. There are two sources that we can rely to: (a) &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.services.webservice.session.aspx"&gt; a
session&lt;/a&gt;, (b) &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.services.webservice.user.aspx"&gt; a
user principal&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
The case (a) is very similar to classic ASP.NET application except that with &lt;a href="http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx"&gt;EnableEventValidation="true"&lt;/a&gt; runtime
did it for us most of the time.&lt;br /&gt;
The case (b) requires reconstruction of the user profile for a user principal and
then we proceed with validation of parameters. 
&lt;/p&gt;
&lt;p&gt;
We may cache user profile in session, in which case we reduce (b) to (a); on the other
hand we may cache user profile in &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx"&gt; Cache&lt;/a&gt;,
which is also similar to (a) but which might be lighter than (at least not heavier
than) the solution with the session.
&lt;/p&gt;
&lt;p&gt;
What we see is that the client session does not free us from server session (or its
alternative).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>Java</category>
      <category>JSF and Facelets</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=7fb6be18-2165-4027-8554-94d7aa5e2683</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7fb6be18-2165-4027-8554-94d7aa5e2683.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,7fb6be18-2165-4027-8554-94d7aa5e2683.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=7fb6be18-2165-4027-8554-94d7aa5e2683</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We were dealing with a datasource of <code>(int? id, string value)</code> pairs in
LINQ. The data has originated from a database where <code>id</code> is unique field.
In the program this datasource had to be seen as a dictionary, so we have written
a code like this:
</p>
        <p style="padding-left: 1em;">
          <code>var dictionary = CreateIDValuePairs().ToDictionary(item =&gt; item.ID, item
=&gt; item.Value)</code>;
</p>
        <p>
That was too simple-minded. This code compiles but crashes at runtime when there is
an <code>id == null</code>.
</p>
        <p>
Well, help warns about this behaviour, but anyway this does not make pain easier. 
</p>
        <p>
In our opinion this restriction is not justified and just complicates the use of <code>Dictionaty</code>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7fb6be18-2165-4027-8554-94d7aa5e2683" />
      </body>
      <title>.NET Dictionary nuisance</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7fb6be18-2165-4027-8554-94d7aa5e2683.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/02/29/NETDictionaryNuisance.aspx</link>
      <pubDate>Wed, 29 Feb 2012 20:42:46 GMT</pubDate>
      <description>&lt;p&gt;
We were dealing with a datasource of &lt;code&gt;(int? id, string value)&lt;/code&gt; pairs in
LINQ. The data has originated from a database where &lt;code&gt;id&lt;/code&gt; is unique field.
In the program this datasource had to be seen as a dictionary, so we have written
a code like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em;"&gt;
&lt;code&gt;var dictionary = CreateIDValuePairs().ToDictionary(item =&amp;gt; item.ID, item
=&amp;gt; item.Value)&lt;/code&gt;;
&lt;/p&gt;
&lt;p&gt;
That was too simple-minded. This code compiles but crashes at runtime when there is
an &lt;code&gt;id == null&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
Well, help warns about this behaviour, but anyway this does not make pain easier. 
&lt;/p&gt;
&lt;p&gt;
In our opinion this restriction is not justified and just complicates the use of &lt;code&gt;Dictionaty&lt;/code&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7fb6be18-2165-4027-8554-94d7aa5e2683" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,7fb6be18-2165-4027-8554-94d7aa5e2683.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=7a2cda28-6e38-48d8-a233-302ba6e1d75e</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=7a2cda28-6e38-48d8-a233-302ba6e1d75e</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A bit history: the <a href="http://www.bphx.com/en/NewsEvents/PressReleases/Pages/072902.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/data/projects.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/data/projects.xml">eXperanto</a> (the design-time
tool and run-time libraries for Java and .NET) were developed, well tested, and delivered
by us to production already several years ago.</li>
          <li>
the client bought this set (the tool and libraries).</li>
          <li>
the set is in production yet already in another big company, and is used time to time
by our company in different migration projects.</li>
          <li>
the client talks with developers of this tool and run-time libraries, and he knows
about this fact.</li>
          <li>
the client uses widely open source solutions even without dedicated vendors or support
warranties.</li>
        </ul>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e" />
      </body>
      <title>eXperanto project actual yet...</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/11/10/eXperantoProjectActualYet.aspx</link>
      <pubDate>Thu, 10 Nov 2011 22:14:09 GMT</pubDate>
      <description>&lt;p&gt;
A bit history: the &lt;a href="http://www.bphx.com/en/NewsEvents/PressReleases/Pages/072902.aspx" target="_blank"&gt;first
release&lt;/a&gt; of this solution was about 9.5 years ago...
&lt;/p&gt;
&lt;p&gt;
Today we've run into a strange situation. One of our clients ask us about automatic
conversion of data from mainframe (that were defined as COBOL copybooks) into XML
or Java/.NET objects. On our suggestion to use &lt;a href="http://www.nesterovsky-bros.com/data/projects.xml"&gt;eXperanto&lt;/a&gt;,
which is well known to him, he stated that he wouldn't like to use a tool of a company
that is no more exists...
&lt;/p&gt;
&lt;p&gt;
The situation, in our opinion, become more strange when you consider the following:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://www.nesterovsky-bros.com/data/projects.xml"&gt;eXperanto&lt;/a&gt; (the design-time
tool and run-time libraries for Java and .NET) were developed, well tested, and delivered
by us to production already several years ago.&lt;/li&gt;
&lt;li&gt;
the client bought this set (the tool and libraries).&lt;/li&gt;
&lt;li&gt;
the set is in production yet already in another big company, and is used time to time
by our company in different migration projects.&lt;/li&gt;
&lt;li&gt;
the client talks with developers of this tool and run-time libraries, and he knows
about this fact.&lt;/li&gt;
&lt;li&gt;
the client uses widely open source solutions even without dedicated vendors or support
warranties.&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</comments>
      <category>.NET</category>
      <category>Java</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=243e0e15-c07d-4a6b-87f9-c0e89f52b781</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,243e0e15-c07d-4a6b-87f9-c0e89f52b781.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,243e0e15-c07d-4a6b-87f9-c0e89f52b781.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=243e0e15-c07d-4a6b-87f9-c0e89f52b781</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We're not big fans of <a href="http://msdn.microsoft.com/en-us/library/bb399572.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://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/a0b94f65-dc89-48f8-8b1a-a299fe0f6381/"> Stored
Proc and Char parm</a></p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=243e0e15-c07d-4a6b-87f9-c0e89f52b781" />
      </body>
      <title>Entity Framework and char parameters</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,243e0e15-c07d-4a6b-87f9-c0e89f52b781.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/09/06/EntityFrameworkAndCharParameters.aspx</link>
      <pubDate>Tue, 06 Sep 2011 21:11:38 GMT</pubDate>
      <description>&lt;p&gt;
We&amp;#39;re not big fans of &lt;a href="http://msdn.microsoft.com/en-us/library/bb399572.aspx"&gt; Entity
Framework&lt;/a&gt;, as we don&amp;#39;t directly expose the database structure to the client
program but rather through stored procedures and functions. So, EF for us is a tool
to expose those stored procedures as .NET wrappers. This limited use of EF still greatly
automates the data access code.
&lt;/p&gt;
&lt;p&gt;
But what we have lately found is that the EF has a problem with char parameters. Namely,
if you import a procedure say &lt;code&gt;MyProc&lt;/code&gt; that accepts &lt;code&gt;char(1)&lt;/code&gt;,
and then will call it through the generated wrapper, the you will see in sql profiler
that &lt;code&gt;char(1)&lt;/code&gt; parameter is passed with many trailing spaces as if it were &lt;code&gt;char(8000)&lt;/code&gt;.
There isn&amp;#39;t necessity to prove that this is highly ineffective.
&lt;/p&gt;
&lt;p&gt;
We can see that the problem happens in VS 2010 designer rather than in the EF runtime,
as SP&amp;#39;s parameters are not attributed with length, see model xml (*.edmx):
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;&amp;lt;Function Name=&amp;quot;MyProc&amp;quot; Schema=&amp;quot;Data&amp;quot;&amp;gt; 
&lt;br /&gt;
&amp;nbsp; ...&lt;br /&gt;
&amp;nbsp; &amp;lt;Parameter Name=&amp;quot;recipientType&amp;quot; Type=&amp;quot;char&amp;quot; Mode=&amp;quot;In&amp;quot;
/&amp;gt;&lt;br /&gt;
&amp;nbsp; ...&lt;br /&gt;
&amp;lt;/Function&amp;gt;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
while if we set:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;&amp;nbsp; &amp;lt;Parameter Name=&amp;quot;recipientType&amp;quot; Type=&amp;quot;char&amp;quot; MaxLength=&amp;quot;1&amp;quot;
Mode=&amp;quot;In&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
the runtime starts working as expected. So the workaround is to fix model file manually.
&lt;/p&gt;
&lt;p&gt;
See also: &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/a0b94f65-dc89-48f8-8b1a-a299fe0f6381/"&gt; Stored
Proc and Char parm&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=243e0e15-c07d-4a6b-87f9-c0e89f52b781" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,243e0e15-c07d-4a6b-87f9-c0e89f52b781.aspx</comments>
      <category>.NET</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
  </channel>
</rss>