Geeks With Blogs

News
View Szymon Kobalczyk's profile on LinkedIn

Szymon Kobalczyk's Blog A Developer's Notebook

There seams to be some confusion around WorkItem's State. I've seen few posts on CAB message boards with questions if it's OK to put certain data into it. I will try to add my two cents to it but note that this is my point of view and it might be wrong.

Let's think for a minute what kinds of data we can have in our applications. First that comes to mind is the data that is presented to the end-user and this of course must be stored in the View (SmartPart) for the time it's displayed. This data can be loaded from some external sources (files, database) but this should be handled by services. To do their work services can also carry some runtime data that is not visible to the end-user.

So we have some flow of data between Views and Services (preferably with Controller or Presenter in the middle). But many times we also need to share same data between several Views. This will be for example in classical master-detail scenario when one View presents list of records and the other one shows details of currently selected record. Clearly the information of selected item lays outside of the service's domain because the same service can be used by several Views so from this standpoint it should be “stateless“. So here is where WorkItem's state comes in handy. In short State property is a loosely typed collection of items identified by some key. In my understanding it should be primary used for communication between SmartParts and other Items contained within the same WorkItem and its child WorkItems.

There are several mechanisms in CAB that help us with this scenarios.

State change notification

Often we want to react somehow to changes in State item's value. CAB automatically publishes events to state changes and allows to declare handlers simply by putting StateChangedAttribute on a method:

[StateChanged("MyProperty")]
public void MyProperty_StateChanged(object sender, StateChangedEventArgs args)
{
 // handle state change
}

The StateChangedEventArgs contain State item's key, and both old and new values. Of course there can be more then one handler to this event. Also check out John Luif's blog entry on this.

Injecting State into child WorkItems

Sometimes we need to pass the current value of some State item from parent WorkItem to the newly created child WorkItem. We can do this by manually copying the respective values or let the CAB do the work for us. To indicate that property value or constructor parameter should be injected from parent WorkItem's State we can use the StateAttribute:

[State("MyProperty")]
public string MyChildProperty
{
get { ... }
set { ... }
}

Note that this will only copy the value when child WorkItem is created and won't update it when it changes on parent WorkItem. There are some restrictions when using state injection and be sure to read two excellent entries on Mariano's Szklanny blog on how to avoid them.

Persisting WorkItem's State

Finally, CAB defines IStatePersistenceService that allows to persist WorkItem's State between sessions. Out of the box CAB provides to implementations: FileStatePersistenceService and IsolatedStorageStatePersistenceService. Both store state in binary files using the BinaryFormater serializer, but the later one uses Isolated Storage so it should work in restricted access scenarios. Both inherit from StreamStatePersistantService base class with you can also use to implement you own service.

This service does not load by default and you need to add following entry in app.config to enable it:

<CompositeUI>
 <services>
 <add serviceType="Microsoft.Practices.CompositeUI.Services.IStatePersistenceService,
Microsoft.Practices.CompositeUI" 
 instanceType="Microsoft.Practices.CompositeUI.Services.IsolatedStorageStatePersistenceService,
Microsoft.Practices.CompositeUI" />
 span class="html">services>
span class="html">CompositeUI>

To store or retrieve WorkItem's State simply use it's Save() and Load() methods. Note that it uses WorkItem's name to uniquely identify that WorkItem's state so make sure you assign it to the same value before saving and loading.

I haven't tried this one yet but it opens some interesting possibilities.


As example of using state let's take a look at the Inventory WorkItem of my sample application. There are three primary views in this WorkItem: Product list, Items list and Item details. When user selects a product category the first view shows list of products for this category. Next when product is selected second view shows product items. Then after selecting the item it's details are presented on the final view. The IDs of currently selected records are stored in Inventory WorkItem's state and the View's are wired to state change events so they react immediately to any changes.

Currently only one view is displayed at a time but now we can easily build an "Advanced View" that will show all three views at once. We will start by creating container SmartParts for both modes. SimpleView hosts a DeckWorkspace while AdvancedView contains ZoneWorkspace with two SplitContainers. Both also contain simple link buttons to switch the view. These buttons invoke a TogleValueCommand that changes the value of the ViewMode property. This in turn rises a StateChange event that is handled in ViewMode_StateChanged method. Finally this method changes the current container view and moves the three inventory views to current workspace. It also tries to maintain current active SmartPart.

You can see the inventory screens in both modes below:

But the nicest part in all this that I didn't have to change the existing SmartParts at all! They were already communicating with State and changing their arrangement on screen didn't required any changes to their code. Isn't that cool or what :-)


Updated 10/05/2009: Since ProjectDistributor.net is gone I’ve moved the source code to my SkyDrive. You can grab it here:

 

Posted on Sunday, February 26, 2006 7:57 AM Development , Smart Client | Back to top


Comments on this post: Understanding Composite UI Application Block, Part VII

# DatagridView Column Header
Requesting Gravatar...
hi,

Read your .NET blog and found it very very interesting. Keep it up.

I need your help and i have been searching the web for solution quite sometime but couldn't found it. The .NET books in Malaysia is quite expensive and most of my solution are usually found in the web or contributors.

My problem is using the datagridview. I have manage to built it but i need to implement the datagrid to have 2 level of header.

Meaning, 2 column headers under one column header. I've been trying to do that but not successful.

I indeed in need of an advice how to approach this problem.

Will be glad if you could assist me.

Thank you
William Wilstroth
SAP R/3 ABAP and .NET
Left by William Wilstroth on Mar 09, 2006 7:12 AM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
William,
I'm affraid that grouping column headers is not supported in DataGridView. You can simulate it by implementing your own header row. I hope my calendar sample will give you some hints how to do it. However if you need this as a general solution I strongly recommend investing in professional grid control like one from Infragistics, Janus or DeveloperExpress.

Regards,
Szymon Kobalczyk.
Left by Szymon on Mar 09, 2006 5:47 PM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
Do you have a link to download the sample app?
Left by MKP on Apr 04, 2006 11:25 AM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
This project is available for download on ProjectDistributor http://projectdistributor.net/Releases/Release.aspx?releaseId=315
Left by Szymon on Apr 04, 2006 11:31 AM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
I'm very excited to see your code as I'm finding the current CAB examples somewhat lacking. I had difficulty installing Pet Shop 3. It wouldn't accept my Window Authentication login or SqlServer login. However, Pet Shop 4 installed fine. When I run your CAB based Pet Shop port I get exceptions when I click on the main page:

Microsoft.Practices.CompositeUI.EventBroker.EventTopicException: One or more exceptions occurred while firing the topic 'topic://WorkitemStateChanged/CategoryId'
at Microsoft.Practices.CompositeUI.EventBroker.EventTopic.CallSubscriptionHandlers(Object sender, EventArgs e, EventTopicFireDelegate[] handlers) in D:\Program Files\Microsoft Composite UI App Block\CSharp\Source\CompositeUI\EventBroker\EventTopic.cs:line 331

Do you have any ideas?
Left by Steve Hill on Apr 21, 2006 3:28 PM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
With regards to the sample application, I'm sorry for the previous post. It was a simple matter of not copying the project dll's to the Win\bin\debug directory. (the inner exception explained this). Unfortunately, now I am unable to find the correct connection strings for app.config because I don't have the Pet Shop 3 installation, only the Pet Shop 4 installation. Other changes include having to delete the SampleVisualizations reference and changing from "Mixed Platforms" to ".Net".
Left by Steve Hill on Apr 21, 2006 4:31 PM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
Steve,
Sorry you had to go through all this trouble. Maybe I should prepare more detailed install instructions. Unfortunatelly my sample will not work with PetShop 4 either. In fact this is completly rewritten application, incompatible with previous versions. I was going to upgrade my sample but I need to concentrate on some other matters at hand.
Left by Szymon on Apr 21, 2006 5:25 PM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
When I attempted to install PetShop3.x into SQL Server 2005 Developer Edition, I ran into a policy issue on my machine. In order to work around it, I had to create the two accounts created by PetShop on my SQL Server using Integrated Security (SQL Logons). I imagine I will have to update the configuration files accordingly.

I didn't have to do anything special to the accounts once created:

mspetshop
mspetshoporders

Hopefully I will get your sample running before lunch... one hour away. Also, have you examined Omar Al Zabir's architecture as described in his 47 page CodeProject Smart Client Contest winning article Developing next generation Smart Clients using .NET 2.0 working with existing .NET 1.1 SOA based XML Web Services? I have examined this client extensively, and it just seems to be the better way to go between the various competing frameworks out there. Whats your take?

http://www.codeproject.com/smartclient/SmartClientSOA.asp?df=100&forumid=203394&select=1460777&msg=1460777
Left by Kenneth Courtney on Apr 25, 2006 2:01 PM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
I've started work to migrate my sample to PetShop 4.0. This should
simplify the installation. It looks promising and I hope that I can deliver
something before weekend. I will post an update here.

As for Omar Al Zabir architecture in my opinion it is complementary to
CAB. For my CAB serves as foundation on which you build your
application. If you choose to create static object model for yout
application it is fine and CAB will easily support this. Of course you
can work with such model without CAB but then you loose many features
such as hierarchical dependency injection.

Thanks for interest
Left by Szymon on Apr 26, 2006 6:39 PM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
Hello.
As with Steve above, do please post some connection strings, as I have not been able to get the MS Installer to load the original PetShop 3.
Thanks
John
Left by John R on Apr 26, 2006 9:33 PM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
Hello.
As with Steve above, do please post some connection strings, as I have not been able to get the MS Installer to load the original PetShop 3.
Thanks
John
Left by John R on Apr 26, 2006 9:34 PM

# CAB isn't so easy to learn...
Requesting Gravatar...
After spending a few hours trying to figure out the responsibilities of all the elements of CAB, I found
Left by Guy Burstein's Blog on Mar 09, 2007 5:14 PM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
HI,
I am working on CAB in C#.Net 2005.
I want destory the smartpart before the closing of the winfow In C#.Net
Plese revert back me

Left by THGUPTA on Mar 29, 2007 6:30 AM

# Composite Application Block themes..
Requesting Gravatar...
I am using Composite Application Block(CAB) in a windows application in .NET Is it possible to apply themes/skins to CAB? and how?

please somebody.. help.. let me know..
Left by poorva on Nov 23, 2007 2:39 PM

# Aw: Composite Application Block themes..
Requesting Gravatar...
CAB does not replace Windows Forms and therefore it does not provide support for themes or skins.

You can create WinForms controls with themes and skins support and use them within CAB. More information about how to use custom controls in CAB can be found here: http://www.codeplex.com/cabext
Left by JBE on Mar 08, 2008 12:48 AM

# help in installing Composite UI Application Block
Requesting Gravatar...
hi i got that error while i rty to installed cab:

roduct: Composite UI Application Block December 2005 (C#) -- Error 1722. There is a problem with this Windows Installer package. A program run as part of the setup did not finish as expected. Contact your support personnel or package vendor. Action SolutionGeneratorInstall, location: C:\WINDOWS\Installer\MSI65F.tmp, command: /i *C:\Program Files\Microsoft Composite UI App Block_\CSharp\* *C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft patterns & practices\Composite UI Application Block December 2005 (C#)\*

i installed already :
1. E.L feb' 2007 ctp
2. gax
3. scsf

anyone know whyyyyyyyyyy?
Left by ziv on Jul 06, 2008 9:38 PM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
This link is broken This project is available for download on ProjectDistributor http://projectdistributor.net/Releases/Release.aspx?releaseId=315 .

Can you provide another?

Also the msi installer for PetShop 3 fails on a password complexity issue (cannot seem to find a solution anywhere)
is there an alternative installation method available.

I have PetShop 4 installed but would like to install PetShop 3 installed in another sql server 2005 instance (sqlexpress in this case)

OS is XP Pro SP3 with SQl Server 2005.
Left by Peter on Oct 04, 2009 7:55 PM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
Hi Peter,
I've added a link to the latest source code at the bottom of the post.

I don't know how to help you with the installation and the code requires PetShop 3 version.
Left by Szymon on Oct 05, 2009 1:13 AM

# re: Understanding Composite UI Application Block, Part VII
Requesting Gravatar...
Hi, got a question.

I work on a similar applicaiton using grid controls from Infragistics and got the same error as below: Do you know what the error meands?


Microsoft.Practices.CompositeUI.EventBroker.EventTopicException: One or more exceptions occurred while firing the topic 'topic://WorkitemStateChanged/CategoryId'
at Microsoft.Practices.CompositeUI.EventBroker.EventTopic.CallSubscriptionHandlers(Object sender, EventArgs e, EventTopicFireDelegate[] handlers) in D:\Program Files\Microsoft Composite UI App Block\CSharp\Source\CompositeUI\EventBroker\EventTopic.cs:line 331
Left by Nigel on Mar 15, 2010 12:55 PM

Your comment:
 (will show your gravatar)


Copyright © Szymon Kobalczyk | Powered by: GeeksWithBlogs.net