<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Infovark Underground</title>
	
	<link>http://underground.infovark.com</link>
	<description>The Infovark technology blog</description>
	<lastBuildDate>Thu, 12 May 2011 16:32:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/InfovarkUnderground" /><feedburner:info uri="infovarkunderground" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Review: Brownfield Application Development in .NET</title>
		<link>http://feedproxy.google.com/~r/InfovarkUnderground/~3/eKG5qcVBJDo/</link>
		<comments>http://underground.infovark.com/2011/04/04/review-brownfield-application-development-in-net/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 01:58:36 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=802</guid>
		<description><![CDATA[If you're inheriting an old .NET application and don't know quite where to begin, Brownfield Application Development in .NET is an excellent guide to tools and practices that can help get you started. 


Related posts:<ol><li><a href='http://underground.infovark.com/2009/05/11/review-framework-design-guidelines/' rel='bookmark' title='Review: Framework Design Guidelines'>Review: Framework Design Guidelines</a></li>
<li><a href='http://underground.infovark.com/2008/11/07/review-working-effectively-with-legacy-code/' rel='bookmark' title='Review: Working Effectively with Legacy Code'>Review: Working Effectively with Legacy Code</a></li>
<li><a href='http://underground.infovark.com/2008/11/05/review-the-pragmatic-programmer/' rel='bookmark' title='Review: The Pragmatic Programmer'>Review: The Pragmatic Programmer</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I purchased my copy of <a href="http://www.amazon.com/Brownfield-Application-Development-Donald-Belcham/dp/1933988711%3FSubscriptionId%3DAKIAIU3RPTD7NQ47YK4A%26tag%3Dinfovark-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D1933988711">Brownfield Application Development in .Net</a> almost as soon as it hit the shelves. I&#8217;ve been reading Kyle Baley&#8217;s entertaining posts about software development on <a href="http://codebetter.com">CodeBetter.com</a> for several years, so I thought I&#8217;d get his and co-author Donald Belcham&#8217;s consolidated development advice in a handy paperback form.</p>
<div class="wp-caption alignleft" style="width: 137px"><a href="http://www.amazon.com/Brownfield-Application-Development-Donald-Belcham/dp/1933988711%3FSubscriptionId%3DAKIAIU3RPTD7NQ47YK4A%26tag%3Dinfovark-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D1933988711"><img src="http://ecx.images-amazon.com/images/I/51nKKDUX4uL._SL160_.jpg" alt="Brownfield Application Development in .NET" /></a><p class="wp-caption-text">Brownfield Application Development in .NET</p></div>
<p>I was surprised to read that <a href="http://codebetter.com/kylebaley/2011/04/01/brownfield-application-development-one-year-later/"><em>Brownfield</em> has been out for a full year now</a>, and figured it was high time to post my review of it. (I&#8217;d been meaning to post a review of the book for several months, but it&#8217;s been on loan to several different people. I didn&#8217;t get it back into my possession until a few weeks ago, which should tell you something about its quality right away.)</p>
<p><em>Brownfield</em> is about the tools and techniques required to get an existing .NET application under control. Sadly, most of us developers have encountered development projects that were rushed, cobbled together, barely functional messes. This book is about reworking the application and its surrounding environment so that development work can continue on a sustainable footing.</p>
<p>The book is divided into two parts: the ecosystem and the code. The ecosystem describes all the necessary components to support modern software development:</p>
<ul>
<li>Source control</li>
<li>Continuous integration</li>
<li>Automated testing</li>
<li>Code analysis and metrics</li>
<li>Defect tracking</li>
</ul>
<p>This was the most valuable part of the book. You&#8217;ll see these topics covered elsewhere, but they&#8217;re often scattered across vendor sites, developer forms, and programmer blogs. Having all these best practices gathered in one place is worth the price of the book alone. It makes a great checklist to use to evaluate the status of your current development projects as well as any future work you undertake. </p>
<p>I&#8217;ve also found it helpful to get Java developers or development teams up to speed on the .NET suite of tools and practices. It&#8217;s saved me a lot of time translating the &#8220;we used to do it like this in Java&#8230;&#8221; conversations into their .NET equivalents.</p>
<p>The second part of the book concerns coding practices that can help sort out a legacy codebase. I found this much less useful than the first section. While <em>Brownfield</em> provides a useful summary, much of this is material that is covered in depth elsewhere. The whirlwind tour of coding practices includes:</p>
<ul>
<li>OO principles</li>
<li>Layers and tiers</li>
<li>Taming dependencies</li>
<li>User interface patterns</li>
<li>Data access patterns</li>
<li>Managing external dependencies</li>
<li>Keeping up momentum</li>
</ul>
<p>The final chapter in the list is the only one that touches on the project management aspects of getting an unruly development effort under control. Though it too is a topic that has been covered elsewhere, I was very glad to see that it was included in the book. I&#8217;ve found that a slapdash approach to application development reflects as much upon poor management practices as it does on a lack of programmer skills &#8212; if not more. </p>
<h4>In conclusion</h4>
<p>If you&#8217;re inheriting an old .NET application and don&#8217;t know quite where to begin, Brownfield Application Development in .NET is an excellent guide to tools and practices that can help get you started. </p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2009/05/11/review-framework-design-guidelines/' rel='bookmark' title='Review: Framework Design Guidelines'>Review: Framework Design Guidelines</a></li>
<li><a href='http://underground.infovark.com/2008/11/07/review-working-effectively-with-legacy-code/' rel='bookmark' title='Review: Working Effectively with Legacy Code'>Review: Working Effectively with Legacy Code</a></li>
<li><a href='http://underground.infovark.com/2008/11/05/review-the-pragmatic-programmer/' rel='bookmark' title='Review: The Pragmatic Programmer'>Review: The Pragmatic Programmer</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/eKG5qcVBJDo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2011/04/04/review-brownfield-application-development-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://underground.infovark.com/2011/04/04/review-brownfield-application-development-in-net/</feedburner:origLink></item>
		<item>
		<title>Using Modal Dialogs with a Splash Screen in WPF</title>
		<link>http://feedproxy.google.com/~r/InfovarkUnderground/~3/IVIfIANeLSk/</link>
		<comments>http://underground.infovark.com/2011/03/23/using-modal-dialogs-with-a-splash-screen-in-wpf/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 01:17:45 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[modal dialog]]></category>
		<category><![CDATA[splash screen]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=792</guid>
		<description><![CDATA[Due to a snag in the way that Visual Studio implements its WPF Splash Screen feature, you can't use modal dialogs before your main window appears. Here are a few workarounds.


<i>No related posts.</i>]]></description>
			<content:encoded><![CDATA[<p>We ran into a strange problem while our Infovark application was starting up. If we displayed a modal dialog prior to our main window, it would appear briefly on the screen, then vanish. The user wouldn&#8217;t have any time to click any buttons, much less actually <em>read</em> the message on the dialog.</p>
<p>We&#8217;d been using modal dialogs in our startup routine for several purposes. We&#8217;d display an error message if certain application prerequisites hadn&#8217;t been met. We might inform the user that the system was performing an upgrade. Or, most important, we might show our trial/evaluation dialog. This last message tells the user how many days were remaining in the evaluation period, and provides a way for the user to visit our website and buy a copy. (Critical functionality for us!)</p>
<p>All of these messages had been working properly in our integration tests. And then one day they stopped working.</p>
<h4>Splish splash</h4>
<p>After looking through our check-in logs and hunting around online, we discovered that the problem had to do with the WPF Splash Screen mechanism. We&#8217;d followed the instructions in <a href="http://msdn.microsoft.com/en-us/library/cc656886.aspx">How To Add a Splash Screen to a WPF Application</a> and added a static image to our project to display to the user while the application was loading.</p>
<p>But apparently, this has a nasty side effect of clobbering any modal dialogs that you show before your first non-modal window. A few other folks have noted the same problem. It&#8217;s been submitted <em>twice</em> on Microsoft Connect as a bug:</p>
<ul>
<li><a href="https://connect.microsoft.com/VisualStudio/feedback/details/381980/wpf-splashscreen-closes-messagebox">WPF splash screen closes MessageBox</a></li>
<li><a href="https://connect.microsoft.com/VisualStudio/feedback/details/600197/wpf-splash-screen-dismisses-dialog-box">WPF splash screen dismisses dialog box</a></li>
</ul>
<p>But it&#8217;s not clear whether Microsoft&#8217;s going to address the issue. The cause of the problem is that a modal dialog without an explicit parent window set will automatically attach itself to an active non-modal window. It&#8217;s a nifty behavior in most cases, but if a modal dialog attaches itself to a splash screen, and the splash screen is dismissed &#8212; well, so is your dialog!</p>
<h4>All washed up</h4>
<p>So, what are our options? One is to avoid Visual Studio&#8217;s built-in splash screen feature entirely and create our own splash screen. <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8dd49fd0-9cc9-43c6-b285-6f119ab8a32e/">This thread in the MSDN forums describes how that can be done.</a></p>
<p>That seemed like too much work for us, which left us with three possible workarounds:</p>
<ol>
<li><a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/116bcd83-93bf-42f3-9bfe-da9e7de37546/">Use a hidden window.</a> Create a tiny, non-modal window to act as a parent for your MessageBox or modal dialog.</li>
<li><a href="http://stackoverflow.com/questions/3891719/messagebox-with-exception-details-immediately-disappears-if-use-splash-screen-in/3892129#3892129">Create non-modal message windows.</a> Change your startup mode to explicit shutdown and use a non-modal window to display your message.</li>
<li>Call MessageBox twice. Apparently, the problem only affects the <em>first</em> modal dialog shown. So you could simply call your modal dialog twice, if you didn&#8217;t mind the flash of the first one opening and closing. </li>
</ol>
<p>I don&#8217;t recommend that last technique &#8212; though it <em>does</em> work &#8212; so I&#8217;m not going to provide a link to it. </p>
<h4>Drying off</h4>
<p>I found several custom implementations of splash screens on CodePlex, but at this stage, so close to our 2.0 release, I didn&#8217;t want to add any code to our project that I didn&#8217;t thoroughly understand.</p>
<p>Since our software licensing code come from a third party, we couldn&#8217;t easily modify all of the windows shown during start-up to be non-modal.</p>
<p>So ultimately we decided to use the hidden window workaround. It seemed the quickest way to get the behavior we wanted. It feels a bit dirty, but I can&#8217;t justify rolling my own splash screen implementation just to display a static &#8220;Loading&#8230;&#8221; image. </p>
<p>I&#8217;d really like to see Microsoft fix this one. If you think so too, <a href="http://connect.microsoft.com/VisualStudio/feedback/details/600197/wpf-splash-screen-dismisses-dialog-box">vote for the issue here</a>. Thanks!</p>


<p><i>No related posts.</i></p><img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/IVIfIANeLSk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2011/03/23/using-modal-dialogs-with-a-splash-screen-in-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://underground.infovark.com/2011/03/23/using-modal-dialogs-with-a-splash-screen-in-wpf/</feedburner:origLink></item>
		<item>
		<title>Highlighting query terms in a WPF TextBlock</title>
		<link>http://feedproxy.google.com/~r/InfovarkUnderground/~3/N5G1-ap2-94/</link>
		<comments>http://underground.infovark.com/2011/03/03/highlighting-query-terms-in-a-wpf-textblock/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 16:16:53 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[highlighting]]></category>
		<category><![CDATA[Lucene.Net]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[textblock]]></category>
		<category><![CDATA[xaml]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=765</guid>
		<description><![CDATA[Highlighting query terms in search results is a common use case in web applications. How can we achieve the same effect in a WPF or Silverlight application?


Related posts:<ol><li><a href='http://underground.infovark.com/2010/12/30/how-to-format-the-xaml-hyperlink-navigateuri/' rel='bookmark' title='How to format the XAML Hyperlink NavigateUri'>How to format the XAML Hyperlink NavigateUri</a></li>
<li><a href='http://underground.infovark.com/2011/02/19/getting-xaml-hyperlink-text-to-wrap/' rel='bookmark' title='Getting XAML Hyperlink text to wrap'>Getting XAML Hyperlink text to wrap</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;d like to highlight the query terms used in a full text search within your WPF or Silverlight application. It&#8217;s a common use case in many web applications, but it&#8217;s becoming an increasingly common one in desktop applications as well.</p>
<p>The effect you want to achieve is simple. Given that a user searched for the words <em>highlighting</em> and <em>terms</em> the following snippet of text:</p>
<blockquote><p>&#8230;we found that highlighting query terms made it much easier for users&#8230;</p></blockquote>
<p>Should be displayed like this:</p>
<blockquote><p>&#8230;we found that <strong>highlighting</strong> query <strong>terms</strong> made it much easier for users&#8230;</p></blockquote>
<h4>A light touch for repeated use</h4>
<p>Your first instinct might be to use a FlowDocument displayed within a RichTextBox or FlowDocumentViewer. FlowDocuments have a convenient API for applying formatting to words or phrases, but they are heavyweight objects. Since we&#8217;ll display many search results on the same screen, the repeated use of FlowDocuments and viewer/editor controls will increase memory use and be a drag on performance. It&#8217;s not a good idea to use them within the ItemsControl or ListBox item templates that present search results.</p>
<p>Fortunately, we can get term highlighting to work within a lightweight TextBlock control, if we use a few tricks. Our approach binds the text snippet to ContentControl using a converter that generates XAML objects.</p>
<h4>The overview</h4>
<p>First, let&#8217;s assume that your full-text search engine produces a string that contains delimiters to indicate where the highlights should begin and end. <a href="http://incubator.apache.org/lucene.net/">Lucene.Net</a>, the content indexing engine we use for Infovark, has a Highlighter plugin that works this way. </p>
<p>In Lucene.Net, the default delimiters used are opening and closing <code>&lt;B&gt;&lt;/B&gt;</code> tags. We&#8217;ll need to change these default settings to use delimiters that won&#8217;t cause trouble for XML parsing. (Remember, your text snippet could contain HTML or characters invalid in an XML document, so you&#8217;ll need to escape its output. We don&#8217;t want our delimiters to get wiped away during the escape process!) </p>
<p>We decided to use the sequences <code>|~S~|</code> and <code>|~E~|</code> as these were highly unlikely to show up naturally in our snippets. Using our example from above, the highlighted snippet from Lucene.Net would look like this:</p>
<blockquote><p>&#8230;we found that |~S~|highlighting|~E~| query |~S~|terms|~E~| made it much easier for users&#8230;</p></blockquote>
<p>Now that we have our search result snippet formatted the way we want, we can build an IValueConverter that generates a TextBlock with internal Run elements. Here&#8217;s the full class. </p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">/// Converts a string containing valid XAML into WPF objects.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#91;</span>ValueConversion<span class="br0">&#40;</span><span class="kw3">typeof</span><span class="br0">&#40;</span><span class="kw4">string</span><span class="br0">&#41;</span>, <span class="kw3">typeof</span><span class="br0">&#40;</span><span class="kw4">object</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">sealed</span> <span class="kw4">class</span> StringToXamlConverter <span class="sy0">:</span> IValueConverter</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// Converts a string containing valid XAML into WPF objects.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;value&quot;&gt;The string to convert.&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;targetType&quot;&gt;This parameter is not used.&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;parameter&quot;&gt;This parameter is not used.&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;culture&quot;&gt;This parameter is not used.&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;returns&gt;A WPF object.&lt;/returns&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">object</span> Convert<span class="br0">&#40;</span><span class="kw4">object</span> value, Type targetType, <span class="kw4">object</span> parameter, CultureInfo culture<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> input <span class="sy0">=</span> value <span class="kw1">as</span> <span class="kw4">string</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>input <span class="sy0">!=</span> <span class="kw1">null</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> escapedXml <span class="sy0">=</span> SecurityElement.<span class="me1">Escape</span><span class="br0">&#40;</span>input<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> withTags <span class="sy0">=</span> escapedXml.<span class="me1">Replace</span><span class="br0">&#40;</span><span class="st0">&quot;|~S~|&quot;</span>, <span class="st0">&quot;&lt;Run Style=<span class="es0">\&quot;</span>{DynamicResource highlight}<span class="es0">\&quot;</span>&gt;&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; withTags <span class="sy0">=</span> withTags.<span class="me1">Replace</span><span class="br0">&#40;</span><span class="st0">&quot;|~E~|&quot;</span>, <span class="st0">&quot;&lt;/Run&gt;&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> wrappedInput <span class="sy0">=</span> <span class="kw4">string</span>.<span class="me1">Format</span><span class="br0">&#40;</span><span class="st0">&quot;&lt;TextBlock xmlns=<span class="es0">\&quot;</span>http://schemas.microsoft.com/winfx/2006/xaml/presentation<span class="es0">\&quot;</span> TextWrapping=<span class="es0">\&quot;</span>Wrap<span class="es0">\&quot;</span>&gt;{0}&lt;/TextBlock&gt;&quot;</span>, withTags<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">using</span> <span class="br0">&#40;</span>StringReader stringReader <span class="sy0">=</span> <span class="kw3">new</span> StringReader<span class="br0">&#40;</span>wrappedInput<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">using</span><span class="br0">&#40;</span>XmlReader xmlReader <span class="sy0">=</span> XmlReader.<span class="me1">Create</span><span class="br0">&#40;</span>stringReader<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> XamlReader.<span class="me1">Load</span><span class="br0">&#40;</span>xmlReader<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// Converts WPF framework objects into a XAML string.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;value&quot;&gt;The WPF Famework object to convert.&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;targetType&quot;&gt;This parameter is not used.&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;parameter&quot;&gt;This parameter is not used.&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;culture&quot;&gt;This parameter is not used.&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;returns&gt;A string containg XAML.&lt;/returns&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">object</span> ConvertBack<span class="br0">&#40;</span><span class="kw4">object</span> value, Type targetType, <span class="kw4">object</span> parameter, CultureInfo culture<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">throw</span> <span class="kw3">new</span> NotImplementedException<span class="br0">&#40;</span><span class="st0">&quot;This converter cannot be used in two-way binding.&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Now I&#8217;ll explain what we&#8217;re doing in the <code>Convert()</code> method in detail.</p>
<h4>Step by step</h4>
<p>First, we check to make sure that we actually have a string object with some text. Then we take that string and run it through the <code>System.Security.SecurityElement.Escape()</code> routine, a handy method in the .NET Framework for replacing invalid XML characters with properly escaped ones.</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">string</span> input <span class="sy0">=</span> value <span class="kw1">as</span> <span class="kw4">string</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span>input <span class="sy0">!=</span> <span class="kw1">null</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">string</span> escapedXml <span class="sy0">=</span> SecurityElement.<span class="me1">Escape</span><span class="br0">&#40;</span>input<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &#8230;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Next, we replace the start delimiters with opening Run tags. Note that we set a style on this tag using a dynamic resource. This lets to apply whatever style we like to the highlighted words without having to mess with our converter logic. We also replace the end delimiters with a closing Run tag.</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">string</span> withTags <span class="sy0">=</span> escapedXml.<span class="me1">Replace</span><span class="br0">&#40;</span><span class="st0">&quot;|~S~|&quot;</span>, <span class="st0">&quot;&lt;Run Style=<span class="es0">\&quot;</span>{DynamicResource highlight}<span class="es0">\&quot;</span>&gt;&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">withTags <span class="sy0">=</span> withTags.<span class="me1">Replace</span><span class="br0">&#40;</span><span class="st0">&quot;|~E~|&quot;</span>, <span class="st0">&quot;&lt;/Run&gt;&quot;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Then we wrap the snippet inside a TextBlock. I&#8217;ve set the TextWrapping property to Wrap so that our line of text doesn&#8217;t run off the edge of the screen in our UI. Note that I&#8217;ve also included the XAML namespace; this will become important later when we use the XamlReader.</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">string</span> wrappedInput <span class="sy0">=</span> <span class="kw4">string</span>.<span class="me1">Format</span><span class="br0">&#40;</span><span class="st0">&quot;&lt;TextBlock xmlns=<span class="es0">\&quot;</span>http://schemas.microsoft.com/winfx/2006/xaml/presentation<span class="es0">\&quot;</span> TextWrapping=<span class="es0">\&quot;</span>Wrap<span class="es0">\&quot;</span>&gt;{0}&lt;/TextBlock&gt;&quot;</span>, withTags<span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Now we should have a well-formed XAML string. We feed this string into a StringReader, then an XmlReader, and finally a XamlReader. The <code>XamlReader.Create()</code> method instantiates the framework objects returned by our converter. </p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">using</span> <span class="br0">&#40;</span>StringReader stringReader <span class="sy0">=</span> <span class="kw3">new</span> StringReader<span class="br0">&#40;</span>wrappedInput<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">using</span><span class="br0">&#40;</span>XmlReader xmlReader <span class="sy0">=</span> XmlReader.<span class="me1">Create</span><span class="br0">&#40;</span>stringReader<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> XamlReader.<span class="me1">Load</span><span class="br0">&#40;</span>xmlReader<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>With our converter working properly, we can use it in the DataTemplates for our search results like this:</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1"><span class="sy0">&lt;</span>ContentControl Content<span class="sy0">=</span><span class="st0">&quot;{Binding TextSnippet, Converter={StaticResource stringToXaml}, Mode=OneTime}&quot;</span><span class="sy0">/&gt;</span></div>
</li>
</ol>
</div>
<p>It&#8217;s quite a bit of work, but the end result is exactly what we want: a lightweight way of highlighting query terms in search results.</p>
<p>If you&#8217;ve got comments or suggestions about this approach, we&#8217;d love to hear them.</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2010/12/30/how-to-format-the-xaml-hyperlink-navigateuri/' rel='bookmark' title='How to format the XAML Hyperlink NavigateUri'>How to format the XAML Hyperlink NavigateUri</a></li>
<li><a href='http://underground.infovark.com/2011/02/19/getting-xaml-hyperlink-text-to-wrap/' rel='bookmark' title='Getting XAML Hyperlink text to wrap'>Getting XAML Hyperlink text to wrap</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/N5G1-ap2-94" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2011/03/03/highlighting-query-terms-in-a-wpf-textblock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://underground.infovark.com/2011/03/03/highlighting-query-terms-in-a-wpf-textblock/</feedburner:origLink></item>
		<item>
		<title>Getting XAML Hyperlink text to wrap</title>
		<link>http://feedproxy.google.com/~r/InfovarkUnderground/~3/E-odY6dI__Y/</link>
		<comments>http://underground.infovark.com/2011/02/19/getting-xaml-hyperlink-text-to-wrap/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 03:20:11 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[hyperlink]]></category>
		<category><![CDATA[text wrap]]></category>
		<category><![CDATA[xaml]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=731</guid>
		<description><![CDATA[Making the XAML Hyperlink wrap its text properly is a tricky proposition.


Related posts:<ol><li><a href='http://underground.infovark.com/2010/12/30/how-to-format-the-xaml-hyperlink-navigateuri/' rel='bookmark' title='How to format the XAML Hyperlink NavigateUri'>How to format the XAML Hyperlink NavigateUri</a></li>
<li><a href='http://underground.infovark.com/2011/03/03/highlighting-query-terms-in-a-wpf-textblock/' rel='bookmark' title='Highlighting query terms in a WPF TextBlock'>Highlighting query terms in a WPF TextBlock</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Don&#8217;t you just hate it when you realize that you not only <em>knew</em> the answer to a question that stumped you all day, but you had actually <em>written it up and posted it</em> to StackOverflow months ago? </p>
<p>Well, in an effort to get the answer to stick inside my very tiny brain, I&#8217;m writing it up here on the Infovark Underground as well. </p>
<p>The problem in question had to do with <a href="http://stackoverflow.com/q/1266668/19112">text wrapping in XAML Hyperlinks</a>. And here&#8217;s the <a href="http://stackoverflow.com/questions/1266668/text-wrapping-in-wpf-hyperlink/3732127#3732127">answer</a> I posted at the time.</p>
<p>And now here&#8217;s my more complete recap so that I hopefully never, <em>ever</em> forget it again.</p>
<h4>Hyperlinks don&#8217;t work the way you expect</h4>
<p>Getting <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.hyperlink.aspx">Hyperlink</a> text to wrap properly in WPF or Silverlight can be tricky. Depending on how you&#8217;ve structured your XAML, the technique you use for wrapping a Hyperlink can change a lot.</p>
<p>First, recall that you can&#8217;t use a Hyperlink element in a XAML file like an ordinary user control. It has to be embedded within a FlowDocument or a TextBlock. It typically looks something like this:</p>
<div class="geshi no xml">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;Hyperlink<span class="re2">&gt;</span></span></span>This is a very, very long hyperlink. Really. It&#39;s so long, you probably want it to wrap somehow.</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/Hyperlink<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>By default, the text of the hyperlink will appear on a single line.</p>
<div id="attachment_735" class="wp-caption aligncenter" style="width: 295px"><a href="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap1.png"><img src="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap1.png" alt="A XAML Hyperlink that doesn&#039;t wrap" title="A XAML Hyperlink that doesn&#039;t wrap" width="285" height="100" class="size-full wp-image-735" /></a><p class="wp-caption-text">A plain vanilla XAML Hyperlink</p></div>
<p>If the code above matches your situation, simply adding TextWrapping=&#8221;Wrap&#8221; to the TextBlock gives you the desired result.</p>
<div class="geshi no xml">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;TextBlock</span> <span class="re0">TextWrapping</span>=<span class="st0">&quot;Wrap&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;Hyperlink<span class="re2">&gt;</span></span></span>This is a very, very long hyperlink. Really. It&#39;s so long, you probably want it to wrap somehow.</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/Hyperlink<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<div id="attachment_743" class="wp-caption aligncenter" style="width: 295px"><a href="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap21.png"><img src="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap21.png" alt="This XAML Hyperlink wraps properly" title="Linkwrap2" width="285" height="100" class="size-full wp-image-743" /></a><p class="wp-caption-text">This simple scenario wraps properly</p></div>
<p>But suppose you had this scenario:</p>
<div class="geshi no xml">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;TextBlock</span> <span class="re0">TextWrapping</span>=<span class="st0">&quot;Wrap&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;Hyperlink<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;TextBlock<span class="re2">&gt;</span></span></span>This is a very, very long hyperlink. Really. It&#39;s so long, you probably want it to wrap somehow.</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/Hyperlink<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>You might be surprised to find that the the TextWrapping=&#8221;Wrap&#8221; attribute has no effect at all on the inner TextBlock. The text stretches out in a single line.</p>
<div id="attachment_737" class="wp-caption aligncenter" style="width: 295px"><a href="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap3.png"><img src="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap3.png" alt="Adding an inner TextBlock to the XAML Hyperlink prevents the text from wrapping" title="Linkwrap3" width="285" height="100" class="size-full wp-image-737" /></a><p class="wp-caption-text">Adding an inner TextBlock prevents the text from wrapping</p></div>
<p>No worries, you think, I&#8217;ll just move the TextWrapping=&#8221;Wrap&#8221; bit to the inner TextBlock, like so.</p>
<div class="geshi no xml">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;Hyperlink<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;TextBlock</span> <span class="re0">TextWrapping</span>=<span class="st0">&quot;Wrap&quot;</span><span class="re2">&gt;</span></span>This is a very, very long hyperlink. Really. It&#39;s so long, you probably want it to wrap somehow. But not like this!</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/Hyperlink<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>This will wrap your text, all right. But it won&#8217;t wrap the underline for the hyperlink. Check it out:</p>
<div id="attachment_738" class="wp-caption aligncenter" style="width: 295px"><a href="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap4.png"><img src="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap4.png" alt="The XAML hyperlink text wraps, but the underline does not." title="Linkwrap4" width="285" height="100" class="size-full wp-image-738" /></a><p class="wp-caption-text">The text of the XAML hyperlink wraps, but the underline does not</p></div>
<p>What&#8217;s going on here? Why does the first, simple scenario work, and everything else fail in odd ways?</p>
<p>The answer is that the text contents of the Hyperlink element get placed within a <a href="http://www.fortes.com/2007/hidden-run">hidden Run</a> element in the first case. This creation of an implied <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.run.aspx">Run</a> element happens frequently in the framework. It happens so often that most of the time you forget it&#8217;s doing it.</p>
<p>Sure enough, if you replace the inner TextBlock with a Run in our second scenario, you get a lovely text-wrapped link.</p>
<p>Here&#8217;s the markup:</p>
<div class="geshi no xml">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;TextBlock</span> <span class="re0">TextWrapping</span>=<span class="st0">&quot;Wrap&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;Hyperlink<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;Run<span class="re2">&gt;</span></span></span>This is a very, very long hyperlink. Really. It&#39;s so long, you probably want it to wrap somehow.</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/Run<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/Hyperlink<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>And here&#8217;s our desired result:</p>
<div id="attachment_744" class="wp-caption aligncenter" style="width: 295px"><a href="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap52.png"><img src="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap52.png" alt="This XAML Hyperlink has its text wrapped and underlined" title="Linkwrap5" width="285" height="100" class="size-full wp-image-744" /></a><p class="wp-caption-text">Now everything works as expected</p></div>
<h4>But it&#8217;s not so simple</h4>
<p>Now I know what you&#8217;re thinking. You&#8217;re wondering what to do if you actually <em>need</em> that inner element to be TextBlock. What if you&#8217;re working with an older version of WPF or Silverlight, where the Run.Text property couldn&#8217;t be data-bound? Or what if you wanted to apply formatting to the text of the link, like bold or italics, neither of which are supported by Run?</p>
<p>Then it gets <em>ugly</em>. You&#8217;ll need something like the following code, which removes the underline from the Hyperlink element and reapplies it at the inner TextBlock.</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sy0">&lt;</span>TextBlock<span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>Hyperlink TextDecorations<span class="sy0">=</span><span class="st0">&quot;None&quot;</span><span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>TextBlock TextWrapping<span class="sy0">=</span><span class="st0">&quot;Wrap&quot;</span> TextDecorations<span class="sy0">=</span><span class="st0">&quot;Underline&quot;</span><span class="sy0">&gt;</span><span class="kw1">This</span> <span class="kw3">is</span> a very, very <span class="kw4">long</span> <span class="sy0">&lt;</span>Run FontWeight<span class="sy0">=</span><span class="st0">&quot;Bold&quot;</span><span class="sy0">&gt;</span>hyperlink<span class="sy0">&lt;/</span>Run<span class="sy0">&gt;</span>. <span class="me1">Really</span>. <span class="me1">It</span><span class="st0">&#39;s so long, you probably want it to wrap somehow.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;/TextBlock&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp; &nbsp; &nbsp; &nbsp;&lt;/Hyperlink&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp; &nbsp;&lt;/TextBlock&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"</span></div>
</li>
</ol>
</div>
<p>This solution isn&#8217;t perfect; sometimes you&#8217;ll see the underline jiggle a bit as you resize your window. But it&#8217;s as close as we can get it for now.</p>
<div id="attachment_741" class="wp-caption aligncenter" style="width: 295px"><a href="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap6.png"><img src="http://underground.infovark.com/wp-content/uploads/2011/02/Linkwrap6.png" alt="A wrapping XAML Hyperlink with an inner TextBlock" title="Linkwrap6" width="285" height="100" class="size-full wp-image-741" /></a><p class="wp-caption-text">A XAML Hyperlink with wrapped and formatted text</p></div>
<p>Who would have thought something as simple as a hyperlink with wrapped text could be so difficult?</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2010/12/30/how-to-format-the-xaml-hyperlink-navigateuri/' rel='bookmark' title='How to format the XAML Hyperlink NavigateUri'>How to format the XAML Hyperlink NavigateUri</a></li>
<li><a href='http://underground.infovark.com/2011/03/03/highlighting-query-terms-in-a-wpf-textblock/' rel='bookmark' title='Highlighting query terms in a WPF TextBlock'>Highlighting query terms in a WPF TextBlock</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/E-odY6dI__Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2011/02/19/getting-xaml-hyperlink-text-to-wrap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://underground.infovark.com/2011/02/19/getting-xaml-hyperlink-text-to-wrap/</feedburner:origLink></item>
		<item>
		<title>How to format the XAML Hyperlink NavigateUri</title>
		<link>http://feedproxy.google.com/~r/InfovarkUnderground/~3/eLFXYtkzCgc/</link>
		<comments>http://underground.infovark.com/2010/12/30/how-to-format-the-xaml-hyperlink-navigateuri/#comments</comments>
		<pubDate>Thu, 30 Dec 2010 17:58:09 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[hyperlink]]></category>
		<category><![CDATA[xaml]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=713</guid>
		<description><![CDATA[Have you ever wanted to construct a dynamic query string for your Hyperlinks in WPF or Silverlight? Me too. Here's how.


Related posts:<ol><li><a href='http://underground.infovark.com/2011/02/19/getting-xaml-hyperlink-text-to-wrap/' rel='bookmark' title='Getting XAML Hyperlink text to wrap'>Getting XAML Hyperlink text to wrap</a></li>
<li><a href='http://underground.infovark.com/2011/03/03/highlighting-query-terms-in-a-wpf-textblock/' rel='bookmark' title='Highlighting query terms in a WPF TextBlock'>Highlighting query terms in a WPF TextBlock</a></li>
<li><a href='http://underground.infovark.com/2009/02/18/using-wcf-to-return-html/' rel='bookmark' title='Using WCF to return HTML'>Using WCF to return HTML</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Have you ever wanted to construct a dynamic query string for your Hyperlinks in WPF or Silverlight? Perhaps it&#8217;s the web developer in me, but I find myself wanting to do this <em>all the time</em>.</p>
<p>Sadly, the Hyperlink element&#8217;s NavigateUri property makes this extremely difficult. Since the NavigateUri property&#8217;s target type is a Uri, not a string, you can&#8217;t use the convenient StringFormat syntax to create dynamic URLs. I started a StackOverflow question to discuss <a href="http://stackoverflow.com/q/3579127/19112">how to use StringFormat with the NavigateUri property</a>. The best approach for now appears to be to write an <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx">IValueConverter</a> that will create the Uri from a string.</p>
<p>This works if you only need to apply a single value. But what if you need to supply multiple values to generate a querystring with several parameters? </p>
<p>A post from Paul Stovell pointed me in the right direction: prior to the introduction of special StringFormat syntax in .NET 3.5 SP1, you needed to <a href="http://www.paulstovell.com/wpf-string-format-multibinding">use multibinding to manipulate strings</a>. We can use this older method to generate the Uri we need for the Hyperlink. </p>
<p>First, create a converter that implements <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx">IMultiValueConverter</a>.</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#91;</span>ValueConversion<span class="br0">&#40;</span><span class="kw3">typeof</span><span class="br0">&#40;</span><span class="kw4">String</span><span class="br0">&#41;</span>, <span class="kw3">typeof</span><span class="br0">&#40;</span>Uri<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">class</span> StringToUriConverter <span class="sy0">:</span> IMultiValueConverter</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">object</span> Convert<span class="br0">&#40;</span><span class="kw4">object</span><span class="br0">&#91;</span><span class="br0">&#93;</span> values, Type targetType, <span class="kw4">object</span> parameter, CultureInfo culture<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri result;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> input <span class="sy0">=</span> <span class="kw4">string</span>.<span class="me1">Format</span><span class="br0">&#40;</span>parameter.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, values<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri.<span class="me1">TryCreate</span><span class="br0">&#40;</span>input, UriKind.<span class="me1">RelativeOrAbsolute</span>, <span class="kw1">out</span> result<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> result;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">object</span><span class="br0">&#91;</span><span class="br0">&#93;</span> ConvertBack<span class="br0">&#40;</span><span class="kw4">object</span> value, Type<span class="br0">&#91;</span><span class="br0">&#93;</span> targetTypes, <span class="kw4">object</span> parameter, CultureInfo culture<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">throw</span> <span class="kw3">new</span> NotImplementedException<span class="br0">&#40;</span><span class="st0">&quot;This converter cannot be used in two-way binding.&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Remember to add the converter application resources in your App.xaml file, so you can reference it throughout your application. </p>
<div class="geshi no xml">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;Application</span>.Resources<span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sc3"><span class="re1">&lt;ResourceDictionary<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;Manager:StringToUriConverter</span> <span class="re0">x:Key</span>=<span class="st0">&quot;stringToUri&quot;</span><span class="re2">/&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sc3"><span class="re1">&lt;/ResourceDictionary<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/Application</span>.Resources<span class="re2">&gt;</span></span></div>
</li>
</ol>
</div>
<p>Then you can use it wherever you need to dynamically create a URI within your WPF or Silverlight application.</p>
<div class="geshi no xml">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sc3"><span class="re1">&lt;Hyperlink<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;Hyperlink</span>.NavigateUri<span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;MultiBinding</span> <span class="re0">ConverterParameter</span>=<span class="st0">&quot;/Projects/ItemIndexPage.xaml?ProjectId={0}&amp;amp;TemplateType={1}&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">Converter</span>=<span class="st0">&quot;{StaticResource stringToUri}&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;Binding</span> <span class="re0">ElementName</span>=<span class="st0">&quot;ProjectId&quot;</span> <span class="re0">Path</span>=<span class="st0">&quot;Text&quot;</span><span class="re2">/&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;Binding</span> <span class="re0">Path</span>=<span class="st0">&quot;Template&quot;</span><span class="re2">/&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;/MultiBinding<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;/Hyperlink</span>.NavigateUri<span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;Run</span> <span class="re0">Text</span>=<span class="st0">&quot;{Binding LinkText}&quot;</span><span class="re2">/&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sc3"><span class="re1">&lt;/Hyperlink<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>Whew. That&#8217;s a lot of extra work just to generate a URL for a hyperlink! </p>
<p>Notice that I had to escape the ampersand in the format string supplied in the ConverterParameter. Since XAML is based on XML, not HTML, you must escape the ampersand within attribute values. This catches me every time.</p>
<p>Generating dynamic URLs is such a basic operation for building navigation-style WPF or Silverlight applications, I&#8217;m surprised the folks at Microsoft didn&#8217;t create a helper syntax to handle this cleanly.</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2011/02/19/getting-xaml-hyperlink-text-to-wrap/' rel='bookmark' title='Getting XAML Hyperlink text to wrap'>Getting XAML Hyperlink text to wrap</a></li>
<li><a href='http://underground.infovark.com/2011/03/03/highlighting-query-terms-in-a-wpf-textblock/' rel='bookmark' title='Highlighting query terms in a WPF TextBlock'>Highlighting query terms in a WPF TextBlock</a></li>
<li><a href='http://underground.infovark.com/2009/02/18/using-wcf-to-return-html/' rel='bookmark' title='Using WCF to return HTML'>Using WCF to return HTML</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/eLFXYtkzCgc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2010/12/30/how-to-format-the-xaml-hyperlink-navigateuri/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://underground.infovark.com/2010/12/30/how-to-format-the-xaml-hyperlink-navigateuri/</feedburner:origLink></item>
		<item>
		<title>An analysis of our database schema</title>
		<link>http://feedproxy.google.com/~r/InfovarkUnderground/~3/lcvrFb-hzKk/</link>
		<comments>http://underground.infovark.com/2010/12/19/an-analysis-of-our-database-schema/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 19:03:07 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database schema]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=702</guid>
		<description><![CDATA[We gave a portion of the Infovark database schema to The Database Programmer to analyze. What suggestions does he have for us?


Related posts:<ol><li><a href='http://underground.infovark.com/2009/02/03/microformats-introduction/' rel='bookmark' title='Microformats Introduction'>Microformats Introduction</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Ken Downs, author of <a href="http://database-programmer.blogspot.com/">The Database Programmer</a> blog, recently asked for <a href="http://database-programmer.blogspot.com/p/submit-your-analysis-request.html">suggestions for database schema to analyze</a>. </p>
<p>I jumped at the chance to have him look over our database design choices. You can find his analysis of our <a href="http://database-programmer.blogspot.com/2010/12/user-submitted-analysis-topic-email.html">Contact email address tables</a>. I&#8217;ll respond to his questions, and any questions raised by the community, in the comments to his posts. </p>
<p>Have a look, and join in the discussion!</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2009/02/03/microformats-introduction/' rel='bookmark' title='Microformats Introduction'>Microformats Introduction</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/lcvrFb-hzKk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2010/12/19/an-analysis-of-our-database-schema/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://underground.infovark.com/2010/12/19/an-analysis-of-our-database-schema/</feedburner:origLink></item>
		<item>
		<title>Reflecting on reflection in .NET</title>
		<link>http://feedproxy.google.com/~r/InfovarkUnderground/~3/l60ni84xAkY/</link>
		<comments>http://underground.infovark.com/2010/07/06/reflecting-on-reflection-in-net/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 13:40:28 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[service locator]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=678</guid>
		<description><![CDATA[System.Reflection is a big hammer in the .NET programmer's toolbox. And I have the sore thumbs to prove it!


Related posts:<ol><li><a href='http://underground.infovark.com/2010/04/20/avoiding-the-new/' rel='bookmark' title='Avoiding the &#8220;new&#8221;'>Avoiding the &#8220;new&#8221;</a></li>
<li><a href='http://underground.infovark.com/2010/06/18/the-service-locator-pattern-is-the-new-global-variable/' rel='bookmark' title='The Service Locator pattern is the new global variable'>The Service Locator pattern is the new global variable</a></li>
<li><a href='http://underground.infovark.com/2008/08/13/creating-dummy-targets-for-configuration-objects/' rel='bookmark' title='Creating Dummy Targets for Configuration Objects'>Creating Dummy Targets for Configuration Objects</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I cringe whenever I spot code that uses System.Reflection. It&#8217;s a powerful weapon in our arsenal of .NET programming techniques &#8212; and a dangerous one.</p>
<p>In two previous posts, I talked about <a href="http://underground.infovark.com/2010/04/20/avoiding-the-new/">the misuse of dependency injection</a> and the <a href="http://underground.infovark.com/2010/06/18/the-service-locator-pattern-is-the-new-global-variable/">problems with the service locator pattern</a>. I should include <a href="http://en.wikipedia.org/wiki/Factory_pattern">factories</a> among the list of dangerous patterns too.</p>
<p>These patterns get overused in many .NET frameworks and toolkits. Some of that stems from a desire to avoid complex configuration. Some of it comes from the natural envy programmers in stuffy statically typed languages feel regarding their freewheeling counterparts using dynamically typed languages. </p>
<p>(Wikipedia has a good article exploring the differences in <a href="http://en.wikipedia.org/wiki/Type_system#Type_checking">type checking in computer language type systems</a>, if you&#8217;re curious about the different paradigms. But I digress.)</p>
<h4>On reflection</h4>
<p>System.Reflection two main uses, as Marc Gravell points out in his <a href="http://stackoverflow.com/questions/1036928/what-problems-does-reflection-solve/1036990#1036990">answer regarding what problems Reflection solves on StackOverflow</a>:</p>
<ol>
<li>Investigating type information</li>
<li>Metaprogramming</li>
</ol>
<p>These two capabilities allow you to shoehorn dynamic or late-binding features into C# applications. In other words, it&#8217;s a <em>clever hack</em>.</p>
<p>Want to enable Ruby-style <a href="http://en.wikipedia.org/wiki/Convention_over_configuration">convention over configuration</a> in your C# framework? Use reflection! Want to reach into <code>private</code> or <code>internal</code> methods for unit testing? Use reflection! Want to add some duck typing goodness to your statically typed language? Use reflection!</p>
<h4>But on further reflection</h4>
<p>The System.Reflection API has some <strong>big</strong> drawbacks. The <a href="http://stackoverflow.com/questions/25458/how-costly-is-reflection-really">performance penalty incurred by reflection</a> is well known. Less well known is that the Reflection API is really difficult to use, especially when inspecting and creating constructor method parameters. So most framework developers skip the hard bits.</p>
<p>This means that most of these frameworks will <em>insist</em> on your using constructors without parameters in your classes. (Sometimes people call parameter-less constructors &#8220;default constructors&#8221; though it isn&#8217;t the same thing.) </p>
<p>But if you can&#8217;t use parameters in the constructors of your objects, <em>you&#8217;re doing object-oriented programming with one hand tied behind your back</em>.</p>
<p>For example, it&#8217;s difficult to achieve object immutability without using a constructor. You&#8217;ll also have a hard time enforcing business rules without constructors. And if you&#8217;re a fan of dependency injection, you&#8217;ll have to implement it via <code>Init()</code> methods or property setters, both of which can cause significant headaches.</p>
<p>So if you work with a framework or API that makes heavy use of reflection, and thus restricts the sort of constructors you use, prepare yourself for significant pain. </p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2010/04/20/avoiding-the-new/' rel='bookmark' title='Avoiding the &#8220;new&#8221;'>Avoiding the &#8220;new&#8221;</a></li>
<li><a href='http://underground.infovark.com/2010/06/18/the-service-locator-pattern-is-the-new-global-variable/' rel='bookmark' title='The Service Locator pattern is the new global variable'>The Service Locator pattern is the new global variable</a></li>
<li><a href='http://underground.infovark.com/2008/08/13/creating-dummy-targets-for-configuration-objects/' rel='bookmark' title='Creating Dummy Targets for Configuration Objects'>Creating Dummy Targets for Configuration Objects</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/l60ni84xAkY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2010/07/06/reflecting-on-reflection-in-net/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://underground.infovark.com/2010/07/06/reflecting-on-reflection-in-net/</feedburner:origLink></item>
		<item>
		<title>The Service Locator pattern is the new global variable</title>
		<link>http://feedproxy.google.com/~r/InfovarkUnderground/~3/PeZyVa5Faz0/</link>
		<comments>http://underground.infovark.com/2010/06/18/the-service-locator-pattern-is-the-new-global-variable/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 15:30:42 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[inversion of control]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[service locator]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=672</guid>
		<description><![CDATA[Is the Service Locator pattern just a sneaky way of creating global variables?


Related posts:<ol><li><a href='http://underground.infovark.com/2010/07/06/reflecting-on-reflection-in-net/' rel='bookmark' title='Reflecting on reflection in .NET'>Reflecting on reflection in .NET</a></li>
<li><a href='http://underground.infovark.com/2010/04/20/avoiding-the-new/' rel='bookmark' title='Avoiding the &#8220;new&#8221;'>Avoiding the &#8220;new&#8221;</a></li>
<li><a href='http://underground.infovark.com/2008/11/19/the-curse-of-the-singleton/' rel='bookmark' title='The Curse of the Singleton'>The Curse of the Singleton</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Mark Seeman, author of an upcoming book about dependency injection, writes that <a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx">Service Locator is an anti-pattern</a>. He shows several code examples illustrating the difficulty of configuring and troubleshooting various implementations of the Service Locator pattern. His observations tally with my thoughts about the <a href="http://underground.infovark.com/2010/04/20/avoiding-the-new/">awkwardness of service locator implementations</a>.</p>
<p>As I was wrestling with yet another third-party software component that made heavy use of an inversion-of-control (IoC) container, I realized that service locators are the new global variable. </p>
<p>Global variables are a <a href="http://en.wikipedia.org/wiki/Code_smell">code smell</a> and should be used sparingly. </p>
<p>This means that you should use a service locator only as a <em>last resort</em>, not as the default way of wiring bits of your application together. </p>
<p>And if you&#8217;re using an IoC container, make sure that you are using it to inject dependencies through your object constructors, rather than calling its static   <code>Resolve<T>()</code> methods from within your classes.</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2010/07/06/reflecting-on-reflection-in-net/' rel='bookmark' title='Reflecting on reflection in .NET'>Reflecting on reflection in .NET</a></li>
<li><a href='http://underground.infovark.com/2010/04/20/avoiding-the-new/' rel='bookmark' title='Avoiding the &#8220;new&#8221;'>Avoiding the &#8220;new&#8221;</a></li>
<li><a href='http://underground.infovark.com/2008/11/19/the-curse-of-the-singleton/' rel='bookmark' title='The Curse of the Singleton'>The Curse of the Singleton</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/PeZyVa5Faz0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2010/06/18/the-service-locator-pattern-is-the-new-global-variable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://underground.infovark.com/2010/06/18/the-service-locator-pattern-is-the-new-global-variable/</feedburner:origLink></item>
		<item>
		<title>Review: Domain-Driven Design</title>
		<link>http://feedproxy.google.com/~r/InfovarkUnderground/~3/tAkRuyyKP64/</link>
		<comments>http://underground.infovark.com/2010/05/25/review-domain-driven-design/#comments</comments>
		<pubDate>Tue, 25 May 2010 18:19:31 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ddd]]></category>
		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=522</guid>
		<description><![CDATA[Our review of Domain-Driven Design, an essential book for learning about modern software architecture and OO design techniques for large-scale applications.


Related posts:<ol><li><a href='http://underground.infovark.com/2009/05/14/validation-in-domain-driven-design/' rel='bookmark' title='Validation in Domain Driven Design'>Validation in Domain Driven Design</a></li>
<li><a href='http://underground.infovark.com/2009/07/23/domain-models-in-high-performance-systems/' rel='bookmark' title='Domain Models in High Performance Systems'>Domain Models in High Performance Systems</a></li>
<li><a href='http://underground.infovark.com/2009/05/11/review-framework-design-guidelines/' rel='bookmark' title='Review: Framework Design Guidelines'>Review: Framework Design Guidelines</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 150px"><a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215%3FSubscriptionId%3DAKIAIU3RPTD7NQ47YK4A%26tag%3Dinfovark-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0321125215"><img src="http://ecx.images-amazon.com/images/I/31ywgz51v-L._SL160_.jpg" alt="Domain-Driven Design by Eric Evans" /></a><p class="wp-caption-text">Domain-Driven Design by Eric Evans</p></div>
<p>I read Eric Evans&#8217; <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215%3FSubscriptionId%3DAKIAIU3RPTD7NQ47YK4A%26tag%3Dinfovark-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0321125215">Domain-Driven Design: Tackling Complexity in the Heart of Software</a> nearly six months ago, but I wanted to take some time to mull over my response to it before posting a review. </p>
<p>Reading Domain-Driven Design can be a challenge. It&#8217;s taken me a while to sort through all the concepts in my head. It&#8217;s also taken me some time to chase down the references and articles related to the book. Eric Evans makes frequent references to design patterns and often cites other books in the software design canon. If you&#8217;re not familiar with these, you&#8217;ll find the book slow going. It&#8217;s definitely worth the effort, however, especially if you work with large codebases.</p>
<p>The key is <em>large</em> codebases. For simple software applications, a straightforward procedural approach to code works just fine. At larger scales, Object Oriented approaches to code work quite well. But neither procedural code nor OO principles give much guidance about large-scale software structures. </p>
<p>After your application grows beyond a dozen classes or so, you need to start thinking about the overall organization of your code. And once you start work on an application with different logical layers or physical tiers, or one that integrates with other software applications, you ought to give Domain-Driven Design a thorough read.</p>
<h4>DDD defined</h4>
<p>Software applications are tools that help people solve problems. The scope of the problem is called the problem domain, or simply the <em>domain</em>. The process of setting up your software to tackle the problem is called domain modeling. The <a href="http://en.wikipedia.org/wiki/Domain_model">domain model</a> consists of the core concepts that help users of the software get the job done or the problem solved.</p>
<p>Domain-Driven Design recognizes that software applications have essential features needed to solve problems in the domain as well as supporting infrastructure. It places the core domain at the center of the design process and lets everything flow outward from that. It&#8217;s an approach sometimes called <a href="http://jeffreypalermo.com/blog/the-onion-architecture-part-1/">onion architecture</a>, in contrast to the traditional &#8220;layer-cake&#8221; architecture diagrams you often see. </p>
<p>We write software to solve problems, so it makes sense that our software should model those problems as accurately as possible. It sounds so simple!</p>
<p>Sadly, in practice, it&#8217;s really hard to get right. Computers force us programmers to think about all sorts of things that have nothing at all to do with the problem we&#8217;re trying to solve:</p>
<ul>
<li>Resource management</li>
<li>Data access and persistence</li>
<li>Responding to user input</li>
<li>Maintaining state</li>
<li>Etc. etc. etc.</li>
</ul>
<p>These things can easily overshadow the problem we&#8217;re trying to solve. For many applications, the amount of infrastructure code you&#8217;ll write will far exceed the amount of domain logic required to solve the problem. And if you&#8217;re not careful, this infrastructure code can spread throughout your application, making it very hard to identify the core concerns of your system.</p>
<p>Eric Evans&#8217; book describes how to see the forest for the trees in large-scale applications.</p>
<h4>Three key DDD concepts</h4>
<p>One of the things Eric Evans said <a href="http://domaindrivendesign.org/library/evans_2009_1">after he wrote the book</a> was that he wished he&#8217;d put it in a different order. I agree! Here are the three most important concepts discussed in the book, from back to front.</p>
<p><strong>Bounded Contexts</strong> are a way to separate concerns within a software application. While the core of your application might deal with video editing or insurance claims processing, you will have other parts of your application that deal with corollary concerns. </p>
<p>If your system interacts with another software application or uses a web service, you obviously have a different context. These third-party components likely have different ways to model the domain, and you&#8217;ll need to translate these to your model.</p>
<p>But sometimes it&#8217;s not clear when you&#8217;ve moved from one problem domain to another. For example, within an employee timekeeping application you might have two subsystems that consider timesheets from a cost accounting perspective and a billing perspective. Though they might look at the same underlying data, the logic of how they work is different.</p>
<p>It&#8217;s important to explicitly recognize these boundaries. These edges will help determine the structure of your application.</p>
<p><strong>Domain Modeling</strong> helps you determine the essential features of your system. Your code should be the literal embodiment of these features. If it&#8217;s a paint application, we&#8217;d expect to see concepts like Canvas and Brush and Color. If it&#8217;s a airline scheduling application, we&#8217;d expect concepts like Seat and Booking and Destination.</p>
<p>Software is an abstract thing. We humans have a difficult time thinking about abstract concepts. The more concrete you can make your core domain concerns, the easier it is for you to reason about. </p>
<p><strong>Ubiquitous Language</strong> is the idea that your software development team should be using, as much as possible, the language of your customer. They need to understand the domain as completely as the client does &#8212; otherwise they&#8217;ll have a hard time designing software to solve the problem! </p>
<p>This is essentially another argument in favor of using real, concrete concepts from the problem domain in your application model. When the junior developer says, &#8220;I get exceptions when I assign this SpecialNeedsPassenger to an ExitRowSeat&#8221;, the airline executive will have some inkling what is going on. And when the customer says to the lead developer, &#8220;Oh no, we always show accumulated vacation within the current pay period&#8221; the developer will have some idea of what needs to change in the code. </p>
<h4>Specific techniques</h4>
<p>Much of the book is an exploration of how these three principles can be used to design (or refactor) a software application so that it makes more sense. Eric Evans discusses several diagramming techniques that can be used to illustrate relationships and boundaries in the domain model. He also highlights several <a href="http://en.wikipedia.org/wiki/Design_pattern">design patterns</a> that are helpful as well.</p>
<p>You can get good advice on applying specific techniques from reading blogs and looking at programming Q&#038;A sites like StackOverflow. But it&#8217;s worthwhile to explore Eric Evans&#8217; take on these and to get a solid grip on the terminology used by the DDD community. </p>
<p>If you&#8217;re a C# developer in particular, it&#8217;s easy to get confused by DDD terms like &#8220;Value Object&#8221; and &#8220;Entity&#8221; which are also used within the Microsoft .NET Framework. They have special meaning to folks in the DDD crowd. </p>
<h4>Conclusion</h4>
<p>This is the single most important book I&#8217;ve read on large-scale software construction. But it&#8217;s a challenging read because you need to know quite a bit about design patterns and have had some experience working on large scale projects. </p>
<p>The book also suffers a bit from the way it&#8217;s organized. As Eric Evans mentions in the introduction, the most important parts of the book are Parts I and IV. It&#8217;s easy to get bogged down in all the detail in the middle.</p>
<p>But those issues shouldn&#8217;t stop you from reading the book or becoming familiar with the concepts of Domain-Driven Design. It&#8217;s quickly become an essential part of the software design canon. We keep it on our <a href="http://astore.amazon.com/infovark-20?node=1">Infovark bookshelf</a> among the other key technical books..</p>
<p>Software architects and software team leads will get the most benefit from the book, but if you&#8217;re a developer on a large enterprise software project or commercial product, you&#8217;ll find the Domain-Driven approach to software construction very useful in your work.</p>
<p>If you want to learn more, check out the <a href="http://en.wikipedia.org/wiki/Domain-driven_design">Domain-Driven Design article on Wikipedia</a> and the <a href="http://domaindrivendesign.org/">Domain-Driven Design Community</a>. You can also listen to this <a href="http://www.infoq.com/interviews/domain-driven-design-eric-evans">short interview about Domain-Driven Design with Eric Evans on InfoQ</a>.</p>
<p>But the best way to get started is to read the book itself.</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2009/05/14/validation-in-domain-driven-design/' rel='bookmark' title='Validation in Domain Driven Design'>Validation in Domain Driven Design</a></li>
<li><a href='http://underground.infovark.com/2009/07/23/domain-models-in-high-performance-systems/' rel='bookmark' title='Domain Models in High Performance Systems'>Domain Models in High Performance Systems</a></li>
<li><a href='http://underground.infovark.com/2009/05/11/review-framework-design-guidelines/' rel='bookmark' title='Review: Framework Design Guidelines'>Review: Framework Design Guidelines</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/tAkRuyyKP64" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2010/05/25/review-domain-driven-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://underground.infovark.com/2010/05/25/review-domain-driven-design/</feedburner:origLink></item>
		<item>
		<title>Avoiding the “new”</title>
		<link>http://feedproxy.google.com/~r/InfovarkUnderground/~3/wFcmi6aEAaw/</link>
		<comments>http://underground.infovark.com/2010/04/20/avoiding-the-new/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 15:34:02 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[inversion of control]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=638</guid>
		<description><![CDATA[Dependency Injection is an important principle of software design, but it can be overused. My biggest concern with overusing inversion-of-control (IoC) containers is that it makes learning a framework ten times harder than it needs to be. 


Related posts:<ol><li><a href='http://underground.infovark.com/2010/07/06/reflecting-on-reflection-in-net/' rel='bookmark' title='Reflecting on reflection in .NET'>Reflecting on reflection in .NET</a></li>
<li><a href='http://underground.infovark.com/2010/06/18/the-service-locator-pattern-is-the-new-global-variable/' rel='bookmark' title='The Service Locator pattern is the new global variable'>The Service Locator pattern is the new global variable</a></li>
<li><a href='http://underground.infovark.com/2008/10/05/one-way-serialization/' rel='bookmark' title='One-Way Serialization'>One-Way Serialization</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In January, Robert Martin posted an article about <a href="http://blog.objectmentor.com/articles/2010/01/17/dependency-injection-inversion">the overuse of dependency injection</a> in many open source frameworks. At the time, I had only an academic interest in the subject. (And Uncle Bob&#8217;s rants are always a fun read.)</p>
<p>But that was <em>before</em> I started taking a look at several open source frameworks for WPF. Now I&#8217;m feeling ranty too!</p>
<p>Dependency Injection is an important principle of software design, but it can be abused. My biggest concern with overusing inversion-of-control (IoC) containers is that it makes learning a framework ten times harder than it needs to be. </p>
<p>First, you can&#8217;t tell precisely when an object gets created. Sure, it&#8217;s nice that your framework provides control over the lifetime of objects, but this should be an optional feature for advanced scenarios. Not every solution requires lazy loading. Sometimes a simple &#8220;new&#8221; will do.</p>
<p>Second, by placing all the wiring and object discovery in the container or service locator, you hide important information about how the framework components fit together. Yes, the global object factory knows what classes go with what types, but you don&#8217;t. That information is hidden among all the framework plumbing.</p>
<p>For similar reasons, Microsoft cautions against using factory methods in place of constructors in its <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Libraries/dp/0321545613%3FSubscriptionId%3DAKIAIU3RPTD7NQ47YK4A%26tag%3Dinfovark-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0321545613">Framework Design Guidelines</a>:</p>
<blockquote><p>Prefer constructors to factories, because they are generally more usable, consistent, and convenient than specialized construction mechanisms.</p></blockquote>
<p>I think that goes <strong>double</strong> for dependency injection and IoC containers. Eliminating constructors makes for less readable code. I can&#8217;t tell what classes other classes depend on to do their jobs. </p>
<h4>My errors should get thrown from my code</h4>
<p>Sure, IoC containers are great when everything gets wired together automagically. But when an error occurs, I want it to be thrown from my code, as close to the source of the error as possible. (Like Jeff Atwood, I assume it&#8217;s <a href="http://www.codinghorror.com/blog/archives/001079.html">always my fault</a>.) </p>
<p>Instead, frameworks that overuse dependency injection throw most of these setup, configuration, and instantiation from its internal IoC container, usually with some horribly generic error message. </p>
<p>Then it&#8217;s up to me to find which dependency failed to resolve, which constructor failed to bind, or which concrete implementation didn&#8217;t get registered. And in order to do that, I have to go spelunking in the framework code &#8212; which I don&#8217;t know well &#8212; rather than the code I was trying to write, which I know quite well.</p>
<p>Frameworks that rely on dependency injection and IoC containers practically <em>require</em> you to have the framework source available and for you to become an expert in its mysterious inner workings. If I need to step through the framework code or use reflector on it, the framework has failed me. </p>
<p>This is exactly what Uncle Bob was decrying in his article. I don&#8217;t <em>want</em> to become an expert in the XYZ framework for ABC. I just want to get my stuff done. </p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2010/07/06/reflecting-on-reflection-in-net/' rel='bookmark' title='Reflecting on reflection in .NET'>Reflecting on reflection in .NET</a></li>
<li><a href='http://underground.infovark.com/2010/06/18/the-service-locator-pattern-is-the-new-global-variable/' rel='bookmark' title='The Service Locator pattern is the new global variable'>The Service Locator pattern is the new global variable</a></li>
<li><a href='http://underground.infovark.com/2008/10/05/one-way-serialization/' rel='bookmark' title='One-Way Serialization'>One-Way Serialization</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/wFcmi6aEAaw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2010/04/20/avoiding-the-new/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://underground.infovark.com/2010/04/20/avoiding-the-new/</feedburner:origLink></item>
	</channel>
</rss>

