<?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>Peli's Farm - Pex, Stubs, Moles, QuickGraph, MbUnit, Reflector Addins - CciSharp</title>
    <link>http://blog.dotnetwiki.org/</link>
    <description>TouchDevelop, Pex4Fun, Rise4Fun, Pex, Moles, QuickGraph, MbUnit, Reflector Addins</description>
    <language>en-us</language>
    <copyright>Jonathan 'Peli' de Halleux</copyright>
    <lastBuildDate>Fri, 29 Jan 2010 17:11:50 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>jonathan.dehalleux@gmail.com</managingEditor>
    <webMaster>jonathan.dehalleux@gmail.com</webMaster>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=e594a555-74cc-4b9b-aeb7-043de086d94e</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,e594a555-74cc-4b9b-aeb7-043de086d94e.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,e594a555-74cc-4b9b-aeb7-043de086d94e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=e594a555-74cc-4b9b-aeb7-043de086d94e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In WPF, one needs to implement the <a href="http://msdn.microsoft.com/en-us/library/ms752914.aspx">DependencyProperty</a> pattern
to make properties bindable. It usually involves a lot of boiler plate code:
</p>
        <ul>
          <li>
adding a static field that names the depency property, 
</li>
          <li>
adding the instance property to the type (and make sure you follow the naming convention), 
</li>
          <li>
add validation methods and wire them in the dependency constructor</li>
        </ul>
        <p>
These are a number steps that need to be done again and again if you are building
new WPF controls. 
</p>
        <p>
This is where a little transformation using <a href="http://ccisamples.codeplex.com/wikipage?title=CciSharp" target="_blank">CciSharp</a> really
helps with such boiler plate code. Using the <a href="http://ccisamples.codeplex.com/wikipage?title=DependencyAutoProperty&amp;referringTitle=CciSharp">DependencyAutoProperty</a> mutator,
you can define dependency properties with a single attribute. The rest is taken and
validated by the compiler. 
</p>
        <p>
          <strong>The simple scenario: Add [DependencyAutoProperty]</strong>
        </p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_4.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_thumb_1.png" width="304" height="116" />
          </a>
        </p>
        <p>
The rewritten code will contain a dependency property for the Value property, and
will rewrite the value property getter and setters to use GetValue, SetValue instead.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_8.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_thumb_3.png" width="873" height="313" />
          </a>
        </p>
        <p>
          <strong>Supporting default values</strong>
        </p>
        <p>
Default values can be specified in the DependencyProperty register method, so we want
to support this as well through the DependencyAutoProperty constructor.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_5.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_thumb.png" width="394" height="112" />
          </a>
        </p>
        <p>
          <strong>Supporting validation</strong>
        </p>
        <p>
Callbacks can be passed in the constructor of DependencyProperty to validate the values
of the property. To support this, we use a simple naming convention: if you specify
the Validate = true constructor argument, the rewritter will look for a static “Validate”
+ property name method whose signature is Func&lt;T, bool&gt;, where T is the property
type. The rewritter will make sure this method is used – or raise a compilation error
if it is missing.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_9.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_thumb_2.png" width="451" height="182" />
          </a>
        </p>
        <p>
        </p>
        <p>
An interresting point here is that the callbacks passed in the DependencyProperty.Register
method are untyped (Func&lt;object, bool&gt;) and the user needs to make the appropriate
casts himself. This is taken care of by the rewritter:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_11.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_thumb_4.png" width="644" height="190" />
          </a>
        </p>
        <p>
Have fun!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=e594a555-74cc-4b9b-aeb7-043de086d94e" />
      </body>
      <title>Implementing DependencyProperty with a single attribute (and CciSharp)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,e594a555-74cc-4b9b-aeb7-043de086d94e.aspx</guid>
      <link>http://blog.dotnetwiki.org/2010/01/29/ImplementingDependencyPropertyWithASingleAttributeAndCciSharp.aspx</link>
      <pubDate>Fri, 29 Jan 2010 17:11:50 GMT</pubDate>
      <description>&lt;p&gt;
In WPF, one needs to implement the &lt;a href="http://msdn.microsoft.com/en-us/library/ms752914.aspx"&gt;DependencyProperty&lt;/a&gt; pattern
to make properties bindable. It usually involves a lot of boiler plate code:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
adding a static field that names the depency property, 
&lt;li&gt;
adding the instance property to the type (and make sure you follow the naming convention), 
&lt;li&gt;
add validation methods and wire them in the dependency constructor&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
These are a number steps that need to be done again and again if you are building
new WPF controls. 
&lt;/p&gt;
&lt;p&gt;
This is where a little transformation using &lt;a href="http://ccisamples.codeplex.com/wikipage?title=CciSharp" target="_blank"&gt;CciSharp&lt;/a&gt; really
helps with such boiler plate code. Using the &lt;a href="http://ccisamples.codeplex.com/wikipage?title=DependencyAutoProperty&amp;amp;referringTitle=CciSharp"&gt;DependencyAutoProperty&lt;/a&gt; mutator,
you can define dependency properties with a single attribute. The rest is taken and
validated by the compiler. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The simple scenario: Add [DependencyAutoProperty]&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_thumb_1.png" width="304" height="116"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
The rewritten code will contain a dependency property for the Value property, and
will rewrite the value property getter and setters to use GetValue, SetValue instead.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_thumb_3.png" width="873" height="313"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Supporting default values&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Default values can be specified in the DependencyProperty register method, so we want
to support this as well through the DependencyAutoProperty constructor.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_5.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_thumb.png" width="394" height="112"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Supporting validation&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Callbacks can be passed in the constructor of DependencyProperty to validate the values
of the property. To support this, we use a simple naming convention: if you specify
the Validate = true constructor argument, the rewritter will look for a static “Validate”
+ property name method whose signature is Func&amp;lt;T, bool&amp;gt;, where T is the property
type. The rewritter will make sure this method is used – or raise a compilation error
if it is missing.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_9.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_thumb_2.png" width="451" height="182"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
An interresting point here is that the callbacks passed in the DependencyProperty.Register
method are untyped (Func&amp;lt;object, bool&amp;gt;) and the user needs to make the appropriate
casts himself. This is taken care of by the rewritter:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_11.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ImplementingDependencyPropertywithasingl_1337E/image_thumb_4.png" width="644" height="190"&gt;&lt;/a&gt; 
&lt;/p&gt;
&gt;
&lt;p&gt;
Have fun!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=e594a555-74cc-4b9b-aeb7-043de086d94e" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,e594a555-74cc-4b9b-aeb7-043de086d94e.aspx</comments>
      <category>CciSharp</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=7f129f81-256f-4228-9750-fcc1f3f8b33d</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,7f129f81-256f-4228-9750-fcc1f3f8b33d.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,7f129f81-256f-4228-9750-fcc1f3f8b33d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=7f129f81-256f-4228-9750-fcc1f3f8b33d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the previous post the <a href="http://blog.dotnetwiki.org/2009/12/31/RichAssertionMessagesUsingCCIAndWithoutExpressionTrees.aspx">fun
of enhancing assertion messages</a>, I eluded about CciSharp… but what is it really?
</p>
        <p>
          <strong>
            <a href="http://ccisamples.codeplex.com/wikipage?title=CciSharp" target="_blank">CciSharp</a>
          </strong> is
a post-compiler framework for .NET. (In a sense, it is just a sample of things that
can be done with  <a href="http://ccimetadat.codeplex.com/">Common Compiler Infrastructure
(CCI)</a> ). CciSharp was designed with the following idea in mind: enable to easily
write assembly mutators and integrate them into the build. In that sense, CciSharp
provides
</p>
        <ul>
          <li>
A minimalistic layer on top of the CCI mutable source model. 
</li>
          <li>
Provide the MSBuild ‘plumbing’ to integrate into a build<em> so you don’t have to
worry about it</em>. 
</li>
        </ul>
        <p>
          <strong>Do you have some Mutators examples?</strong>
        </p>
        <p>
Are you wondering the kind of problems can you solve with a post-compiler? Let’s take
a look at a bunch of example (the full source of those samples is available at <a href="http://ccisamples.codeplex.com">http://ccisamples.codeplex.com</a>):
</p>
        <ul>
          <li>
            <a href="http://ccisamples.codeplex.com/wikipage?title=LazyProperty&amp;referringTitle=CciSharp">Lazy
Property</a>: A lazy property computes its result once and caches it. Since it is
not supported out of the box by C# or VB.NET, it requires boiler plate code to implement
it. I’ve written mutator that takes a property makred with a [Lazy] attribute and
implements this pattern. Here’s an example before and after of a property that returns
the Environment.TickCount.<br /><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_10.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb_4.png" width="219" height="192" /></a><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_8.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb_3.png" width="305" height="449" /></a></li>
          <li>
            <a href="http://ccisamples.codeplex.com/wikipage?title=WeakLazyProperty&amp;referringTitle=CciSharp">Weak
Lazy Property</a>: The way we cached the property result above has a dangerous side-effect:
it bloats your memory. Instead of storing the result has a ‘hard’ reference, one can
use a <a href="http://msdn.microsoft.com/en-us/library/system.weakreference.aspx" target="_blank">WeakReference</a> instead.
That way, the GC can reclaim the cached references whenever memory pressure becomes
too high (the GC is really a great built-in caching mechanism in .NET). Again, it
takes a bunch of boiler plate code to implement this pattern, which is generated automatically
by the another CciSharp mutator. Here’s another before/after example:<br /><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_12.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb_5.png" width="266" height="189" /></a><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_6.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb_2.png" width="496" height="437" /></a></li>
          <li>
            <a href="http://ccisamples.codeplex.com/wikipage?title=ReadOnlyAutoProperty&amp;referringTitle=CciSharp">ReadOnly
Auto Property</a>: Auto-properties are a great feature of C# but somehow I miss the
‘readonly’ keyword. The best that C# gives us is an auto-property with a private setter,
which is not quite the same as ‘readonly’ fields. CciSharp contains a mutator that,
given an auto-property with a getter and private setter,  (1) deletes  the
setter, (2) makes the backing field readonly and (3) updates all the calls to the
setter to direct access to the backing field. That way I get exactly what I want:
a truly <em>readonly</em> auto-property. The snippet below shows the before/after
transformation.<br /><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb.png" width="259" height="434" /></a><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb_1.png" width="289" height="361" /></a><br /></li>
        </ul>
        <p>
Those are a couple scenarios that tools like CciSharp enable. In the next post, we’ll
look at the <strong>DependencyProperty</strong> pattern and how CciSharp can really
help there.
</p>
        <p>
HAPPY NEW YEAR!!!!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=7f129f81-256f-4228-9750-fcc1f3f8b33d" />
      </body>
      <title>CciSharp, a post-compiler for .NET based on CCI</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,7f129f81-256f-4228-9750-fcc1f3f8b33d.aspx</guid>
      <link>http://blog.dotnetwiki.org/2010/01/01/CciSharpAPostcompilerForNETBasedOnCCI.aspx</link>
      <pubDate>Fri, 01 Jan 2010 18:35:54 GMT</pubDate>
      <description>&lt;p&gt;
In the previous post the &lt;a href="http://blog.dotnetwiki.org/2009/12/31/RichAssertionMessagesUsingCCIAndWithoutExpressionTrees.aspx"&gt;fun
of enhancing assertion messages&lt;/a&gt;, I eluded about CciSharp… but what is it really?
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;a href="http://ccisamples.codeplex.com/wikipage?title=CciSharp" target="_blank"&gt;CciSharp&lt;/a&gt;&lt;/strong&gt; is
a post-compiler framework for .NET. (In a sense, it is just a sample of things that
can be done with&amp;nbsp; &lt;a href="http://ccimetadat.codeplex.com/"&gt;Common Compiler Infrastructure
(CCI)&lt;/a&gt; ). CciSharp was designed with the following idea in mind: enable to easily
write assembly mutators and integrate them into the build. In that sense, CciSharp
provides
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
A minimalistic layer on top of the CCI mutable source model. 
&lt;li&gt;
Provide the MSBuild ‘plumbing’ to integrate into a build&lt;em&gt; so you don’t have to
worry about it&lt;/em&gt;. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;Do you have some Mutators examples?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Are you wondering the kind of problems can you solve with a post-compiler? Let’s take
a look at a bunch of example (the full source of those samples is available at &lt;a href="http://ccisamples.codeplex.com"&gt;http://ccisamples.codeplex.com&lt;/a&gt;):
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://ccisamples.codeplex.com/wikipage?title=LazyProperty&amp;amp;referringTitle=CciSharp"&gt;Lazy
Property&lt;/a&gt;: A lazy property computes its result once and caches it. Since it is
not supported out of the box by C# or VB.NET, it requires boiler plate code to implement
it. I’ve written mutator that takes a property makred with a [Lazy] attribute and
implements this pattern. Here’s an example before and after of a property that returns
the Environment.TickCount.&lt;br&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_10.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb_4.png" width="219" height="192"&gt;&lt;/a&gt; &lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_8.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb_3.png" width="305" height="449"&gt;&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://ccisamples.codeplex.com/wikipage?title=WeakLazyProperty&amp;amp;referringTitle=CciSharp"&gt;Weak
Lazy Property&lt;/a&gt;: The way we cached the property result above has a dangerous side-effect:
it bloats your memory. Instead of storing the result has a ‘hard’ reference, one can
use a &lt;a href="http://msdn.microsoft.com/en-us/library/system.weakreference.aspx" target="_blank"&gt;WeakReference&lt;/a&gt; instead.
That way, the GC can reclaim the cached references whenever memory pressure becomes
too high (the GC is really a great built-in caching mechanism in .NET). Again, it
takes a bunch of boiler plate code to implement this pattern, which is generated automatically
by the another CciSharp mutator. Here’s another before/after example:&lt;br&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_12.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb_5.png" width="266" height="189"&gt;&lt;/a&gt; &lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_6.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb_2.png" width="496" height="437"&gt;&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://ccisamples.codeplex.com/wikipage?title=ReadOnlyAutoProperty&amp;amp;referringTitle=CciSharp"&gt;ReadOnly
Auto Property&lt;/a&gt;: Auto-properties are a great feature of C# but somehow I miss the
‘readonly’ keyword. The best that C# gives us is an auto-property with a private setter,
which is not quite the same as ‘readonly’ fields. CciSharp contains a mutator that,
given an auto-property with a getter and private setter,&amp;nbsp; (1) deletes&amp;nbsp; the
setter, (2) makes the backing field readonly and (3) updates all the calls to the
setter to direct access to the backing field. That way I get exactly what I want:
a truly &lt;em&gt;readonly&lt;/em&gt; auto-property. The snippet below shows the before/after
transformation.&lt;br&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb.png" width="259" height="434"&gt;&lt;/a&gt; &lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/CciSharpapostcompilerfor.NETbasedonCCI_14CE9/image_thumb_1.png" width="289" height="361"&gt;&lt;/a&gt; 
&lt;br&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Those are a couple scenarios that tools like CciSharp enable. In the next post, we’ll
look at the &lt;strong&gt;DependencyProperty&lt;/strong&gt; pattern and how CciSharp can really
help there.
&lt;/p&gt;
&lt;p&gt;
HAPPY NEW YEAR!!!!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=7f129f81-256f-4228-9750-fcc1f3f8b33d" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,7f129f81-256f-4228-9750-fcc1f3f8b33d.aspx</comments>
      <category>CciSharp</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=9dce05c9-217b-45a7-a8d1-2dce2792116d</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=9dce05c9-217b-45a7-a8d1-2dce2792116d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It is holiday time so I could spend some time to play with <a href="http://ccimetadata.codeplex.com/">CCI</a>.
The result is a bunch of interesting assembly mutators. Let’s start with the first
one: automatic rich assertion messages.
</p>
        <p>
          <strong>The problem with Assertions</strong>
        </p>
        <p>
When an assertion fails, the error message is usually insufficient to understand failure.
The problem is that we are usually lacking the values in the expression to immediately
diagnose the problem. Consider this example,
</p>
        <blockquote>
          <p>
Assert.True(x == 123);
</p>
        </blockquote>
        <p>
When this assertion fails, one would like to know what was the value of ‘x’. Of course,
that value of x is not serialized in the test output since the assertion method only
sees the ‘false’ value. What we would really like to get is something like ‘x (64)
!= 123’. A number of techniques have been developed to work around this issue.
</p>
        <p>
          <strong>Solution #1: Specialized Assertions</strong>
        </p>
        <p>
A common approach is to provide specialized assertion methods with enhanced logging.
For example, a special method to test equality:
</p>
        <blockquote>
          <p>
Assert.Equal(x , 123);
</p>
        </blockquote>
        <p>
When this assertion fails, the Equal method has the value of both sides of the equality
and can render it in the message: ‘expected 123, actual 64’. Unfortunately, expressions
are more readable when you write them, and specialized assertions cannot cover all
scenarios. This takes us to the next solution.
</p>
        <p>
          <strong>Solution #2: Expression Trees at Run time</strong>
        </p>
        <p>
          <a href="http://themechanicalbride.blogspot.com/">Jafar Husain</a> wrote <a href="http://themechanicalbride.blogspot.com/2009/06/better-unit-tests-with-testassert-for.html">a
very interresting post</a> on how to use Expression Trees to generate rich assertion
messages (this is <a href="http://www.gallio.org/api/html/M_MbUnit_Framework_AssertEx_That.htm">also
supported in MbUnit</a>).
</p>
        <blockquote>
          <p>
Assert.True(<a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx">() =&gt;</a> x
== 123);
</p>
        </blockquote>
        <p>
Instead of taking a bool, the assert method takes an expression tree (Expression&lt;Func&lt;bool&gt;&gt;).
Thus, when the expression evaluates to false, the expression tree can be traversed
to extract interesting values (such as x) and generate a rich log of the failure. 
</p>
        <p>
Unfortunately, there is a major drawback to this technique: what used to be 3 MSIL
instructions (ldloc.1, ldc.i4 123, ceq) to evaluate the condition becomes hundreds
of methods calls, millions of instructions executed through the System.Linq namespaces.
This performance is a overhead on the <a href="http://research.microsoft.com/pex/">Pex</a> whitebox
analysis.
</p>
        <p>
Jafar considers unit test frameworks as an extension of the compiler and uses expression
trees to access what the compiler knows: expression and statements. Following his
idea takes us to the CCI based solution.
</p>
        <p>
          <strong>Solution #3: Assembly Rewritter at Compile time</strong>
        </p>
        <p>
In the previous approach, expression trees were used to build a little compiler <strong>at
runtime</strong>. A better solution would be to rewrite the expressions <strong>at
compile time. </strong>In other words, we want to build an assembly mutator that takes
the original method call and appends code that generates the logging:
</p>
        <blockquote>
          <p>
Assert.True(x == 123<strong>, String.Format(“x == 123 where x = ‘{0}’”, x)</strong>);
</p>
        </blockquote>
        <p>
The assembly mutator extracts the expression sources from the pdb (‘x == 123’), collect
the local/variable/field references by traversing the expression tree, generate a
String.Format friendly message and replace the assertion method call to the assertion
method that takes the additional string.
</p>
        <p>
          <strong>Hello </strong>
          <a href="http://ccimetadata.codeplex.com/">
            <strong>Common Compiler
Infrastructure</strong>
          </a>
          <strong> (CCI) and CciSharp</strong>
        </p>
        <p>
Of course, to rewrite an assembly at compile time, we need a framework that can read
and write MSIL, decompile expressions etc… This is where CCI (from <a href="http://ccimetadata.codeplex.com">http://ccimetadata.codeplex.com</a> )
comes. It allows to manipulate .NET assemblies using an object model and save them
back to disk. The <a href="http://ccisamples.codeplex.com/wikipage?title=AssertMessage&amp;referringTitle=CciSharp">assertion
message mutator</a> is just one of other mutations that have been or will be implemented
as part of <a href="http://ccisamples.codeplex.com/wikipage?title=CciSharp"><strong>CciSharp</strong></a><strong>,
a post compiler for .NET that is built on top of CCI</strong>.
</p>
        <p>
(There’s already a bunch of useful operators in CciSharp that i’ve been coding over
the holidays: assigning the value of auto-properties, making auto-properties readonly
or lazy (or weakly lazy), or even implementing the DependencyProperty stuff automatically.
We’ll talk about this later.)
</p>
        <p>
          <strong>Where can I find it?</strong>
        </p>
        <p>
          <strong>CciSharp</strong> binaries and sources are avaiable on <a title="http://ccisamples.codeplex.com/wikipage?title=CciSharp" href="http://ccisamples.codeplex.com/wikipage?title=CciSharp">http://ccisamples.codeplex.com/wikipage?title=CciSharp</a>.
Grab it!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=9dce05c9-217b-45a7-a8d1-2dce2792116d" />
      </body>
      <title>Rich Assertion Messages using CCI (and without expression trees)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/12/31/RichAssertionMessagesUsingCCIAndWithoutExpressionTrees.aspx</link>
      <pubDate>Thu, 31 Dec 2009 04:45:27 GMT</pubDate>
      <description>&lt;p&gt;
It is holiday time so I could spend some time to play with &lt;a href="http://ccimetadata.codeplex.com/"&gt;CCI&lt;/a&gt;.
The result is a bunch of interesting assembly mutators. Let’s start with the first
one: automatic rich assertion messages.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The problem with Assertions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
When an assertion fails, the error message is usually insufficient to understand failure.
The problem is that we are usually lacking the values in the expression to immediately
diagnose the problem. Consider this example,
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.True(x == 123);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
When this assertion fails, one would like to know what was the value of ‘x’. Of course,
that value of x is not serialized in the test output since the assertion method only
sees the ‘false’ value. What we would really like to get is something like ‘x (64)
!= 123’. A number of techniques have been developed to work around this issue.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution #1: Specialized Assertions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
A common approach is to provide specialized assertion methods with enhanced logging.
For example, a special method to test equality:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.Equal(x , 123);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
When this assertion fails, the Equal method has the value of both sides of the equality
and can render it in the message: ‘expected 123, actual 64’. Unfortunately, expressions
are more readable when you write them, and specialized assertions cannot cover all
scenarios. This takes us to the next solution.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution #2: Expression Trees at Run time&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://themechanicalbride.blogspot.com/"&gt;Jafar Husain&lt;/a&gt; wrote &lt;a href="http://themechanicalbride.blogspot.com/2009/06/better-unit-tests-with-testassert-for.html"&gt;a
very interresting post&lt;/a&gt; on how to use Expression Trees to generate rich assertion
messages (this is &lt;a href="http://www.gallio.org/api/html/M_MbUnit_Framework_AssertEx_That.htm"&gt;also
supported in MbUnit&lt;/a&gt;).
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.True(&lt;a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx"&gt;() =&amp;gt;&lt;/a&gt; x
== 123);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Instead of taking a bool, the assert method takes an expression tree (Expression&amp;lt;Func&amp;lt;bool&amp;gt;&amp;gt;).
Thus, when the expression evaluates to false, the expression tree can be traversed
to extract interesting values (such as x) and generate a rich log of the failure. 
&lt;/p&gt;
&lt;p&gt;
Unfortunately, there is a major drawback to this technique: what used to be 3 MSIL
instructions (ldloc.1, ldc.i4 123, ceq) to evaluate the condition becomes hundreds
of methods calls, millions of instructions executed through the System.Linq namespaces.
This performance is a overhead on the &lt;a href="http://research.microsoft.com/pex/"&gt;Pex&lt;/a&gt; whitebox
analysis.
&lt;/p&gt;
&lt;p&gt;
Jafar considers unit test frameworks as an extension of the compiler and uses expression
trees to access what the compiler knows: expression and statements. Following his
idea takes us to the CCI based solution.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution #3: Assembly Rewritter at Compile time&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
In the previous approach, expression trees were used to build a little compiler &lt;strong&gt;at
runtime&lt;/strong&gt;. A better solution would be to rewrite the expressions &lt;strong&gt;at
compile time. &lt;/strong&gt;In other words, we want to build an assembly mutator that takes
the original method call and appends code that generates the logging:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.True(x == 123&lt;strong&gt;, String.Format(“x == 123 where x = ‘{0}’”, x)&lt;/strong&gt;);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The assembly mutator extracts the expression sources from the pdb (‘x == 123’), collect
the local/variable/field references by traversing the expression tree, generate a
String.Format friendly message and replace the assertion method call to the assertion
method that takes the additional string.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Hello &lt;/strong&gt;&lt;a href="http://ccimetadata.codeplex.com/"&gt;&lt;strong&gt;Common Compiler
Infrastructure&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; (CCI) and CciSharp&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Of course, to rewrite an assembly at compile time, we need a framework that can read
and write MSIL, decompile expressions etc… This is where CCI (from &lt;a href="http://ccimetadata.codeplex.com"&gt;http://ccimetadata.codeplex.com&lt;/a&gt; )
comes. It allows to manipulate .NET assemblies using an object model and save them
back to disk. The &lt;a href="http://ccisamples.codeplex.com/wikipage?title=AssertMessage&amp;amp;referringTitle=CciSharp"&gt;assertion
message mutator&lt;/a&gt; is just one of other mutations that have been or will be implemented
as part of &lt;a href="http://ccisamples.codeplex.com/wikipage?title=CciSharp"&gt;&lt;strong&gt;CciSharp&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;,
a post compiler for .NET that is built on top of CCI&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
(There’s already a bunch of useful operators in CciSharp that i’ve been coding over
the holidays: assigning the value of auto-properties, making auto-properties readonly
or lazy (or weakly lazy), or even implementing the DependencyProperty stuff automatically.
We’ll talk about this later.)
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Where can I find it?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;CciSharp&lt;/strong&gt; binaries and sources are avaiable on &lt;a title="http://ccisamples.codeplex.com/wikipage?title=CciSharp" href="http://ccisamples.codeplex.com/wikipage?title=CciSharp"&gt;http://ccisamples.codeplex.com/wikipage?title=CciSharp&lt;/a&gt;.
Grab it!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=9dce05c9-217b-45a7-a8d1-2dce2792116d" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</comments>
      <category>CCI</category>
      <category>CciSharp</category>
      <category>Testing</category>
    </item>
  </channel>
</rss>