State Injection on Work Items...has this changed?

Topics: CAB & Smart Client Software Factory
Dec 3, 2005 at 12:32 PM
originally posted by: eduncan

I am trying to upgrade to the November release of the CAB, but am seeing some unusual behaviour during the buildup of the WorkItem. Specifically, in my work item I have a property that looks like this:

<State("Account")> _
Public Property Account() As Account
Get
Return CType(State("Account"), Account)
End Get
Set(ByVal value As Account)
State("Account") = value
End Set
End Property

And I am adding my work item to the root work item like this:

_rootWorkItem.State("Account") = New Account(831831, DateTime.Now)
Dim browseAccountWorkItem As BrowseAccountWorkItem = _rootWorkItem.Items.AddNew(Of BrowseAccountWorkItem) _
("BrowseAccountWorkItem")

The problem i am seeing is that the underlying state field in the base WorkItem class has not been initialized when the object builder tries to inject the Account state. Walking through the code, it looks like the base WorkItem class has a InitializeWorkItem() method that is used to initialize all of the fields, etc., but this is being called at the end of the buildup, after the PropertySetterStrategy has already tried to inject the state. Is this by design? If so, how can I seamlessly allow my state to pass through a child work item and ultimately into a view?

Thanks,

Eric
Dec 3, 2005 at 12:42 PM
originally posted by: BradWilsonMSFT

The State attribute very likely doesn't do what you expect it to do (i.e., it pulls state from the parent work item, not the self). You certainly don't need it, since you have access to the State bag directly (you are implementing a WorkItem, after all).
Dec 3, 2005 at 12:56 PM
originally posted by: eduncan

What I'm trying to do is inject the state from the parent work item into the child by putting the State attribute on the child's property definition. Then, within the property itself I'm trying to add the value to the child's state bag so that it can then in turn be injected into the child's views when they are created. This is supposed to work, as documented in the help under 'Working with WorkItems' (step 4) quoted below:

If a parent WorkItem contains state that is required in this WorkItem, use the State attribute to inject state items (each specified by a key) into the child WorkItem automatically.
C Copy Code
State("Customer")
public Customer Customer
{
get { return (Customer)State"Customer"; }
set { State"Customer" = value; }
}


Visual Basic Copy Code
<State("Customer")> _
Public Property Customer() As Customer
Get
Return DirectCast(State("Customer"), Customer)
End Get
Set(ByVal value As Customer)
State("Customer") = value
End Set
End Property


This used to work, but doesn't anymore because the child's state bag is null when the parent tries to inject the state during the buildup process. As a workaround, I have to remove the State attribute from the child work item's property, and manually set the property on the child after it's been created and before I call Run(). This doesn't seem to be the way it's supposed to work to me.

Eric
Dec 6, 2005 at 11:52 AM
originally posted by: DLorenz

Well, I inject the parent's state into the child workitem during the Run() process of the WorkItem (Handling OnRunStarted, I think).

My guess is that they changed the order they do things, so it injects the state first, then creates the WorkItem collections. I would think you could change the order to make it work again?
Dec 22, 2005 at 11:11 AM
originally posted by: gluque

Hi, I have the same issue, I have resolved by making the property WriteOnly

<State("Customer")> _
Public WriteOnly Property Customer() As Customer
Set(ByVal value As Customer)
State("Customer") = value
End Set
End Property


but this is not what I need, I need to read the property in the controller

Thanks in advance
Jan 13, 2006 at 8:55 AM
originally posted by: buckj000

I got the same thing. Pass the object to the child in the Run method and forget about injection from parent to child workitems. It's not worth the hassle.
Feb 1, 2006 at 6:17 AM
originally posted by: BradWilsonMSFT

Since you're implementing a WorkItem, and want the parent's version of State, why not just call Parent.State"key"?
Feb 2, 2006 at 9:13 AM
originally posted by: Saramcc

Question: if you use Parent.State"key", and then want to save the child WorkItem, will the Parent State be serialized with the child WorkItem? I'm assuming not. So this is potentially a case where we'd want the desired behavior of injecting the Parent state into the child.

By the way, I'm running into the same issue when trying to use the attribute and will probably go the route of initializing the child state in the OnRunStarted method.
Feb 2, 2006 at 10:49 AM
originally posted by: BradWilsonMSFT

Using dependency injection does not solve your problem, either. If you keep state in your parent, it will not be saved when you are. It will only be saved when the parent is saved.
Mar 4, 2006 at 12:01 PM
originally posted by: maturmel

But it will be saved because the injected state objects from the parent are copied to the State bag of the child, if you follow the help article that has been mentionned in a previous post on this thread.

The point is, state injection should work, and it does not in the current cab release. The documentation shows clearly how to do it, but I've lost half a day of work trying to understand why I kept having a NullReferenceException, until I realized the State property of the child work item was still "null" when the strategy was injecting state values into it. And then I got to this thread. :)
Mar 21, 2006 at 4:39 AM
originally posted by: _JERKER_

Parent.State[] will not work when you have workitems in more than two levels, or I'm I wrong?

I guess that this is a bug and will be fixed next release - please, let us know that this is the case!?
Apr 13, 2006 at 11:01 PM
originally posted by: cpiock

I have the same problem i put some data on protected override void AfterShellCreated() in the rootworkitem.state. The i have one child workitem that will read this state entry but the state property is always null so i can't be set with the injection. The injection works fine, the value in the set of the properties is set right. only the child workitem has no state.
Is this a bug? Are there some solutions. The strange thing is that there is a moment where the state property is set but then on the injection is null?
Apr 14, 2006 at 5:35 AM
originally posted by: marianoszklanny

I came up with a solution time ago, I think you will find these posts useful:

CAB: Injecting State into Child Work Items
http://staff.southworks.net/blogs/mariano/archive/2006/02/17/210.aspx

CAB: Injecting State into Child Work Items through Custom Attribute
http://staff.southworks.net/blogs/mariano/archive/2006/02/20/CABInheritState.aspx
Apr 18, 2006 at 9:41 PM
originally posted by: cpiock

works ... thx