Saturday 11 December 2010

MVVM RelayCommands and BackgroundWorker

This is largely cribbed from elsewhere, but I wanted to have a system where a RelayCommand starts off an asynchronous update of the Model.

First we have to have a flag to hold the idle status
public bool IsIdle ( get; set; }
The RelayCommand command needs to bind its CanExecute to this property

_getCommandData = new RelayCommand (
() => GetTimeSheet(),
() => IsIdle);

This means that the WPF item bound to this command (in this case a button), is disabled when the idle status is set to false.

So in order to use this, and to not bung up the main UI thread, we need to run the command in a BackgroundWorker thread

protected void DoWork(Action work)
{
idle = false;
var worker = new BackgroundWorker();
worker.DoWork += (sender, e) => work();
worker.RunWorkerCompleted += (sender, e) => Idle = true;
worker.RunWorkerAsync();
}

public void GetTimeSheet()
{
DoWork (delegate() {
_model.GetTimeSheet();
TimeSheetData = _model.TimeSheetData;
});
}

Saturday 6 March 2010

TLBs and Delphi

We register the capabilities of Delphi applications using TLB files. However, from reading MSDN documentation, "Installation package authors are strongly advised against using the TypeLib table. Instead, they should register type libraries by using the Registry table". Does anyone have any advice on how to do this in a 'Delphi' way for Windows 7?

Saturday 27 February 2010

Delphi/Surround/Cruisecontrol.net howto (updated)

This is a further draft of what I had to do to get CruiseControl.Net and Delphi to play nicely - I will fill out further the details later!

  1. Configured IIS in Windows (see here)
  2. Install CruiseControl.Net 3.5 RC1 (see release notes) - Make sure that the user that the CruiseControl.Net is being run by is able to see and use Delphi (i.e. probably not by default)
  3. Install CCTray
  4. Set the BDS environment setting to point to where Delphi lives (this may have been done when Delphi was installed)
  5. Create a definitions.xml file that has all of the required defaults, for accessing Surround, MSBuild, etc.
  6. Create a project to test. It is difficult to post a real example, as this is code that my company owns, rather than me, but an example could be pulled out and added to this article.
  7. Setup connection to Seapine Surround. I had a real problem with using Delphi and Surround, as by default the code is checked out as readonly, and Delphi needs to write to the working directory (especially when building typelibraries, etc). I have not spent too much time fiddling with it as I have a patch for the CruiseControl.NET source.
  8. Setup the MSBuild section to build the application (in the definitions.xml file)
  9. Setup the configuration to be managed by CruiseControl.NET and Surround, so that the files can be just checked out, modified and then checked in again - this avoids having to access the server machine.
  10. Each developer now gets emailed whenever a build fails, and we can see the running of tests as well, after each build, see below.
  11. Modify the DUnit tests to output XML.

This I did using the code from the link above, and changed the base UnitTest program code to look like this:

var
res : TTestResult;

...

if IsConsole then
if ParamCount <> 0 then
if (copy(ParamStr(1), 0, 5) = '/xml=') then
res := XMLTestRunner.RunRegisteredTests(copy(ParamStr(1), 6, length(ParamStr(1)) -5))
else
res := TextTestRunner.RunRegisteredTests
else
res := TextTestRunner.RunRegisteredTests
else
GUITestRunner.RunRegisteredTests;

if (res.ErrorCount <> 0) then
halt (1);

Other things I have done ...

  • Modify (legally I'm not sure if I can publish it) the code from Run-Time Systems Cyclomatic Complexity Calculator for Delphi so that it outputs the results as an XML file - it actually does this already internally, but doesn't write the results out.
  • Create a task to generate developer documentation using doc-o-matic as a task.

Saturday 2 January 2010

MVC Framework in Delphi

A real world example can be found here on Joanna Carter's pages.