Today I Learned

Setup

During this past week, I’ve been working on a new feature and during development, I ended up with code that looked like this:

So the code is pretty straight forward, I have a PermissionChecker whose job is to use the IModuleDisabler to turn off certain modules depending upon the user permissions. Pretty straightforward implementation.

Now that the solution is fleshed out, it’s time to write some tests around this. When it comes to testing classes that have dependencies on non-trivial classes, I use NSubstitute, a mocking tool, to create mock versions of those dependencies. In this case, NSubstitute allows me to test how the IModuleDisabler is being used by the PermissionsChecker.

For example, let’s say that I wanted to test how the PermissionChecker interacts with the IModuleDisabler when the user has a partial access, I’d write a test that looks like the following:

In the above test, our assertion step is to check if the mockDisabler received a single call to the DisableReportModule. If it didn’t receive a call, then the test fails. We can write similar tests for the different modules that should be disabled for the partial rights permission and follow a similar pattern for the full rights permission.

However, things get a bit more interesting when we’re testing what happens if the user is an admin. If we follow the same pattern, we’d end up with a test that looks like this:

This solution works for now, however, there is a major maintainability issue, can you spot it?

Problem

The issue arises when we add a new module to be disabled which forces the IModuleDisabler to implement a new method. In that case, you need to remember to update this test to also check that the new method wasn’t being called. If you forget, this test would still pass, but it’d pass for the wrong reason.

To help illustrate, let’s say that another method, DisableImportModule, has been added to the IModuleDisabler interface. In addition, we also need to make sure that this is called when users have partial access, but should not be called for users who are admins or users who have full access.

To fulfill those requirements, we modify the PermissionChecker as so:

At this point, we’d write another test for when the a user has partial access, the import module should be disabled. However, it’s very unlikely that we’d remember to update the test for the admin. Remember, for the admin, we’re checking that it received no calls to any disable methods and the way we’re doing that is by checking each method individually.

Solution

There’s got to be a better way. After some digging around, I found that any NSubstitute mock, has a ReceivedCalls method that returns all calls that the mock received. With this new knowledge, we can refactor the previous test with the following:

This solution is much better because if we add more modules, this test is still checking to make sure that admin users do not have any modules disabled.

Summary

When using a NSubstitute mock and you need to make sure that it received no calls to any methods or properties, you can using NSubsitute’s ReceivedCalls in conjuction with CollectionAssert.IsEmpty to ensure that the substitute was not called.

 

Today I Learned

So today, I was working on a login form at work that has a Username text box and a Password text box. So far, so good. As most things though, there are some business rules to show particular validation errors before the user clicks the login button.

loginForm

When I designed the form originally, I used DevExpress’ WinForm controls to make it a bit more sleek. With that being said, I was also using their validation provider to show inline error messages for the controls.

During the development, I had used DevExpress’s built-in custom validation rules to say that the password could not be empty and if it was, show this particular error message. This worked great, until someone entered a password that was just a space. Then the application crashed because the presenter was asked to do some work with an invalid password.

Looking back at the built-in rule, there wasn’t an easy way to handle a password that consisted of all blanks, but there could 1 or more of them.

After digging around the documentation, it looked like I should create a custom Validation Rule.

So the first step is to create a new class that inherits from ValidationRule

After creating this rule, the next step is to override the Validate method and add my custom validation logic here

Now that the custom rule is defined, I needed to hook it up to the password text box

Because of this, whenever the control is being validated, the control will use the CustomPasswordValidationRule class to run the new logic which is awesome. However, it will use a default error message and icon which is less than ideal.

To get around that, we can set some properties as part of the constructor for the class. I could have set the properties as part of setting up the validation provider, but none of the details change. Either it validates with no problems or it should show this particular error message with this particular error icon.

What’s really great about this solution is that now I can test that the validation logic using unit tests where before I would have had to write UI tests.

Today I Learned

There is nothing new in the world except the history you do not know.
– Harry S. Truman

The more experience I gain problem solving, the more this holds true. For this post, I’m going to first discuss the problem that I was trying to solve. Next, I’ll show what my first solution was, followed by the shortcomings of this solution. Thirdly, we’ll iterate over a better solution to the problem. This in turn, will provide the motivation for what the Chain of Responsibility is and how to implement. Finally, I’ll wrap up with what the benefits were of using this design. To experiment and to see, all code samples can be found at: https://bitbucket.org/TheSoftwareMentor/chain-of-responsibility-pattern.

Problem I was trying to solve

As part of the process of installing our software, there are scripts that will update the database from it’s current version to the latest version. As it stands, it needs to be able to upgrade a database from any version to the current version.

Previous Solution

The first thing that comes to me is that I need to apply database scripts in a sequential way. For example, if the database’s current version is 1.0 and the latest version is 3.0, it would need to apply the script to upgrade the database from 1.0 to 2.0 and then apply the script to upgrade the database from 2.0 to 3.0.

For the first implementation, there were only two versions, 1.0 and 2.0. Since I didn’t want to build in a lot of functionality if it wasn’t needed yet, I created a helper method that returns the correct updater for a given version. In the below code, if the version does not exist, I assume the database does not exist and return the class that will create the database. Otherwise if the version is 1.0, I return a class that is responsible for the upgrading a database from 1.0 to 2.0. If the version is 2.0, I return a class that doesn’t do anything (i.e. there’s no upgrades to be done).

[code language=”csharp”] public IDatabaseUpdater GetDatabaseUpdater(string version)
{
if (string.IsNullOrWhiteSpace(version))
return new DatabaseCreator();
if (version == "1.0")
return new Database100To200Updater();
if (version == "2.0")
return new CurrentVersionUpdater();

throw new ArgumentException("The version " + version + " is not supported for database upgrades.");
}
[/code]

Problem with solution

This solution worked well when there only three possible actions (create a new database, apply the single script, or do nothing). However, we are now going to be shipping version 3.0 and there will need to be a new class that is responsible for upgrading the 2.0 to 3.0. In order to add this functionality, I’d have to do the following:

  1. Create the Database200To300Updater class that contained the logic for updating the database from 2.0 to 3.0.
  2. Modify the Database100To200Updater class to also use the Database200To300Updater in order to perform the next part of the upgrade.
  3. Add additional logic to the above method so that if the database is 2.0 to return the Database200To300Updater class.

After making the modifications, the method now looks like
[code langauge=”csharp” highlight=”6,8″] public IDatabaseUpdater GetDatabaseUpdater(string version)
{
if (string.IsNullOrWhiteSpace(version))
return new DatabaseCreator();
if (version == "1.0")
return new Database100To200Updater(new Database200To300Updater());
if (version == "2.0")
return new Database200To300Updater();
if (version == "3.0")
return new CurrentVersionUpdater();

throw new ArgumentException("The version " + version + " is not supported for database upgrades.");
}
[/code]

So far, so good, we now have the logic to be able to apply scripts in order, however, now that we’ve added version 3.0, I start to wonder what I would do if we added more versions? After some thought, it would look identical to the previous steps (see below for what would happen if we added version 4.0).

[code language=”csharp” highlight=”6,8″] public IDatabaseUpdater GetDatabaseUpdater(string version)
{
if (string.IsNullOrWhiteSpace(version))
return new DatabaseCreator();
if (version == "1.0")
return new Database100To200Updater(new Database200To300Updater(new Database300To400Updater()));
if (version == "2.0")
return new Database200To300Updater(new Database300To400Updater());
if (version == "3.0")
return new Database300To400Updater();
if (version == "4.0")
return new CurrentVersionUpdater();

throw new ArgumentException("The version " + version + " is not supported for database upgrades.");
}
[/code]

If we create some variables to hold onto these classes, and reorder the if statements, we can write this helper method as:

[code language=”csharp” highlight=”8,9,10″] public IDatabaseUpdater GetDatabaseUpdater(string version)
{
if (string.IsNullOrWhiteSpace(version))
return new DatabaseCreator();
if (version == "4.0")
return new CurrentVersionUpdater();

var database300Updater = new Database300To400Updater();
var database200Updater = new Database200To300Updater(database300To400Updater);
var database100Updater = new Database100To200Updater(database200To300Updater);

if (version == "1.0")
return database100Updater;
if (version == "2.0")
return new database200Updater;
if (version == "3.0")
return new database300Updater;

throw new ArgumentException("The version " + version + " is not supported for database upgrades.");
}
[/code]

Motivation for the Chain of Responsibility

What I find interesting in this design is that I’ve now chained these updater classes together so that if the version 1.0 is returned, it will also use the 2.0 updater, which in turn calls the 3.0 updater. It was at this point, that I remembered a design pattern that followed this structure.

In this design pattern, you essentially have Handlers (in my case updaters) that check to see if they can handle the request. If so, they do and that stops the chain. However, if they can’t handle the request, they pass it to their Successor (which was also a Handler) to handle the request. The design pattern I was thinking about is the Chain of Responsibility pattern.

In order to implement this pattern, you need to have an IHandler interface that exposes a Handle method and either a method or property to set the Successor. The method is the action to take (in our case Update) and the Successor represents the next Handler in the chain if the request could not be handled. The second component is referred to as ConcreteHandlers and they are just the implementors of the interface. One way to implement this is like the following:

[code language=”csharp”] public interface IHandler
{
IHandler Successor { get; set; }
void Update(int version);
}

public class ConcreteHandlerA : IHandler
{
public IHandler Successor { get; set; }

public void Update(int version)
{
if (CanTheRequestBeHandled)
{
// handle the request
}
else
{
Successor.Update(version);
}
}
}
[/code]

The main difference between the pattern and what I need is that instead of doing if (canHandle)/else call Successor, what I’m really looking for is to run the upgrade script if the version we’re upgrading to is higher than our current version and then always call the successor. Given this change, here’s what that new implementation looks like:

[code highlight=”11″ language=”csharp”] public class ConcreteHandlerA : IHandler
{
public Successor { get; set; }

public void Update(int version)
{
if (CanTheRequestBeHandled)
{
// handle the request
}
Successor.Update(version);
}
}
[/code]

Implementing the Chain of Responsibility

Now that I know the pattern to use and how it works, I need to update the IDatabaseUpdater interface to follow the IHandler interface. Next, I will need to modify the concrete handlers to use the new interface correctly.

Implementing the Handler

First, we will update our IDatabaseUpdater interface to follow the IHandler look:

Before

[code language=”csharp”] public interface IDatabaseUpdater
{
void Update(int version);
}
[/code]

After

[code language=”csharp”] public interface IDatabaseUpdateHandler
{
void Update(int version);
IDatabaseUpdateHandler Successor { get; set; }
}
[/code]

Implementing the Concrete Handler

Second, we will need to update our concrete handlers to implement the interface correctly and to update their UpdateMethod to follow the design. In my case, the concrete handlers perform similar logic, so one of the classes is used for an example.

Before

[code language=”csharp”] public class Database100To200Updater : IDatabaseUpdater
{
private Database200To300Updater _successor;
public Database100To200Updater(Database200To300Updater successor)
{
if (successor == null)
throw new ArgumentNullException("successor");

_successor = successor;
}

public void Update()
{
Console.WriteLine("Updating the database to version 2.0");
_successor.Update();
}
}
[/code]

After

Thanks to the public property, I was able to remove the private member and that in turn allowed me to remove the constructor.
[code language=”csharp”] public class Database100To200Updater : IDatabaseUpdateHandler
{
public void Update(int version)
{
if (version >= 2)
Console.WriteLine("Updating the database to version 2.0");

if (Successor != null)
Successor.Update(version);
}

public IDatabaseUpdateHandler Successor
{
get;
set;
}
}
[/code]

Updating the Helper Method

Now that we’ve updated the interface and implementors, it’s time to update the helper method to take advantage of the new design.

[code language=”csharp”] public IDatabaseUpdateHandler GetDatabaseUpdater(string version)
{
if (string.IsNullOrWhiteSpace(version))
return new DatabaseCreator();

var database300To400 = new Database300To400Updater();
var database200To300 = new Database200To300Updater();
var database100To200 = new Database100To200Updater();

database100To200.Successor = database200To300;
database200To300.Successor = database300To400;

return database100To200;
}
[/code]

Chain of Responsibility is great, here’s why

What I really like about the chain of responsibility pattern is that I was able to connect my upgrade classes together in a consistent fashion. Another reason why I like this pattern is that it forces me to have the logic to determine wheter I should run the update or not inside the individual classes instead of the helper method. This produces more readable code which then lends itself to easier maintainability.

Today I Learned

During this past week, I was working with our intern and showing him some cool debugging tricks when I came across a massive method. I gave him 30 seconds to look at it and tell me what he thought the method was doing. After a while, he was able to figure it out, but the fact that it wasn’t easy discernible was enough to give pause.

The lesson here is that if you can’t determine what the method is doing easily, then it’s probably doing way too much (violating the Single Responsibility Principle) and needs to be broken into more easily readable pieces.

To demonstrate what I mean, I wrote a program that inserts Messages into a database. A Message contains a description, the number (for identification when troubleshooting) and the module. We would have issues where different messages would have the same number which would cause confusion when troubleshooting errors.

In the program I wrote, the user provides the message and what module the message belongs to and the program automatically generates the message number and inserts the message into the database.

For brevity’s sake, shown below is the logic for determining what the next message number should be.

[code language=”csharp”] public int GetNextAlertAndErrorModuleNumber(string module)
{
if (String.IsNullOrEmpty(module))
throw new ArgumentException("module cannot be null or empty");

if (_connection == null)
_connection = CreateConnection();

var results = new List<int>();

_connection.Open();
var cmd = new SqlCommand("dbo.GetAlertData", _connection);
cmd.CommandType = CommandType.StoredProcedure;

var reader = cmd.ExecuteReader();
while (reader.Read())
{
if (!reader["ALERT_ID_NUMBER"].ToString().Contains(module))
continue;

var pieces = reader["ALERT_ID_NUMBER"].ToString().Split(‘ ‘);

results.Add(Int32.Parse(pieces[1]));
}
if (reader != null)
reader.Close();

cmd = new SqlCommand("dbo.GetErrorData";, _connection);
cmd.CommandType = CommandType.StoredProcedure;

reader = cmd.ExecuteReader();
while (reader.Read())
{
if (!reader["ERROR_ID_NUMBER"].ToString().Contains(module))
continue;

var pieces = reader["ERROR_ID_NUMBER"].ToString().Split(‘ ‘);

results.Add(Int32.Parse(pieces[1]));
}
if (reader != null)
reader.Close();

if (_connection != null)
_connection.Close();

return results.Max() + 1;
}
[/code] The method itself isn’t complex, just calling some stored procedures, parsing the output and adding the number to a list. However, it’s not abundantly clear what the purpose of the calling the stored procedures.

First, it looks like we’re reading the alerts error numbers from a stored procedure call, why don’t we extract that logic out to a helper method and have the public method call the helper?

[code language=”csharp” highlight=”12, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55″]

<pre>
public int GetNextAlertAndErrorModuleNumber(string module)
{
if (String.IsNullOrEmpty(module))
throw new ArgumentException(&amp;amp;quot;module cannot be null or empty&amp;amp;quot;);

if (_connection == null)
_connection = CreateConnection();

var results = new List<int>();

_connection.Open();
results.AddRange(ReadAlerts(module.ToUpper()));

var cmd = new SqlCommand("dbo.GetErrorData", _connection);
cmd.CommandType = CommandType.StoredProcedure;

var reader = cmd.ExecuteReader();
while (reader.Read())
{
if (!reader["ERROR_ID_NUMBER"].ToString().Contains(module))
continue;

var pieces = reader["ERROR_ID_NUMBER&"].ToString().Split(‘ ‘);

results.Add(Int32.Parse(pieces[1]));
}
if (reader != null)
reader.Close();

if (_connection != null)
_connection.Close();

return results.Max() + 1;
}

private List&amp;amp;lt;int&amp;amp;gt; ReadAlerts(string module)
{
var results = new List<int>();
var cmd = new SqlCommand("dbo.GetAlertData", _connection);
cmd.CommandType = CommandType.StoredProcedure;

var reader = cmd.ExecuteReader();
while (reader.Read())
{
if (!reader["ALERT_ID_NUMBER"].ToString().Contains(module))
continue;

var pieces = reader["ALERT_ID_NUMBER"].ToString().Split(‘ ‘);
results.Add(Int32.Parse(pieces[1]));
}
if (reader != null)
reader.Close();

return results;
}
[/code]

By doing this, we fix two issues at once. First, we’ve given a name to the process of reading the alerts which in turns allows us to quickly understand what the public method should be doing (i.e. improved readability).

Second, it allows us for easier debugging because we now have smaller components. For example, let’s say that we were getting the wrong value. In the first implementation, we would have to put breakpoints in different areas trying to determine which part was broken. However, in the new form, we can check to see if ReadAlerts is behaving correctly. If it isn’t, we now know the bug has to be in that method, otherwise, it’s in the rest.

For the next step, you may have noticed that we can repeat the same refactoring trick again, except this time, we can extract the logic for reading the errors into a helper method.

[code language=”csharp” highlight=”11,12″] public int GetNextAlertAndErrorModuleNumber(string module)
{
if (String.IsNullOrEmpty(module))
throw new ArgumentException("module cannot be null or empty");

if (_connection == null)
_connection = CreateConnection();
_connection.Open();

var results = new List<int>();
results.AddRange(ReadAlerts(module.ToUpper()));
results.AddRange(ReadErrors(module.ToUpper()));

if (_connection != null)
_connection.Close();

return results.Max() + 1;
}

private List&amp;amp;lt;int&amp;amp;gt; ReadAlerts(string module)
{
var results = new List<int>();
var cmd = new SqlCommand("dbo.GetAlertData", _connection);
cmd.CommandType = CommandType.StoredProcedure;

var reader = cmd.ExecuteReader();
while (reader.Read())
{
if (!reader["ALERT_ID_NUMBER"].ToString().Contains(module))
continue;

var pieces = reader["ALERT_ID_NUMBER"].ToString().Split(‘ ‘);
results.Add(Int32.Parse(pieces[1]));
}
if (reader != null)
reader.Close();

return results;
}

private List&amp;amp;lt;int&amp;amp;gt; ReadErrors(string module)
{
var results = new List<int>();
var cmd = new SqlCommand("dbo.GetErrorData", _connection);
cmd.CommandType = CommandType.StoredProcedure;

var reader = cmd.ExecuteReader();
while (reader.Read())
{
if (!reader["ERROR_ID_NUMBER"].ToString().Contains(module))
continue;

var pieces = reader["ERROR_ID_NUMBER"].ToString().Split(‘ ‘);
results.Add(Int32.Parse(pieces[1]));
}
if (reader != null)
reader.Close();

return results;
}
[/code]

After the changes, anyone who looks at the public API can easily see that it’s reading from both Alerts and Errors. This is really powerful because now you can communicate with non-technical people about requirements and what the code is doing.

Let’s say that in the future, the requirements change and this conversation plays out:

Alice (QA, finding an error) – Hey Bob, I was running one of our test plans and it looks like that we’re getting the wrong message number if we’re trying to add a new message and there are warning messages in the database. We are including the warning table when figuring that out, right?

Bob (Engineer, finding the root cause) – Hey you’re right, it looks like we’re only using the alerts and error tables when calculating the next number. Why don’t we write up a ticket for that and get a fix in?

The key point is that no matter how large a method is, there always have to be steps being performed in some order (by definition of an algorithm) and this is the key to refactoring larger methods into more maintainable pieces of code. The trick is determining what those steps are and making decisions on whether to make helper methods or helper classes.

If those steps become complicated, then they should be broken out into helper methods. As time progresses and those helper methods start to become more complicated, then those helper methods should in turn become classes of their own.

Today I Learned

When working in a start-up, without fail, there’s always a need for a way to install our desktop application onto customer machines in a quick/painless fashion. This in itself requires quite a bit of research (Is xcopy enough or what tool do I need to use?) and in typical fashion you need to make a decision without knowing all of the requirements.

The biggest problem with current installer technologies is that it takes a significant amount of work to get one going. Going with a scripting solution like NSIS or WiX and it’s your first time? Be prepared for the steep learning curve for both (and in the meantime you don’t have a lot to show for your work). Using an installer wizard like InstallShield? Be prepared for both the steep learning curve and the massive amount of documentation on how to create the installer.

What I really needed was some tool that could easily make an installer for me without many bells and whistles just so that management could use the tool and give feedback. In short, I was really looking for a minimum viable installer.

After digging around, I found Excelsior, a very easy to use tool for creating installer packages. It’s not the easiest thing to automate, but for creating one-time installers or creating them infrequently, this tool is fantastic.

Getting Started With Excelsior

After installation, go ahead and start Excelsior and click “New”.

In general, the flow for creating the installer is to first choose which files need to be included in the installer. In this application, it’s pretty easy to specify which files to copy:

excelsior_step_1

On the next screen, you can specify the names of the company and product, the version, and (in my opinion, the most useful), shortcuts for your application.

excelsior_step_2

On the third screen, you can add the End User License Agreement (EULA), a splash screen, and custom actions that can be ran after install (updating configuration files or launching other applications). In this screenshot, I added a EULA and an always run action to start the installed application.

excelsior_step_3

After finishing up that screen, save the installation package files and let Excelsior create the installer. By saving the installation package files, Excelsior has a way to track if there’s a new version of the file in the directory you specified earlier. So auto-updating files is great, however, there’s not a way for it to determine if new files should be added or not.

Pros

  • Simple to get started with (stumbling on my own, I was able to get an installer up and running within an hour).
  • Has a decent amount of customization (shortcuts, EULA, custom actions).
  • Can export to NSIS so can incorporate installer creation into continuous integration.
  • Free for personal and commercial use, have another product, Excelsior Delivery that has a few more bells and whistles that costs about $100.

Cons

  • Does not support custom screens out of the box. For example, I needed a screen that would allow the end user to specify a SQL Server database instance. Excelsior cannot do this in the UI, but can be done by coding it in NSIS.
  • There’s not a whole of documentation on the product so it’s usually faster to stumble around in the application looking for a feature.
  • There’s no way for it to automatically add files to the list, however, it can auto-update already added files.