As I'm just learning good OOP design, I often don't have any idea as to how to refactor my (already working) code, and how to properly implement separation of concerns. I also don't know how much I have to cover with tests: I tried too little and was lost in refactoring, I tried too much and couldn't change my design without breaking a lot of tests, so I didn't know if everything was still working or not. So, I'm going to apply my new idea of Test Driven Refactoring and hope that it would bring me somewhere.
Here's the story. We have a form with a textbox and a button. I enter "Martin" in the text box, press a button, and a message box saying "Hello Martin" should appear.
Should I apply MVP or MVC here? Probably, but I won't. Folks say, let the tests drive your design, so I'm letting them. But first, I want to make it work as quickly as I can, so I write a simple test using NUnitForms (see below), and I quickly get the following code:
Private Sub HelloButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelloButton.Click
MessageBox.Show("Hello " & Me.NameTextBox.Text, "Hello")
End Sub
I quickly see that my form is doing too much. It's been created for the purpose of showing the UI elements and reacting to the user input. However, in this code it decides how to greet the user. It decides how to construct the message, and that the messagebox should be shown.
How do I refactor this code? First, let's see the test:
<Test()> Sub PressingTheButtonShowsAMessage()
Dim form = CreateForm()
form.Show()
Dim boxTester As New TextBoxTester("NameTextBox", form)
boxTester.Enter("Martin")
Dim buttonTester As New ButtonTester("HelloButton", form)
ExpectModal("Hello", AddressOf HelloHandler)
buttonTester.Click()
End Sub
Public Sub HelloHandler()
Dim messageBoxTester = New MessageBoxTester("Hello")
Assert.AreEqual("Hello Martin", messageBoxTester.Text, "Invalid message text")
messageBoxTester.ClickOk()
End Sub
Function CreateForm() As Windows.Forms.Form
Return New TestDrivenRefactoring.Form1
End Function
What is X? It should be created using a single string, and it should provide the same string to B. It could be just a string, but another TDR principle states that such intermediate objects should be flexible, so it's best to make it a custom object. Why not an interface? Could be as well an interface, but we have a simple data transfer object here, with no behaviour, so a custom class should be fine. Let's call it PersonData, and let's rename B to MessageBoxService.
Here's our test for the new class:
<Test()> Sub CallingTheShowMessageMethodShowsAMessage()
ExpectModal("Hello", AddressOf HelloHandler)
Dim messenger As New MessageBoxService
Dim message As New Message With {.Text = "Hello Martin"}
messenger.ShowMessage(message)
End Sub
We see that the setup portion contains just enough: the first line is an expectation which is sort of an assert, creating the two objects doesn't count, and the only nontrivial thing is setting the Text property of the Message object. Now we clearly have a messaging component that encapsulates showing a message box and has no other concerns.
But what about the rest of the application? We can refactor our it straightforwardly:
Private Sub HelloButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelloButton.Click
Dim messenger As New MessageBoxService
Dim message As New Message With {.Text = "Hello " & Me.NameTextBox.Text}
messenger.ShowMessage(Message)
End Sub
But at this very moment the problem is that we can't test the remaining piece. So, we have to inject a dependency. Now, we don't want hold a reference to the MessageBoxService inside our form class, since we are not sure we'll need it after the refactoring is finished. So, we use a ServiceLocator pattern.
First we introduce the IMessageService interface:
Public Interface IMessageService
Sub ShowMessage(ByVal message As Message)
End Interface
Next, we do something that is considered one of the worst practices: we introduce a global variable:
Module Globals
Public Services As System.ComponentModel.Design.ServiceContainer
End Module
Now we can obtain a reference to our MessageBoxService as simple as
Dim messenger = Services.GetService(GetType(IMessageService))
Services.AddService(GetType(IMessageService), New MessageBoxService)
(to be continued)