<?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>CheckPointe Development</title>
	<atom:link href="http://chkpointedev.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://chkpointedev.com</link>
	<description>Optimizing Automation Through .NET</description>
	<lastBuildDate>Fri, 27 Aug 2010 13:52:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>CAL Cookbook Part4</title>
		<link>http://chkpointedev.com/2010/01/cal-cookbook-part4/</link>
		<comments>http://chkpointedev.com/2010/01/cal-cookbook-part4/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 01:13:00 +0000</pubDate>
		<dc:creator>Al Alberto</dc:creator>
				<category><![CDATA[CAL]]></category>

		<guid isPermaLink="false">http://chkpointedev.com/?p=290</guid>
		<description><![CDATA[Continuing from the previous article, we can see that the main feature of Prism (Composite Application Library) is its support for building complex applications but more importantly building &#8216;composable&#8217; applications. By &#8216;composable&#8217; I mean applications that dynamically configure their composition at run time. This feature allows applications to be easily updated and extended in the [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing from the previous <a href="http://chkpointedev.com/2009/11/cal-cookbook-part3">article</a>, we can see that the main feature of Prism (Composite Application Library) is its support for building complex applications but more importantly building &lsquo;composable&rsquo; applications. By &lsquo;composable&rsquo; I mean applications that dynamically configure their composition at run time. This feature allows applications to be easily updated and extended in the field.</p>
<p>Since a Prism application is composed of a collection of loosely coupled modules, modules can be updated without requiring the whole application to be recompiled. But more importantly (for me that is) is the ability to add or remove modules dynamically (through configuration).</p>
<p>In<a href="http://chkpointedev.com/2009/11/cal-cookbook-part2"> Part2 </a>we indicated that there are three different ways to specify the modules that make up a Prism application. We can specify them in a special section of the config file, in a pre-defined directory, or programmatically. When the application starts up, it will load the modules that have been specified. A consequence of this is that we are able to have each installation be a different version of the application!</p>
<p>In the sample code for this article I&rsquo;ve chosen to use the config file as the mechanism to specify the application modules. The code below shows how the modules are specified.</p>
<pre><code>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;configuration&gt;
  &lt;configSections&gt;
    &lt;section name=&quot;modules&quot; type=&quot;Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite&quot;/&gt;
  &lt;/configSections&gt;
  &lt;modules&gt;
    &lt;module assemblyFile=&quot;Modules/WidgeNet2.Reporting.dll&quot;
      moduleType=&quot;WidgeNet2.Reporting.Reporting, WidgeNet2.Reporting&quot;
      moduleName=&quot;Reporting&quot;/&gt;
    &lt;module assemblyFile=&quot;Modules/WidgeNet2.Weeble.dll&quot;
       moduleType=&quot;WidgeNet2.Weeble.Weeble, WidgeNet2.Weeble&quot;
       moduleName=&quot;Weeble&quot;/&gt;
    &lt;module assemblyFile=&quot;Modules/WidgeNet2.Weebit.dll&quot;
            moduleType=&quot;WidgeNet2.Weebit.Weebit, WidgeNet2.Weebit&quot;
            moduleName=&quot;Weebit&quot;/&gt;
  &lt;/modules&gt;
&lt;/configuration&gt;
</code></pre>
<p>In Part2 we also indicated that by overriding the <em>GetModuleCatalog() </em>method in the <em>Bootstrapper </em>class we can specify the mechanism we want to use for module identification. Since I chose the configuration file approach, we make use of the helper class <em>ConfigurationModuleCatalog </em>to provide that functionality. The definition of the special section is pretty intuitive and all you have to do is add an entry for each module that is to be loaded.</p>
<p>In the sample code each module also includes the associated UI for each module. This makes it trivial to add or remove functionality from the application. If you comment out the entry for the Reporting module in the sample code and re-run the application you will see that the Reports tab is no longer available to the user. Also remember that the definition of a module is up to you. It could be several screens of related functionality or it may be a simple service with no UI.</p>
<p>Another facility built in to Prism which facilitates the loose coupling of modules is the Event Aggregator. This is essentially a publish/subscribe service for custom events. There are a number of common design conditions that can be solved or improved using the mechanism provided by the Event Aggregator.</p>
<p>One situation that comes up often is synchronization of disconnected data. In the sample project there are two screens that display data related to Weebits. One screen controls the creation and editing of Weebits (Weebit Editor) and another screen displays the available Weebits with which the user will define Weebles.</p>
<p>The Weebit Editor screen will always have current data since it controls what is in the data store. The Weeble Editor screen loads the available Weebits when it is initially loaded but if new ones are created the list won&rsquo;t be current. This is a typical problem when there are multiple screens for the same data source.</p>
<p>One, not so elegant solution, is to provide a &lsquo;refresh&rsquo; button on the secondary screen which will refresh the screen with the current data. Another less elegant solution is to provide a timer that will refresh the screen periodically with the current data in the data store.</p>
<p>The right solution is to have the data store notify the secondary screen(s) whenever the data has been updated. In the sample code we have a service (WeebitDataSvc) that manages the data associated with Weebit objects. What we want is for that service to generate an event (publish) whenever a new Weebit is created. Using this approach the Weeble screen can subscribe to the event that will be published and will always have the latest list of Weebit items.</p>
<p>The first thing that&rsquo;s needed is to define a class that will be used to communicate information regarding the creation of a new Weebit (the event). The following code shows the definition for our NewWeebitEvent class.</p>
<pre><code>
using Microsoft.Practices.Composite.Presentation.Events;

namespace WidgeNet2.Common.Events
{
    public class NewWeebitEvent : CompositePresentationEvent<string>
    {
    }
}

</string></code></pre>
<p>The event class derives from CompositePresentationEvent and also defines the payload that will be communicated with the publication. In our case all we want to communicate is the name of the newly created Weebit. But we could just as easily have sent the whole Weebit object as the payload. The CompositePresentationEvent class provides the Publish and Subscribe methods which clients use to subscribe to publication events and to publish events.</p>
<p>Once you have the message defined then it&rsquo;s just a matter of indicating who is going to publish the event and who is going to subscribe to the event. The WeebitDataSvc controls the data store for the Weebit data so it will publish an event when a new Weebit is created. In the constructor for the service we pass in a reference to the Prism EventAggregator service which is then stored locally and used to publish our event. Here&rsquo;s the code for that.</p>
<pre><code>
public bool NewWeebit(WeebitData weebit)
{
	&hellip;
        //Publish the event
        eventAggregator.GetEvent<newweebitevent>().Publish(weebit.Name);
	&hellip;
}

</newweebitevent></code></pre>
<p>The code simply gets a reference to the NewWeebitEvent using the GetEvent method of the EventAggregator. We then call Publish on the returned reference passing the weebit name as the payload.</p>
<p>And on the Weeble Editor screen we subscribe to the event as shown below.</p>
<pre><code>
public WeebleEditorViewModel(IWeebitDataSvc weebitDataSvc, IEventAggregator eventAggregator)
{
    this.weebitDataSvc = weebitDataSvc;
    this.eventAggregator = eventAggregator;

    //Get current list of weebits
    weebits = new ObservableCollection<string>(weebitDataSvc.GetWeebitNames());

    //Subscribe to new weebits event
    NewWeebitEvent newWeebitEvent = eventAggregator.GetEvent<newweebitevent>();

    if (subscriptionToken != null)
    {
        newWeebitEvent.Unsubscribe(subscriptionToken);
    }

    subscriptionToken = newWeebitEvent.Subscribe(NewWeebitEventHandler, ThreadOption.UIThread, false, null);

}
public void NewWeebitEventHandler(string newWeebitName)
{
    //Just add it to the list
    weebits.Add(newWeebitName);
}

</newweebitevent></string></code></pre>
<p>A reference to EventAggregator service gets passed in the constructor. Subscribing to an event is pretty straight forward. The EventAggregator maintains a list of subscriptions to publication events. We first get a reference to a NewWeebitEvent by using the GetEvent method of the EventAggregator. We then use that reference to subscribe to that publication by calling the Subscribe method and passing a reference to our event handler. The EventAggregator returns a token so that we can maintain a reference count of our subscriptions. This is important so that we don&rsquo;t get called multiple times for an event. When the WeebitDataSvc publishes the event the EventAggregator will forward the payload to all subscribers of that event using the event handler.</p>
<p>The EventAggregator is powerful feature provided with Prism which will help in de-coupling your components. However it only helps with inprocess communication. We&rsquo;ll explore another approach for an inter-process publish/subscribe service in a subsequent article.</p>
]]></content:encoded>
			<wfw:commentRss>http://chkpointedev.com/2010/01/cal-cookbook-part4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
