BankTeller QuickStart

Topics: CAB & Smart Client Software Factory
Jan 4, 2006 at 4:21 PM
originally posted by: yarezki

In the BankTeller Module under workItems/BankTeller we have a CustomerQueueView which uses the CustomerQueueController. In the CustomerQueueView we have:

private CustomerQueueController myController;

// We need our controller, so that we can work with a customer when
// the user clicks on one
CreateNew
public CustomerQueueController MyController
{
set { myController = value; }
}

Now let us assume that:

SmartPart
public partial class CustomerQueueView : UserControl, ICustomerQueueView
{

}

and CustomerQueueController is defined as follows:

public class CustomerQueueController : Controller
{
public CustomerQueueController(ICustomerQueueView view)
{
// some code
}
}

What changes do I need to make to CustomerQueueView to get it to accept my controller (i.e. CustomerQueueController)

private CustomerQueueController myController;

// We need our controller, so that we can work with a customer when
// the user clicks on one
CreateNew
public CustomerQueueController MyController
{
set { myController = value; }
}

TIA
Yazid
Jan 5, 2006 at 7:24 AM
originally posted by: BradWilsonMSFT

This type of circular dependency isn't easily resolved with the dependency injection system. When we do MVP or MVC, we tend towards a setter for the view:

public class MyController : Controller
{
private MyView _view;

public MyView View
{
set { _view = value; }
}
}

public class MyView
{
private MyControler _controller;

CreateNew
public MyController Controller
{
set { _controller = value; _controller.View = this; }
}
}

When doing MVP, you will put an interface on the view for testability purposes, and your setter on the presenter will take the interface instead of the concrete type.
Jan 5, 2006 at 7:43 AM
originally posted by: yarezki

Do you mean

public class MyController : Controller
{
private IView _view;

public IView View
{
set { _view = value; }
}
}

public class MyView:IView
{
private MyControler _controller;

CreateNew
public MyController Controller
{
set { _controller = value; _controller.View = this; }
}
}
Jan 5, 2006 at 7:55 AM
originally posted by: BradWilsonMSFT

Yep! :)
Jan 5, 2006 at 4:59 PM
originally posted by: yarezki

Cool, in the message board

Bugs & troubleshooting discussion under Labs Hands On / NUNIT - MockView. I have used the above technique on lab 1 to move all the business logic to controller and thus we could test the GPSView using a mockview.

Thanks
Jan 6, 2006 at 3:25 PM
originally posted by: yarezki

I have used this on hands on lab 1 and it works. Now I want to test it so I created a GPSMockView:

public class GPSMockView:IGPS
{

#region IGPS Members
private string latitude;
private string distance;
public string Latitude
{
get { return latitude;}
set { latitude = value; }
}

public string Distance
{
get { return distance; }
set { distance = value;}
}

#endregion
}

GPSController is defined as follows:

public class GPSController
{
private IGPS _view;
public IGPS View
{
set { _view = value; }
}

private WorkItem parentWorkItem;

ServiceDependency
public WorkItem ParentWorkItem
{
get { return parentWorkItem; }
set { parentWorkItem = value; }
}
//TODO:
private IGPSService gpsService;
ServiceDependency
public IGPSService GPS
{
get { return gpsService; }
set { gpsService = value; }
}
public void CalculateDistance()
{
IDistanceCalculatorService calc = parentWorkItem.Services.Get<IDistanceCalculatorService>();
_view.Distance = calc.ComputeDistance(gpsService.GetLatitude(), gpsService.GetLongitude()).ToString();

}
public void CalculateLatitude()
{
_view.Latitude = gpsService.GetLatitude().ToString();
}
}

My test class is as follows:

TestClass
public class GPSViewFixtures
{

public GPSViewFixtures()
{
}
TestMethod
public void CalculateLatitude()
{
GPSMockView view = new GPSMockView();

GPSController controller = new GPSController();
controller.View = view;

controller.CalculateLatitude();
Assert.AreEqual("42", view.Latitude);
}
}

When I call controller.CalculateLatitude(), I get gpsService = null when I run it from a test project. However, when I run it normally (i.e. from the GUI) everything works. Is there anything I could add so that gpsService is not null.

TIA
Yazid