<?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>The Inquisitive Coder - Davy Brion&#039;s Blog &#187; Silverlight</title>
	<atom:link href="http://davybrion.com/blog/category/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://davybrion.com/blog</link>
	<description>Trying to walk that thin line between intelligence and ignorance</description>
	<lastBuildDate>Thu, 29 Jul 2010 20:51:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>The MVVM Pattern Is Highly Overrated</title>
		<link>http://davybrion.com/blog/2010/07/the-mvvm-pattern-is-highly-overrated/</link>
		<comments>http://davybrion.com/blog/2010/07/the-mvvm-pattern-is-highly-overrated/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 14:23:58 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Code Quality]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2392</guid>
		<description><![CDATA[If you&#8217;re doing Silverlight or WPF, you&#8217;ve no doubt come across the MVVM (Model-View-ViewModel) pattern. It seems to be the most popular client-side architecture pattern used among Silverlight/WPF developers. I find the pattern to be highly overrated, and actually have some big issues with the whole thing. First, let&#8217;s briefly cover what MVVM is about [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re doing Silverlight or WPF, you&#8217;ve no doubt come across the MVVM (Model-View-ViewModel) pattern.  It seems to be the most popular client-side architecture pattern used among Silverlight/WPF developers.  I find the pattern to be highly overrated, and actually have some big issues with the whole thing. </p>
<p>First, let&#8217;s briefly cover what MVVM is about for those of you who don&#8217;t know yet.  MVVM virtually eliminates all of the code that would typically be placed in the code-behind file of your View (a user control, a screen, whatever) by putting all of that logic in the ViewModel.  The ViewModel itself is never tightly coupled to the View. If it does have a reference to it, it&#8217;s typically through an interface that the View implements instead of a reference of the actual type of the View.  This increases, or better yet, introduces testability to a large part of your UI code that you normally wouldn&#8217;t be able to cover with unit tests if you&#8217;d go with the traditional &#8220;put it all in the code-behind&#8221;-approach.</p>
<p>The ViewModel typically contains properties for the data that is to be shown in the View, and also raises notification events when the data in those properties changes.  The View uses the powerful data-binding capabilities of Silverlight/WPF to bind the properties of the ViewModel to other user controls that the View is composed of.  User-events that are captured by the View are sent to the ViewModel through Commands.  Typically, these commands execute a method in the ViewModel which contains some kind of logic&#8230; usually to either send the updated data in the ViewModel&#8217;s properties to the Model (usually a Service Layer in Silverlight, in WPF it could be either a true Domain Model or also a Service Layer), to perform some business logic in the Model, or to retrieve data from the Model so it can be placed in the ViewModel&#8217;s properties so the View can bind to it.</p>
<p>That is, in a nutshell, how the MVVM pattern works.  So why do i have issues with this? You can develop and test most of the application&#8217;s logic without being dependent on a physical View and that is typically a Good Thing, no? It sure is!  However, my problem with this approach is that  too many responsibilities are concentrated within the ViewModel.  Its main responsibilities are to facilitate databinding <em>and</em> to interact with the Model.  And that, in my opinion, isn&#8217;t very clean.  In some ways, you could actually think of the ViewModel as a glorified code-behind, with the only difference being that it&#8217;s not tightly coupled to the (physical) View.</p>
<p>In most (if not all) MVVM implementations, a ViewModel has many possible reasons to be changed.  It might need to be changed because of different data-binding requirements.  Then again, it might also require changes when a part of the Model is modified.  Now, i&#8217;m sure many of you can agree with me when i say that two of the most important principles in software development are <a href="http://en.wikipedia.org/wiki/Separation_of_concerns">Seperation of Concerns (SoC)</a> and the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle (SRP)</a>.  Obviously, the ViewModel doesn&#8217;t fare well when it comes to both of these principles. </p>
<p>Lets forget about MVVM for a second and focus on the concerns and responsibilities that a user control can have&#8230; say, a user control that shows customer information and allows the user to edit that data so it can be persisted:</p>
<ul>
<li>visualization of the actual control (its own layout as well drawing other user controls that it is composed of)</li>
<li>communication/interaction with the Model</li>
<li>making data (from the Model) available so it can be displayed</li>
<li>outputting data in the correct user controls (for instance: various textboxes)</li>
<li>(simple validation) of modified/inputted data (for instance: no string values for numeric fields, etc&#8230;)</li>
</ul>
<p>Without MVVM, all of these would be taken care of in the View.  Obviously, not a good idea right?  After all, if it were a good idea, we&#8217;d never have had a reason to start using MVVM in the first place.</p>
<p>Now, with MVVM, a lot of people would divide these concerns and responsibilities like this:</p>
<p>View:</p>
<ul>
<li>visualization of the actual control (its own layout as well drawing other user controls that it is composed of)</li>
<li>outputting data in the correct user controls (for instance: various textboxes)</li>
<li>(simple validation) of modified/inputted data (for instance: no string values for numeric fields, etc&#8230;)</li>
</ul>
<p>ViewModel:</p>
<ul>
<li>communication/interaction with the Model</li>
<li>making data (from the Model) available so it can be displayed</li>
</ul>
<p>In this case, the View still has 3 responsibilities which is still too much according to &#8216;the guidelines&#8217;, but it&#8217;s not that big of a deal (though plenty of people would argue that the simple validation would be better placed in the ViewModel).  You&#8217;re highly unlikely to actually want to write automated tests for pure visualization anyway and the SRP is not something that you absolutely need to follow to the extreme in every single place.  For the View, this is really not a problem and very much acceptable.</p>
<p>The ViewModel however has 2 important responsibilities in this case, and i&#8217;d argue that these 2 things should not be done in the same place.  Making data available is done through data-binding.  To do this, you need a set of properties and you need to raise the necessary events.  In most cases, raising those events is very straightforward, but in more complex controls you might need a bit of additional logic to determine which event should be raised at what point.  The other important responsibility is the communication/interaction with the Model.  In most Silverlight applications, the Model will be a Service Layer.  To communicate with this Service Layer, you need Service Proxies.  That means that your ViewModel is essentially responsible for communicating with the Service Layer, dealing with business exceptions that might be thrown by some service calls, and dealing with technical exceptions that can occur simply because of network-related problems.  Group all of those together and i don&#8217;t think i&#8217;m going out on a limb here by saying that that is a lot of logic to put in <em>one</em> class, no?</p>
<p>(Sidenote: what i don&#8217;t really understand is that many people who vigorously advocate adherence to SRP and SoC in their domain and business code don&#8217;t seem to hold their UI code to the same standards. I do.)</p>
<p>At work, we do <em>a lot</em> of Silverlight development.  We typically have around 5 Silverlight projects in active development at the same time.  And it&#8217;s been that way for over a year now.  That equals a lot of Silverlight code that we&#8217;ve written and experience and knowledge that we&#8217;ve built up.  And we haven&#8217;t used MVVM for any of it.  All this time, we&#8217;ve been using the MVP pattern (Supervising Controller variant) with a slight twist.  That twist being a slimmed down version of a <a href="http://www.martinfowler.com/eaaDev/PresentationModel.html">Presentation Model</a>.  Our Presentation Model&#8217;s sole responsibility is to facilitate data-binding, and in some cases, a touch of validation is added as well.</p>
<p>If we go back to our previous example of the customer screen, the responsibilities and concerns would be divided like this in our MVP-PMlight approach:</p>
<p>View:</p>
<ul>
<li>visualization of the actual control (its own layout as well drawing other user controls that it is composed of)</li>
<li>outputting data in the correct user controls (for instance: various textboxes)</li>
<li>(simple validation) of modified/inputted data (for instance: no string values for numeric fields, etc&#8230;)</li>
</ul>
<p>Presenter:</p>
<ul>
<li>communication/interaction with the Model based on the contents of the Presentation Model</li>
</ul>
<p>Presentation Model: </p>
<ul>
<li>making data (from the Model) available so it can be displayed</li>
</ul>
<p>Which leads to classes which are more focused on their task instead of trying to focus on too many things at the same time.  In my opinion, this approach is much better/cleaner than MVVM.  Not only is there a noticeable benefit in code quality (classes are more focused), there is also increased potential to reuse our &#8216;light Presentation Models&#8217; in multiple controls.  Testability is increased over MVVM as well since it&#8217;s always easier to test classes which are focused versus testing classes which have too many responsibilities.  All in all, a couple of important benefits and we still haven&#8217;t thought of a real drawback compared to MVVM.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/07/the-mvvm-pattern-is-highly-overrated/feed/</wfw:commentRss>
		<slash:comments>56</slash:comments>
		</item>
		<item>
		<title>Introducing AgUnit: Silverlight Unit Testing With Resharper</title>
		<link>http://davybrion.com/blog/2010/05/introducing-agunit-silverlight-unit-testing-with-resharper/</link>
		<comments>http://davybrion.com/blog/2010/05/introducing-agunit-silverlight-unit-testing-with-resharper/#comments</comments>
		<pubDate>Mon, 31 May 2010 07:29:30 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[resharper]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/05/introducing-agunit-silverlight-unit-testing-with-resharper/</guid>
		<description><![CDATA[A couple of months ago, i showed a sneak preview of a Silverlight unit testing Resharper plugin that one of my coworkers was working on.&#160; I’m glad to say it’s finally been released and you can get it here Those are Silverlight tests, executed through Resharper within the Silverlight runtime]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago, i showed a <a href="http://davybrion.com/blog/2009/12/unit-tests-and-silverlight/" target="_blank">sneak preview</a> of a Silverlight unit testing Resharper plugin that one of my coworkers was working on.&#160; I’m glad to say it’s finally been released and you can get it <a href="http://agunit.codeplex.com/" target="_blank">here</a> <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://davybrion.com/pictures/IntroducingAgUnitSilverlightUnitTestingW_8615/agunit.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="agunit" border="0" alt="agunit" src="http://davybrion.com/pictures/IntroducingAgUnitSilverlightUnitTestingW_8615/agunit_thumb.png" width="644" height="226" /></a> </p>
<p>Those are Silverlight tests, executed through Resharper within the Silverlight runtime <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/05/introducing-agunit-silverlight-unit-testing-with-resharper/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What Business Applications Could (Or Should) Look Like</title>
		<link>http://davybrion.com/blog/2010/01/what-business-applications-could-or-should-look-like/</link>
		<comments>http://davybrion.com/blog/2010/01/what-business-applications-could-or-should-look-like/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 16:39:45 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Opinions]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2145</guid>
		<description><![CDATA[A lot of us develop business applications, right? Ever got tired of the same old boring and traditional user interfaces that are used for these applications?&#160; At Item Solutions we like to do things differently from what other companies are doing, and we apply that principle to our user interfaces as well.&#160; One of my [...]]]></description>
			<content:encoded><![CDATA[<p>A lot of us develop business applications, right? Ever got tired of the same old boring and traditional user interfaces that are used for these applications?&#160; At <a href="http://www.itemsolutions.com" target="_blank">Item Solutions</a> we like to do things differently from what other companies are doing, and we apply that principle to our user interfaces as well.&#160; One of my coworkers recently created a couple of screencasts of some of our Silverlight applications and i figured you might find them interesting.&#160; You’ll see that in most cases, the way the data is presented isn’t quite how most applications do it, and the way you interact with the data is often different as well.&#160; Not only is this more interesting to work on, most users actually love the user experience that these interfaces offer them.</p>
<p>Check out the movies, and if you want a bit more information on what these applications do, just <a href="http://www.itemsolutions.com/products/products2/interfaces" target="_blank">click here</a>.</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8439903&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8439903&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object></p>
<p><a href="http://vimeo.com/8439903">CRM</a> from <a href="http://vimeo.com/user2878714">itemsolutions</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8439918&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8439918&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object></p>
<p><a href="http://vimeo.com/8439918">Genesis</a> from <a href="http://vimeo.com/user2878714">itemsolutions</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8439847&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8439847&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object></p>
<p><a href="http://vimeo.com/8439847">Glance</a> from <a href="http://vimeo.com/user2878714">itemsolutions</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8439789&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8439789&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object></p>
<p><a href="http://vimeo.com/8439789">Emmaus</a> from <a href="http://vimeo.com/user2878714">itemsolutions</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8440076&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8440076&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object></p>
<p><a href="http://vimeo.com/8440076">Synergy</a> from <a href="http://vimeo.com/user2878714">itemsolutions</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8439937&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8439937&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object></p>
<p><a href="http://vimeo.com/8439937">Nokeos</a> from <a href="http://vimeo.com/user2878714">itemsolutions</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/what-business-applications-could-or-should-look-like/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Tests And Silverlight</title>
		<link>http://davybrion.com/blog/2009/12/unit-tests-and-silverlight/</link>
		<comments>http://davybrion.com/blog/2009/12/unit-tests-and-silverlight/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 13:03:31 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2009/12/unit-tests-and-silverlight/</guid>
		<description><![CDATA[This is something that Steven De Kock and Tom Ceulemans (two coworkers of mine without a blog, so no link) have been working on.&#160; Those are Silverlight tests, executing in the Silverlight runtime, within a full browser context. And yes, they will release it as an open source project soon]]></description>
			<content:encoded><![CDATA[<p><a href="http://davybrion.com/blog/wp-content/uploads/2009/12/Capture.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Capture" border="0" alt="Capture" src="http://davybrion.com/blog/wp-content/uploads/2009/12/Capture_thumb.png" width="644" height="255" /></a> </p>
<p>This is something that Steven De Kock and Tom Ceulemans (two coworkers of mine without a blog, so no link) have been working on.&#160; Those are Silverlight tests, executing in the Silverlight runtime, within a full browser context.</p>
<p>And yes, they will release it as an open source project soon <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/unit-tests-and-silverlight/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Integrated Security With Silverlight And WCF</title>
		<link>http://davybrion.com/blog/2009/11/integrated-security-with-silverlight-and-wcf/</link>
		<comments>http://davybrion.com/blog/2009/11/integrated-security-with-silverlight-and-wcf/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 11:41:32 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1856</guid>
		<description><![CDATA[I lost some time yesterday trying to get a Silverlight client to use Integrated Security with a WCF service so i figured i&#8217;d post the steps necessary to make it work here. First of all, you need to make sure that your IIS installation has support for Windows Authentication. Go to Add/Remove Programs (appwiz.cpl), click [...]]]></description>
			<content:encoded><![CDATA[<p>I lost some time yesterday trying to get a Silverlight client to use Integrated Security with a WCF service so i figured i&#8217;d post the steps necessary to make it work here.</p>
<p>First of all, you need to make sure that your IIS installation has support for Windows Authentication.  Go to Add/Remove Programs (appwiz.cpl), click on Turn Windows Features on or off, select Internet Information Services &#8211; World Wide Web Services &#8211; Security and make sure that Windows Authentication is checked.</p>
<p>Next, you need to make sure that the virtual directory where you&#8217;re hosting the WCF service has Windows Authentication enabled.  Open Internet Information Services Manager (inetmgr), select the virtual directory where the WCF service is hosted, click on the Authentication icon and enable Windows Authentication. </p>
<p>After that, you need to add the following to the binding configuration of the service endpoint (in the host, obviously):</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">security</span><span style="color: blue;"> </span><span style="color: red;">mode</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">TransportCredentialOnly</span>&quot;<span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">transport</span><span style="color: blue;"> </span><span style="color: red;">clientCredentialType</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Windows</span>&quot;<span style="color: blue;"> /&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">security</span><span style="color: blue;">&gt;</span></p>
</div>
<p></code></p>
<p>I only got it working with basicHttpBinding, so unfortunately i can no longer use the customBinding to use binary XML&#8230;</p>
<p>In your Silverlight project, open the ServiceReferences.ClientConfig file and add the following to the binding configuration:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">security</span><span style="color: blue;"> </span><span style="color: red;">mode</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">TransportCredentialOnly</span>&quot;<span style="color: blue;"> /&gt;</span></p>
</div>
<p></code></p>
<p>After that, you should be able to do this in your WCF service:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">WindowsIdentity</span> myuser = <span style="color: #2b91af;">ServiceSecurityContext</span>.Current.WindowsIdentity;</p>
</div>
<p></code></p>
<p>And that should return the Windows user of the user running the Silverlight client.</p>
<p>For the record: this is with Silverlight 3&#8230; i have no idea if it&#8217;ll work with Silverlight 2</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/11/integrated-security-with-silverlight-and-wcf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Event Aggregation</title>
		<link>http://davybrion.com/blog/2009/10/event-aggregation/</link>
		<comments>http://davybrion.com/blog/2009/10/event-aggregation/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 14:12:52 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1708</guid>
		<description><![CDATA[We&#8217;re experimenting at work with a bit of a different approach as to how we structure our views and how they will interact with each other. We already know that our views will be reused in many different contexts so having them communicate with other views is something that needs to be done in a [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re experimenting at work with a bit of a different approach as to how we structure our views and how they will interact with each other.  We already know that our views will be reused in many different contexts so having them communicate with other views is something that needs to be done in a very loosely coupled manner.  I don&#8217;t want any of the views to even know about the existence of other views, let alone having them know about specific instances of them.</p>
<p>But these views do have to interact with each other.  I didn&#8217;t want to use typical events because that would require either a certain view to know about another view to be able to subscribe to its events, or you&#8217;d need some other component which knows which views need to be hooked to each other.  We really need maximum flexibility for what we have in mind with our views, so it only made sense to finally start using the <a href="http://martinfowler.com/eaaDev/EventAggregator.html">Event Aggregation pattern</a>.  The idea is that a view can basically publish events, without knowing who is subscribed to these events, and that suscribers will be notified whenever these events occur.  However, subscribers don&#8217;t know anything about who is publishing the events.  Instead, both publishers and subscribers only know of the Event Aggregator.  A publisher tells the aggregator to publish an event to all subscribed listeners for that event.  Each subscribers simply tells the aggregator &#8220;i&#8217;d like to be notified whenever this event occurs, and i don&#8217;t care where it comes from&#8221;.<br />
Plenty of implementations of this pattern can be found online already, so i figured: why not add my own? :p</p>
<p>Fist of all, an event is nothing more than a class that inherits from this class:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">abstract</span> <span class="cb1">class</span> <span class="cb2">Event</span> {}</p>
</div>
<p></code></p>
<p>Every event should inherit from this class, and add whatever necessary properties that are important for that particular type of event. </p>
<p>If a class is interested in listening to a specific event, it needs to implement the following interface:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IListener</span> { }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IListener</span>&lt;TEvent&gt; : <span class="cb2">IListener</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">where</span> TEvent : <span class="cb2">Event</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Handle(TEvent receivedEvent);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>If a class is interested in multiple events, it simply needs to implement the generic IListener interface for each type of event that it wants to handle.</p>
<p>Then we obviously need the Event Aggregator.  I wanted an aggregator that allowed listeners to either subscribe/unsubscribe to/from very specific events, or just subscribe/unsubscribe to/from whatever it supports.  So i have the following IEventAggregator interface:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IEventAggregator</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Publish&lt;TEvent&gt;(TEvent message) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Publish&lt;TEvent&gt;() <span class="cb1">where</span> TEvent : <span class="cb2">Event</span>, <span class="cb1">new</span>();</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Subscribe(<span class="cb2">IListener</span> listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Unsubscribe(<span class="cb2">IListener</span> listener);</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Subscribe&lt;TEvent&gt;(<span class="cb2">IListener</span>&lt;TEvent&gt; listener) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Unsubscribe&lt;TEvent&gt;(<span class="cb2">IListener</span>&lt;TEvent&gt; listener) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>The Subscribe and Unsubscribe methods that simply take an IListener reference will either subscribe or unsubscribe the given listener to/from every event that it can handle.  In other words, for every generic IListener interface that it implements.  Yet you also have the ability to subscribe/unsubscribe from a specific event type.</p>
<p>And here&#8217;s the implementation:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
.cb3 { color: #a31515; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">EventAggregator</span> : <span class="cb2">IEventAggregator</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">readonly</span> <span class="cb1">object</span> listenerLock = <span class="cb1">new</span> <span class="cb1">object</span>();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">protected</span> <span class="cb1">readonly</span> <span class="cb2">Dictionary</span>&lt;<span class="cb2">Type</span>, <span class="cb2">List</span>&lt;<span class="cb2">IListener</span>&gt;&gt; listeners = <span class="cb1">new</span> <span class="cb2">Dictionary</span>&lt;<span class="cb2">Type</span>, <span class="cb2">List</span>&lt;<span class="cb2">IListener</span>&gt;&gt;();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">readonly</span> <span class="cb2">IDispatcher</span> dispatcher;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> EventAggregator(<span class="cb2">IDispatcher</span> dispatcher)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">this</span>.dispatcher = dispatcher;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Subscribe(<span class="cb2">IListener</span> listener)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ForEachListenerInterfaceImplementedBy(listener, Subscribe);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Unsubscribe(<span class="cb2">IListener</span> listener)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ForEachListenerInterfaceImplementedBy(listener, Unsubscribe);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">static</span> <span class="cb1">void</span> ForEachListenerInterfaceImplementedBy(<span class="cb2">IListener</span> listener, <span class="cb2">Action</span>&lt;<span class="cb2">Type</span>, <span class="cb2">IListener</span>&gt; action)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> listenerTypeName = <span class="cb1">typeof</span>(<span class="cb2">IListener</span>).Name;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> interfaceType <span class="cb1">in</span> listener.GetType().GetInterfaces().Where(i =&gt; i.Name.StartsWith(listenerTypeName)))</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Type</span> typeOfEvent = GetEventType(interfaceType);</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (typeOfEvent != <span class="cb1">null</span>)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; action(typeOfEvent, listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">static</span> <span class="cb2">Type</span> GetEventType(<span class="cb2">Type</span> type)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (type.GetGenericArguments().Count() &gt; 0)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> type.GetGenericArguments()[0];</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb1">null</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Subscribe&lt;TEvent&gt;(<span class="cb2">IListener</span>&lt;TEvent&gt; listener) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Subscribe(<span class="cb1">typeof</span>(TEvent), listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">protected</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Subscribe(<span class="cb2">Type</span> typeOfEvent, <span class="cb2">IListener</span> listener)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">lock</span> (listenerLock)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (!listeners.ContainsKey(typeOfEvent))</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listeners.Add(typeOfEvent, <span class="cb1">new</span> <span class="cb2">List</span>&lt;<span class="cb2">IListener</span>&gt;());</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (listeners[typeOfEvent].Contains(listener))</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">throw</span> <span class="cb1">new</span> <span class="cb2">InvalidOperationException</span>(<span class="cb3">&quot;You're not supposed to register to the same event twice&quot;</span>);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listeners[typeOfEvent].Add(listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Unsubscribe&lt;TEvent&gt;(<span class="cb2">IListener</span>&lt;TEvent&gt; listener) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Unsubscribe(<span class="cb1">typeof</span>(TEvent), listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">protected</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Unsubscribe(<span class="cb2">Type</span> typeOfEvent, <span class="cb2">IListener</span> listener)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">lock</span>(listenerLock)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (listeners.ContainsKey(typeOfEvent))</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listeners[typeOfEvent].Remove(listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Publish&lt;TEvent&gt;(TEvent message) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> typeOfEvent = <span class="cb1">typeof</span>(TEvent);</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">lock</span> (listenerLock)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (!listeners.ContainsKey(typeOfEvent)) <span class="cb1">return</span>;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> listener <span class="cb1">in</span> listeners[typeOfEvent])</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> typedReference = (<span class="cb2">IListener</span>&lt;TEvent&gt;)listener;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dispatcher.BeginInvoke(() =&gt; typedReference.Handle(message));</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Publish&lt;TEvent&gt;() <span class="cb1">where</span> TEvent : <span class="cb2">Event</span>, <span class="cb1">new</span>()</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Publish(<span class="cb1">new</span> TEvent());</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>In case you&#8217;re wondering&#8230; the IDispatcher interface is merely a way to wrap Silverlight&#8217;s real Dispatcher.  We wrap it so we can use a different implementation of the IDispatcher in our automated tests.  Other than that, the implementation is very straightforward.</p>
<p>We started using this very recently, so this implementation might change in the upcoming weeks, but for now it does what it needs to do and it does so pretty well.  In our case, every view&#8217;s Presenter automatically has an IEventAggregator property so whenever we need to publish an event, we can simply do something like EventAggregator.Publish(new SomeEvent(someParameter)).  Or, Presenters that need to listen to events can simply say EventAggregator.Subscribe(this) or only subscribe to some specific events whenever they need to and their specific Handle method will be called whenever someone publishes the event.  This also allowed us to get rid of the somewhat awkward syntax when testing events (subscription, unsubscription and the actual handing of events) with mocking frameworks.</p>
<p>And as a last bonus, i put a call to the Unsubscribe(IListener) method in the Dispose method of our base Presenter implementation.  Which means that none of them will be left subscribed to events by accident anymore <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/event-aggregation/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>5 Reasons Why Silverlight Is My Preferred Web Development Platform</title>
		<link>http://davybrion.com/blog/2009/09/5-reasons-why-silverlight-is-my-preferred-web-development-platform/</link>
		<comments>http://davybrion.com/blog/2009/09/5-reasons-why-silverlight-is-my-preferred-web-development-platform/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 12:57:50 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Opinions]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1616</guid>
		<description><![CDATA[Item Solutions (where i work) has been using Silverlight ever since the first version came out, and by now we have quite a bit of experience and dare i say expertise in it. We generally suggest using Silverlight for every new project that we do, and the only time we don&#8217;t use it is when [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.itemsolutions.com/home">Item Solutions</a> (where i work) has been using Silverlight ever since the first version came out, and by now we have quite a bit of experience and dare i say expertise in it.  We generally suggest using Silverlight for every new project that we do, and the only time we don&#8217;t use it is when customers don&#8217;t want us to use it.  This happens very rarely though.  </p>
<p>While i generally don&#8217;t get involved in most of the UI stuff, i&#8217;m pretty happy with this because i seriously like Silverlight as a platform.  Below is my list of top 5 reasons why Silverlight is my preferred platform for Web Development nowadays:</p>
<ol>
<li>
<p><strong>The ability to create an excellent UIX which definitely matters to customers.</strong></p>
<p>
We have some projects at work where the Silverlight UI definitely receives a &#8220;wow&#8221; response from customers.  Not only because the UI is extremely responsive and snappy, but also because we can use new ways of visualizing data and enabling user actions in the UI in ways that are either completely new for web applications, or just new in general.  We can easily create much more intuitive user interfaces with a responsiveness that can easily compete with that of desktop applications.
</p>
</li>
<li>
<p><strong>Low bandwidth overhead</strong></p>
<p>
Downloading the XAP file can sometimes be painfully slow, depending on your bandwidth obviously, but once it&#8217;s downloaded it&#8217;s also cached by the user&#8217;s browser.  Everything that happens after that consumes very little bandwidth.  There is no CSS, no HTML markup, no javascript, &#8230; the only thing that goes over the wire is the data that you actually need.  And with Silverlight&#8217;s Binary XML feature you&#8217;re also using less bandwidth than before.  This might not seem like a big deal to you, but bandwidth has a huge impact on the client-side responsiveness as well as your server-side ability to process requests.
</p>
</li>
<li>
<p><strong>The client is stateful again</strong></p>
<p>
This one is huge to me.  We don&#8217;t have to deal with things like ViewState or SessionState or anything like that.  If a client has retrieved a set of data, it can just keep it in memory if it makes sense to do so.  This is especially useful for things like user-profiles, or static data that never changes but it has an impact on pretty much everything you develop.  You can simply keep whatever data you need in memory, and it has no impact on the server at all.  Well, it actually reduces the amount of data that you need to send to the server (or receive), and the number of requests you need the server to handle to implement the functionality you need client-side.  This is pretty much a win-win for both the client and the server whereas with typical ASP.NET development, either the client or the server has to take some kind of a hit when it comes to maintaining state (even if it&#8217;s only a little).
</p>
</li>
<li>
<p><strong>The ability to write the client almost entirely in C#</strong></p>
<p>
I don&#8217;t know about you, but my HTML, CSS and Javascript skills are extremely limited.  This seriously reduces my effectiveness when i need to make changes in the presentation layer in typical ASP.NET applications.  And while my XAML skills aren&#8217;t what they should be, my C# skills are sufficient for me to be effective in client-side changes.  I can easily look at a piece of client-side code and make whatever change i need to make without having to consult a reference guide.  That&#8217;s not the biggest advantage to being able to write the client in C# though.  Existing C# skills are very easily transferable to the Silverlight side of things, which means that a larger pool of developers is available to work on your front-end.  True, there is a bit of a learning curve when it comes to Silverlight, but i&#8217;d argue that there is a much steeper learning curve to doing traditional client-side web development <strong>effectively</strong>.
</p>
</li>
<li>
<p><strong>It&#8217;s only going to get better</strong></p>
<p>
As much as i like Silverlight, it is by no means perfect and there is plenty of room for improvement.  That&#8217;s normal though for such a young platform.  In the future we&#8217;ll see plenty of new libraries and tools that will become available to Silverlight developers which should enable us to create even better web applications.  Microsoft is investing a lot of money and effort into it, and i&#8217;m pretty sure that others will follow.
</p>
</li>
</ol>
<p>Now, you won&#8217;t hear me say that Silverlight is the perfect solution for every web application.  SEO is still a problem, as well as acceptance of the Silverlight plugin in corporate environments (though this will only improve over time).  But, in the situations where you can use Silverlight, it certainly pays off to do so.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/09/5-reasons-why-silverlight-is-my-preferred-web-development-platform/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Finding Memory Leaks In Silverlight With WinDbg</title>
		<link>http://davybrion.com/blog/2009/08/finding-memory-leaks-in-silverlight-with-windbg/</link>
		<comments>http://davybrion.com/blog/2009/08/finding-memory-leaks-in-silverlight-with-windbg/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 17:00:15 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Memory Management]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1496</guid>
		<description><![CDATA[As i mentioned in a previous post, you can attach WinDbg to a browser to find memory leaks in your Silverlight applications. I figured it would be a good idea to write down how this process works, since i always end up having to look it up again whenever i need to do this. I [...]]]></description>
			<content:encoded><![CDATA[<p>As i mentioned in a <a href="http://davybrion.com/blog/2009/08/tracking-dangling-object-references-in-silverlight/">previous post</a>, you can attach WinDbg to a browser to find memory leaks in your Silverlight applications.  I figured it would be a good idea to write down how this process works, since i always end up having to look it up again whenever i need to do this.</p>
<p>I wrote a very simple Silverlight application which has a rather typical memory leak. Here&#8217;s the actual code:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">event</span> <span class="cb2">EventHandler</span>&lt;<span class="cb2">MyEventArgs</span>&gt; MyEvent = <span class="cb1">delegate</span> { };</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">void</span> CreateNewViewButton_OnClick(<span class="cb1">object</span> sender, <span class="cb2">RoutedEventArgs</span> e)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewContainer.Children.Clear();</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> newView = <span class="cb1">new</span> <span class="cb2">MyView</span>();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MyEvent += newView.EventHandler;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewContainer.Children.Add(newView);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>For some of you, the memory leak is already very clear.  Like i said, it&#8217;s a very simple example <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Let&#8217;s go through the process of finding and fixing the memory leak using WinDbg. First of all, download Debugging Tools For Windows (which contains the WinDbg executable) <a href="http://www.microsoft.com/whdc/devtools/debugging/installx86.mspx">here</a> and install it.</p>
<p>Then we start our application in Internet Explorer (for some reason i can&#8217;t use WinDbg to inspect the managed memory heap with Firefox, so i just use Internet Explorer for this stuff) and use it.  In the case of my example, that means clicking the button which creates a new view a couple of times.</p>
<p>Open WinDbg.exe and select the &#8216;Attach to a Process&#8217; menu item in the &#8216;File&#8217; menu and select the iexplore.exe process.</p>
<p>Then you need to load the correct version of sos.dll:</p>
<p>
<img src="http://davybrion.com/blog/wp-content/uploads/2009/08/step1.png" alt="step1" title="step1" width="606" height="15" class="size-full wp-image-1498" />
</p>
<p>After that we can see which types of our MySilverlightApplication namespace are present in the managed heap, including how many instances of them:</p>
<p>
<img src="http://davybrion.com/blog/wp-content/uploads/2009/08/step2.png" alt="step2" title="step2" width="952" height="122" class="size-full wp-image-1499" />
</p>
<p>As you can see, there are 13 instances of our MyView type present in the heap.  Using the value in the MT column, we can drill down further:</p>
<p>
<img src="http://davybrion.com/blog/wp-content/uploads/2009/08/step3.png" alt="step3" title="step3" width="512" height="266" class="size-full wp-image-1500" />
</p>
<p>This shows the memory address of each instance of the MyView type in the heap.  Now we can see if there are any live references to these instances:</p>
<p>
<img src="http://davybrion.com/blog/wp-content/uploads/2009/08/step4.png" alt="step4" title="step4" width="831" height="192" class="size-full wp-image-1501" />
</p>
<p>This is actually for the first address that was listed.  As you can see, it is still a reachable reference, which means it will not be collected by the garbage collector. The chain of references clearly indicates that the instance is still referenced from our event in the MainPage instance.  All of the previously listed instances show the same reference chain, so this is clearly a memory leak.  Even though we should only have one active reference of MyView at any point in time of this application, the MyEvent event on MainPage clearly keeps each instance of MyView alive. </p>
<p>The correct way to fix this is to make sure that whenever we remove an instance of MyView, we need to unsubscribe it from the MainPage&#8217;s MyEvent handler.  Always remember this rule when it comes to dealing with events: if the publisher of the event has a longer lifetime than the subscriber of the event, then you absolutely have to unsubscribe each subscriber from the event or the publisher will keep references to each subscriber (preventing them from being garbage collected) for as long as the publisher is alive.</p>
<p>Here&#8217;s the modified version of the above code which avoids the memory leak:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">event</span> <span class="cb2">EventHandler</span>&lt;<span class="cb2">MyEventArgs</span>&gt; MyEvent = <span class="cb1">delegate</span> { };</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">void</span> CreateNewViewButton_OnClick(<span class="cb1">object</span> sender, <span class="cb2">RoutedEventArgs</span> e)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CleanUpPreviousView();</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> newView = <span class="cb1">new</span> <span class="cb2">MyView</span>();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MyEvent += newView.EventHandler;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewContainer.Children.Add(newView);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">void</span> CleanUpPreviousView()</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (ViewContainer.Children.Count &gt; 0)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> myView = ViewContainer.Children[0] <span class="cb1">as</span> <span class="cb2">MyView</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (myView != <span class="cb1">null</span>) MyEvent -= myView.EventHandler;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewContainer.Children.Clear();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>Let&#8217;s see if this really fixed the memory leak.  If we fire up the application and press the button a couple of times, the application should normally only have one live reference of MyView in memory.</p>
<p>
<img src="http://davybrion.com/blog/wp-content/uploads/2009/08/step5.png" alt="step5" title="step5" width="952" height="125" class="size-full wp-image-1502" />
</p>
<p>I clicked the button 5 times, and the above output shows that there are 5 instances of MyView on the heap.  So did we fix the leak or not?  Check the output below:</p>
<p>
<img src="http://davybrion.com/blog/wp-content/uploads/2009/08/step6.png" alt="step6" title="step6" width="622" height="604" class="size-full wp-image-1497" />
</p>
<p>As you can see, only the last instance of MyView is actively referenced somewhere.  That means that the first 4 instances are ready to be collected during the next garbage collection.  </p>
<p>One thing i don&#8217;t understand though, is that the reference chain of the last instance doesn&#8217;t mention MainPage or the event handler anymore.  But when i attached Visual Studio&#8217;s debugger to the browser instance i could clearly see that the MyEvent of MainPage indeed contained an event handler that pointed to this MyView instance.  I&#8217;m far from a WinDbg and SOS expert so i have no idea why the reference chain doesn&#8217;t reflect this.  Perhaps someone with more WinDbg and SOS knowledge can shed some light on this?</p>
<p>Either way, this approach is a pretty good way of finding memory leaks in your Silverlight code.  In a real application it&#8217;s obviously a bit more complicated to find the exact cause of a leak compared to this simple example, but it&#8217;s still pretty doable.  Just execute the !dumpheap -stat -type YourRootNameSpaceHere and look for unusually high numbers of instances of your types.  Then you can start looking at each instance to figure out what&#8217;s going on.  And for a nice list of commands that you can execute in WinDbg with SOS, be sure to check <a href="http://msdn.microsoft.com/en-us/library/bb190764.aspx">this</a> out.</p>
<p>Also, keep in mind that you can do this for every .NET process, and not just Silverlight.  Though you would need to load the sos.dll file of your particular .NET version.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/08/finding-memory-leaks-in-silverlight-with-windbg/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Refactor Safe Implementation Of INotifyPropertyChanged</title>
		<link>http://davybrion.com/blog/2009/08/refactor-safe-implementation-of-inotifypropertychanged/</link>
		<comments>http://davybrion.com/blog/2009/08/refactor-safe-implementation-of-inotifypropertychanged/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 09:34:31 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1483</guid>
		<description><![CDATA[One thing that always annoyed the hell out of me when implementing INotifyPropertyChanged for Silverlight (or WPF) databinding was the fact that you have to provide the name of the property as a string. There are some cool AOP solutions to this, but if you can&#8217;t use those for whatever reason, you could use LINQ&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that always annoyed the hell out of me when implementing INotifyPropertyChanged for Silverlight (or WPF) databinding was the fact that you have to provide the name of the property as a string.  There are some cool AOP solutions to this, but if you can&#8217;t use those for whatever reason, you could use LINQ&#8217;s Expressions to avoid having to use strings.</p>
<p>We already have a base PresentationModel which has the following method:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> NotifyPropertyChanged(<span style="color: blue;">string</span> propertyName)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PropertyChanged(<span style="color: blue;">this</span>, <span style="color: blue;">new</span> <span style="color: #2b91af;">PropertyChangedEventArgs</span>(propertyName));</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>We also have a generic PresentationModel class which inherits from this class:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PresentationModel</span>&lt;TPresentationModel&gt; : <span style="color: #2b91af;">PresentationModel</span> </p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">where</span> TPresentationModel : <span style="color: #2b91af;">PresentationModel</span>&lt;TPresentationModel&gt;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> NotifyPropertyChanged(<span style="color: #2b91af;">Expression</span>&lt;<span style="color: #2b91af;">Func</span>&lt;TPresentationModel, <span style="color: blue;">object</span>&gt;&gt; expression)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> memberExpression = expression.Body <span style="color: blue;">as</span> <span style="color: #2b91af;">MemberExpression</span>;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (memberExpression == <span style="color: blue;">null</span>)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">&quot;You must specify a property!&quot;</span>);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; NotifyPropertyChanged(memberExpression.Member.Name);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>Notice that you pass an expression to the NotifyPropertyChanged method.  It can be used like this:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">MyTestPresentationModel</span> : <span style="color: #2b91af;">PresentationModel</span>&lt;<span style="color: #2b91af;">MyTestPresentationModel</span>&gt;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> myString;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> MyString</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> myString;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> </p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { </p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; myString = <span style="color: blue;">value</span>; </p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; NotifyPropertyChanged(t =&gt; t.MyString);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>That&#8217;s pretty cool, but you should be able to test this easily, right? That&#8217;s pretty easy to do as well:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PresentationModelTest</span>&lt;TPresentationModel&gt; <span style="color: blue;">where</span> TPresentationModel : <span style="color: #2b91af;">PresentationModel</span>&lt;TPresentationModel&gt;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> AssertThatPropertyChangedIsCorrect&lt;TArgument&gt;(TPresentationModel model,</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Expression</span>&lt;<span style="color: #2b91af;">Func</span>&lt;TPresentationModel, TArgument&gt;&gt; property, TArgument value)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> memberInfo = ((<span style="color: #2b91af;">MemberExpression</span>)property.Body).Member;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> propertyName = memberInfo.Name;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">bool</span> eventProperlyTriggered = <span style="color: blue;">false</span>;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; model.PropertyChanged += (s, e) =&gt; eventProperlyTriggered = e.PropertyName.Equals(propertyName);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">typeof</span>(TPresentationModel).GetProperty(propertyName).SetValue(model, value, <span style="color: blue;">null</span>);</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.IsTrue(eventProperlyTriggered);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">TestClass</span>]</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PresentationModelTests</span> : <span style="color: #2b91af;">PresentationModelTest</span>&lt;<span style="color: #2b91af;">MyTestPresentationModel</span>&gt;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">TestMethod</span>]</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> NotifyPropertyChanged_WithExpression_TriggersCorrectPropertyChangedEvent()</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> model = <span style="color: blue;">new</span> <span style="color: #2b91af;">MyTestPresentationModel</span>();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AssertThatPropertyChangedIsCorrect(model, m =&gt; m.MyString, <span style="color: #a31515;">&quot;blah&quot;</span>);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>There we go&#8230; no more strings so refactoring won&#8217;t break anything, and it&#8217;s easy to test as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/08/refactor-safe-implementation-of-inotifypropertychanged/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Tracking Dangling Object References In Silverlight</title>
		<link>http://davybrion.com/blog/2009/08/tracking-dangling-object-references-in-silverlight/</link>
		<comments>http://davybrion.com/blog/2009/08/tracking-dangling-object-references-in-silverlight/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 15:39:58 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Memory Management]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1478</guid>
		<description><![CDATA[One of the downsides of working with a young platform like Silverlight is that the tool support isn&#8217;t quite &#8216;there&#8217; yet, particularly when it comes to profiling for performance and memory usage. And as you may or may not know, Silverlight makes it very easy to introduce memory leaks into your code. Since you can&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides of working with a young platform like Silverlight is that the tool support isn&#8217;t quite &#8216;there&#8217; yet, particularly when it comes to profiling for performance and memory usage.  And as you may or may not know, Silverlight makes it very easy to introduce memory leaks into your code.  Since you can&#8217;t just attach a memory profiler to your Silverlight application, optimizing memory usage or tracking down a leak can be a real pain in the ass.</p>
<p>In the past i have resorted to grabbing a memory dump of the browser and analyzing the content of the managed heap with windbg to track down which instances where being kept in memory.  While this works, it&#8217;s pretty time consuming and can quite easily lead you down the wrong path.  One of the most important things that you want to know in this specific case is: which types are being kept in memory, and how many of them?</p>
<p>In this case, being able to query something during debugging regarding which instances are still kept alive in memory at certain times is sufficient for me.  So i came up with the following class:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ObjectTracker</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">readonly</span> <span style="color: blue;">object</span> monitor = <span style="color: blue;">new</span> <span style="color: blue;">object</span>();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">WeakReference</span>&gt; objects = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">WeakReference</span>&gt;();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">bool</span>? shouldTrack;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Track(<span style="color: blue;">object</span> objectToTrack)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (ShouldTrack())</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">lock</span> (monitor)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; objects.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">WeakReference</span>(objectToTrack));</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; </p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">bool</span> ShouldTrack()</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (shouldTrack == <span style="color: blue;">null</span>)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; shouldTrack = <span style="color: #2b91af;">Debugger</span>.IsAttached;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> shouldTrack.Value;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: blue;">object</span>&gt; GetAllLiveTrackedObjects()</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">lock</span> (monitor)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">GC</span>.Collect();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> objects.Where(o =&gt; o.IsAlive).Select(o =&gt; o.Target);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>The idea is basically to just keep a list of WeakReferences of objects you want to track (only if a debugger is attached), and later on you can &#8216;query&#8217; the live instances through the GetAllLiveTrackedObjects method.  This method obviously performs a full garbage collect to make sure only objects that are really still alive are returned.  Again, you should almost never do a GC.Collect() manually, but in this case the method will only be called while you&#8217;re debugging.</p>
<p>Then, you strategically put the following line in the constructor of every kind of type you&#8217;d like to track:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ObjectTracker</span>.Track(<span style="color: blue;">this</span>);</p>
</div>
<p></code></p>
<p>I have that line in the constructor of my base View class, my base Controller class and in my base Disposable class.  So now, when i want to check if there are any dangling references to instances of types that i really don&#8217;t want hanging around, i can just do something like this:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> liveObjects = <span style="color: #2b91af;">ObjectTracker</span>.GetAllLiveTrackedObjects();</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: #2b91af;">Debugger</span>.IsAttached)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Debugger</span>.Break();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>And then manually inspect the content of the liveObjects collection.  It&#8217;s far from perfect, but at least it&#8217;s an easy way to at least know which instances (that i care about) are being kept in memory.  It&#8217;ll do until we get better tool support :p</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/08/tracking-dangling-object-references-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Hosting Silverlight Client Apps In A Silverlight Host App</title>
		<link>http://davybrion.com/blog/2009/08/hosting-silverlight-client-apps-in-a-silverlight-host-app/</link>
		<comments>http://davybrion.com/blog/2009/08/hosting-silverlight-client-apps-in-a-silverlight-host-app/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 13:23:28 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1468</guid>
		<description><![CDATA[For one of our customers, we need to be able to host multiple Silverlight applications within one dedicated Silverlight &#8216;host&#8217; application. Prism offers this ability, but we already have our own implementations for pretty much all of the other stuff that Prism offers (and that we need), so we figured we might as well implement [...]]]></description>
			<content:encoded><![CDATA[<p>For one of our customers, we need to be able to host multiple Silverlight applications within one dedicated Silverlight &#8216;host&#8217; application.  <a href="http://compositewpf.codeplex.com/">Prism</a> offers this ability, but we already have our own implementations for pretty much all of the other stuff that Prism offers (and that we need), so we figured we might as well implement this hosting feature ourselves.  It turns out it&#8217;s pretty easy to do this.</p>
<p>The general idea is to be able to implement each client application in its own Silverlight Application Project, and to have the ability to download each client aplication&#8217;s XAP file into the Host application and display it within the designated visual area for the currently active client application.</p>
<p>First, we need an interface that each client application needs to contain an implementation of:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IClientApplication</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Initialize();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Cleanup();</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">UIElement</span> VisualContainer { <span class="cb1">get</span>; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>The VisualContainer property needs to return a UIElement which is basically the main visual container element of the client application.  This could be its main page or just a grid or just about any graphical component.</p>
<p>Then we need a class to download XAP files based on their URI. Since you have to do all of these things asynchronously in Silverlight, and the generally used approach seems to be the event-based async pattern, i use the following base eventargs class for all of my asynchronous operations:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
.cb3 { color: #a31515; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">abstract</span> <span class="cb1">class</span> <span class="cb2">AsyncOperationCompletedEventArgs</span>&lt;T&gt; : <span class="cb2">EventArgs</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">protected</span> AsyncOperationCompletedEventArgs(T result, <span class="cb2">Exception</span> error)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">this</span>.result = result;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Error = error;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb2">Exception</span> Error { <span class="cb1">get</span>; <span class="cb1">set</span>; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">readonly</span> T result;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> T Result</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">get</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (Error != <span class="cb1">null</span>)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">throw</span> <span class="cb1">new</span> <span class="cb2">InvalidOperationException</span>(<span class="cb3">&quot;The operation did not complete succesfully, please retrieve the Error property&quot;</span>);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> result;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>And then we have the DownloadCompletedEventArgs class:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">DownloadCompletedEventArgs</span> : <span class="cb2">AsyncOperationCompletedEventArgs</span>&lt;<span class="cb2">Stream</span>&gt;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> DownloadCompletedEventArgs(<span class="cb2">Stream</span> result, <span class="cb2">Exception</span> e) : <span class="cb1">base</span>(result, e) {}</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>Now we can write our simple FileDownloader class:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IFileDownloader</span> </p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">event</span> <span class="cb2">EventHandler</span>&lt;<span class="cb2">ProgressChangedEventArgs</span>&gt; ProgressChanged;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">event</span> <span class="cb2">EventHandler</span>&lt;<span class="cb2">DownloadCompletedEventArgs</span>&gt; DownloadCompleted;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> StartDownloading(<span class="cb2">Uri</span> locationOfResource);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">FileDownloader</span> : <span class="cb2">IFileDownloader</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">event</span> <span class="cb2">EventHandler</span>&lt;<span class="cb2">ProgressChangedEventArgs</span>&gt; ProgressChanged = <span class="cb1">delegate</span> { };</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">event</span> <span class="cb2">EventHandler</span>&lt;<span class="cb2">DownloadCompletedEventArgs</span>&gt; DownloadCompleted = <span class="cb1">delegate</span> { };</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">void</span> StartDownloading(<span class="cb2">Uri</span> locationOfResource)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> webClient = <span class="cb1">new</span> <span class="cb2">WebClient</span>();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; webClient.DownloadProgressChanged += webClient_DownloadProgressChanged;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; webClient.OpenReadCompleted += webClient_OpenReadCompleted;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; webClient.OpenReadAsync(locationOfResource);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">void</span> webClient_OpenReadCompleted(<span class="cb1">object</span> sender, <span class="cb2">OpenReadCompletedEventArgs</span> e)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">DownloadCompletedEventArgs</span> eventArgs;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (!e.Cancelled &amp;&amp; e.Error == <span class="cb1">null</span>)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; eventArgs = <span class="cb1">new</span> <span class="cb2">DownloadCompletedEventArgs</span>(e.Result, <span class="cb1">null</span>);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">else</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; eventArgs = <span class="cb1">new</span> <span class="cb2">DownloadCompletedEventArgs</span>(<span class="cb1">null</span>, e.Error);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DownloadCompleted(<span class="cb1">this</span>, eventArgs);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">void</span> webClient_DownloadProgressChanged(<span class="cb1">object</span> sender, <span class="cb2">DownloadProgressChangedEventArgs</span> e)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ProgressChanged(<span class="cb1">this</span>, <span class="cb1">new</span> <span class="cb2">ProgressChangedEventArgs</span>(e.ProgressPercentage, <span class="cb1">null</span>));</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>We also need something to extract the assemblies from a downloaded XAP file:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
.cb3 { color: #a31515; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IXapReader</span> </p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">IEnumerable</span>&lt;<span class="cb2">Assembly</span>&gt; GetAssemblies(<span class="cb2">Stream</span> stream);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">XapReader</span> : <span class="cb2">IXapReader</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb2">IEnumerable</span>&lt;<span class="cb2">Assembly</span>&gt; GetAssemblies(<span class="cb2">Stream</span> stream)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> assemblies = <span class="cb1">new</span> <span class="cb2">List</span>&lt;<span class="cb2">Assembly</span>&gt;();</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> deploymentPart <span class="cb1">in</span> GetDeploymentParts(stream))</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; assemblies.Add(LoadAssembly(deploymentPart, stream));</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> assemblies;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">static</span> <span class="cb2">Assembly</span> LoadAssembly(<span class="cb2">XElement</span> deploymentPart, <span class="cb2">Stream</span> stream)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> assemblyPart = <span class="cb1">new</span> <span class="cb2">AssemblyPart</span>();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> source = deploymentPart.Attribute(<span class="cb3">&quot;Source&quot;</span>).Value;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> streamResourceInfo = <span class="cb2">Application</span>.GetResourceStream(</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">StreamResourceInfo</span>(stream, <span class="cb3">&quot;application/binary&quot;</span>),</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">Uri</span>(source, <span class="cb2">UriKind</span>.Relative));</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> assemblyPart.Load(streamResourceInfo.Stream);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">static</span> <span class="cb2">IEnumerable</span>&lt;<span class="cb2">XElement</span>&gt; GetDeploymentParts(<span class="cb2">Stream</span> stream)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> appManifest = GetApplicationManifest(stream);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> deploymentRoot = <span class="cb2">XDocument</span>.Parse(appManifest).Root;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> deploymentRoot.Elements().First().Elements();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">static</span> <span class="cb1">string</span> GetApplicationManifest(<span class="cb2">Stream</span> stream)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb1">new</span> <span class="cb2">StreamReader</span>(<span class="cb2">Application</span>.GetResourceStream(</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">StreamResourceInfo</span>(stream, <span class="cb1">null</span>),</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">Uri</span>(<span class="cb3">&quot;AppManifest.xaml&quot;</span>, <span class="cb2">UriKind</span>.Relative)).Stream)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .ReadToEnd();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>And then we can write a class which can retrieve the type which implements the IClientApplication interface from a downloaded XAP file:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IClientApplicationTypeLoader</span> </p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">event</span> <span class="cb2">EventHandler</span>&lt;<span class="cb2">ClientApplicationLoadedEventArgs</span>&gt; LoadCompleted;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">event</span> <span class="cb2">EventHandler</span>&lt;<span class="cb2">ProgressChangedEventArgs</span>&gt; ProgressChanged;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Load(<span class="cb2">Uri</span> xapLocation);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">ClientApplicationTypeLoader</span> : <span class="cb2">IClientApplicationTypeLoader</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">event</span> <span class="cb2">EventHandler</span>&lt;<span class="cb2">ClientApplicationLoadedEventArgs</span>&gt; LoadCompleted = <span class="cb1">delegate</span> { };</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">event</span> <span class="cb2">EventHandler</span>&lt;<span class="cb2">ProgressChangedEventArgs</span>&gt; ProgressChanged = <span class="cb1">delegate</span> { };</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb2">IFileDownloader</span> fileDownloader;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb2">IXapReader</span> xapReader;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> ClientApplicationTypeLoader(<span class="cb2">IFileDownloader</span> fileDownloader, <span class="cb2">IXapReader</span> xapReader)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">this</span>.fileDownloader = fileDownloader;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">this</span>.xapReader = xapReader;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fileDownloader.ProgressChanged += downloader_ProgressChanged;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fileDownloader.DownloadCompleted += downloader_DownloadCompleted;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">void</span> Load(<span class="cb2">Uri</span> xapLocation)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fileDownloader.StartDownloading(xapLocation);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">void</span> downloader_ProgressChanged(<span class="cb1">object</span> sender, <span class="cb2">ProgressChangedEventArgs</span> e)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ProgressChanged(<span class="cb1">this</span>, e);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">void</span> downloader_DownloadCompleted(<span class="cb1">object</span> sender, <span class="cb2">DownloadCompletedEventArgs</span> e)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (e.Error != <span class="cb1">null</span>)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; LoadCompleted(<span class="cb1">this</span>, <span class="cb1">new</span> <span class="cb2">ClientApplicationLoadedEventArgs</span>(<span class="cb1">null</span>, e.Error));</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> assemblies = xapReader.GetAssemblies(e.Result);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> clientApplicationType = FindClientApplicationType(assemblies);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; LoadCompleted(<span class="cb1">this</span>, <span class="cb1">new</span> <span class="cb2">ClientApplicationLoadedEventArgs</span>(clientApplicationType, <span class="cb1">null</span>));</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb2">Type</span> FindClientApplicationType(<span class="cb2">IEnumerable</span>&lt;<span class="cb2">Assembly</span>&gt; assemblies)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> assembly <span class="cb1">in</span> assemblies)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> type <span class="cb1">in</span> assembly.GetTypes())</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (type.GetInterfaces().Contains(<span class="cb1">typeof</span>(<span class="cb2">IClientApplication</span>)))</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> type;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb1">null</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>Obviously, this code doesn&#8217;t deal with the possibility of multiple or no IClientApplication implementations within the same XAP file.  Not really relevant for this post though <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>And now, we can simply do something like this within our host application:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; clientApplicationTypeLoader.LoadCompleted += loader_LoadCompleted;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; clientApplicationTypeLoader.ProgressChanged += loader_ProgressChanged;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; clientApplicationTypeLoader.Load(xapUri);</p>
</div>
<p></code></p>
<p>and the LoadCompleted event handler could look like this:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
.cb3 { color: #a31515; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> loader_LoadCompleted(<span class="cb1">object</span> sender, <span class="cb2">ClientApplicationLoadedEventArgs</span> e)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (e.Error != <span class="cb1">null</span>)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">throw</span> <span class="cb1">new</span> <span class="cb2">Exception</span>(<span class="cb3">&quot;error while retrieving client application&quot;</span>, e.Error);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CleanupCurrentClientApp();</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> clientApp = (<span class="cb2">IClientApplication</span>)<span class="cb2">Activator</span>.CreateInstance(e.Result);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; clientApp.Initialize();</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AppContainer.Children.Add(clientApp.VisualContainer);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">void</span> CleanupCurrentClientApp()</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (AppContainer.Children.Count == 0)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> currentClientApp = (<span class="cb2">IClientApplication</span>)AppContainer.Children[0];</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; currentClientApp.Cleanup();</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AppContainer.Children.Clear();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>And that&#8217;s all there is to it.</p>
<p>Update: download a sample of this <a href="http://davybrion.com/blog/wp-content/uploads/2009/08/SilverlightHost.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/08/hosting-silverlight-client-apps-in-a-silverlight-host-app/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Silverlight Localization Gotcha</title>
		<link>http://davybrion.com/blog/2009/03/silverlight-localization-gotcha/</link>
		<comments>http://davybrion.com/blog/2009/03/silverlight-localization-gotcha/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 13:09:36 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1196</guid>
		<description><![CDATA[Wasted a bit of time figuring this out, so i&#8217;m posting it here for future reference. We have this multi-language silverlight app where the UI needs to be shown in either English or Dutch. So you know, i copied my TextResources.resx file (which contains the translated strings in English) and created a TextResources.nl.resx file where [...]]]></description>
			<content:encoded><![CDATA[<p>Wasted a bit of time figuring this out, so i&#8217;m posting it here for future reference.  We have this multi-language silverlight app where the UI needs to be shown in either English or Dutch.  So you know, i copied my TextResources.resx file (which contains the translated strings in English) and created a TextResources.nl.resx file where i replaced the English strings with the Dutch strings.</p>
<p>In the output folder of the Silverlight project, you can clearly see that the Our.KickAss.Silverlight.Project.resources.dll file is located in the &#8216;nl&#8217; folder.  The XAP file that is being copied to the actual web project however, doesn&#8217;t contain the dutch resources.</p>
<p>Apparantly, the key to &#8216;fixing&#8217; this is opening the .csproj file of your Silverlight project, and then you need to modify the SupportedCultures element so it contains the cultures you&#8217;re supporting:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">SupportedCultures</span><span style="color: blue;">&gt;</span>en;nl<span style="color: blue;">&lt;/</span><span style="color: #a31515;">SupportedCultures</span><span style="color: blue;">&gt;</span></p>
</div>
<p></code></p>
<p>There might be a way to do this from the UI within Visual Studio, but i sure didn&#8217;t find it anywhere. </p>
<p>And now you can simply switch between the translations setting the generated resource class&#8217;s static Culture property to the culture you want, for instance:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">TextResources</span>.Culture = <span style="color: blue;">new</span> <span style="color: #2b91af;">CultureInfo</span>(<span style="color: #a31515;">&quot;nl-BE&quot;</span>);</p>
</div>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/03/silverlight-localization-gotcha/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cross-Platform .NET Business Applications?</title>
		<link>http://davybrion.com/blog/2009/03/cross-platform-net-business-applications/</link>
		<comments>http://davybrion.com/blog/2009/03/cross-platform-net-business-applications/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 07:59:48 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1155</guid>
		<description><![CDATA[Was just going over the new Silverlight 3 features. One thing in particular that i find very interesting: the out-of-browser capabilities: The new out of browser experience in Silverlight 3 enables users to place their favorite Silverlight applications directly onto their PC and Mac, with links on the desktop and start menu—all without the need [...]]]></description>
			<content:encoded><![CDATA[<p>Was just going over the new <a href="http://silverlight.net/getstarted/silverlight3/default.aspx">Silverlight 3 features</a>.  One thing in particular that i find very interesting: the out-of-browser capabilities:</p>
<blockquote><p>
The new out of browser experience in Silverlight 3 enables users to place their favorite Silverlight applications directly onto their PC and Mac, with links on the desktop and start menu—all without the need to download an additional runtime or browser plug-in.
</p></blockquote>
<p>So we can create .NET applications that run on the Mac now <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Sure, they&#8217;ll be sandboxed and you can&#8217;t do everything a real native application could do, but this could still be a pretty compelling deployment choice.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/03/cross-platform-net-business-applications/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Silverlight&#8217;s ProgressBar And Possible Memory Leaks</title>
		<link>http://davybrion.com/blog/2009/02/silverlights-progressbar-and-possible-memory-leaks/</link>
		<comments>http://davybrion.com/blog/2009/02/silverlights-progressbar-and-possible-memory-leaks/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 15:42:44 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Memory Management]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1012</guid>
		<description><![CDATA[This has got to be the weirdest memory leak i&#8217;ve ever investigated. We have this kick-ass Silverlight application, but unfortunately it suffered from very high memory usage that went up rather rapidly. So i attached windbg to the browser&#8217;s process, took a memory dump and checked out which objects were still available in the heap. [...]]]></description>
			<content:encoded><![CDATA[<p>This has got to be the weirdest memory leak i&#8217;ve ever investigated.  We have this kick-ass Silverlight application, but unfortunately it suffered from very high memory usage that went up rather rapidly.  So i attached windbg to the browser&#8217;s process, took a memory dump and checked out which objects were still available in the heap.  Much to my surprise, pretty much everything we instantiated was retained in memory and never got removed from the heap.</p>
<p>So i started looking into the usual things: making sure disposable instances where disposed of properly, that evenhandlers were unregistered properly, etc.  I went over the code and it seemed to be alright.  Stepping through the code with a debugger verified that disposables were indeed disposed of, and that all event handlers were unregistered.</p>
<p>So why was pretty much everything kept in memory?  Further research with windbg showed that every ProgressBar instance that was ever created (and we use a lot of them, basically every time we make a call to the application server) kept a reference to the UserControl it was placed on and thus, kept the UserControl and all the references it contained alive.  In our case, that includes our presentation models and obviously all of the contained child UserControls.</p>
<p>The ProgressBar is defined like this:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">ProgressBar</span><span style="color: red;"> Height</span><span style="color: blue;">=&quot;40&quot;</span><span style="color: red;"> Style</span><span style="color: blue;">=&quot;{</span><span style="color: #a31515;">StaticResource</span><span style="color: red;"> ourKickAssStyle</span><span style="color: blue;">}&quot;</span><span style="color: red;"> VerticalAlignment</span><span style="color: blue;">=&quot;Center&quot;</span><span style="color: red;"> Width</span><span style="color: blue;">=&quot;40&quot;</span><span style="color: red;"> IsIndeterminate</span><span style="color: blue;">=&quot;True&quot;/&gt;</span></p>
</div>
<p></code></p>
<p>The key here is the usage of the IsIndeterminate property&#8230; setting this to true causes the ProgressBar to move continuously without respecting any current Value property.  You know, basic stuff.  The thing is&#8230; if i change the definition of the ProgressBar to this:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">ProgressBar</span><span style="color: red;"> Height</span><span style="color: blue;">=&quot;40&quot;</span><span style="color: red;"> Style</span><span style="color: blue;">=&quot;{</span><span style="color: #a31515;">StaticResource</span><span style="color: red;"> ourKickAssStyle</span><span style="color: blue;">}&quot;</span><span style="color: red;"> VerticalAlignment</span><span style="color: blue;">=&quot;Center&quot;</span><span style="color: red;"> Width</span><span style="color: blue;">=&quot;40&quot; /&gt;</span></p>
</div>
<p></code></p>
<p>The memory leak suddenly went away <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Obviously, this isn&#8217;t a solution because the ProgressBar now doesn&#8217;t really indicate any progress and we need our kick ass custom animation to retain the coolness of the application.</p>
<p>So for some reason, when you set the ProgressBar&#8217;s IsIndeterminate property to true, it actually keeps all of its references alive even when the ProgressBar control is removed from its parent control.  Happy times.</p>
<p>We now have the following ugly method in one of our base UI classes:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> StopProgressBars(<span style="color: #2b91af;">DependencyObject</span> dependencyObject)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> count = <span style="color: #2b91af;">VisualTreeHelper</span>.GetChildrenCount(dependencyObject);</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = 0; i &lt; count; i++)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> child = <span style="color: #2b91af;">VisualTreeHelper</span>.GetChild(dependencyObject, i);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (child != <span style="color: blue;">null</span>)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> progressBar = child <span style="color: blue;">as</span> <span style="color: #2b91af;">ProgressBar</span>;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (progressBar != <span style="color: blue;">null</span>)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; progressBar.IsIndeterminate = <span style="color: blue;">false</span>;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; StopProgressBars(child);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/02/silverlights-progressbar-and-possible-memory-leaks/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Excellent Tool For Silverlight Developers</title>
		<link>http://davybrion.com/blog/2009/02/excellent-tool-for-silverlight-developers/</link>
		<comments>http://davybrion.com/blog/2009/02/excellent-tool-for-silverlight-developers/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 07:15:49 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1003</guid>
		<description><![CDATA[A coworker of mine pointed this out&#8230; if you&#8217;re doing Silverlight development, make sure you install Silverlight Spy!]]></description>
			<content:encoded><![CDATA[<p>A coworker of mine pointed this out&#8230; if you&#8217;re doing Silverlight development, make sure you install <a href="http://firstfloorsoftware.com/silverlightspy/">Silverlight Spy</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/02/excellent-tool-for-silverlight-developers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Poll: What Will Silverlight&#8217;s Future Look Like?</title>
		<link>http://davybrion.com/blog/2009/02/poll-what-will-silverlights-future-look-like/</link>
		<comments>http://davybrion.com/blog/2009/02/poll-what-will-silverlights-future-look-like/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 09:59:03 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=942</guid>
		<description><![CDATA[I&#8217;m currently working on a project at work where we&#8217;re using Silverlight for the UI. I wasn&#8217;t a big fan of Silverlight before i started working with it, but you can consider me converted from now on. It truly is a compelling development platform, and i believe it will be the best choice for a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on a project at work where we&#8217;re using Silverlight for the UI.  I wasn&#8217;t a big fan of Silverlight before i started working with it, but you can consider me converted from now on.  It truly is a compelling development platform, and i believe it will be the best choice for a lot of web applications (note that i&#8217;m not saying it would be the best choice for all web applications) for the next couple of years.</p>
<p>But it appears that the people who feel this way about Silverlight&#8217;s future are in the minority.  You don&#8217;t see a lot of the really big name .NET bloggers talking about Silverlight (although there are a few), though i&#8217;m hoping that will change over time.  In the meantime, i&#8217;d like to know what you think Silverlight&#8217;s future will look like.  I&#8217;ve put up a new poll with the following possible answers:</p>
<ul>
<li>It&#8217;s never going to take off in a meaningful way</li>
<li>It might pick up some steam for media-related purposes, but that&#8217;s about it</li>
<li>It will pick up more adopters as the platform matures and offers more out-of-the-box capabilities for typical business applications</li>
<li>It&#8217;s too different from traditional web development to appeal to most web developers</li>
<li>It will reach wide adoption, but never more than that of typical web apps</li>
<li>Someday it will be the default development platform for .NET web developers</li>
</ul>
<p>(NOTE: for those of you reading this through the RSS feed, the poll is only accessible on my site and is located at the top right corner of the page)</p>
<p>There will obviously be some missing options, so feel free to point them out in the comments. I&#8217;m looking forward to the results and your thoughts on this topic <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/02/poll-what-will-silverlights-future-look-like/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
