<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Felice Pollano Blog - CodeProject</title>
    <link>http://www.felicepollano.com/</link>
    <description>The official Fatica Labs Blog!</description>
    <language>en-us</language>
    <copyright>Felice Pollano</copyright>
    <lastBuildDate>Fri, 20 Jul 2012 09:31:10 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>felice@felicepollano.com</managingEditor>
    <webMaster>felice@felicepollano.com</webMaster>
    <item>
      <trackback:ping>http://www.felicepollano.com/Trackback.aspx?guid=17b081fa-d909-492d-848b-4a0eb158b75c</trackback:ping>
      <pingback:server>http://www.felicepollano.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.felicepollano.com/PermaLink.aspx?guid=17b081fa-d909-492d-848b-4a0eb158b75c</pingback:target>
      <dc:creator>Felice Pollano</dc:creator>
      <wfw:comment>http://www.felicepollano.com/CommentView.aspx?guid=17b081fa-d909-492d-848b-4a0eb158b75c</wfw:comment>
      <wfw:commentRss>http://www.felicepollano.com/SyndicationService.asmx/GetEntryCommentsRss?guid=17b081fa-d909-492d-848b-4a0eb158b75c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
As <a href="http://weblogs.asp.net/scottgu/archive/2012/07/19/entity-framework-and-open-source.aspx">announced
by Scott Guthrie</a> EF is today available as Open Source on <a href="http://entityframework.codeplex.com/">Codeplex</a>.
As usual I had a first glance at the code to see what's inside. Is a big codebase
as you can guess,but even with a first sight it is possible to spot some interesting
things to learn. Here my list:
</p>
        <ul>
          <li>
            <a href="http://entityframework.codeplex.com/SourceControl/changeset/view/f26ae451505b#src/EntityFramework.PowerShell/ConnectionFactoryConfig/SqlServerDetector.cs">Discover
local Sql server instances</a>
          </li>
          <li>
            <a href="http://entityframework.codeplex.com/SourceControl/changeset/view/f26ae451505b#src/EntityFramework/Utilities/TypeExtensions.cs">Interesting
Type extensions ( IsNullable/IsCollection )</a>
          </li>
          <li>
            <a href="http://entityframework.codeplex.com/SourceControl/changeset/view/f26ae451505b#src/EntityFramework/Utilities/ExpressionExtensions.cs">Extension
for LambdaExpression</a>
          </li>
          <li>
            <a href="http://entityframework.codeplex.com/SourceControl/changeset/view/f26ae451505b#src/EntityFramework.SqlServer/SqlGen/SqlGenerator.cs">A
fully fledged ExpressionVisitor generating SQL</a>
          </li>
          <li>
            <a href="http://entityframework.codeplex.com/SourceControl/changeset/view/f26ae451505b#src/EntityFramework/Migrations/Utilities/IndentedTextWriter.cs">A
TextWriter with Ident support</a>
          </li>
        </ul>
        <p>
So nothing really complex, just good code snippets. Interesting, they internally uses <a href="http://xunit.codeplex.com/">XUnit</a> for
unit testing, not MSTest, and the framework for mocking is <a href="http://moq.github.com/moq/">MoQ</a>.
</p>
        <img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=17b081fa-d909-492d-848b-4a0eb158b75c" />
      </body>
      <title>The 5 thing to immediately grab from EF Open Source</title>
      <guid isPermaLink="false">http://www.felicepollano.com/PermaLink.aspx?guid=17b081fa-d909-492d-848b-4a0eb158b75c</guid>
      <link>http://www.felicepollano.com/2012/07/20/The5ThingToImmediatelyGrabFromEFOpenSource.aspx</link>
      <pubDate>Fri, 20 Jul 2012 09:31:10 GMT</pubDate>
      <description>
&lt;p&gt;
As &lt;a href="http://weblogs.asp.net/scottgu/archive/2012/07/19/entity-framework-and-open-source.aspx"&gt;announced
by Scott Guthrie&lt;/a&gt; EF is today available as Open Source on &lt;a href="http://entityframework.codeplex.com/"&gt;Codeplex&lt;/a&gt;.
As usual I had a first glance at the code to see what's inside. Is a big codebase
as you can guess,but even with a first sight it is possible to spot some interesting
things to learn. Here my list:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://entityframework.codeplex.com/SourceControl/changeset/view/f26ae451505b#src/EntityFramework.PowerShell/ConnectionFactoryConfig/SqlServerDetector.cs"&gt;Discover
local Sql server instances&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://entityframework.codeplex.com/SourceControl/changeset/view/f26ae451505b#src/EntityFramework/Utilities/TypeExtensions.cs"&gt;Interesting
Type extensions ( IsNullable/IsCollection )&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://entityframework.codeplex.com/SourceControl/changeset/view/f26ae451505b#src/EntityFramework/Utilities/ExpressionExtensions.cs"&gt;Extension
for LambdaExpression&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://entityframework.codeplex.com/SourceControl/changeset/view/f26ae451505b#src/EntityFramework.SqlServer/SqlGen/SqlGenerator.cs"&gt;A
fully fledged ExpressionVisitor generating SQL&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://entityframework.codeplex.com/SourceControl/changeset/view/f26ae451505b#src/EntityFramework/Migrations/Utilities/IndentedTextWriter.cs"&gt;A
TextWriter with Ident support&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
So nothing really complex, just good code snippets. Interesting, they internally uses &lt;a href="http://xunit.codeplex.com/"&gt;XUnit&lt;/a&gt; for
unit testing, not MSTest, and the framework for mocking is &lt;a href="http://moq.github.com/moq/"&gt;MoQ&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=17b081fa-d909-492d-848b-4a0eb158b75c" /&gt;</description>
      <comments>http://www.felicepollano.com/CommentView.aspx?guid=17b081fa-d909-492d-848b-4a0eb158b75c</comments>
      <category>C#</category>
      <category>CodeProject</category>
    </item>
    <item>
      <trackback:ping>http://www.felicepollano.com/Trackback.aspx?guid=d0df046c-d902-44bb-922c-55c4856945df</trackback:ping>
      <pingback:server>http://www.felicepollano.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.felicepollano.com/PermaLink.aspx?guid=d0df046c-d902-44bb-922c-55c4856945df</pingback:target>
      <dc:creator>Felice Pollano</dc:creator>
      <wfw:comment>http://www.felicepollano.com/CommentView.aspx?guid=d0df046c-d902-44bb-922c-55c4856945df</wfw:comment>
      <wfw:commentRss>http://www.felicepollano.com/SyndicationService.asmx/GetEntryCommentsRss?guid=d0df046c-d902-44bb-922c-55c4856945df</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p align="justify">
There are scenarios in which <a href="http://nhforge.org" target="_blank">NHibernate</a> performance
decrease even if we do all the effort to correctly use it. This could happen if we
need in some circumstances to load a lot of record ( I explicitly use record instead
of ‘Entity’ ) from some relational structures, and doing this the <strong>OR/M</strong> way
means overload the session with a lot of entities, that is painful in term of speed.
Other cases happens when we need to  write or update something that is not properly
represented in the entity model we have, maybe because the model is more “read” oriented.
Other cases? I’m not able to grasp all  of course, but I’m sure that you face
some if you use an OR/M ( not necessarily NH ) in your daily basis. Using NHibernate
an alternative could be using <strong>FlushMode=Never</strong> in session, but you
still have all the OR/M plumbing in the hydrating entity code that negatively impacts
the performances. I obtained impressive results in solving such a situation, by using <a href="http://code.google.com/p/dapper-dot-net/" target="_blank">Dapper</a>,
a so called single file OR/M. It is a single file that provider some <strong>IDbConnection</strong> extension
methods, those methods works on an already opened connection, so we can use the connection
sticked to the NHibernate open session, as here below: 
</p>
        <pre lang="C#">// don't get confused by LinqToNh Query&lt;&gt; this one is the Dapper query
// acting on the CONNECTION :)

session.Connection.Query&lt;MyDto&gt;("select Name=t.Name,Mail=t.Mail from mytable t where t.Valid=@Valid",new{Valid=true});




</pre>
        <p>
you obtain back a big recordset of MyDto instances in almost the same time if you
wire by hand a DateReader vertical on the dto, with all the error checking. 
</p>
        <h1>
        </h1>
        <h5>So why don’t use it always?
</h5>
        <p>
Because despite the name Dapper is not an OR/M, it does not keep track of modified
entities, it does not help you in paginating results or lazy load the entity graph,
neither helps in porting from one SQL dialect to another. 
</p>
        <h6>
        </h6>
        <h5>Is this strategy used somewhere else?
</h5>
        <p>
You probably find interesting to read <a href="http://samsaffron.com/archive/2011/03/30/How+I+learned+to+stop+worrying+and+write+my+own+ORM" target="_blank">this
post</a> by <a href="http://samsaffron.com/" target="_blank">Sam Saffron</a>, this
solution is used in Stackoverflow.com combined with the LinqToSql OR/M to help when
the OR/M performance are not enough. 
</p>
        <p>
By my test I experienced a performance increase of <strong>10x</strong> in a very
hacking situation, but I can’t show the case since it is not public code. Something
more scientific about performance is <a href="http://code.google.com/p/dapper-dot-net/" target="_blank">here</a>. 
</p>
        <img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=d0df046c-d902-44bb-922c-55c4856945df" />
      </body>
      <title>NHibernate performance hacks</title>
      <guid isPermaLink="false">http://www.felicepollano.com/PermaLink.aspx?guid=d0df046c-d902-44bb-922c-55c4856945df</guid>
      <link>http://www.felicepollano.com/2012/04/12/NHibernatePerformanceHacks.aspx</link>
      <pubDate>Thu, 12 Apr 2012 08:41:12 GMT</pubDate>
      <description>&lt;p align="justify"&gt;
There are scenarios in which &lt;a href="http://nhforge.org" target="_blank"&gt;NHibernate&lt;/a&gt; performance
decrease even if we do all the effort to correctly use it. This could happen if we
need in some circumstances to load a lot of record ( I explicitly use record instead
of ‘Entity’ ) from some relational structures, and doing this the &lt;strong&gt;OR/M&lt;/strong&gt; way
means overload the session with a lot of entities, that is painful in term of speed.
Other cases happens when we need to&amp;nbsp; write or update something that is not properly
represented in the entity model we have, maybe because the model is more “read” oriented.
Other cases? I’m not able to grasp all&amp;nbsp; of course, but I’m sure that you face
some if you use an OR/M ( not necessarily NH ) in your daily basis. Using NHibernate
an alternative could be using &lt;strong&gt;FlushMode=Never&lt;/strong&gt; in session, but you
still have all the OR/M plumbing in the hydrating entity code that negatively impacts
the performances. I obtained impressive results in solving such a situation, by using &lt;a href="http://code.google.com/p/dapper-dot-net/" target="_blank"&gt;Dapper&lt;/a&gt;,
a so called single file OR/M. It is a single file that provider some &lt;strong&gt;IDbConnection&lt;/strong&gt; extension
methods, those methods works on an already opened connection, so we can use the connection
sticked to the NHibernate open session, as here below: 
&lt;/p&gt;
&lt;pre lang="C#"&gt;// don't get confused by LinqToNh Query&amp;lt;&amp;gt; this one is the Dapper query
// acting on the CONNECTION :)

session.Connection.Query&amp;lt;MyDto&amp;gt;("select Name=t.Name,Mail=t.Mail from mytable t where t.Valid=@Valid",new{Valid=true});




&lt;/pre&gt;
&lt;p&gt;
you obtain back a big recordset of MyDto instances in almost the same time if you
wire by hand a DateReader vertical on the dto, with all the error checking. 
&lt;/p&gt;
&lt;h1&gt;
&lt;/h1&gt;
&lt;h5&gt;So why don’t use it always?
&lt;/h5&gt;
&lt;p&gt;
Because despite the name Dapper is not an OR/M, it does not keep track of modified
entities, it does not help you in paginating results or lazy load the entity graph,
neither helps in porting from one SQL dialect to another. 
&lt;/p&gt;
&lt;h6&gt;
&lt;/h6&gt;
&lt;h5&gt;Is this strategy used somewhere else?
&lt;/h5&gt;
&lt;p&gt;
You probably find interesting to read &lt;a href="http://samsaffron.com/archive/2011/03/30/How+I+learned+to+stop+worrying+and+write+my+own+ORM" target="_blank"&gt;this
post&lt;/a&gt; by &lt;a href="http://samsaffron.com/" target="_blank"&gt;Sam Saffron&lt;/a&gt;, this
solution is used in Stackoverflow.com combined with the LinqToSql OR/M to help when
the OR/M performance are not enough. 
&lt;/p&gt;
&lt;p&gt;
By my test I experienced a performance increase of &lt;strong&gt;10x&lt;/strong&gt; in a very
hacking situation, but I can’t show the case since it is not public code. Something
more scientific about performance is &lt;a href="http://code.google.com/p/dapper-dot-net/" target="_blank"&gt;here&lt;/a&gt;. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=d0df046c-d902-44bb-922c-55c4856945df" /&gt;</description>
      <comments>http://www.felicepollano.com/CommentView.aspx?guid=d0df046c-d902-44bb-922c-55c4856945df</comments>
      <category>CodeProject</category>
      <category>Dapper</category>
      <category>NHibernate</category>
      <category>ORM</category>
    </item>
    <item>
      <trackback:ping>http://www.felicepollano.com/Trackback.aspx?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</trackback:ping>
      <pingback:server>http://www.felicepollano.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.felicepollano.com/PermaLink.aspx?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</pingback:target>
      <dc:creator>Felice Pollano</dc:creator>
      <wfw:comment>http://www.felicepollano.com/CommentView.aspx?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</wfw:comment>
      <wfw:commentRss>http://www.felicepollano.com/SyndicationService.asmx/GetEntryCommentsRss?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Some days ago I came <a href="http://code.google.com/p/moq/issues/detail?id=249" target="_blank">in
this issue regarding thread non-safety</a> when using <a href="https://github.com/Moq" target="_blank">MoQ</a>.
So I simple create <a href="https://github.com/FelicePollano/Moq4" target="_blank">my
own fork on GitHub</a> and solved the issue, that was really easy to do, and as a
result I obtained a Mock stable even when mocked methods are called from multiple
threads. I created a bounch of test to prove that worked, here below one as a sample:
</p>
        <p>
          <a href="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/clip_image001_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image001" border="0" alt="clip_image001" src="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/clip_image001_thumb.png" width="429" height="349" />
          </a>
        </p>
        <p>
So with this I made the Mock thread safe at the infrastructure level, that mean no
more strange NullreferenceException and others. <strong><em>But what if we want our
mock not thread safe? </em></strong>I mean there could be situation in which <strong>we
want to ensure the system under test calls a certain method from a single thread</strong>, 
in other word we want the mock to <strong>explicitly require single thread access
to certain methods</strong>. This could happen for example when we are mocking some
UI components, but there is such situations every time the object we are mocking is
intrinsically non thread safe and the SUT is multithreaded. So I extended the MoQ
fluent language from the internal and I obtain something like…  the example below: 
</p>
        <p>
          <a href="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/moqts_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="moqts" border="0" alt="moqts" src="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/moqts_thumb.png" width="542" height="478" />
          </a>
        </p>
        <p>
So in the setup phase we declare a method ( or a setter, or a getter, as usual ) to
be SingleThread(). This yield a mock throwing when the method is called from a different
thread from the one which did the setup. 
</p>
        <p>
If you are happy with this modifications ( you would for sure find helpful the thread
safety by its own ) feel free to check out <a href="https://github.com/FelicePollano/Moq4" target="_blank">my
code fork on GitHub</a>, in any case I’m trying to have that modification pulled from
the main stream.
</p>
        <img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=ece637df-2668-46c2-b0e7-9d9b1ef8ce64" />
      </body>
      <title>Moq4 thread safety and more</title>
      <guid isPermaLink="false">http://www.felicepollano.com/PermaLink.aspx?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</guid>
      <link>http://www.felicepollano.com/2012/03/21/Moq4ThreadSafetyAndMore.aspx</link>
      <pubDate>Wed, 21 Mar 2012 15:53:26 GMT</pubDate>
      <description>&lt;p&gt;
Some days ago I came &lt;a href="http://code.google.com/p/moq/issues/detail?id=249" target="_blank"&gt;in
this issue regarding thread non-safety&lt;/a&gt; when using &lt;a href="https://github.com/Moq" target="_blank"&gt;MoQ&lt;/a&gt;.
So I simple create &lt;a href="https://github.com/FelicePollano/Moq4" target="_blank"&gt;my
own fork on GitHub&lt;/a&gt; and solved the issue, that was really easy to do, and as a
result I obtained a Mock stable even when mocked methods are called from multiple
threads. I created a bounch of test to prove that worked, here below one as a sample:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/clip_image001_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image001" border="0" alt="clip_image001" src="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/clip_image001_thumb.png" width="429" height="349"&gt;&lt;/a&gt; 
&lt;p&gt;
So with this I made the Mock thread safe at the infrastructure level, that mean no
more strange NullreferenceException and others. &lt;strong&gt;&lt;em&gt;But what if we want our
mock not thread safe? &lt;/em&gt;&lt;/strong&gt;I mean there could be situation in which &lt;strong&gt;we
want to ensure the system under test calls a certain method from a single thread&lt;/strong&gt;,&amp;nbsp;
in other word we want the mock to &lt;strong&gt;explicitly require single thread access
to certain methods&lt;/strong&gt;. This could happen for example when we are mocking some
UI components, but there is such situations every time the object we are mocking is
intrinsically non thread safe and the SUT is multithreaded. So I extended the MoQ
fluent language from the internal and I obtain something like…&amp;nbsp; the example below: 
&lt;p&gt;
&lt;a href="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/moqts_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="moqts" border="0" alt="moqts" src="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/moqts_thumb.png" width="542" height="478"&gt;&lt;/a&gt; 
&lt;p&gt;
So in the setup phase we declare a method ( or a setter, or a getter, as usual ) to
be SingleThread(). This yield a mock throwing when the method is called from a different
thread from the one which did the setup. 
&lt;/p&gt;
&lt;p&gt;
If you are happy with this modifications ( you would for sure find helpful the thread
safety by its own ) feel free to check out &lt;a href="https://github.com/FelicePollano/Moq4" target="_blank"&gt;my
code fork on GitHub&lt;/a&gt;, in any case I’m trying to have that modification pulled from
the main stream.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=ece637df-2668-46c2-b0e7-9d9b1ef8ce64" /&gt;</description>
      <comments>http://www.felicepollano.com/CommentView.aspx?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</comments>
      <category>C#</category>
      <category>CodeProject</category>
      <category>MoQ</category>
    </item>
  </channel>
</rss>