Dependencies: WorkItem - Presenter - View

Topics: CAB & Smart Client Software Factory
Sep 20, 2006 at 8:50 AM
originally posted by: PauloMorgado

I have the need to add a new work item which will create a new presenter with a dependencie on the created work item wich creates a view with a dependency on the created presenter. Something like this:

WortItem:
...
newWorkItem = workItem.WorkItems.AddNew<NewWorkItemType>();
newWorkItem.Items.AddNew<Presenter>();
...

Presenter:
...
public Presenter(ServiceDependency NewWorkItemType workItem)
{
workItem.SmartParts.AddNew<SmartPartType>();
}
...

View:
...
public View(ServiceDependency Presenter presenter)
{
...
}
...

But I'm getting expections saying that NewWorkItemType is not available in the current context in the presenter's constructor.

How can I do this?
Sep 20, 2006 at 8:57 AM
originally posted by: PauloMorgado

I should be using a DependencyAttribute and not a ServiceDependencyAttribute, right?
Sep 20, 2006 at 10:38 AM
originally posted by: JKraft4PIT

Why do you have to do this?

For a workitem to have a view and preseneter the relation is like this:

This is VB.Net notation, I don't know C# well enough to free hand

Workitem creates a view. "Workitem.Workspaces("MySpace").Show(Workitem.SmartPrats.AddNew(of MyView));"

View creates a presenter. "CreateNew _
Public Writeonly Property SetPresenter as Presenter
Set (byval Value as Presenter)
myPresenter = value
myPresenter.View = Me
End Set
End Property"

View knows about its view, and presenter knows about the workitem (through injection).

The View requests information form the presenter "ViewData = MyPresenter.GetData()"
The presenter decides what to do (get data, forward to workitem, raise event, ect...)

So i don't know whay you are adding a presneter to the workitems collection. The workitem shouldn't know about the presenter directly.

You should look at the Appraiser Workbench project or the Bank onefrom the SCSF (Smart Client Software Factory).

But if you have to do it the way above, i can probably help you out. But I would like to know why to get a better read on it.
Sep 20, 2006 at 11:08 AM
originally posted by: askew

    • ach! JKraft beat me to it. :)

I use the Smart Client Software Factory to generate modules and views.
This is the generated code for a new module:

namespace Test.Module1
{
public class Module : ModuleInit
{
private WorkItem _rootWorkItem;

InjectionConstructor
public Module(ServiceDependency WorkItem rootWorkItem)
{
_rootWorkItem = rootWorkItem;
}

public override void Load()
{
base.Load();

ControlledWorkItem<ModuleController> workItem = _rootWorkItem.WorkItems.AddNew<ControlledWorkItem<ModuleController>>();
workItem.Controller.Run();
}
}
}


This is the generated code for a new View in that Module:

namespace Test.Module1
{
SmartPart
public partial class TestView1 : UserControl, ITestView1
{
public TestView1()
{
InitializeComponent();
}

/// <summary>
/// Sets the presenter. The dependency injection system will automatically
/// create a new presenter for you.
/// </summary>
CreateNew
public TestView1Presenter Presenter
{
set
{
_presenter = value;
_presenter.View = this;
}
}

protected override void OnLoad(EventArgs e)
{
_presenter.OnViewReady();
}
}
}

The created Module has a _rootWorkItem that loads a ModuleController as subordinate workItem.
The created View for this Module has no workItem of its own, and loads its Presenter via the ObjectBuilder's CreateNew, which wires up the dependencies appropriately.

Hopefully you can see what you need to do with these snippits.
Sep 20, 2006 at 10:32 PM
originally posted by: PauloMorgado

Thanks for the replies.

Like the name says, WorkItem objects are used to managed work items, not just to control modules. I my application commands (not the same as CAB commands) create or (use already created work items) and execute them. Some work items will have presenters that may have different views that may or may not need to know it's presenter (a readonly view doesn't need to know it's presenter).

It's a bit more complicated than the reference application.