What is the difference between Items and SmartParts of WorkItem

Oct 25, 2007 at 8:16 PM
Under the WorkItem class, there are two ManagedObjectCollection collections: WorkItem.Items and WorkItem.SmartParts. I am not sure what is the difference between them.

As far as a SmartPart or UserControl view concern, they looks like the same. For example, I can added a SmartPart in either way as followings:

WorkItem.Items.AddNew<T>(viewKey); // viewKey is a string as key
// or 

And by using the following code I can retrieve the item added:

T workItem = WorkItem.Items.Get<T>(viewKey);

even the item was added to SmartParts. I am not sure what is the difference and what is the best or recommended method to add a SmartPart view.
Oct 26, 2007 at 4:37 PM
The SmartParts collection only store all the objects that have the SmartPart attribute. This attribute is added by the Add View (with Presenter) recipe. You can see it in the top of the class definition.
The Items collection contains all the objects added to the WorkItem, including SmartParts.
Look at the following example code:

// View1 and View2 are UserControls marked with the SmartPart attribute.
View1 v1 = WorkItem.Items.AddNew<View1>("view1");
int n1 = WorkItem.Items.AddNew<int>("number1");
View2 v2 = WorkItem.SmartParts.AddNew<View2>("view2");
int n2 = WorkItem.SmartParts.AddNew<int>("number2");
In this example, the SmartParts collection will contain an instance of View1 identified with the ID "view1" and an instance of View2 identified with the ID "view2". It will not contain the integer identified with "number2"
The Items collection will contain all the object created in the previous code (including "view2" and "number2").

This is a good practice to separate the different types of items that you will need create in your application.

Please let me know if this helps.

Mariano Converti
Oct 26, 2007 at 11:58 PM
Great! That explains well. In our project, it is not clear which one should be used. In some cases Items were used while others were SmartParts. Even they all work, but as Mariano said the "good practice" should use them in the right way.

By the way, I have two questions related to this issue. I tried to add a view with Smart Part to SmartParts, and I could get the added view back by using Items as I posted in my first post. It looks like that CAB automatically saves a view object in SmartParts collection and it could be retrieved from it or from Items<T>.Get(). In other word, no matter how the view is added, it can be retrieved either from SmartParts or Items. It is quite confusing. Or my guess might be wrong?

The next question is actually not a question. This is my understanding of WorkItem or something I can draw from Mariano's post. Items collection could be used to save a value type data such as Integer or a reference type data such as an object so that the value or object's property values can be set or retrieved to communicate between Modules. Is that right?
Oct 30, 2007 at 5:02 PM
I have a related question to this issue. If I want to remove a view from CAB's collection (Items and SmartParts). Since smartpart view is added to both Items and SmarptParts collections, in which order should I remove it? Items.Rmove(view) then SmartParts.Remove(view), the other way, or just one remove?
Oct 31, 2007 at 6:51 PM

If you want to remove a SmartPart you should remove it from any of the two collections (Items or SmartParts). Your view will be also removed in the other collection.

Please let me know if this helps.

Mariano Converti