<?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>Tue, 17 Nov 2009 20:14:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CAL Cookbook Part3</title>
		<link>http://chkpointedev.com/2009/11/cal-cookbook-part3/</link>
		<comments>http://chkpointedev.com/2009/11/cal-cookbook-part3/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 18:54:55 +0000</pubDate>
		<dc:creator>Al Alberto</dc:creator>
				<category><![CDATA[CAL]]></category>
		<category><![CDATA[Composite Application Library]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://chkpointedev.com/?p=255</guid>
		<description><![CDATA[So what are Weebits? Well, Weebits are what makes up Weebles and Weebles ares what WidgeNet, Inc. produces. Got it? No matter, it&#8217;s all fiction. Think of Weebits as parts(pieces) that are used to make products (Weebles), is that better? In the last article we had added a module to our CAL application and that [...]]]></description>
			<content:encoded><![CDATA[<p>So what are Weebits? Well, Weebits are what makes up Weebles and Weebles ares what WidgeNet, Inc. produces. Got it? No matter, it&rsquo;s all fiction. Think of Weebits as parts(pieces) that are used to make products (Weebles), is that better? In the last <a href="http://chkpointedev.com/wp-content/plugins/fckeditor-for-wordpress-plugin/fckeditor/editor/dialog/chkpointedev.com/2009/11/cal-cookbook-part2">article</a> we had added a module to our CAL application and that module included a Weebit data service. We are going to continue with that by adding a presentation layer to the module. This module will then encapsulate everything that is related to Weebits.</p>
<p>We are still dealing with &lsquo;what is a module&rsquo;, so we are not going to be concerned too much with the UI composition facilities in CAL. We&rsquo;ll get to those in later articles. I mention that because there will be some areas where we&rsquo;ll gloss over something that won&rsquo;t make sense right away.</p>
<p>In a previous <a href="http://chkpointedev.com/wp-content/plugins/fckeditor-for-wordpress-plugin/fckeditor/editor/dialog/chkpointedev.com/2009/05/editable-listbox-for-wpf">article</a> I described how to create an editable listbox. We are going to use that code as the presentation for our Weebit module. You can refer to that article for details on the code but the download sample project also contains all of the code. The way you choose to present data to your user will obviously depend on what it is that you are presenting. I chose to use a listbox but I could have just as easily implemented a master/detail view of the same data. And one of the benefits of using &nbsp;CAL is that I can make that change later and it would not affect the design of the application. Along the same lines, the presentation could also be developed by a different design group which facilitates parallel development.</p>
<p><img alt="" width="308" height="368" src="http://chkpointedev.com/wp-content/uploads/WidgeNet2_Sol3.png" /></p>
<p>Since the code for the presentation is described in a previous article I won&rsquo;t repeat it here. The above figure shows the changes that I made to the solution. As you can see there are two views that were defined, <i>WeebitItemCtl</i> is a UserControl that is used to render the listbox items and <i>WeebitEditorView</i> is the editor window that contains the listbox. Both of these can be seen below in the completed application (for this article).</p>
<p><img alt="" width="602" height="404" src="http://chkpointedev.com/wp-content/uploads/WidgeNet2_Part3.png" /></p>
<p>For each of the views mentioned above there &nbsp;is also an associated viewmodel class in order to implement the separated presentation pattern. If you look at the source for each view and viewmodel there really is nothing there that is specific to CAL. So we need to look at the <i>Initialize</i>() method since that&rsquo;s where we said the module gets its identity.</p>
<pre><code>
    public class Weebit : IModule
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;

        public Weebit(IUnityContainer container, IRegionManager regionManager)
        {
            this.container = container;
            this.regionManager = regionManager;
        }
        public void Initialize()
        {
            container.RegisterType(new ContainerControlledLifetimeManager());
            container.RegisterType();

            WeebitEditorView view = container.Resolve<weebiteditorview></weebiteditorview>();

            regionManager.AddToRegion(&quot;MainRegion&quot;, view);
        }
   }
</code></pre>
<p>Ignore the RegionManager for now since it is associated with the UI composition services which we will cover later. For now just note that it somehow manages the view.&nbsp;The first statement you should recognize from the previous article. That&rsquo;s where the container is controlling the lifetime of the object. By the way, we could have also created an instance of <i>WeebitDataSvc</i> locally and then just have the container pass out references to our instance as needed by the application (you&rsquo;d use <i>RegisterInstance</i> instead of <i>RegisterType</i>). When would you do that? When you need to have more control over the instancing and release of your object. Perhaps there are some resources that you have to manage.</p>
<p>In the next statement we are registering the <i>WeebitEditorViewModel</i> class with the container. Using this form, the container will create a new instance for each reference needed. And as we saw in the previous article we need to kick-start things since the container won&rsquo;t create anything until it&rsquo;s required. So in the next statement &nbsp;we are getting a reference to the <i>WeebitEditorView</i> so we can pass it to the RegionManager. But more importantly so that it gets created.</p>
<p>Now we can start to analyze some of the benefits of CAL. It&rsquo;s already doing a lot for us even in this minimal application. In the first CAL <a href="http://chkpointedev.com/wp-content/plugins/fckeditor-for-wordpress-plugin/fckeditor/editor/dialog/chkpointedev.com/2009/07/cal-for-automation-applications">article</a> we started talking about the problems of code dependencies and how nice it would be if there was a &lsquo;<i>factory&rsquo;</i> that would handle the instancing for us and pass out references as needed. Well that&rsquo;s one of the things that CAL&rsquo;s Unity container provides for us. In the sample project we are examining, we know that there is an instance of <i>WeebitDataSvc</i> . But we did not create it. We also know that an instance of <i>WeebitEditorViewModel</i> has been created and we had nothing to do with that. And more importantly of all is that all three,<i> WeebitEditorView, WeebitEditorViewModel, </i>and<i> WeebitDataSvc</i> are &lsquo;hooked up&rsquo; correctly since the data is being displayed (view needs viewmodel and viewmodel needs dataservice) . And that magic is the dependency injection facility that the Unity container provides. &nbsp;To see the mechanism we look at the constructor signature for the classes.</p>
<pre><code>
    public partial class WeebitEditorView : UserControl
    {
        public WeebitEditorView(IWeebitEditorViewModel viewModel)
        {
            this.DataContext = viewModel;
            InitializeComponent();
        }
    }
</code></pre>
<p>The constructor for the WeebitEditorView <i>specifies</i> that it needs a reference to a <i>WeebitEditorViewModel</i> . It uses it to set up the DataContext for the view in order to implement the separated presentation pattern. Since we had registered the <i>WeebitEditorViewModel</i> type with the container it new that before it could create an instance of the view, it needed to create an instance of <i>WeebitEditorViewModel</i>&nbsp;(in order to be able to pass a reference). Likewise if we look at the <i>WeebitEditorViewModel</i> class constructor we see a similar approach.</p>
<pre><code>
    public WeebitEditorViewModel(IWeebitDataSvc weebitSvc)
        {
            this.weebitSvc = weebitSvc;
            ...
        }
</code></pre>
<p>The definition <i>specifies</i> that it needs a reference to <i>WeebitDataSvc</i>. Again, the container sees that before it can create an instance of <i>WeebitEditorViewModel</i> to pass to the view, it needs to create an instance of <i>WeebitDataSvc</i> in order to pass the reference. So all we have to do is tell the container about the types we have defined and then just include in our constructor declaration any objects that we need and everything is taken care of for us. That&rsquo;s nice.</p>
<p>Now let&rsquo;s prove some of the instancing comments we made above.&nbsp;Revise the code in the <i>Initialize</i>() method as shown below.</p>
<pre><code>
            ...
            WeebitEditorView view = container.Resolve<weebiteditorview></weebiteditorview>();

            regionManager.AddToRegion(&quot;MainRegion&quot;, view);

            WeebitEditorView view2 = container.Resolve<weebiteditorview></weebiteditorview>();
        }
</code></pre>
<p>Now set a breakpoint in the constructor for <i>WeebitEditorViewModel</i> and <i>WeebitDataSvc</i>. If you run the application you will see that indeed only one instance of the <i>WeebitDataSvc</i> is created and two instances of the <i>WeebitEditorViewModel</i> as expected.</p>
<p>One final note on the code in the Initialize() method. You may have noticed that I didn&rsquo;t register a type for the <i>WeebitEditorView</i>&nbsp;yet the container was able to create an instance of it. Apparently it works because there is a dependency defined for the object but I&rsquo;m not clear as to why it&rsquo;s so.</p>
<p>So to summarize, you can see that the container has taken care of all the instantiation and reference management for us. All we&rsquo;ve had to do is register the types and specify any object lifetime requirements that we would like observed. Then &nbsp;all we have to do is indicate in the constructor what object references we need. That&rsquo;s really powerful in making our application loosely coupled. And what&rsquo;s more we can get references to any object in any module. The only concern we should have may be in the sequencing order for module declaration and circular references. Both of those have bit me. But we can&rsquo;t show those at this point since we have only one module. So let&rsquo;s add some more meat to the application in the next article.</p>
]]></content:encoded>
			<wfw:commentRss>http://chkpointedev.com/2009/11/cal-cookbook-part3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
