<?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>Akash Kava &#187; Delegation</title>
	<atom:link href="http://akashkava.com/blog/tag/delegation/feed/" rel="self" type="application/rss+xml" />
	<link>http://akashkava.com/blog</link>
	<description>Founder of NeuroSpeech</description>
	<lastBuildDate>Mon, 19 Sep 2011 16:21:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>UI Delegation Pattern for Data Controls in WPF/Silverlight</title>
		<link>http://akashkava.com/blog/258/ui-delegation-pattern-for-data-controls-in-wpfsilverlight/</link>
		<comments>http://akashkava.com/blog/258/ui-delegation-pattern-for-data-controls-in-wpfsilverlight/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 10:44:00 +0000</pubDate>
		<dc:creator>Akash Kava</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Delegation]]></category>
		<category><![CDATA[Pattern]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://akashkava.com/blog/?p=258</guid>
		<description><![CDATA[This is Preliminary documentation about UI Delegation Pattern to create Clean and Reusable Data Components and UI Markups in a Business Application. UI Delegation Pattern for WPF and Silverlight View more presentations from Akash Kava. UI Delegation Pattern I would call it simply divide and rule, UI Delegation Pattern is narrowing the focus towards piece [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://akashkava.com/blog/258/ui-delegation-pattern-for-data-controls-in-wpfsilverlight/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "UI+Delegation+Pattern+for+Data+Controls+in+WPF%2FSilverlight";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fakashkava.com%2Fblog%2F258%2Fui-delegation-pattern-for-data-controls-in-wpfsilverlight%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fakashkava.com%2Fblog%2F258%2Fui-delegation-pattern-for-data-controls-in-wpfsilverlight%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p align="justify">This is Preliminary documentation about UI Delegation Pattern to create Clean and Reusable Data Components and UI Markups in a Business Application.</p>
<div align="justify">
<div style="padding-bottom: 0px; padding-left: 0px; width: 425px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:77a73d93-0392-4904-b5ec-376ec54e12c8" class="wlWriterEditableSmartContent">
<div>
<div style="width:425px" id="__ss_3832620"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/ackava/ui-delegation-pattern-for-wpf-and-silverlight" title="UI Delegation Pattern for WPF and Silverlight">UI Delegation Pattern for WPF and Silverlight</a></strong><object width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=uidelegationpatternfordatacontrolsinwpfandsilverlight-100423123459-phpapp02&amp;stripped_title=ui-delegation-pattern-for-wpf-and-silverlight" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=uidelegationpatternfordatacontrolsinwpfandsilverlight-100423123459-phpapp02&amp;stripped_title=ui-delegation-pattern-for-wpf-and-silverlight" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/ackava">Akash Kava</a>.</div>
</div>
</div>
</div></div>
<h2 align="justify">UI Delegation Pattern</h2>
<p align="justify">I would call it simply divide and rule, UI Delegation Pattern is narrowing the focus towards piece of UI Element on the screen and have a UI component to fulfill every specific business needs.</p>
<p align="justify">User Interface is the direct connection between User and your business application, thought MVC, MVVM models exist, but they are found to be very useful in CRUD business applications, but when your application is more then CRUD and it involves complex UI and media / hardware interfaces for scientific and entertainment related applications, MVC/MVVM are little difficult to maintain over huge applications. This pattern is already in existence at many places. </p>
<h2 align="justify">Initial Rough Draft</h2>
<p align="justify">This is initial rough draft, I intend to collect problems and solutions and then I want to give a good name to this pattern, for now I am calling this as “UI Delegation Pattern” however I don&#8217;t know whether it suits the name correctly or not. But your opinions are most welcome, if such pattern is already named and exists then I am sorry if I am unaware of it, but please suggest if you know so.</p>
<h2 align="justify">UI Markup vs. Editor Files</h2>
<p align="justify">Earlier Visual Basic or Visual Studio would create .frm and resource files that would contain the contents of UI design created in “Designer/Editor” inside an IDE. These Editors would create code that is required to execute at runtime to create desired user interface. However since Editors would create code with specific style and pattern, these editor files are more or less very structured and they are less buggy. Of course too much of code will certainly make Editor very slow but at the same time, the consistency of UI code is preserved. But you are too much dependent on the Editor and complex UI could be very difficult to work in Editors.</p>
<p align="justify">Where else UI Markup based on xml, gives full freedom to user to create highly complex UI, but at the same time there is no consistency left as everyone writes code very differently. Markups gets piled into one file and it becomes way to difficult to maintain and understand how it works.</p>
<h2 align="justify">Identify Markup Noise</h2>
<p align="justify">My first approach is to identify problems with Markup Noise, so lets identify what is markup noise. When I see any html, aspx, php or such web scripts, the most annoying things I discover that in spite&#160; great CSS and Modular programming support, the scripts becomes a big junk to maintain. Sure WPF / Silverlight are also text markups, hand coded markups becomes way to big junk to manage.</p>
<p align="justify">Bigger problem is to visualize markups as they become more complex. As more and more lines of code gets piled into markup, more time is spent understanding and troubleshooting the UI.</p>
<h2 align="justify">How to Reduce Markup Noise?</h2>
<p align="justify">
<p align="justify">CSS brought a very useful way to reduce markup noise. Still lot of developers write inline styles and I hate to see that. Ideally nicely written code should have inline styles as small as possible.</p>
<h3 align="justify">Appearance Delegation</h3>
<p align="justify">CSS comes under category of Appearance Delegation where your delegate the look and feel of your component to CSS file. In WPF and Silverlight the themes are styles are introduced and you must have all your styles and look and feel related code into resources only.</p>
<p align="justify">The markup containers (Connection Containers) such as Window / Page / User Control should not have any inbuilt styles at all.</p>
<h3 align="justify">Business Logic Delegation</h3>
<p align="justify">This was not possible in plain html, but aspx, php etc did brought concept of server side controls where in you could inject business logic specific to components and reuse them. But these components (user controls / custom controls ) are only designed when they need to be reused, but even if it will be used only once, I would still suggest its better to have components as small as possible.</p>
<p align="justify">Example, Country Combo drop down , Gender Combo drop down, these boxes are nothing but set of options provided to user to select one of them. Now nobody makes specific custom controls for such items, but lets see a practical example.</p>
<h2 align="justify">Bad Markup Example</h2>
<div align="justify">
<pre class="csharpcode">    <span class="kwrd">&lt;</span><span class="html">Window.Resources</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">XmlDataProvider</span> <span class="attr">x:Key</span><span class="kwrd">=&quot;CountryData&quot;</span> <span class="attr">XPath</span><span class="kwrd">=&quot;/Countries&quot;</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">Countries</span> <span class="attr">xmlns</span><span class="kwrd">=&quot;&quot;</span><span class="kwrd">&gt;</span>
                <span class="kwrd">&lt;</span><span class="html">Country</span>
                    <span class="attr">CountryName</span><span class="kwrd">=&quot;United States&quot;</span>
                    <span class="attr">CountryCode</span><span class="kwrd">=&quot;US&quot;</span><span class="kwrd">/&gt;</span>
                <span class="kwrd">&lt;</span><span class="html">Country</span>
                    <span class="attr">CountryName</span><span class="kwrd">=&quot;United States&quot;</span>
                    <span class="attr">CountryCode</span><span class="kwrd">=&quot;US&quot;</span><span class="kwrd">/&gt;</span>
            <span class="kwrd">&lt;/</span><span class="html">Countries</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">XmlDataProvider</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Window.Resources</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">StackPanel</span><span class="kwrd">&gt;</span>

        <span class="kwrd">&lt;</span><span class="html">ComboBox</span>
            <span class="attr">ItemsSource</span><span class="kwrd">=&quot;{Binding Source={StaticResource CountryData},
                XPath=Country}&quot;</span>
            <span class="attr">DisplayMemberPath</span><span class="kwrd">=&quot;@CountryName&quot;</span>
            <span class="attr">SelectedValuePath</span><span class="kwrd">=&quot;@CountryCode&quot;</span>
            <span class="kwrd">/&gt;</span>

        <span class="kwrd">&lt;</span><span class="html">ComboBox</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">sys:String</span><span class="kwrd">&gt;</span>Male<span class="kwrd">&lt;/</span><span class="html">sys:String</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">sys:String</span><span class="kwrd">&gt;</span>Female<span class="kwrd">&lt;/</span><span class="html">sys:String</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">ComboBox</span><span class="kwrd">&gt;</span>

    <span class="kwrd">&lt;/</span><span class="html">StackPanel</span><span class="kwrd">&gt;</span></pre>
</div>
<p align="justify">
<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>
<p align="justify">Now this file defines a resource xml for Country List, and it is bounded by ComboBox to list countries and another ComboBox defines inline items to be displayed.</p>
<h3 align="justify">Let us identify “Controls and Connection Containers”.</h3>
<p align="justify">The window, that hosts Country List and Gender Combo Boxes is <strong>“Connection Container” </strong>in which we should only host controls and connect inter dependent properties of every controls.</p>
<p align="justify">The Connection Container should not have any code that is not related to either any other items or business logic of the container at all.</p>
<p align="justify">List of Countries and Gender items are not at all related to anyone else except for the drop downs. This is pure markup junk where we have so many unnecessary information.</p>
<h2>Solution</h2>
<p>Define custom controls as shown below and see how small our markup becomes. And move Country List xml to some resource file.</p>
<pre class="csharpcode">    <span class="kwrd">public</span> <span class="kwrd">class</span> CountryComboBox : ComboBox
    {

        <span class="kwrd">public</span> CountryComboBox()
        {
            <span class="kwrd">this</span>.DisplayMemberPath = <span class="str">&quot;@CountryName&quot;</span>;
            <span class="kwrd">this</span>.SelectedValuePath = <span class="str">&quot;@CountryCode&quot;</span>;

            Binding b = <span class="kwrd">new</span> Binding();
            b.XPath = <span class="str">&quot;Country&quot;</span>;
            b.Source = FindResource(<span class="str">&quot;CountryData&quot;</span>);
            <span class="kwrd">this</span>.SetBinding(ItemsSourceProperty, b);
        }

    }</pre>
<p>
<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>
<pre class="csharpcode">    <span class="kwrd">public</span> <span class="kwrd">class</span> GenderComboBox : ComboBox
    {
        <span class="kwrd">public</span> GenderComboBox()
        {
            <span class="kwrd">this</span>.ItemsSource =
                <span class="kwrd">new</span> <span class="kwrd">string</span>[] {
                    <span class="str">&quot;Male&quot;</span>,
                    <span class="str">&quot;Female&quot;</span>
                };
        }
    }</pre>
<p>&#160;</p>
<p>The final Markup.</p>
<pre class="csharpcode">    <span class="kwrd">&lt;</span><span class="html">StackPanel</span><span class="kwrd">&gt;</span>

        <span class="kwrd">&lt;</span><span class="html">local:CountryComboBox</span>
            <span class="kwrd">/&gt;</span>

        <span class="kwrd">&lt;</span><span class="html">local:GenderComboBox</span>
            <span class="kwrd">/&gt;</span>

    <span class="kwrd">&lt;/</span><span class="html">StackPanel</span><span class="kwrd">&gt;</span></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>&#160;</p>
<p>Wow, the markup is very clean now. Also the code is easy to understand.</p>
<h2>Conclusion</h2>
<p align="justify">The connection container (Window / Page / User Control) should only have interdependent declarations and should not have any items that are only specific to one control. It should be as clean as possible and it should execute all necessary business logic for the component.</p>
<p align="justify">Configuration items, even resource bindings should be avoided in Connection Containers. They should be narrowed down to custom controls as much as possible.</p>
<p align="justify">This increases readability, reusability and helps in troubleshooting while focusing by narrowing the target.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fakashkava.com%2Fblog%2F258%2Fui-delegation-pattern-for-data-controls-in-wpfsilverlight%2F&amp;title=UI%20Delegation%20Pattern%20for%20Data%20Controls%20in%20WPF%2FSilverlight"><img src="http://akashkava.com/blog/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://akashkava.com/blog/258/ui-delegation-pattern-for-data-controls-in-wpfsilverlight/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

