Friday, May 29, 2009

A New Look

I decided to go with a slightly new look, I kept the colors the same, but I removed the fixed width of the content to make it easier to display source code and I changed how source code is displayed by adding a border around it and by adding auto scrolling.

Hopefully this will make reading this a little easier.

Sunday, May 17, 2009

Development Heros

I pose the question, who is your development hero? Who has done the most to enlighten and inspire you about software development?

There are many rock star's in our industry Martin Fowler, Uncle Bob Martin, Scott Hanselman, etc. While they write the books and blogs we read and are in the pod casts we listen to and the screen casts we watch, are they truly who inspire us? Or is it closer to home, a mentor or collage professor maybe? For me the answer is simple the person who showed me how to use my first computer 25 years ago. Nine years later he gave me my first programing book (Terbo Pascal 4.5). Since then he has been an endless source in information, a solid sounding board for ideas, and a guild though the world of software design. While we haven't always agrees on everything, see drinking the koolaid, he introduced me to N-tier design, Inversion of control, and why interfaces are so important. While I may have learned more about the specifics from others, it was his guidance that showed me the way. While this may sound like the rantings of a fan boy, I think that it's OK, he may not have been a rock star in our industry, but his simple lessens and guidance let me down the path to good software design, and even at the age of 30 I'm was still inspired by my older brother, may he rest in peace Benjamin Clements 1974 - 2009

Thursday, May 7, 2009

The Provider Factory Pattern

At our weekly tech lunch we watched Jean-Paul Boodhoo on Demystifying Design Patterns Part 1 on dnrtv.com and he showed us how to use the "The Provider Factory Pattern" which is basically using a factory to give a data provider that is completely abstracted away from the database request methods that use it.

This is some very cool stuff basically you have a factory that provides a methods for getting a connection
public IDbConnection GetConnection()
{
IDbConnection connection = _frameworkDBProviderFactory.CreateConnection();
connection.ConnectionString = _authenticationSettings.ConnectionString;
return connection;
}

though an interface so you can call any type of Database who's connection object implements the IDbConnection interface (SQLServer, MySQL, Oracle, etc.) (see full source)

To use this you create a request method
public bool AuthenticateUser(string userName, string password)
{
bool success = false;
DataTable requestTable = new DataTable();
using (IDbConnection conn = UserDataProvider.GetConnection())
{
conn.Open();
IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "select UserName, Password from users where UserName = @Username and Password = @Password";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(UserDataProvider.AddParameterWithValue("@UserName",userName));
cmd.Parameters.Add(UserDataProvider.AddParameterWithValue("@Password", password));
IDataReader reader = cmd.ExecuteReader();

using (reader)
{
requestTable.Load(reader);
}
}
if (requestTable.Rows.Count==1 )
{
success = true;
}
requestTable.Dispose();
return success;
}

The request method can open this connection, pass in your ANSI SQL query, parameters, etc. then execute it. (see full source)

The Method doesn't know or care what kind of data base it's talking to, additionally starting in the 2.0 framework you can pull the type of Database from the connection string configuration so all you have to do is have the database client lib included in your project and your good to go.

Friday, April 17, 2009

Continues Integration with Team City

At Netdug last night the subject was continues integration with Team City, so I finally got the opportunity to see a continues integration server in action. As a demonstration the presenter had RoboDojo (created by David Star of Elegant Code) David committed a change to his project to on codeplex and with in a few minutes the project built, ran his tests, etc.

Inspired by this my project tonight was to set up Team City. The Installation was fairly simple, I set up my Sample Project, using the sln2008 builder output to a virtual directory in IIS so I can check the output for both the webforum and webservices. Everything just worked until I tried to get the mstest to run the unit tests. It kept failing to run the unit tests, but didn't tell me.

This bothered be a little, after searching online for a while I couldn't find anything but how to do it with msbuild, or with one of the other builders. Finally after playing around with the setting I found that I could get the mstests to run if I used an older nunit test runner that supported both nunit and msunit, and honestly I really like doing that way I only have one place to add test projects.

So what I was expecting to be a 30-60min project tuned into a 3 hour project, but in the end, it really wasn't that bad, the only major complaint I have now is that Team city is kind of a memory hog (it's written in java, go figure) so I don't think I can really run it on the same box I do development on, but it wasn't designed to.

My eventual goal is to do continues integration at work, but I think that's going to be a hard sell right now, so I'm starting off with built deployments, this way I can show the benefits of doing this, then work on getting continues integration. Now that I have the proof of concept done, I can give a realistic time line on how long it would take to set this up at work.

Monday, April 13, 2009

Rolling your own Mock Objects

One of the problems I have with doing unit testing is the mock objects. the principal is simple enough, I create an object that implements the interface I'm trying to mock, the only problem is I end up creating a least 2+ mocks for every class I'm trying to test. I could use something like Rino mocks but there may be some company policy against using open source, or you just don't want to keep it as simple as passable.

Talking to another developer about this he pointed all I needed to do was to give my mock objects some state to handle what passes or fails; BRILLIANT!! why didn't I think of that. So I took the base class

I added 2 collections:
  1. A Collection of string to handle messages added by the various mocked methods to tell the unit test what has occurred
  2. A second collection of strings to to tell the mock to fail and on what methods based on messages passed to the mock object
public class BaseTestRequest
{
public Collection Messages { get; set; }
public Collection FailOn { get; set; }
private string _currentMessage = string.Empty;

public string CurrentMessage
{
get { return _currentMessage; }
set
{
_currentMessage = value;
Messages.Add(value);
}
}

public BaseTestRequest()
{
Messages = new Collection();
FailOn = new Collection();
}

public bool WasSuccessful()
{
return (!FailOn.Contains(_currentMessage));
}
}
Then I can mock an object like this
public class TestAuthenticationRequests : BaseTestRequest, IAuthenticationRequests
{
public bool AuthenticateUser(string userName, string password)
{
CurrentMessage ="AuthenticateUser";
return WasSuccessful();
}
}

to see the full code with comments check out this Sample Code from the sample project.