Monthly Archives: June 2007

Walking Through Passive View

Standard

walk.png 

DevX have published an interesting romp through the refactoring of a traditional forms app into a shiny new  n-tier architecture utilises MVP.

It compares the difference in coding style between traditional gui programming and MVP, then moves into unit testing, services and dependency injection.  

Take a look:  

Layered Architecture, Dependency Injection, and Dependency Inversion

For a less formal and more extensive overview, this series of articles by Jeremy Miller is great:

Build your own CAB   (No, not a taxi or a Pocket PC install, but Microsoft Patterns and Practice’s “Composite Application Block”) 

Advertisements

Dave Cheong’s Blog – Lots of Nuggets to Share

Standard

Although my original intent was simply to share technical information on this blog, I come across something really useful and special every now and then.

If you’re interested in personal improvement, take a look at Dave Cheong’s Blog.

There’s many useful little articles that act as a pithy reminder of what you should be doing.  Examples include:

VSTO Outlook Add-in Debug errors even for simple HelloWorld add-in

Standard

Recently I have been developing an Outlook addin utilising a Model View Controller Architecture and Mocks.

This architecture has given me a great deal of testability of my addin without having to run Outlook.  The downside of this of course is that today when I went to run the Outlook Addin for real it didn’t work.

I pressed “run”, Outlook started, then the IDE went out of debug.  No breakpoints hit, no errors, no nothing.  Great!

The False Solution

As it turns out the “Windows Live Toolbar” (whose indexing I use all the time in Outlook) uses .net Framework 1.1.  It internally forces Outlook addins to use 1.1, which means my 2.0 addin failed to work.  Not fun.

I uninstalled the Windows Live Toolbar and now I can debug successfully.

I guess this highlights the fact that you still need to do testing of the real application as well as.

The post that helped me work out the problem is here:

VSTO Outlook Add-in Debug errors even for simple HelloWorld add-in – MSDN Forums

The Real Solution – An update to the original post

This is happening again!  I’d re-uninstall the Windows Live toolbar if I could but it’s not there anymore!

As it turns out that somehow my addin had become “disabled”.  Apparently if one addin is “disabled”, all will be disabled.  Re – Enabling Addins (or un-disabling) has to be done from the highly intuitive location of the Outlook checkbox.

The steps I took.

1. Go into Help->About and click on the “Disabled Items…” button.  Reenable the addin.

image

2. Go into Tools->Options->Other->Advanced Options->Com Addins.  Remove the Addin

image

3. Quit outlook, then check task manager and ensure “OUTLOOK.EXE” is not running.  If it is, “End Process” it.

4. You should now be able to debug addins again.

ThoughtEx – State of the Art Mind Mapping

Standard

Recently I had a discussion with Joseph Cooney, one of the creators of Thoughtex Mind Mapping application.

This application  has many unique user features such as:

  • Graphical Concept Maps
  • Rich editing within nodes 
  • Contextual Internet search
  • Simplified approach to file management

It’s also very interesting technically, as it utilises RIA technologies such as:

  • Windows Presentation Foundation
  • Click Once

This sort of thing shows how powerful Click Once can be.

Joseph told me the contextual search utilised the Yahoo mashup Apis for searching as they had quite a good licence.  If you want to do something similar check them out.

Presenter First Pattern

Standard

There’s a really interesting podcast series running over on Microsoft’s Channel 9 ARCast.net that involves not only a discussion of the “Presenter First” pattern, but a discussion of Model View Presenter in general.  I’ve found it a great introduction to the subject.

It’s in 3 parts, starting with:

http://channel9.msdn.com/ShowPost.aspx?PostID=312315

 

Raising events (from a mock) using Rhino Mocks in VB.Net

Standard

 I’ve been doing some investigation of Passive View (MVP) architecture for Smart Client applications.

 The article Raising events (from a mock) using Rhino Mocks By Jean-Paul S. Boodhoo outlines the issues and gives some solutions to mocking “View” classes in MVP from a C# point of view.  In this post I’ll show some equivalent VB.Net code samples.

About Passive View 

The Passive View involves three components:

  • Model – Responsible for reading, saving  and validating the business data
  • View – Responsible for displaying data and accepting input from the user
  • Presenter – It provides the “glue” between the View and Presenter.  It copies data from the Model to the View and relays commands from the View to the Model.

(This differs from MVC in that there is no direct communication between Model and View)

The model and the view implement interfaces which are used by the Presenter to communicate with them.

By utilising interfaces, these interfaces can be “Mocked” using a mocking framework such as Rhino Mock.

About Mocking

The purpose of mocking is to ease testing by allowing the developer to create mock implementations of custom objects and verify the interactions using unit testing.

In MVP, the view is usually implemented by a difficult-to-test (or slow-to-test) user interface form.  Using a Mocking framework allows you to create a substitute for the user interface that runs quickly and accurately simulates the user interface from the Presenter’s viewpoint.

Find out more about Rhino Mocks at it’s project page.

Raising Events

An important aspect of the View interface is that it defines events that are raised in order to communicate input actions to the Presenter.

This is not completely straight forward.

Here’s an example of one of a View interface:

Public Interface IPersonalView
    Property Name() As String
    Property Address() As String
    Property Age() As Integer
    Event OnRead()
    Event OnSave()
End Interface

Here’s an example of a presenter that consumes views that implement the IPersonalView interface:

Public Class PersonalPresenter

    Protected WithEvents View As IPersonalView
    Protected Model As IPersonalModel
    Public Sub New(ByVal view As IPersonalView, ByVal model As IPersonalModel)
        Me.View = view
        Me.Model = model
    End Sub

    Public Sub DisplayCustomerDetails() Handles View.OnRead

        ' Ask Model to populate itself
        Model.DoRead()

        ' Transfer information
        With Me.View
            .Name = Model.Name
            .Address = Model.Address
            .Age = Model.Age
        End With

    End Sub
    Public Sub DoUpdate() Handles View.OnSave
        With Me.Model
            .Name = View.Name
            .Address = View.Address
            .Age = View.Age
        End With

        Model.DoSave()
    End Sub

End Class

You’ll notice that the first thing the presenter does (using the WithEvents keyword) is subscribe to events. 

In order to correctly “mock” a view then, we have to program the mock to expect this subscription.  Here is a Test:

<TestClass()> _
Public Class PersonalPresenterTest

    Private moMockery As MockRepository
    <TestInitialize()> _
    Public Sub MyTestInitialize()
        moMockery = New MockRepository

    End Sub

    <TestCleanup()> _
    Public Sub MyTestCleanup()

        moMockery.VerifyAll()

    End Sub
    <TestMethod()> _
    Public Sub Construction_ShouldSubscribeToEventsOnView()
        ' Mock the View
        Dim loView As IPersonalView = moMockery.CreateMock(Of IPersonalView)() 

        AddHandler loView.OnRead, Nothing
        LastCall.Constraints(Constraints.Is.NotNull())

        AddHandler loView.OnSave, Nothing
        LastCall.Constraints(Constraints.Is.NotNull())

        moMockery.ReplayAll()
        Dim target As PersonalPresenter = New PersonalPresenter(loView, Nothing)

    End Sub

End Class

 The important lines here are:

AddHandler loView.OnRead, Nothing
LastCall.Constraints(Constraints.Is.NotNull())
 The first line simulates what the WithEvents call does, which is to call AddHandler on all the handled events.

Raising An Event

Listening for the wireup isn’t enough however.    To properly simulate true view actions, we need to raise events to the Presenter (such as OnRead).  Rhino Mock provides a special object called an “EventRaiser”.

Call LastCall.GetEventRaiser after the AddHandler to get this object, then call the Raise method to raise the event:

' Mock the View
Dim loView As IPersonalView = moMockery.CreateMock(Of IPersonalView)() 

AddHandler loView.OnRead, Nothing
LastCall.Constraints(Constraints.Is.NotNull())

Dim loOnReadEventRaiser As Rhino.Mocks.Interfaces.IEventRaiser = LastCall.GetEventRaiser

AddHandler loView.OnSave, Nothing
LastCall.Constraints(Constraints.Is.NotNull())

moMockery.ReplayAll()

Dim target As PersonalPresenter = New PersonalPresenter(loView, loModel)

' Raise the read event on the "view"
loOnReadEventRaiser.Raise()

This gives me the fundamental building blocks for mocking views.

Creating Commands in XAML only

Standard

Previously I posted an article about defining “Commands” in WPF applications and responding in code.

A way of doing this in XAML only is setting up a binding to a property on another control.

In this example, a toggle button is used to toggle the blod state on a rich text box.

For example:

<Toolbar>

<ToggleButton MinWidth="40" Command="EditingCommands.ToggleBold" CommandTarget="{Binding ElementName=XAMLRichBox}" TextBlock.FontWeight="Bold">B</ToggleButton>

</Toolbar>

 

I got this idea from this article: Mastering the WPF RichTextBox