<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Parallel Thinking</title>
	<atom:link href="http://www.acorns.com.au/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.acorns.com.au/blog</link>
	<description></description>
	<lastBuildDate>Fri, 11 Dec 2009 02:50:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>XSS Attack your database to detect missing Output Encoding</title>
		<link>http://www.acorns.com.au/blog/?p=154</link>
		<comments>http://www.acorns.com.au/blog/?p=154#comments</comments>
		<pubDate>Fri, 11 Dec 2009 02:50:29 +0000</pubDate>
		<dc:creator>Corneliu</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Improve Your Code]]></category>
		<category><![CDATA[Secure your code]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[XSSAttack]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.acorns.com.au/blog/?p=154</guid>
		<description><![CDATA[Cross Site Scripting (XSS) Attacks must be one of the most popular type of attacks of websites these days with maybe only SQL Injection attacks getting more attention that that.
XSS Attacks
The concept is quite simple for Persistent Attacks. User A attacks a website and via some vulnerability, it manages to push an XSS script into [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross Site Scripting (XSS) Attacks</a> must be one of the most popular type of attacks of websites these days with maybe only SQL Injection attacks getting more attention that that.</p>
<h2>XSS Attacks</h2>
<p>The concept is quite simple for Persistent Attacks. User A attacks a website and via some vulnerability, it manages to push an XSS script into the storage (some database most of the times) of the website.</p>
<p>User B looks at the attacked website and part of the content delivered by the website the injected XSS from the User A is delivered to User B. Now, depending on what the script was, User A could have a great deal of control on what User A does and sees. From session stealing to running operation on behalf of User B. </p>
<p>These type of attacks can be devastating for a website with one of my preferred examples being the <a href="http://namb.la/popular/">Samy is my hero XSS</a> <a href="http://en.wikipedia.org/wiki/Samy_(XSS)">attack on MySpace</a> back in 2006</p>
<p>Almost every year I talked at <a href="www.msteched.com/australia/Public/default.aspx">Microsoft TechEd</a> I had to say something about XSS and how dangerous it is and I’m always surprised that very few people know about it and try to take measures to avoid XSS attacks on their websites.</p>
<h2>Mitigations</h2>
<p>Some of the mitigations factors are <a href="http://www.owasp.org/index.php/Category:Input_Validation">Input Validation</a>, Input Filtering and <a href="www.owasp.org/.../XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet ">Output Encoding</a>. None of this mitigations have any value if used alone or incompletely.</p>
<p>Input Validation and Input Filtering are mandatory but don’t have them as your only protection. There are tools out there that can try to automatically throw hundreds of attacks at your application to see how it responds to input validation.</p>
<p>However I think Output encoding is the most critical one as the other ones are just security gates that sooner or later someone will be able to bypass one way or another. Output encoding is also the hardest one to test as you have to verify that your website properly encodes every bit of data that is/was/will be potentially modifiable by a malicious user.</p>
<h2>XSS Attack Tool</h2>
<p>This is where the XSS Attack Tool comes into place. The tool will simulate an attack on your database and update up to 5000 rows in every table and replace your strings in your database with random XSS attacks. Just imagine a malicious user had direct access to your database and tried to to his best to XSS attack your site.</p>
<p>After you run the tool go and browse your website. Considering the large number of XSS scripts injected in your database you should expect some of the scripts to surface in the UI as potential attacks. This will allow you to find and encode all the places where data could surface. The tool is not intended to cover every scenario possible so using the tool does not guarantee that your website is not vulnerable, however it’s yet another tool in the arsenal we are required to have to better protect our sites.</p>
<h3>Usage</h3>
<ol>
<li><a href="http://www.acorns.com.au/files/xssattack.1.0.zip">Download XSS Attack 1.0</a>. </li>
<li>Unzip </li>
<li><strong>BACKUP your database.</strong> </li>
<li><strong>Now BACKUP your database again</strong>. Are you sure you did that? Do it one more time just to be sure you can get back to it. </li>
<li>Modify the “Database” connection string in the XSSAttack.exe.config to point to your database </li>
<li>Enter any Schema, Table or Column name (format Schema, Schema.Table or Schema.Table.Column) in the ignore field in the config file to let the tool know those should be ignored.
<ul>
<li>Ignore tables like System Properties tables that contain properties required for your site to run or Reference Data tables that can’t be modified or never surface back to the user </li>
</ul>
</li>
<li>Run the tool and watch it update your database.
<ul>
<li>The tool might fail to update some columns every now and then or fail if you have some custom indexes or referential integrity. </li>
</ul>
</li>
<li>Go back to your website and browse. If some scripts make it to the surface un-encoded you should see it pretty quickly. </li>
<li>Note that you might have to do some manual updates to your database to get your site up and running again (e.g. fix your user name so you can login or your password). </li>
<li>If <strong>ALL </strong>your website still looks good run the tool few more times and try again. </li>
</ol>
<p><strong>Note:</strong> The tool DOES NOT INSERT any data in your database. It only updates existing rows so make sure your database has a good sample set of your data that you can use for testing.</p>
<h2>Demo</h2>
<p>My current demo site, a GUID Store (just in case anyone wants to buy a GUID):</p>
<p> <img src="/images/blog/xsswebsitestart.jpg" />
<p>Running the tool:</p>
<p> <img src="/images/blog/xssrunthetool.jpg" />
<p>Loading the website:</p>
<p> <img src="/images/blog/xssfirstrun.jpg" /> <img src="/images/blog/xssfirstrun2.jpg" />
<p>And few more alerts and errors.</p>
<p>I did a restore of the database and tried a new run of the tool and got different results:</p>
<p> <img src="/images/blog/xsssecondrun1.jpg" />
<p>The tool might give you false positives, e.g. modify columns that can never be modified by anyone and the results of which should not be encoded or not manage to create injections so take it’s results with a bit of salt. It’s for you to decide what a good result is and maybe try restore your database to a known state, configure the tool to ignore more columns and re-run the process.</p>
<p>Note: I’m not responsible for any loos, damage, security issues, fame or whatever before, during or after using this tool. This is a tool for developers and security reviewers so treat it as such. Make sure you backup your database before running this tool and make sure you never run this on a production system. The tool injects scripts from other websites that could be malicious to your browser or OS.</p>
<p>If you do find the tool useful then drop me an email at corneliu at acorns dot com dot au to let me know how you used it.</p>
<p><a href="http://www.acorns.com.au/files/xssattack.1.0.zip">Download XSS Attack 1.0</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.acorns.com.au/blog/?feed=rss2&amp;p=154</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Merging WPFThemes with your own styles</title>
		<link>http://www.acorns.com.au/blog/?p=149</link>
		<comments>http://www.acorns.com.au/blog/?p=149#comments</comments>
		<pubDate>Mon, 07 Dec 2009 03:50:22 +0000</pubDate>
		<dc:creator>Corneliu</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.acorns.com.au/blog/?p=149</guid>
		<description><![CDATA[Last few months I’ve been working on a WPF and as my design skills are close to zero I’ve decided to the the app a bit of sexiness by using the WPF goodness of Themes. Best place to start for that are the WPFThemes on CodePlex.
Sample from one of the themes:




&#60;!&#8211; Button &#8211;&#62;
&#60;Style x:Key=&#34;{x:Type Button}&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>Last few months I’ve been working on a WPF and as my design skills are close to zero I’ve decided to the the app a bit of sexiness by using the WPF goodness of Themes. Best place to start for that are the <a href="http://wpfthemes.codeplex.com/">WPFThemes on CodePlex</a>.</p>
<p><strong>Sample from one of the themes:</strong></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e87460f6-82f7-4dd8-bc84-0b43d13be385" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #fff; max-height: 300px; overflow: auto">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color:#a31515"></span><span style="color:#008000">&lt;!&#8211; Button &#8211;&gt;</span></li>
<li style="background: #f3f3f3"><span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Style</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> TargetType</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;&gt;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;IsEnabled&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;true&quot;/&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;IsTabStop&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;true&quot;/&gt;</span></li>
</ol>
</div>
</div>
</div>
<p>Applying the standard themes to your project is pretty straight forward if you want to use 100% of the theme:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f484c34d-ef0e-4e86-a21f-d0a46203b116" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #fff; max-height: 300px; overflow: auto">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color:#0000ff">public</span> <span style="color:#0000ff">partial</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">ApplicationInstance</span> : <span style="color:#2b91af">Application</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">protected</span> <span style="color:#0000ff">override</span> <span style="color:#0000ff">void</span> OnStartup(<span style="color:#2b91af">StartupEventArgs</span> e)</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">ThemeManager</span>.ApplyTheme(<span style="color:#0000ff">this</span>, <span style="color:#a31515">&quot;ShinyDarkTeal&quot;</span>);</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[...]</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
</ol>
</div>
</div>
</div>
<p>This works a treat however because the default styles are all keyed on the types there is no way for you to “customize” the styles in the themes and add new properties to them without modifying the original themes (which would create a maintenance nightmare and no chance of you ever merging with new version of WPFThemes) or use named styles which would imply you have to define the style of each element in all your code.</p>
<h3>Option 1: Modify original theme:</h3>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7e3b5862-5c01-44c8-9781-66ca1edc8efb" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #fff; max-height: 300px; overflow: auto">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Style</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> TargetType</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;IsEnabled&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;true&quot;/&gt;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;Height&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;50&quot; /&gt;</span><span style="color:#a31515"> </span><span style="color:#008000">&lt;!&#8211; my custom property &#8211;&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;IsTabStop&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;true&quot;/&gt;</span></li>
</ol>
</div>
</div>
</div>
<p>Proper maintenance nightmare.</p>
<h3>Option 2: Create named style:</h3>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1e997fca-d0b9-4eeb-bb6b-12d8aee4b693" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #fff; max-height: 300px; overflow: auto">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Style</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;MyButton&quot;</span><span style="color:#ff0000"> TargetType</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> BasedOn</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">StaticResource</span><span style="color:#0000ff"> {</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}}</span><span style="color:#0000ff">&quot;&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;Height&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;50&quot; /&gt;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;MinWidth&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;90&quot; /&gt;</span></li>
<li style="background: #f3f3f3"><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Style</span><span style="color:#0000ff">&gt;</span></li>
</ol>
</div>
</div>
</div>
<p>Ugly as you need to use named keys through your application.</p>
<h3>Option 3: Override it as window level not application level</h3>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6cb1fb57-19a8-4eac-8bce-adbbd6c91c7f" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #fff; max-height: 300px; overflow: auto">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Window.Resources</span><span style="color:#0000ff">&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Style</span><span style="color:#ff0000"> TargetType</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> BasedOn</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">StaticResource</span><span style="color:#0000ff"> {</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}}</span><span style="color:#0000ff">&quot;&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;Height&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;50&quot; /&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Style</span><span style="color:#0000ff">&gt;</span></li>
</ol>
</div>
</div>
</div>
<p>Ugly as you have to maintain it for each window. </p>
<p>Note: You can’t use option 3 at application level as the BasedOn will be applied based on the standard style not the themed one.</p>
<p>None of the two options sounded like a good option so when all I wanted to do is be able to set default Width or Height of my buttons:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:173c3f52-ebcc-48a9-9d7a-4b78c04b23a2" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #fff; max-height: 300px; overflow: auto">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Style</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> TargetType</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;Height&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;50&quot; /&gt;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;MinWidth&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;90&quot; /&gt;</span></li>
<li style="background: #f3f3f3"><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Style</span><span style="color:#0000ff">&gt;</span></li>
</ol>
</div>
</div>
</div>
<p>Wouldn’t it be nice if we could no “use the theme as it’ given to us” but “merge the theme” into our code as have it as the base of what we want to build on top of it.</p>
</p>
<h2>Merging themes and overwriting via target inheritance</h2>
<p>The only other option that I found was to do a custom load of the theme file as named keys and not as types even if they are defined as types and force my custom styles to inherit from the theme styles.</p>
<p>First the code:</p>
<p><strong>CustomStyles.xaml – my custom (master) styles</strong></p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d2b1b729-6b76-4180-925f-f9b861d93fbe" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #fff; max-height: 300px; overflow: auto">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Style</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> TargetType</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;Height&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;50&quot; /&gt;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;MinWidth&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;90&quot; /&gt;</span></li>
<li style="background: #f3f3f3"><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Style</span><span style="color:#0000ff">&gt;</span></li>
</ol>
</div>
</div>
</div>
<p>x:Key is x:Type Button so I want this style to get applied to all my buttons.</p>
<p><strong>Theme.xaml – default theme from WPFThemes</strong></p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f829406b-e4c3-45ad-b5f7-6d410573cafe" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #fff; max-height: 300px; overflow: auto">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Style</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> TargetType</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;IsEnabled&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;true&quot;/&gt;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;IsTabStop&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;true&quot;/&gt;</span></li>
</ol>
</div>
</div>
</div>
<p>These are the styles from the themes. We don’t want to modify them but we would like to merge them with our Custom Styles.</p>
<p><strong>Merge code:</strong></p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2a2fdded-dd41-4c05-8b01-7e9938ee1bd3" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #fff; max-height: 300px; overflow: auto">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color:#0000ff">private</span> <span style="color:#0000ff">void</span> MergeThemeFile()</li>
<li style="background: #f3f3f3">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af">ResourceDictionary</span> themeDictionary = <span style="color:#2b91af">ThemeManager</span>.GetThemeResourceDictionary(<span style="color:#a31515">&quot;BlueGlossyControls&quot;</span>);</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff">string</span> customStylesXaml = <span style="color:#a31515">@&quot;/myassembly;component/Resources/Styles/CustomStyles.xaml&quot;</span>;</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#2b91af">ResourceDictionary</span> customStyles = <span style="color:#2b91af">Application</span>.LoadComponent(<span style="color:#0000ff">new</span> <span style="color:#2b91af">Uri</span>(customStylesXaml, <span style="color:#2b91af">UriKind</span>.Relative)) <span style="color:#0000ff">as</span> <span style="color:#2b91af">ResourceDictionary</span>;</li>
<li>&#160;&#160;&#160;&#160;</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#008000">// Force remove of the custom styles dictionary so it does not get loaded </span></li>
<li>&#160;&#160;&#160;&#160;DropDictionary(customStylesXaml);</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af">ResourceDictionary</span> finalDictionary = <span style="color:#0000ff">new</span> <span style="color:#2b91af">ResourceDictionary</span>();</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000">// we have two dictionaries, try to make the customStyles be the master by inheriting them from the theme</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#008000">// update this, drop the xType if we already have some types and merge</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff">foreach</span>(<span style="color:#2b91af">DictionaryEntry</span> resourceEntry <span style="color:#0000ff">in</span> themeDictionary)</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">if</span> (resourceEntry.Value <span style="color:#0000ff">is</span> <span style="color:#2b91af">Style</span> &amp;&amp; resourceEntry.Key <span style="color:#0000ff">is</span> <span style="color:#2b91af">Type</span>)</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">Type</span> themeKeyType = resourceEntry.Key <span style="color:#0000ff">as</span> <span style="color:#2b91af">Type</span>;</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">Style</span> themeStyle = resourceEntry.Value <span style="color:#0000ff">as</span> <span style="color:#2b91af">Style</span>;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">// resource for a specific type, try to merge with customStyles</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">var</span> localStyle = customStyles[resourceEntry.Key] <span style="color:#0000ff">as</span> <span style="color:#2b91af">Style</span>;</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">if</span> (localStyle != <span style="color:#0000ff">null</span>)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">// make local style inherit theme style</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">string</span> themeKey = <span style="color:#a31515">&quot;Theme-&quot;</span> + themeKeyType.FullName;</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;finalDictionary[themeKey] = themeStyle;&#160;&#160;&#160;&#160;<span style="color:#008000">// save the theme as &quot;Theme-[FullName]&quot;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;localStyle.BasedOn = themeStyle;&#160;&#160;&#160;&#160;<span style="color:#008000">// make local style based on the Theme one</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;finalDictionary[themeKeyType] = localStyle;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">continue</span>;</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;finalDictionary.Add(resourceEntry.Key, resourceEntry.Value);</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#0000ff">foreach</span> (<span style="color:#2b91af">DictionaryEntry</span> resourceEntry <span style="color:#0000ff">in</span> customStyles)</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">if</span> ( !finalDictionary.Contains(resourceEntry.Key))</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;finalDictionary[resourceEntry.Key] = resourceEntry.Value;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff">this</span>.Resources.MergedDictionaries.Add(finalDictionary);</li>
<li style="background: #f3f3f3">}</li>
<li><span style="color:#0000ff">private</span> <span style="color:#0000ff">void</span> DropDictionary(<span style="color:#0000ff">string</span> name)</li>
<li style="background: #f3f3f3">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> resource <span style="color:#0000ff">in</span> <span style="color:#0000ff">this</span>.Resources.MergedDictionaries)</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">if</span> (resource.Source.ToString() == name)</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">this</span>.Resources.MergedDictionaries.Remove(resource);</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">break</span>;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;}</li>
<li>}</li>
</ol>
</div>
</div>
</div>
<p>This code will try to load the theme file and see if we have any keys as types that we override in the CustomStyles.xaml.</p>
<p>If we have a “conflict” we change the x:Key of the theme style to a named key and we change the BasedOn of the custom style to be based on the theme style. If there is no conflict we load the old theme. </p>
<p>This merges the two files like this:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e71b3cbf-693d-4b39-a34f-01dc1ecefe7e" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #fff; max-height: 300px; overflow: auto">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color:#a31515"></span><span style="color:#008000">&lt;!&#8211; Original Style From Theme: x:Key=&quot;{x:Type Button}&quot; &#8211;&gt;</span></li>
<li style="background: #f3f3f3"><span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Style</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;ThemeGlassyControls&quot;</span><span style="color:#ff0000"> TargetType</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;&gt;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;IsEnabled&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;true&quot;/&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;IsTabStop&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;true&quot;/&gt;</span></li>
<li><span style="color:#a31515"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Style</span><span style="color:#0000ff">&gt;</span></li>
<li style="background: #f3f3f3"><span style="color:#a31515"></span><span style="color:#008000">&lt;!&#8211; Original Style from CustomStyles based on the new Theme style&#8211;&gt;</span></li>
<li><span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Style</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> TargetType</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">x</span><span style="color:#0000ff">:</span><span style="color:#a31515">Type</span><span style="color:#ff0000"> Button}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> BasedOn</span><span style="color:#0000ff">=&quot;ThemeGlassyControls&quot;&gt;</span></li>
<li style="background: #f3f3f3">&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;Height&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;50&quot; /&gt;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#a31515"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Setter</span><span style="color:#ff0000"> Property</span><span style="color:#0000ff">=&quot;MinWidth&quot;</span><span style="color:#ff0000"> Value</span><span style="color:#0000ff">=&quot;90&quot; /&gt;</span></li>
<li style="background: #f3f3f3"><span style="color:#a31515"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Style</span><span style="color:#0000ff">&gt;</span></li>
</ol>
</div>
</div>
</div>
<p>We then load the new combined style.</p>
<p>This get the best of the two words out there combined. We can apply a theme while also overwriting/merging parts of it.</p>
<p>Merged styles: WPFThemes Gradients, My Width/Height</p>
<p><img src="/images/blog/mergedstyles.jpg"/></p>
<p>Now the next challenge is to be able to override part of the content. I’d like my buttons to have two lines of text. One with the text and one with the shortcut defined via a dependency property. I’d like to keep the theme contents of the button and be able to add to that in a very simply way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.acorns.com.au/blog/?feed=rss2&amp;p=149</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mazda6.Net CarPC: Putting it all together</title>
		<link>http://www.acorns.com.au/blog/?p=148</link>
		<comments>http://www.acorns.com.au/blog/?p=148#comments</comments>
		<pubDate>Mon, 26 Oct 2009 15:03:48 +0000</pubDate>
		<dc:creator>Corneliu</dc:creator>
				<category><![CDATA[CarPC]]></category>
		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://www.acorns.com.au/blog/?p=148</guid>
		<description><![CDATA[Intro
As a follow up from a previous post on Mazda6.Net CarPC: Getting the parts I’ve managed to purchase most of the parts including the Double-Din case from www.bybyte.com in which I had to fit the screen (and in my plan the mother board and HDD as well).
The Via M10000 board fits perfectly in the double-din [...]]]></description>
			<content:encoded><![CDATA[<h2>Intro</h2>
<p>As a follow up from a previous post on <a href="http://www.acorns.com.au/blog/?p=135">Mazda6.Net CarPC: Getting the parts</a> I’ve managed to purchase most of the parts including the Double-Din case from <a href="http://www.bybyte.com">www.bybyte.com</a> in which I had to fit the screen (and in my plan the mother board and HDD as well).</p>
<p>The Via M10000 board fits perfectly in the double-din case with just 3cm sticking out at the back which kind of worried me but I hoped I would have enough clearance in the car for those extra 3cm.</p>
<p>Fitting the motherboard <em>almost</em> worked according to the plan except that the power plug for the Via board is to on the side of the board thus there is not enough clearance next to it to plug the M3 ATX power adapter. Thus I’ve got stuck and dropped the project for several months until I found out from one of my customers that I can buy a <a href="http://www.mini-box.com/M3-ATX-DC-DC-ATX-Automotive-Computer-car-PC-Power-Supply">20-pin ATX Extension Cable</a> that will get me unstuck by allowing me to move the M3 ATX power anywhere I see if.</p>
<p>With a new $10 ATX Extension cable in my hands I’ve decided it’s the right time to re-start (or just finish) my CarPC.</p>
<p>Couple of night of work to put all the bits an pieces together and voiala, CarPC working on the bench running WinXP. WinXP? Gosh, I can’t run my car on WinXP, so I started to install Windows 7 on the 5 years old Via M10000 using a USB CD Drive via the slow USB 1.1 ports. Installation time: <strong>2.6h</strong>.</p>
<h2>Upgrade: From Via to Intel</h2>
<p>Fallback to Plan A.2. Buy a new sleek Intel Atom Mini-ATX board: Intel D945GCLF2 Motherboard &#8211; Dual-Core Intel Atom 330(1.6GHz), 945GC, 2xDDR2-533, 1xPCI, 2xSATA-II, GigLAN, 5.1Chl, VGA/S-Video, Mini-ITX&lt;/i&gt; @ $115.70 + 1 x Corsair 2048MB PC2-5300 667MHz DDR2 RAM &#8211; 5-5-5-15 @ $56.10 ea.</p>
<p>Install:</p>
<ul>
<li>Windows 7: Installation time: <strong>18 minutes</strong>. </li>
<li>Drivers </li>
<li>Centrafuse </li>
<li>BlueSoleil Bluetooth </li>
<li>GPS Drivers (I can’t get the GPS working) </li>
<li>Virgin Mobile 3G Drivers </li>
</ul>
<p>Few days later my CarPC is installed and working on the test bench, time to get a booking to the Car Audio Installation Specialists.</p>
<h3>Test bench installing Windows 7</h3>
<p><a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/IMG_0256.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0256" border="0" alt="IMG_0256" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/IMG_0256_thumb.jpg" width="244" height="184" /></a> </p>
<p>Few more days of work to get it installed in the double-din case behind the touch screen:</p>
<h3>View from above</h3>
<p><a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01851.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01851" border="0" alt="DSC01851" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01851_thumb.jpg" width="244" height="184" /></a></p>
<p>The (dodgy) carton protection is for the M3 ATX power source.</p>
<h3>View from the side</h3>
<p><a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01853.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01853" border="0" alt="DSC01853" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01853_thumb.jpg" width="244" height="184" /></a> </p>
<p>Big day comes that the system is almost all running and decide to use my last day of paternity leave to go and get the beast installed. Arranged an appointment with Druery Car Stereo to get it installed.</p>
<h2>Last preparations</h2>
<p>Evening before the appointment with the car stereo guys who were about to tear my car apart I did the last “in-car” tests.</p>
<p><a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01855.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01855" border="0" alt="DSC01855" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01855_thumb.jpg" width="244" height="184" /></a> <a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01864.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01864" border="0" alt="DSC01864" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01864_thumb.jpg" width="244" height="184" /></a> <a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01869.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01869" border="0" alt="DSC01869" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01869_thumb.jpg" width="244" height="184" /></a> </p>
<p>The computer was powered from one cigarette lighter, the screen from another one.</p>
<h2>Big day: Car Installation</h2>
</p>
</p>
</p>
<h3>Step 1: Tearing the car apart</h3>
<p><a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01870.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01870" border="0" alt="DSC01870" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01870_thumb.jpg" width="244" height="184" /></a> </p>
<p>This went very well but with unexpected results. The computer would not fit in the dashboard. It’s too long!</p>
<p>As you can see in this picture the motherboard is few centimetres longer than its case plus you have to add the length of the VGA plug and cable on top of this. That made the complete assembly about 6-8cm longer than a standard double-din, and that was just 3cm too long so the complete computer didn’t fit in the car.</p>
<p><a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01852.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01852" border="0" alt="DSC01852" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01852_thumb.jpg" width="244" height="184" /></a> </p>
<p>I only had two options left:</p>
<ol>
<li>Put the car back together as it was and go home with no CarPC </li>
<li>Install the screen and put cables so I can move the computer under the passenger seat. </li>
</ol>
<p>Option 2 was the preferred one and we started to cut the original car wiring and get cables from behind the screen to under the passenger seat. </p>
<h3>Step 2: Getting cables from behind the screen to under the passenger seat</h3>
<p><a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01871.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01871" border="0" alt="DSC01871" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01871_thumb.jpg" width="244" height="184" /></a> <a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01872.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC0<br />
1872" border="0" alt="DSC01872" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01872_thumb.jpg" width="244" height="184" /></a> <a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01873.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01873" border="0" alt="DSC01873" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01873_thumb.jpg" width="244" height="184" /></a> <a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01874.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01874" border="0" alt="DSC01874" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01874_thumb.jpg" width="244" height="184" /></a> </p>
<h3>Step 3: Install the screen in a custom Mazda fascia to make it look stock-standard</h3>
<p>This also didn’t really work according to the plan as the screen was slightly smaller and the fascia was slightly bigger though they were both ‘universal standard’.</p>
<p><a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01876.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01876" border="0" alt="DSC01876" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01876_thumb.jpg" width="244" height="184" /></a> </p>
<p>After a bit of fiddling we managed to put them together to make them look like one unit. It’s ok. It’s not perfect as there is a 3-4mm gap but it still looks good.</p>
<p>Day was over and had to take the car back home. Total billable time: 3.5h. Total time spent including searching documentation for cable and lunch: 6h.</p>
<p>We did no testing whatsoever of all the system or even if the cables are good so there was a high degree of chance of failure.</p>
<h2>First tests</h2>
<p>Yup. It failed. First test when I got home with a shiny screen in the car … failed. Screen was not powering on. It was completely blank which was not a good sign. Spent a good 3 nights with my head under the dashboard trying to track the power cable from the screen only to figure out that the power plug towards the car was not plugged in well. However that plug was quite deep in the middle of the dashboard, behind the panels I didn’t know/want to take out. </p>
<p><a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01877.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01877" border="0" alt="DSC01877" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01877_thumb.jpg" width="244" height="184" /></a> </p>
<p>Here the screen is powered from an external source just to test that the screen works if it gets power.</p>
<p><a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01879.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01879" border="0" alt="DSC01879" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01879_thumb.jpg" width="244" height="184" /></a> <a href="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01878.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSC01878" border="0" alt="DSC01878" src="http://www.acorns.com.au/Images/blog/Mazda6.NetCarPCPuttingitalltogetherPlanA_10139/DSC01878_thumb.jpg" width="244" height="184" /></a> </p>
<p>I’ve started working to get the computer installed under the car seat and hit another set of challenges. The VGA cable from the screen is too short. It’s only sticking out about 3cm from the hole in the carpet, not enough to get it stable in the back of the PC. Now I’ll have to add a VGA extension.</p>
<p>More news soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.acorns.com.au/blog/?feed=rss2&amp;p=148</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Step by Step Database Encryption</title>
		<link>http://www.acorns.com.au/blog/?p=147</link>
		<comments>http://www.acorns.com.au/blog/?p=147#comments</comments>
		<pubDate>Sat, 18 Jul 2009 13:07:11 +0000</pubDate>
		<dc:creator>Corneliu</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Secure your code]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.acorns.com.au/blog/?p=147</guid>
		<description><![CDATA[I’m sure there are a billion articles out there talking about using database encryption but I didn’t really find one that simply has a quick and simple guide.
Step 0: What do we need
The simplest approach is to use: a Master key on the database, a certificate and a symmetric key.
Step 1: Create Master Key
MSDN: The [...]]]></description>
			<content:encoded><![CDATA[<p>I’m sure there are a billion articles out there talking about using database encryption but I didn’t really find one that simply has a quick and simple guide.</p>
<h2>Step 0: What do we need</h2>
<p>The simplest approach is to use: a Master key on the database, a certificate and a symmetric key.</p>
<h2>Step 1: Create Master Key</h2>
<blockquote><p><a href="http://msdn.microsoft.com/en-us/library/ms174382.aspx" target="_blank">MSDN:</a> The database master key is a symmetric key used to protect the private keys of certificates and asymmetric keys that are present in the database.</p>
</blockquote>
<pre class="csharpcode"><span class="kwrd">CREATE</span> MASTER <span class="kwrd">KEY</span> ENCRYPTION <span class="kwrd">BY</span> PASSWORD = <span class="str">'Some Really long and complex password'</span></pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>If you have no idea for a long and complex password you can use a <a href="http://www.freepasswordgenerator.com/" target="_blank">free online password generator</a>. Use a length of 28 or longer.</p>
<h2>Step 2: Generate a Certificate</h2>
<blockquote>
<p><a href="http://msdn.microsoft.com/en-us/library/ms187798.aspx" target="_blank">MSDN</a>: […] when SQL Server generates a self-signed certificate, the private key is always created. By default, the private key is encrypted using the database master key. If the database master key does not exist and no password is specified, the statement will fail.</p>
</blockquote>
<pre class="csharpcode"><span class="kwrd">CREATE</span> CERTIFICATE MyDatabaseCertificate
   <span class="kwrd">WITH</span> SUBJECT = <span class="str">'Certificate For My Database'</span>,
   EXPIRY_DATE = <span class="str">'10/31/2011'</span>;</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<h2>Step 3: Create the symmetrical key used for the encryption</h2>
<blockquote>
<p><a href="http://msdn.microsoft.com/en-us/library/ms188357.aspx" target="_blank">MSDN</a>: When a symmetric key is created, the symmetric key must be encrypted by using at least one of the following: certificate, password, symmetric key, asymmetric key, or PROVIDER.</p>
</blockquote>
<pre class="csharpcode"><span class="kwrd">CREATE</span> SYMMETRIC <span class="kwrd">KEY</span> MyDatabase_Key <span class="kwrd">WITH</span> ALGORITHM = AES_256
    ENCRYPTION <span class="kwrd">BY</span> CERTIFICATE MyDatabaseCertificate;</pre>
<h2>Step 4: Give rights to the user(s) that will encrypt and decrypt using the certificate and key</h2>
<p>In order to use (to open) the certificate for encryption and decryption you need to give view definition and control rights on the certificate to the user(s) that will do the operations.</p>
<pre class="csharpcode"><span class="kwrd">GRANT</span> <span class="kwrd">VIEW</span> DEFINITION <span class="kwrd">ON</span> CERTIFICATE :: MyDatabaseCertificate <span class="kwrd">TO</span> &quot;MyDbUser&quot;
<span class="kwrd">GRANT</span> CONTROL <span class="kwrd">ON</span> CERTIFICATE :: MyDatabaseCertificate <span class="kwrd">TO</span> &quot;MyDbUser&quot;</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>NOTE: the usage of quotation marks instead of normal apostrophes around the user name.</p>
<p>If you use Integrated Security your user would look like “DOMAIN\UserName”:</p>
<pre class="csharpcode"><span class="kwrd">GRANT</span> <span class="kwrd">VIEW</span> DEFINITION <span class="kwrd">ON</span> CERTIFICATE :: MyDatabaseCertificate <span class="kwrd">TO</span> &quot;SERVER\MyWebUser&quot;
<span class="kwrd">GRANT</span> CONTROL <span class="kwrd">ON</span> CERTIFICATE :: MyDatabaseCertificate <span class="kwrd">TO</span> &quot;SERVER\MyWebUser&quot;</pre>
<h2>Step 5: Encrypting and Decrypting Data</h2>
<p>To encrypt and decrypt data you need to be able to <strong>open</strong> the symmetric key. In order to open a symmetric key you need to be in a stored procedure. You can’t open the certificate from a function or from code via TSQL calls.</p>
<p>Procedure to encrypt credit card numbers:</p>
<pre class="csharpcode"><span class="kwrd">CREATE</span> <span class="kwrd">PROCEDURE</span> [dbo].[usp_EncryptCreditCardNumber]
(
    @CreditCardNumber <span class="kwrd">AS</span> <span class="kwrd">VARCHAR</span>(16)
    , @<span class="kwrd">Result</span> VARBINARY(32) <span class="kwrd">OUTPUT</span>
)
<span class="kwrd">AS</span>
<span class="kwrd">BEGIN</span>
    <span class="kwrd">SET</span> NOCOUNT <span class="kwrd">ON</span>

    <span class="kwrd">OPEN</span> SYMMETRIC <span class="kwrd">KEY</span> MyDatabase_Key
    DECRYPTION <span class="kwrd">BY</span> CERTIFICATE MyDatabaseCertificate

    <span class="kwrd">SET</span> @<span class="kwrd">Result</span> = EncryptByKey(Key_GUID(<span class="str">'MyDatabase_Key'</span>), @CreditCardNumber)

    <span class="kwrd">CLOSE</span> SYMMETRIC <span class="kwrd">KEY</span> MyDatabase_Key
END</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Procedure to decrypt credit card numbers:</p>
<pre class="csharpcode"><span class="kwrd">CREATE</span> <span class="kwrd">PROCEDURE</span> [dbo].[usp_DecryptCreditCardNumber]
(
    @EncryptedCreditCardNumber VARBINARY(32)
    , @CreditCardNumber <span class="kwrd">AS</span> <span class="kwrd">VARCHAR</span>(16) <span class="kwrd">OUTPUT</span>
)
<span class="kwrd">AS</span>
<span class="kwrd">BEGIN</span>
    <span class="kwrd">SET</span> NOCOUNT <span class="kwrd">ON</span>

    <span class="kwrd">OPEN</span> SYMMETRIC <span class="kwrd">KEY</span> MyDatabaseKey
    DECRYPTION <span class="kwrd">BY</span> CERTIFICATE MyDatabaseCertificate

    <span class="kwrd">SET</span> @CreditCardNumber = <span class="kwrd">CONVERT</span>(<span class="kwrd">VARCHAR</span>(16), DecryptByKey(@EncryptedCreditCardNumber))

    <span class="kwrd">CLOSE</span> SYMMETRIC <span class="kwrd">KEY</span> MyDatabaseKey
END</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<h2>Step 6: Creating the table that will contain the encrypted data</h2>
<p>Note: The encrypted column(s) have to be of type VarBinary of a length enough to keep the encrypted value of the data. Length depends on the algorithm used which in our scenario is <a href="http://en.wikipedia.org/wiki/Advanced_Encryption_Standard" target="_blank">AES_256</a>. The AES works on blocks of 16 bytes so you should align your data to the next 16 size than the one that you have to encrypt.</p>
<p>So, lets just say you want to encrypt credit card details: You could create a table like:</p>
<pre class="csharpcode"><span class="kwrd">CREATE</span> <span class="kwrd">TABLE</span> [dbo].[CreditCardDetails](
    [CreditCardDetailsId] [<span class="kwrd">int</span>] <span class="kwrd">IDENTITY</span>(1,1) <span class="kwrd">NOT</span> <span class="kwrd">NULL</span>,
    [CreditCardNumber] [varbinary](32) <span class="kwrd">NULL</span>, <span class="rem">-- Encrypted Credit Card</span>
    [CreditCardLast4Digits] [<span class="kwrd">char</span>](4) <span class="kwrd">NULL</span>,
    [CreditCardExpMonth] [tinyint] <span class="kwrd">NULL</span>,
    [CreditCardExpYear] [<span class="kwrd">int</span>] <span class="kwrd">NULL</span>,
    [CreditCardName] [<span class="kwrd">varchar</span>](255) <span class="kwrd">NULL</span>,
    [CreditCardTypeId] [<span class="kwrd">int</span>] <span class="kwrd">NULL</span>,
 <span class="kwrd">CONSTRAINT</span> [PK_CreditCardDetails] <span class="kwrd">PRIMARY</span> <span class="kwrd">KEY</span> <span class="kwrd">CLUSTERED</span>
([CreditCardDetailsId] <span class="kwrd">ASC</span>)
)</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>You <strong>really</strong> want to keep the <strong>Last4Digits</strong> of the card in a separate column so you can present them in different UI items <strong>without</strong> having to decrypt the real credit card number.</p>
<h2>Step 7: Inserting credit card details</h2>
<p>Once we have the encrypt and decrypt procedure, the insert and retrieve become simple two step processes.</p>
<p>Save and encrypt:</p>
<pre class="csharpcode"><span class="kwrd">CREATE</span> <span class="kwrd">PROC</span> [dbo].[usp_CreditCardDetailsInsert]
    @CreditCardExpMonth tinyint,
    @CreditCardExpYear <span class="kwrd">int</span>,
    @CreditCardLast4Digits <span class="kwrd">nchar</span>(4),
    @CreditCardName <span class="kwrd">varchar</span>(255),
    @CreditCardNumber <span class="kwrd">VARCHAR</span>(16),
    @CreditCardTypeId <span class="kwrd">int</span>
<span class="kwrd">AS</span>
    <span class="kwrd">SET</span> NOCOUNT <span class="kwrd">ON</span>
    <span class="kwrd">SET</span> XACT_ABORT <span class="kwrd">ON</span>

    <span class="kwrd">BEGIN</span> <span class="kwrd">TRAN</span>

    <span class="kwrd">DECLARE</span> @EncryptedCreditCardNumber VARBINARY(32)
    <span class="kwrd">EXECUTE</span> dbo.usp_EncryptCreditCardNumber @CreditCardNumber, @EncryptedCreditCardNumber <span class="kwrd">OUTPUT</span>

    INSERT <span class="kwrd">INTO</span> [dbo].[CreditCardDetails] ([CreditCardExpMonth], [CreditCardExpYear], [CreditCardLast4Digits], [CreditCardName], [CreditCardNumber], [CreditCardTypeId])
    <span class="kwrd">SELECT</span> @CreditCardExpMonth, @CreditCardExpYear, @CreditCardLast4Digits, @CreditCardName, @EncryptedCreditCardNumber, @CreditCardTypeId

    <span class="kwrd">COMMIT</span>
GO</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Retrieve and decrypt:</p>
<pre class="csharpcode"><span class="kwrd">CREATE</span> <span class="kwrd">PROC</span> [dbo].[usp_CreditCardDetailsSelectForPayment]
    @CreditCardDetailsId <span class="kwrd">INT</span>
<span class="kwrd">AS</span>
    <span class="kwrd">SET</span> NOCOUNT <span class="kwrd">ON</span>
    <span class="kwrd">SET</span> XACT_ABORT <span class="kwrd">ON</span>

    <span class="rem">-- retrieve the encrypted credit card</span>
    <span class="kwrd">DECLARE</span> @EncryptedCreditCardNumber VARBINARY(32)
    <span class="kwrd">SELECT</span> @EncryptedCreditCardNumber = CreditCardNumber
    <span class="kwrd">FROM</span> dbo.CreditCardDetails
    <span class="kwrd">WHERE</span>  [CreditCardDetailsId] = @CreditCardDetailsId

    <span class="kwrd">DECLARE</span> @CreditCardNumber <span class="kwrd">varchar</span>(16)
    <span class="rem">-- decrypt</span>
    <span class="kwrd">EXECUTE</span> [dbo].[usp_DecryptCreditCardNumber] @EncryptedCreditCardNumber, @CreditCardNumber <span class="kwrd">OUTPUT</span>

    <span class="rem">-- select the other bits</span>
    <span class="kwrd">SELECT</span> [CreditCardDetailsId], [CreditCardExpMonth], [CreditCardExpYear], [CreditCardLast4Digits], [CreditCardName], @CreditCardNumber <span class="kwrd">AS</span> CreditCardNumber, [CreditCardTypeId]
    <span class="kwrd">FROM</span>   [dbo].[CreditCardDetails]
    <span class="kwrd">WHERE</span>  [CreditCardDetailsId] = @CreditCardDetailsId

GO</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p><strong>PLEASE</strong>&#160; make sure that you have another procedure for retrieving the non-critical data like name and last 4 digits of the card for display purposes <strong>without having to decrypt the credit card</strong>. </p>
<p>The <strong>only reason</strong> why you would need to decrypt the credit card is to send it to a payment gateway to process a transaction.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.acorns.com.au/blog/?feed=rss2&amp;p=147</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Do you want to &#8220;change&#8221; your Optus details? Just login. Here are the usernames &amp; passwords.</title>
		<link>http://www.acorns.com.au/blog/?p=146</link>
		<comments>http://www.acorns.com.au/blog/?p=146#comments</comments>
		<pubDate>Thu, 14 May 2009 11:54:27 +0000</pubDate>
		<dc:creator>Corneliu</dc:creator>
				<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.acorns.com.au/blog/?p=146</guid>
		<description><![CDATA[For reasons I’ll detail in a separate post I had to enter today in an Optus shop.
And, in a pure display of ignorance to any basic security measures, they had all the usernames and passwords for all the systems where the dealers could connect and do changes to accounts, create new accounts or remove them [...]]]></description>
			<content:encoded><![CDATA[<p>For reasons I’ll detail in a separate post I had to enter today in an Optus shop.</p>
<p>And, in a pure display of ignorance to any basic security measures, they had all the <strong>usernames and passwords for all the systems</strong> where the dealers could connect and do changes to accounts, create new accounts or remove them <strong>printed and stuck at the top of the screen</strong>.</p>
<p>An no, this was not on a computer somewhere in the back of the store but on a computer in the middle of the room full of people.</p>
<p><a href="http://www.acorns.com.au/Images/blog/DoyouwanttochangeyourOptusdetailsJustlo_13407/optus.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="optus" border="0" alt="optus" src="http://www.acorns.com.au/Images/blog/DoyouwanttochangeyourOptusdetailsJustlo_13407/optus_thumb.jpg" width="404" height="304" /></a>&#160; </p>
<p>(I’ve reduced the size and quality of the image enough to have all the information impossible to read.)</p>
<p>Not only that I took this picture with my mobile but I managed to get friendly enough to play with the computer after the dealer logged in to a system that I could use to change my personal details (or anyone else’s details). I even did a view source on the (poorly coded) code and tried to hack some URLs together to avoid a post back on the page. </p>
<p>Uh, who, me? Never…</p>
<p><strong>So where do you keep your passwords?</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.acorns.com.au/blog/?feed=rss2&amp;p=146</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Microsoft SDC Open Day</title>
		<link>http://www.acorns.com.au/blog/?p=145</link>
		<comments>http://www.acorns.com.au/blog/?p=145#comments</comments>
		<pubDate>Fri, 01 May 2009 00:07:27 +0000</pubDate>
		<dc:creator>Corneliu</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Readify]]></category>

		<guid isPermaLink="false">http://www.acorns.com.au/blog/?p=145</guid>
		<description><![CDATA[On Monday the 6th of April Microsoft Australia organized the SDC Open Day 2009 event (SDC stands for [Microsoft] Software Development Centre, part of the Microsoft Services (Australia).
For over six years, Microsoft Australia’s Solutions Development Centre has been successfully delivering complex custom-developed applications for customers. We follow a set of processes that have enabled us [...]]]></description>
			<content:encoded><![CDATA[<p>On Monday the 6th of April Microsoft Australia organized the <a href="https://www.microsoft.com/australia/services/microsoftservices/sdc_openday.mspx" target="_blank">SDC Open Day 2009</a> event (SDC stands for [Microsoft] <a href="https://www.microsoft.com/australia/services/microsoftservices/srv_msdc.mspx" target="_blank">Software Development Centre</a>, part of the <a href="http://www.microsoft.com/australia" target="_blank">Microsoft Services (Australia)</a>.</p>
<blockquote><p>For over six years, Microsoft Australia’s Solutions Development Centre has been successfully delivering complex custom-developed applications for customers. We follow a set of processes that have enabled us to build high-performing teams delivering quality solutions. At the SDC Open Day we talked about how we have achieved this success, sharing our processes and techniques with an audience of software development professionals. </p>
<p>In the presentations below we provide an overview of the SDC, a day in the life for the team and details on how we approach each of the key disciplines in an SDC project team.</p>
</blockquote>
<p>Together with few others like <a href="http://blogs.msdn.com/tomholl/" target="_blank">Tom Hollander</a>, Prasadi de Silva (they are Microsoft so they don’t really count), , <a href="http://www.teknologika.com/blog/im-presenting-at-the-microsoft-sdc-open-day/" target="_blank">Bruce</a>, Sarah from <a href="http://www.devtest.com/" target="_blank">DevTest</a> and Simon and Emma from <a href="http://www.avanade.com" target="_blank">Avanade</a> we were invited to do talks on different aspects of the software development process in the SDC from Project Management, Development, Testing to Setup and Deployment.</p>
<p>The videos from the day <a href="https://www.microsoft.com/australia/services/microsoftservices/sdc_openday.mspx" target="_blank">are all published now</a>. The fifth one in the list is my presentation <img src='http://www.acorns.com.au/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  however I can’t seem to be able to play it as I continuously get an error.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.acorns.com.au/blog/?feed=rss2&amp;p=145</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Leaving Readify: Got a new job &amp; new responsibilities</title>
		<link>http://www.acorns.com.au/blog/?p=144</link>
		<comments>http://www.acorns.com.au/blog/?p=144#comments</comments>
		<pubDate>Sun, 26 Apr 2009 08:33:44 +0000</pubDate>
		<dc:creator>Corneliu</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Readify]]></category>

		<guid isPermaLink="false">http://www.acorns.com.au/blog/?p=144</guid>
		<description><![CDATA[It’s public now that as of the 15th of May I’ve resigned from the position of Senior Consultant in Readify.
I’ve worked with some of the best minds in the industry from Darren, Mitch, PaulG and PaulS (who is still Temporarily Offline), Philip, Damian, Tatham, Francois (who’s now enjoying his trip around the world), Aymeric and [...]]]></description>
			<content:encoded><![CDATA[<p>It’s public now that as of the 15th of May I’ve resigned from the position of Senior Consultant in <a href="http://www.readify.net" target="_blank">Readify</a>.</p>
<p>I’ve worked with some of the best minds in the industry from <a href="http://showusyourcode.spaces.live.com/" target="_blank">Darren</a>, <a href="http://www.notgartner.com" target="_blank">Mitch</a>, <a href="http://weblogs.asp.net/pglavich/" target="_blank">PaulG</a> and <a href="http://www.paulstovell.net/" target="_blank">PaulS</a> (who is still Temporarily Offline), <a href="http://www.philipbeadle.net/" target="_blank">Philip</a>, <a href="http://damianedwards.wordpress.com/" target="_blank">Damian</a>, <a href="http://blog.tatham.oddie.com.au/" target="_blank">Tatham</a>, <a href="http://www.mytb.org/francoisbeaussier" target="_blank">Francois</a> (who’s now enjoying his trip around the world), Aymeric and lots, lots more.</p>
<p>I’ve worked with some great people from outside Readify like <a href="http://blogs.msdn.com/tomholl/" target="_blank">Tom Hollander</a>, <a href="http://blogs.msdn.com/rockyh/" target="_blank">Rocky Heckman</a>, <a href="http://www.teknologika.com/" target="_blank">Bruce McLeod</a>, to mention only a few.. </p>
<p>I think this was the best, coolest and most enjoyable job I’ve ever had, with involvement in various projects from fixing simple (sometimes crappy) VB.Net apps to technical lead of large projects, performance reviews and improvements, security reviews and guidance, threat modelling new or existing systems, architecture and design of new systems, architecture reviews, complete or partial refactoring and rewrites of applications, mentoring, guidance, training, talking, presenting or attending loads of conferences, whinging, crying (not really) and contesting or challenging technical decisions (most of the times offering alternatives), building awesome relationships with my teams and my customers (oh, well, at least that’s what I believe I did <img src='http://www.acorns.com.au/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</p>
<p>Even more I also received an <a href="http://www.acorns.com.au/blog/?p=137" target="_blank">MVP award in Development Security</a>.</p>
<p>I’ve loved my position and the people I’ve been working with.</p>
<p>But now it’s time to move on to a new challenge and a new role. I’ve accepted a new role as a (full-time) Solutions Architect and 2IC with <a href="http://www.classfinancialsystems.com.au/" target="_blank">Class Financial Systems</a>. More or less I’m going back into financial systems where most of my background and experience was before I’ve joined Readify.</p>
<p>I’ll have a great new set of challenges from leading a new team to delivering a new project from zero to hero (or great success). I kind of started to forget what it means to be part of a project form end to end. I’ll also try to use this opportunity to learn, apply, present and (cross-fingers) be approved as a <a href="http://www.microsoft.com/learning/mcp/architect/solutions/default.mspx" target="_blank">Microsoft Certified Architect: Solutions</a>.</p>
<p>I truly hope I’ll keep in good contact with all Readify, Microsoft, friends and partners and I hope to see most of you at user groups, TechEd, CodeCamp, MVP Summit and other conferences.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.acorns.com.au/blog/?feed=rss2&amp;p=144</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>WCF Dynamic Client Proxy &#8211; Implementing IDisposable</title>
		<link>http://www.acorns.com.au/blog/?p=143</link>
		<comments>http://www.acorns.com.au/blog/?p=143#comments</comments>
		<pubDate>Thu, 19 Feb 2009 09:28:33 +0000</pubDate>
		<dc:creator>Corneliu</dc:creator>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[WCF Dynamic Proxy]]></category>

		<guid isPermaLink="false">http://www.acorns.com.au/blog/?p=143</guid>
		<description><![CDATA[As my good friend and Readify colleague Buddhike observed the generated WCF Dynamic Proxy was not implementing IDisposable. This is a bit scary as I was quite sure I had that implementation prepared done already so all I can image is that for some reason (no source control?) I’ve been working on an older code [...]]]></description>
			<content:encoded><![CDATA[<p>As my good friend and Readify colleague <a href="http://geeksdiary.com/default.aspx">Buddhike</a> observed the generated <a href="http://www.acorns.com.au/blog/?p=142">WCF Dynamic Proxy</a> was not implementing IDisposable. This is a bit scary as I was quite sure I had that implementation <strike>prepared</strike> done already so all I can image is that for some reason (no source control?) I’ve been working on an older code base.</p>
<p>So, here is a new implementation that is implementation that implements IDisposable. The only trick is that you have to cast the received proxy to IDisposable in order to use it in a using statement. As a difference from the WCF implementation, the dynamic proxy does not throw at all from the implementation of the IDisposable so it’s safe to use in a using statement:</p>
<pre class="code"><span style="color: #2b91af">IService1 </span>service = <span style="color: #2b91af">WCFClientProxy</span>&lt;<span style="color: #2b91af">IService1</span>&gt;.GetReusableFaultUnwrappingInstance(<span style="color: #a31515">&quot;Service1&quot;</span>);
<span style="color: blue">using </span>(service <span style="color: blue">as </span><span style="color: #2b91af">IDisposable</span>)
{
    service.MyOperation1(<span style="color: #a31515">&quot;a&quot;</span>, 0);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>Please download the new version of <a href="http://www.acorns.com.au/files/ACorns.WCF.DynamicProxy.1.3.1.zip">WCF Client Proxy 1.3.1</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.acorns.com.au/blog/?feed=rss2&amp;p=143</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WCF Dynamic (ClientBase) Proxy part three: Connection Pooling and Automatic Disposing</title>
		<link>http://www.acorns.com.au/blog/?p=142</link>
		<comments>http://www.acorns.com.au/blog/?p=142#comments</comments>
		<pubDate>Tue, 17 Feb 2009 04:14:23 +0000</pubDate>
		<dc:creator>Corneliu</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[WCF Dynamic Proxy]]></category>

		<guid isPermaLink="false">http://www.acorns.com.au/blog/?p=142</guid>
		<description><![CDATA[A while ago I published a small and neat Dynamic Proxy that could be used to automatically create for you implementation of the (WCF) ClientBase&#60;T&#62; so you would not have to generate that from the service interface and hand-coded or even bother to maintain.
One of it’s great advantages was that you could ask for a [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago <a href="http://www.acorns.com.au/blog/?p=113">I published a small and neat Dynamic Proxy</a> that could be used to automatically create for you implementation of the (WCF) ClientBase&lt;T&gt; so you would not have to generate that from the service interface and hand-coded or even bother to maintain.</p>
<p>One of it’s great advantages was that you could ask for a “Reusable” proxy which was basically a wrapper around the proxy that you didn’t have to close/dispose in case of a fault.</p>
<p>As <a href="http://safari.oreilly.com/0596526997/orm9780596526993-APP-C-SECT-7">you might know WCF requires</a> that you dispose of the proxy if there is any type of fault detected. Doing this is a pain in any type of code as you have to manage that connection life-time while actually all you care most of the times is that you talk to the service and not the life-time of your connection. The WCF Client Proxy was also doing this management for you allowing you so simply focus on your business and not on opening/closing your connections.</p>
<p>However there are moments in which you also want to specifically close your proxy for example using a <em>using</em> statement. Because the WCF Client Proxy was returning you the exact interface that you requested and if that interface was not implementing IDisposable you had to reside on a trick and cast the received proxy to IDisposable (as the proxy generates that behinds the scenes for you anyway) and use it like this:</p>
<pre class="code"><span style="color: #2b91af">IService1 </span>service = <span style="color: #2b91af">WCFClientProxy</span>&lt;<span style="color: #2b91af">IService1</span>&gt;.GetReusableInstance(<span style="color: #a31515">&quot;Service1&quot;</span>);
<span style="color: blue">using</span>((<span style="color: #2b91af">IDisposable</span>)service)
{
    service.MyOperation1();
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This was again a bit ugly as you have to care about the connection and you can’t just use it as any other interface. You do have to be aware that your interface represents a WCF service that you want to dispose of.</p>
<p>One other potential scenario that several people hit is in websites that talk to external WCF services and you use the same service from within the same page several times (for example from different controls). In practice in this scenario you will open/close the same proxy multiple times during the lifetime of the page.</p>
<h2>Connection Pooling</h2>
<p>In order to simplify the management of WCF connections during the lifetime of a webpage or even a WCF Operation Call and reduce the overhead of caring to dispose of proxies I’ve now added a <em>ProxyConnectionPool</em> class to the dynamic proxy that can be enabled to automatically pickup all the created WCF connections and pool them (as in return the same one back to you if you use it from the same thread and it’s safe to reuse).</p>
<p>To enable the connection pooling all you have to do is:</p>
<pre class="code"><span style="color: #2b91af">ProxyConnectionPool</span>.EnableConnectionPool = <span style="color: blue">true</span>;</pre>
<p><a href="http://11011.net/software/vspaste"></a>Now every time you do a call do GetReusableInstance or other methods on the WCFClientProxy the connection you receive might be a pooled one (same tread only pooling) or a new one just registered with the pool. This should improve your performance as <a href="http://weblogs.asp.net/pglavich/archive/2007/02/24/wcf-proxy-performance-vs-wse-v3.aspx">creating WCF Proxies is an expensive process</a> that you want to avoid as much as possible.</p>
<h2>Disposing the pool</h2>
<p>Once you finish your work on that thread you can simply ask the connection pool to dispose all the WCF connections from the pool using:</p>
<pre class="code"><span style="color: #2b91af">ProxyConnectionPool</span>.Current.Dispose();</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h2>Automatic disposing</h2>
<p>If you use WCF inside a website to call other WCF services you should then simply include the following module in your web.config so you get automatic disposing of all WCF connections created on each page request (with peace of mind included).</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">system.web</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">httpModules</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">add </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">WcfConnectionPool</span>&quot; <span style="color: red">type</span><span style="color: blue">=</span>&quot;<span style="color: blue">ACorns.WCF.DynamicClientProxy.Pool.WcfConnectionPoolHandler,</span><span style="color: blue">ACorns.WCF.DynamicClientProxy</span>&quot;<span style="color: blue">/&gt;
    &lt;/</span><span style="color: #a31515">httpModules</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">system.web</span><span style="color: blue">&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This module will automatically enable the connection pool for you and make sure all the WCF proxies created are nicely disposed at the end of each page request. This truly allows you to focus on writing your business code and not bothering about connection management of your WCF services. Your code will now become:</p>
<pre class="code"><span style="color: #2b91af">IService1 </span>service = <span style="color: #2b91af">WCFClientProxy</span>&lt;<span style="color: #2b91af">IService1</span>&gt;.GetReusableInstance(<span style="color: #a31515">&quot;Service1&quot;</span>);
service.MyOperation1();</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Let someone else deal with the fact that you use WCF <img src='http://www.acorns.com.au/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Licence</h2>
<p>I was requested several times about the licence this code is. Here is my official statement:</p>
<p><em>This package is provided &quot;AS IS,&quot; without express or implied warranty of any kind, and may be used and modified.<br />
    <br />This package may be used in corporate applications without any pretence.</em></p>
<p>However I would appreciate if you would drop me a line to let me know you are using it to corneliu at acorns.com.au or even donate some money by PayPal to the same address <img src='http://www.acorns.com.au/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Download</h2>
<p>Here is the latest version of the <a href="http://www.acorns.com.au/files/ACorns.WCF.DynamicProxy.1.3.zip">WCF Client Proxy 1.3.0</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.acorns.com.au/blog/?feed=rss2&amp;p=142</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing a super fast deep-property reader/writer using delegates</title>
		<link>http://www.acorns.com.au/blog/?p=141</link>
		<comments>http://www.acorns.com.au/blog/?p=141#comments</comments>
		<pubDate>Mon, 16 Feb 2009 12:24:26 +0000</pubDate>
		<dc:creator>Corneliu</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.acorns.com.au/blog/?p=141</guid>
		<description><![CDATA[For a while now I’ve been writing to write a non-reflection based property reader that allows me to read properties of an object in a deep hierarchy.
So lets say you have the following simple class hierarchy:
public class FirstLevel
{
    public string Leaf { get; set; }
}
public class SecondLevel
{
    public FirstLevel [...]]]></description>
			<content:encoded><![CDATA[<p>For a while now I’ve been writing to write a non-reflection based property reader that allows me to read properties of an object in a deep hierarchy.</p>
<p>So lets say you have the following simple class hierarchy:</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">FirstLevel
</span>{
    <span style="color: blue">public string </span>Leaf { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
}
<span style="color: blue">public class </span><span style="color: #2b91af">SecondLevel
</span>{
    <span style="color: blue">public </span><span style="color: #2b91af">FirstLevel </span>First { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
}
<span style="color: blue">public class </span><span style="color: #2b91af">Root
</span>{
    <span style="color: blue">public </span><span style="color: #2b91af">SecondLevel </span>Prop { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Then you want to read the Leaf from the FirstLevel by looking at the Root level and all you have is the path to the property: “SecondLevel.FirstLevel.Leaf”.</p>
<p>Using reflection is a simple process of recursively going through the object, finding the proper property doing a GetValue on it and then repeating the process until you find your property. This is ok but it’s very slow. Very very slow.</p>
<p>Your other two alternatives if you don’t want the bear the hit of reflection is to code emit a method that would resemble something like this:</p>
<pre class="code"><span style="color: blue">public static string </span>GetLeaf(<span style="color: #2b91af">Root </span>root)
{
    <span style="color: #2b91af">SecondLevel </span>secondLevel = root.Prop;
    <span style="color: blue">if </span>( secondLevel != <span style="color: blue">null </span>)
    {
        <span style="color: #2b91af">FirstLevel </span>firstLevel = secondLevel.First;
        <span style="color: blue">if </span>( firstLevel != <span style="color: blue">null </span>)
        {
            <span style="color: blue">return </span>firstLevel.Leaf;
        }
    }
    <span style="color: blue">return null</span>;
}</pre>
<p>Or use dynamically created delegates to map to the get_Property and set_Property methods generated for each property.</p>
<p>&#160;</p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Reflection;

<span style="color: blue">namespace </span>ACorns.Utils
{
    <span style="color: blue">public interface </span><span style="color: #2b91af">IPropertyAccessor
    </span>{
        <span style="color: blue">object </span>GetValue(<span style="color: blue">object </span>target);
        <span style="color: blue">void </span>SetValue(<span style="color: blue">object </span>target, <span style="color: blue">object </span>value);
    }

    <span style="color: blue">public interface </span><span style="color: #2b91af">IPropertyAccessor</span>&lt;TargetObject, FinalProperty&gt;
    {
        FinalProperty GetValue(TargetObject target);
        <span style="color: blue">void </span>SetValue(TargetObject target, FinalProperty value);
    }

    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Super-dooper, super-fact deep property extractor.
    </span><span style="color: gray">/// </span><span style="color: green">You can use it to get/set properties deep in an object hierarchy without using reflection.
    </span><span style="color: gray">/// </span><span style="color: green">Please cache the returned IPropertyAccessor if you want to reuse it.
    </span><span style="color: gray">/// </span><span style="color: green">Good performance is only achived with cached IPropertyAccessor(s)!
    </span><span style="color: gray">///
    /// </span><span style="color: green">Usage: IPropertyAccessor accessor = PropertyExtractor.GetAccessor(typeof(Root), &quot;Prop.First.Leaf&quot;, true);
    </span><span style="color: gray">/// </span><span style="color: green">accessor.GetValue(target);
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public static class </span><span style="color: #2b91af">PropertyExtractor
    </span>{
        <span style="color: blue">public static </span>IPropertyAccessor&lt;TargetObject, FinalProperty&gt;
            GetAccessor&lt;TargetObject, FinalProperty&gt;(<span style="color: #2b91af">Type </span>targetType, <span style="color: blue">string </span>propertyNames)
        {
            <span style="color: blue">return </span>GetAccessor&lt;TargetObject, FinalProperty&gt;(targetType, propertyNames, <span style="color: blue">true</span>);
        }
        <span style="color: blue">public static </span>IPropertyAccessor&lt;TargetObject, FinalProperty&gt;
            GetAccessor&lt;TargetObject, FinalProperty&gt;(<span style="color: #2b91af">Type </span>targetType, <span style="color: blue">string </span>propertyNames, <span style="color: blue">bool </span>throwOnNull)
        {
            IPropertyAccessor internalPropertyAccessor = GetAccessor(targetType, propertyNames, throwOnNull);

            <span style="color: #2b91af">Type </span>accessorType = <span style="color: blue">typeof</span>(TypedPropertyAccessor&lt;,&gt;).MakeGenericType(<span style="color: blue">typeof</span>(TargetObject), <span style="color: blue">typeof</span>(FinalProperty));
            IPropertyAccessor&lt;TargetObject, FinalProperty&gt; propertyAccessor =
                (IPropertyAccessor&lt;TargetObject, FinalProperty&gt;)<span style="color: #2b91af">Activator</span>.CreateInstance(accessorType, internalPropertyAccessor);

            <span style="color: blue">return </span>propertyAccessor;
        }
        <span style="color: blue">public static </span>IPropertyAccessor GetAccessor(<span style="color: #2b91af">Type </span>targetType, <span style="color: blue">string </span>propertyNames, <span style="color: blue">bool </span>throwOnNull)
        {
            <span style="color: blue">string</span>[] deepPropertyNames = propertyNames.Split(<span style="color: #a31515">'.'</span>);
            IPropertyAccessor internalPropertyAccessor = GetAccessor(targetType, deepPropertyNames, 0, throwOnNull);
            <span style="color: blue">return </span>internalPropertyAccessor;
        }

        <span style="color: blue">private static </span>IPropertyAccessor GetAccessor(<span style="color: #2b91af">Type </span>targetType, <span style="color: blue">string</span>[] deepPropertyNames, <span style="color: blue">int </span>level, <span style="color: blue">bool </span>throwOnNull)
        {
            <span style="color: blue">string </span>property = deepPropertyNames[level];

            <span style="color: #2b91af">PropertyInfo </span>propertyInfo = targetType.GetProperty(property);

            <span style="color: green">// Create a delegate to a get_ method. The delegate looks like
            // Func&lt;TargetType, PropertyType&gt; func to a property like class TargetType { public PropertyType { get; } }
            </span><span style="color: #2b91af">Type </span>getterDelegateType = <span style="color: blue">typeof</span>(Func&lt;,&gt;).MakeGenericType(targetType, propertyInfo.PropertyType);
            <span style="color: #2b91af">Delegate </span>getDelegate = <span style="color: #2b91af">Delegate</span>.CreateDelegate(getterDelegateType, propertyInfo.GetGetMethod());

            IPropertyAccessor accessor;

            level++;
            <span style="color: blue">if </span>(level &lt; deepPropertyNames.Length)
            {
                <span style="color: green">// Recursive detect the down the property
                </span>IPropertyAccessor nextLevelAccessor = GetAccessor(propertyInfo.PropertyType, deepPropertyNames, level, throwOnNull);

                <span style="color: #2b91af">Type </span>accessorType = <span style="color: blue">typeof</span>(PropertyAccessor&lt;,&gt;).MakeGenericType(targetType, propertyInfo.PropertyType);
                accessor = (IPropertyAccessor)<span style="color: #2b91af">Activator</span>.CreateInstance(accessorType, getDelegate, nextLevelAccessor, throwOnNull);
            }
            <span style="color: blue">else
            </span>{
                <span style="color: #2b91af">Type </span>setterDelegateType = <span style="color: blue">typeof</span>(Action&lt;,&gt;).MakeGenericType(targetType, propertyInfo.PropertyType);
                <span style="color: #2b91af">Delegate </span>setDelegate = <span style="color: #2b91af">Delegate</span>.CreateDelegate(setterDelegateType, propertyInfo.GetSetMethod());

                <span style="color: #2b91af">Type </span>accessorType = <span style="color: blue">typeof</span>(LeafPropertyAccessor&lt;,&gt;).MakeGenericType(targetType, propertyInfo.PropertyType);
                accessor = (IPropertyAccessor)<span style="color: #2b91af">Activator</span>.CreateInstance(accessorType, getDelegate, setDelegate);
            }

            <span style="color: blue">return </span>accessor;
        }

        <span style="color: blue">#region </span>TypedPropertyAccessor
        <span style="color: blue">internal sealed class </span><span style="color: #2b91af">TypedPropertyAccessor</span>&lt;T, U&gt; : IPropertyAccessor&lt;T, U&gt;
        {
            <span style="color: blue">private readonly </span>IPropertyAccessor _next;

            <span style="color: blue">public </span>TypedPropertyAccessor(IPropertyAccessor next)
            {
                _next = next;
            }

            <span style="color: blue">public </span>U GetValue(T target)
            {
                <span style="color: blue">return </span>(U) _next.GetValue(target);
            }

            <span style="color: blue">public void </span>SetValue(T target, U value)
            {
                _next.SetValue(target, value);
            }
        }
        <span style="color: blue">#endregion

        #region </span>Recursive Property Accessors
        <span style="color: blue">internal sealed class </span><span style="color: #2b91af">PropertyAccessor</span>&lt;T, U&gt; : IPropertyAccessor
        {
            <span style="color: blue">private readonly </span>Func&lt;T,U&gt; _readDelegate;
            <span style="color: blue">private readonly </span>IPropertyAccessor _next;
            <span style="color: blue">private readonly bool </span>_throwOnNull;

            <span style="color: blue">public </span>PropertyAccessor(Func&lt;T, U&gt; readDelegate, IPropertyAccessor next, <span style="color: blue">bool </span>throwOnNull)
            {
                _readDelegate = readDelegate;
                _throwOnNull = throwOnNull;
                _next = next;
            }
            <span style="color: blue">public object </span>GetValue(<span style="color: blue">object </span>target)
            {
                <span style="color: blue">object </span>result = _readDelegate((T)target);
                <span style="color: blue">if </span>(result == <span style="color: blue">null</span>)
                {
                    <span style="color: blue">if </span>(_throwOnNull)
                        <span style="color: blue">throw new </span><span style="color: #2b91af">NullReferenceException</span>(<span style="color: #a31515">&quot;Property '&quot; </span>+ _readDelegate.Method.Name + <span style="color: #a31515">&quot;' on '&quot; </span>+
                            <span style="color: blue">typeof</span>(T).Name + <span style="color: #a31515">&quot;' returned null.&quot;</span>);
                    <span style="color: blue">else
                        return default</span>(U);
                }
                <span style="color: blue">return </span>_next.GetValue(result);
            }
            <span style="color: blue">public void </span>SetValue(<span style="color: blue">object </span>target, <span style="color: blue">object </span>value)
            {
                <span style="color: blue">object </span>result = _readDelegate((T)target);
                <span style="color: blue">if </span>(result == <span style="color: blue">null</span>)
                {
                    <span style="color: blue">if </span>(_throwOnNull)
                        <span style="color: blue">throw new </span><span style="color: #2b91af">NullReferenceException</span>(<span style="color: #a31515">&quot;Property '&quot; </span>+ _readDelegate.Method.Name + <span style="color: #a31515">&quot;' on '&quot; </span>+
                            <span style="color: blue">typeof</span>(T).Name + <span style="color: #a31515">&quot;' returned null.&quot;</span>);
                    <span style="color: blue">else
                        return</span>;
                }
                _next.SetValue((U)result, value);
            }
        }
        <span style="color: blue">internal sealed class </span><span style="color: #2b91af">LeafPropertyAccessor</span>&lt;T, U&gt; : IPropertyAccessor
        {
            <span style="color: blue">private readonly </span>Func&lt;T, U&gt; _readDelegate;
            <span style="color: blue">private readonly </span>Action&lt;T, U&gt; _setDelegate;

            <span style="color: blue">public </span>LeafPropertyAccessor(Func&lt;T, U&gt; readDelegate, Action&lt;T, U&gt; setDelegate)
            {
                _readDelegate = readDelegate;
                _setDelegate = setDelegate;
            }
            <span style="color: blue">public object </span>GetValue(<span style="color: blue">object </span>target)
            {
                <span style="color: blue">object </span>result = _readDelegate((T)target);
                <span style="color: blue">return </span>result;
            }
            <span style="color: blue">public void </span>SetValue(<span style="color: blue">object </span>target, <span style="color: blue">object </span>value)
            {
                _setDelegate((T)target, (U)value);
            }
        }
        <span style="color: blue">#endregion
    </span>}
}</pre>
<p>To use it you would request an IPropertyExtractor and then ask it to do a GetValue for you:</p>
<p>[TestMethod]<br />
  <br /><span style="color: blue">public void </span>ExtractPropertyFromLeafFixture()</p>
<p>{</p>
<p>&#160;&#160;&#160; Root r = <span style="color: blue">new </span>Root();</p>
<p>&#160;&#160;&#160; r.Prop = <span style="color: blue">new </span>SecondLevel();</p>
<p>&#160;&#160;&#160; r.Prop.First = <span style="color: blue">new </span>FirstLevel();</p>
<p>&#160;&#160;&#160; r.Prop.First.Leaf = <span style="color: #a31515">&quot;original value&quot;</span>;</p>
<p>&#160;&#160;&#160; IPropertyAccessor accessor = PropertyExtractor.GetAccessor(<span style="color: blue">typeof</span>(Root), <span style="color: #a31515">&quot;Prop.First.Leaf&quot;</span>, <span style="color: blue">false</span>);</p>
<p>&#160;&#160;&#160; Assert.IsNotNull(accessor);</p>
<p>&#160;&#160;&#160; <span style="color: blue">object </span>value = accessor.GetValue(r);</p>
<p>&#160;&#160;&#160; Assert.AreEqual(<span style="color: #a31515">&quot;original value&quot;</span>, value.ToString());</p>
<p>&#160;&#160;&#160; accessor.SetValue(r, <span style="color: #a31515">&quot;new value&quot;</span>);</p>
<p>&#160;&#160;&#160; <br />&#160;&#160;&#160; value = accessor.GetValue(r);</p>
<p>&#160;&#160;&#160; Assert.AreEqual(<span style="color: #a31515">&quot;new value&quot;</span>, value.ToString());</p>
<p>}</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The beauty of this approach is that once the IPropertyAccessor is created (using Reflection) in the beginning,&#160; the Get/Set on it is done via a set of direct delegate calls to the property and calls via the interface to the next level.</p>
<p>The callstack is thus very small and efficient:</p>
<p><a href="http://www.acorns.com.au/Images/blog/Writingasuperfastdeeppropertyreaderwrite_12CE2/image.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://www.acorns.com.au/Images/blog/Writingasuperfastdeeppropertyreaderwrite_12CE2/image_thumb.png" width="727" height="160" /></a></p>
<p>Make sure you cache this accessor if you need it again at a later time as creating it is expensive.</p>
<p>The performance should be as good as you can get without using code emitting. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.acorns.com.au/blog/?feed=rss2&amp;p=141</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
