08 November 2015
In a previous post I talked about the importance of good naming when writing code. The title of that post is “Don’t Make Me Think… About Your Code”. This post will talk about meaningful classes and can be considered as an addition to the “writing code” series.
In an ASP.NET MVC project, suppose there is a controller method that accepts and handles a FormCollection
class like this:
public ActionResult MyMethod(FormCollection formCollection)
{
string name = formCollection["Name"];
string email = formCollection["Email"];
string message = formCollection["Message"];
// do something with name, email, message.
}
In an ideal situation, a strongly-typed model should be used so that the framework can handle the model binding.
Say that it’s not possible to use a strongly-typed model. How can we make the code above better?
In the code above we can see that individual form items are being assigned to variables, which are then used later in the code. This parsing of the form also happens in the controller action.
The main improvement that could be done is to create a different class to encapsulate the variables and to move the parsing to that class. Something like this:
public class MyContactForm
{
private MyContactForm() { }
public string Email { get; private set; }
public string Message { get; private set; }
public string Name { get; private set; }
public static MyContactForm Create(FormCollection formCollection)
{
if (formCollection == null) throw new ArgumentNullException("formCollection");
return new MyContactForm
{
Email = formCollection["Email"],
Message = formCollection["Message"],
Name = formCollection["Name"]
};
}
}
And the usage:
public ActionResult MyMethod(FormCollection formCollection)
{
var myContactForm = MyContactForm.Create(formCollection);
// do something with myContactForm
}
Here are the advantages of this approach:
There are other benefits as well, such as the potential of reusability. However, the benefits listed above are enough to justify the effort of creating a new class. That is to say, even if the new class is never actually re-used, it should still get created.
In this post we looked at creating a meaningful class that provides a better encapsulation / responsibility / naming experience. This kind of effort should be done even in the absence of the potential for reuse.
If you like this post you may be interested in my previous post about Centralizing Session Access.