There’s no I in Interface. Oh, wait…

An entry about arcitechture | inversion of control Publication date 7. September 2008 13:59

Last month, BDD founder Dan North visited Bergen to talk at their local NNUG event. I didn’t have a chance to attend (Bergen is a bit far; damn these Norwegian mountains :p), but I’ve talked to a few people who did and apparently it was a really good event. Jon Torresdal wrote on his blog that Dan North talked about this idea of giving the “I” prefix commonly found in C# interface names meaning:

“Dan said, why not use the I for something meaningful? Ask the interface the question: What do you do? And the interface says: ISendEmail or ISearchFiles or IPingComputers.”

I think this is a great idea. Doing this not only makes your code more expressive – it also helps you enforce the Single Responsibility Principle because it makes the concern of any given class very explicit:

public class WebFormsAuthenticator : IAuthenticateUsers
{
    public bool Authenticate(string username, string password)
    {
        return Membership.ValidateUser(username, password);
    }
}

And this not only works for any classes implementing the interfaces, but also those depending on them – it becomes especially visible if you’re using some form of dependency injection:

[Authorize(Roles="Administrator")]
public class UsersController : Controller
{
    private readonly IManageUsers _users;
    private readonly IAuthenticateUsers _authenticator;
 
    public UsersController(IManageUsers users, IAuthenticateUsers authenticator)
    {
        _users = users;
        _authenticator = authenticator;
    }
 
    /** snip **/
}

In the above example for instance, the fact that our UsersController seems to be concerned with both the managing and authentication of users is an in-your-face indication that it is possibly talking on too much responsibility (I say possibly, because it might very well be OK for it to depend on both).

I’m using this naming style in an ASP.NET MVC project I’m current working on to see how it holds up on a larger scale; so far it looks promising.

Currently rated 4.3 by 3 people

  • Currently 4.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Powered by BlogEngine.NET 1.4.5.0

Welcome!

My name is Fredrik Kalseth, and this is my blog - thanks for visiting! I am fortunate enough to work with what I love for a living, and this blog is essentially the biproduct of that.

I work as a senior consultant for Capgemini, and am also an active participant in the Norwegian .NET community, as an avid attendee but also as a speaker (most recently at NNUG and MSDN Live).

As a developer, I have a wide circle of interest. My primary passion is for agile, test-driven development, with focus on best practices and clean code. That said, I also love to work on the frontend, especially with web development.

On Twitter? My handle is fkalseth. On LinkedIn? I`m there too.

NDC 2010

The conference to attend this summer happens June 16th-18th in Oslo, Norway. Are you going? Be sure to catch my talk on AOP while you're there!

 

Disclaimer

This is a personal blog; any opinions expressed here are my own and do not necessarily reflect those of my employer. All content herein is my own original creation, and as such is protected by copyright law. Unless otherwise stated, all source code posted on this blog is freely usable under the Microsoft Permissive License.

What Readers Talk About

Comment RSS