• CAB: Injecting State into Child Work Items through Custom Attribute

    Published by Mariano Szklanny on February 21st, 2006 1:00 am under CAB, SCSF, Smart Clients

    14 Comments

    [Important: see this post for SC-SF compatible implementation]

    In my previous post CAB: Injecting State into Child Work Items I described three solutions for passing state objects from a work item to a newly created child work item.
    The third solution consisted of creating a custom attribute and an ObjectBuilder strategy in order to be able to inherit the state automatically from the parent.
    I’ve developed this solution and the code is available for download.

    Instructions:

    1. Download the file CompositeUIExtensions.State.zip
    2. Extract the solution and build it. If you experience problems, check that the references to the CAB assemblies are correct.
    3. In your solution projects (where you have the work items and the shell), add a reference to CompositeUIExtensions.State.dll located in [folder]\bin\Debug\
    4. Open the work items you want them to have the state inherited. Include the following using statement:

      using CompositeUIExtensions.State;

    5. Then, add the attribute [InheritState] before the declaration of the work item. The parameters supported are:
      1. (optional) SetWorkItemID: Indicates whether the ID of the work item should be set with the id parameter of the AddNew() method. The default value is false.
      2. (optional) Keys: Keys of the state objects to be copied. If ommited, all state objects will be copied.
    6. In your shell application (e.g: FormShellApplication) override the AddBuilderStrategies method and insert the following code:

      protected override void AddBuilderStrategies(Microsoft.Practices.ObjectBuilder.Builder builder)
      {
           base.AddBuilderStrategies(builder);
              builder.Strategies.AddNew(Microsoft.Practices.ObjectBuilder.BuilderStage.Initialization);
      }

    7. From now on, when you create a child work item with the AddNew() method you will have the state objects inherited automatically from the parent.

    Examples:

    1. Inheriting all state objects:

      // In the child work item

      [InheritState]
      public class ChildWorkItem : WorkItem

      {
               // …
      }

    2. Inheriting only some state objects:

      // In the child work item

      [InheritState("Customer", "Account")]
      public class ChildWorkItem : WorkItem

      {
               // …
      }

    3. Inheriting only some state objects and setting the workitem ID from the id parameter of the AddNew() method:

      // In the child work item

      [InheritState(true, "Customer", "Account")]
      public class ChildWorkItem : WorkItem

      {
               // …
      }

      // In the parent work item

      ChildWorkItem child = WorkItems.AddNew<ChildWorkItem>(“CustomerView#1″);

  • 14 Comments:

    1. Chris Holmes said on February 21, 2006:

      Awesome Mariano! Saw this linked from the CAB message boards.

      That’s the way to take advantage of the the ObjectBuilder :-)

    2. http:// said on February 23, 2006:

      Good stuff. Just curious why the InheritState attribute doesn’t support inheritance? If I have a base WorkItem class that several plugins use, the state is not updated in the plugins unless I change the Attribute and Strategy to support inheritance.

    3. Mariano said on March 2, 2006:

      UncleFestis, thanks for your comment. I agree with your point of view, on most cases it may be better to let the attribute support inheritance.

      I’ll change the code to support it.
      Cheers,
      Mariano Szklanny

    4. http:// said on May 5, 2006:

      I am trying to use this model in my Smart Clinet. I was able to complete upto step 5. On step 6, when I compile I got an error saying
      “No suitable method found on override” do you know why I might be getting it?
      I am using 2006 Jan drop of objectbuilder and corresponding Composite UI.

    5. http:// said on May 6, 2006:

      Nair, check that your class extends FormShellApplication (or another CabApplication derived class), because AddBuilderStrategies is a method defined in CabApplication.

      Cheers,
      Mariano

    6. http:// said on May 8, 2006:

      Holla Mariano,

      I am newbie so sorry about this questions as I am learning, I created the project using the SC-BAT and GAT.
      1. I put the addbuilderstratergies in the ShellApplication.
      2. Then I created a module. In the module I added a view.
      3. I put the [Inheritstate..] in the view and everything compiled correct.
      But it doesn’t solve my problem. One thing I noticed is the view is derived from UserControl and not the workItem. Couple of questions
      1. Is it the right place to put the inheritstate? How does the workitem controller will get the state information from the shell application?
      2. I have a view and a controller, I want to keep the state in both view and controller since the bussiness logic is in the controller, can I still use the state to keep the value in the controller, since controller is constructed through view???

      Thanks in advance.

    7. http:// said on May 9, 2006:

      Just to be sure I am clear, in my project I have shellapplication. I want to create a user object here and want it to pass around through out the application. How can I do that?

      I have shell application, and then a module (with workitemcontroller). In the module, I am adding a view (user control not the workitem). How can make the shell application to pass the user information from shellapplication through moduleinit to the view.

      On the side note, can a child created from the view also inherit the object to make changes??

      Thanks in advnace for the great help.

    8. http:// said on May 9, 2006:

      Well, I finally figured out, it is not possible to do the same implmentation with this design for SC-BAT model. This model will work when you add workitems and the attributes copy the data from parent. So this Parent-Child model thats it.
      If you want to implement it for SC-BAT, we need to extend you app more to include the WorkItemController and Form as well. Did I understand this correct?

      By the way, I learned about attributes and its usage with your great code.

      Thanks again.

    9. http:// said on May 12, 2006:

      Nair, as you said this solution may not be the best for SCSF (SC-BAT) based applications.

      I’ll come up with a new solution ASAP.

      Thanks for you feedback and I’m glad you can learn from this blog.

    10. Philippe Lombaers said on October 3, 2006:

      Any news about the SCSF version ?
      Thanks.

    11. http:// said on October 11, 2006:

      I finally found some time to implement a SC-SF version :)
      Check it out here:

      http://staff.southworks.net/blogs/mariano/archive/2006/10/11/_5B00_InheritState_5D00_-attribute-working-in-SC_2D00_SF-based-applications.aspx

      Cheers,
      Mariano

    12. http:// said on June 1, 2007:

      [StateChanged] attributes on the parent WorkItem is not called though when a child WorkItem changes the state’s value.

    13. Jak said on October 6, 2008:

      broken link

      thanks, it’s a very usefull article, but :

      I can not download the source file
      http://blogs.southworks.net/blogs/mariano/content/code/CompositeUIExtensions.State.zip

      from this link, I get this message mentionning that
      “Registration has been disabled”

      http://blogs.southworks.net/wp-signup.php?new=blogs

      anybody has downloaded the file recently CompositeUIExtensions.State.zip ?

      JaK

    14. Mariano Szklanny said on October 8, 2008:

      Hi JaK,
      Thanks for the feedback. I have repaired the link.

      Mariano

    Leave a comment

    Your email address will not be published.