Controller and multiple Views?

Topics: CAB & Smart Client Software Factory
Oct 7, 2005 at 10:17 AM
originally posted by: jnicholsonfl1

If I have a WorkItem for a particular use case, say that of adding a new user and I have at least two ways, meaning visual approaches, for doing so, would these each be separate Views? Can they share a Controller since the WorkItem is going to perform virtually the same for all ways of adding a new user?
Oct 8, 2005 at 5:54 PM
originally posted by: BradWilsonMSFT

Sure, I think it's entirely reasonable to be able to use one controller/presenter type with multiple view types.
Jul 13, 2006 at 9:00 AM
originally posted by: JKraft4PIT

I am at this point where I have a single controller and multiple views.

I was wondering how I would implemet this. I can use a CreateNew injection on one view for the initial controller, but then how do I get that controller reference to another view?

This doesn't seem right or smart, I am currently using 2 controllers but would like to use one.
Jul 13, 2006 at 9:17 AM
originally posted by: ChrisHolmes

Have the WorkItem create the Presenter first and add it to its Item collection. Then use ServiceDependency attribute on the Views to inject the controller when the WorkItem creates those views.
Jul 13, 2006 at 10:28 AM
originally posted by: JKraft4PIT

This is what I think you mean.

'WorkItem Code


Protected Overrides Sub OnRunStarted()
MyBase.OnRunStarted()
dim myController as new MyController
me.items.add(myController)
end sub

'View Code

Private innerMyController as MyController

<Dependency()> _
public writeonly property SetController as MyController
set (byval value as MyController)
innerMyController = value
end set
end property
Jul 13, 2006 at 11:46 AM
originally posted by: JKraft4PIT

I tried what I posted here and 2 controller were still built. I removed the workitem code and then only one controller was built and both had seen it.

Why does this happen?
I add dependency to the views of the controller.
Is CAB supose to build a controller when using dependency injection without the CreateNew? If so is that the same for service dependency?
Jul 18, 2006 at 11:40 AM
originally posted by: ChrisHolmes

You are getting two controllers because of the way the Dependency Injection framework is looking for them. You are not naming the Controller you are looking for, so it is creating a new one.

In your Dependency attribute you need to specific the name of the Controller that your WorkItem created, like so:

Dependency(Name="MyController")

You also need to make sure your WorkItem, when it creates the Controller, gives it a strong name.

Documentation on this is in the CAB docs:

ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.practices.2005Nov.cab/CAB/html/02-110-Design%20of%20the%20Injection%20Model.htm
Jul 18, 2006 at 1:28 PM
originally posted by: JKraft4PIT

Thanks for the answer, but I thought it would only create one controller for each type of controller like it does for service dependency injection (or at least how it is working for me).

'View one code
Private _myService as MyService

<Service Dependency()> _
public writeonly property SetService as MyService
set (value as MyService)
_myService = value
end set
end property


'View two code
Private _myService as MyService

<Service Dependency()> _
public writeonly property SetService as MyService
set (value as MyService)
' value.GetHashCode.ToString same as View one
_myService = value
end set
end property

I create View one then View 2.

Am I missing something?
Jul 18, 2006 at 1:35 PM
originally posted by: ChrisHolmes

Yes, you're missing something.

When you create the Controller you need to give it a strong name. That will make it unique and thus views can fetch it, and only it. Just like when you add a SmartPart to a WorkItem, you need to give it a name so you can refer to it later. Anything stuffed into the WorkItem's collection bag (like a Controller) needs to have a name in order to be referred to exactly.

So the WorkItem would create the controller like so:

MyController controller = WorkItem.Items.AddNew<MyController>("StrongNameHere");

Once the WorkItem has created the controller in such a manner, all other views can fetch it directly via the name.

View1:

public View1(Dependency(Name="StrongNameHere") MyController controller)
{
this.controller = controller;
}


View2:

public View1(Dependency(Name="StrongNameHere") MyController controller)
{
this.controller = controller;
}



Both views will have the same controller if you do it this way. This is not like services (and my bad for using ServiceDependency earlier in this thread, sorry if that caused confusion). In the Service container, you can simply fetch a service by its type, and you will get the one and only one service. But with a Dependency that is not a service, contained in the WorkItem collection, you need to refer to a name in order to get the one and only one Controller.