Question about using the [State] in CAB

Topics: CAB & Smart Client Software Factory
Feb 2, 2007 at 5:11 PM
Hi there,
I have couple questios about using State storage. I know if an object will be shared cross by most of the modules or workitems, I will put it in RootWorkItem. In my case, I have a Region dataset and get it from a webservice when the UI start, then save it into a RootWorkItem.State"Region". Here is my question:

Inthe child workitem, how can I get this State"Region"? I tried put the following code in ModuleController.cs, but it throw out error:

State("LegalEntity")
public DataSet LegalEntity
{
set { WorkItem.State"LegalEntity" = value; }
}

I removed the above code and put following code in ModuleController.Run()
WorkItem.State"LegalEntity" = WorkItem.RootWorkItem.State"LegalEntity";
Then it works and I can get the State in my Views. by using
State("LegalEntity")
public DataSet LegalEntity
{
set { WorkItem.State"LegalEntity" = value; }
}
What is the right way to do so?

Feb 2, 2007 at 7:50 PM
The right way is to avoid using State altogether. The WorkItem container is more than capable of handling storage of your business objects directly and referencing them via a string key. Then you can make full use of the Dependency Injection framework to use your item anywhere you like.
Feb 3, 2007 at 1:10 AM
Chris,
Thanks for the reply. Is there any sample code you can give out? From what you have said here, I guess what you mean is to use WorkItem.State to do the work or WorkItem.Item.AddNew<>()? Thanks.
Feb 3, 2007 at 4:02 PM
Sample code coming up :)

In your ModuleController you'd do something like this (assuming you've fetched your Region dataset from somewhere)

RootWorkItem.Items.Add(region, "LegalEntity");

You say you want to use this in a view, so pick which dependency injection method you want to use: constructor-based or property-based. Like so:

// Constructor based injection

private Region _region;

Injection Constructor
public MyView(Dependency(Name="LegalEntity") Region region)
{
_region = region;
}


// Property-based injection

Dependency(Name="LegalEntity")
public Region Region
{
set
{
_region= value;
}
}


Feb 5, 2007 at 1:52 PM
Hi, please take a look at this post for more information regarding the State bag:

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

Cheers,
Mariano Szklanny
http://staff.southworks.net/mariano