You should unit test your controller, NOT!

Unit Testing MVC

Unit testing per se is a controversial topic, what you should unit test is another controversial topic. The topic of this post started by a short conversation during a .NET London Group meeting back in 2010, I thought about it and then gave it more thoughts, I started using it in my projects, then now I have enough experience to share my thoughts. Here is my opinion, if you agree or disagree, then please do comment.

Read More

Three Steps to Get Fat-Razor MVC Views on Diet

Fat Razor in MVC

Let me first define the terms that I will be referring to:

Presentation Code: Is the minimal code in a view that is necessary to display visual elements, but does not take any business decision. This is a fictional simple example:

// Business Logic Code. This code understands how the business works
// (don't do this)
if (Model.IsMonthly && !Model.IsPaid && Model.Payment >= 12 && ...)
{
  <p>Some warning message</p>
}

// Presentation Code. This code is only concerned with whether to
// show or hide a UI element.
if (Model.ShouldWarn)
{
  <p>Some warning message</p>
}

Fat-Razor: I’ve coined this term to define ASP.NET Razor views with a lot of Razor/C# code that is not presentation code. This is about the Razor/C# code specifically and not about HTML/CSS/JS.

Model: This is an overloaded term. However, in this context, it is an object of data representation such as a record in the database.

Viewmodel: They are data transfer objects that are meant to be view-specific and to carry exactly what the view needs, no less and no more. Generally, viewmodels are non-reusable and each vm is meant to to be tightly coupled to one view, however, there are exceptions depending on what you are doing.

To give you a flavour of a model and a viewmodel:

// An example of a model
public class Person
{
    public Guid PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public short YearOfBirth { get; set; }
    public DateTime LastAuth { get; set; }
    // This might represent a one-to-many db relationship
    public Order[] Orders { get; set; }
}

// An example of a viewmodel
public class ProfileViewModel
{
    // Doesn't necessarily need an id

    // Combined first and last name
    public string Name { get; set; }
    // Calculated from YearOfBirth
    public short Age { get; set; }
    // E.g. "One day ago" and constructed from LastAuth
    public string LastSeen { get; set; }
    // Calculated by projections on Orders
    public short NumberOfAllOrders { get; set; }
    public short NumberOfUndeliveredOrders { get; set; }
}

Read More

Using C# .NET User Defined Functions (UDF) in Excel

Excel auto complete showing functions

N.B.Throughout this post I am using Excel 2010 and Visual Studio 2010.

Writing a UDF in VBA to be exposed to Excel cells is straight forward, just write the function in a VBA module and Bob’s your uncle. However, it is slightly trickier to expose your functions to Excel in a managed language, such as C# or F#.

Read More

PayPal Charity Hack 2010 and JustGiving REST APIs

Adam Tibi at Paypal Charity Hack 2010 speaking to the audience
Speaking to the audience about JustGiving's RESTful APIs

Charity Hack is an annual event held and sponsored by PayPal UK at their venue in Richmond, London on a Saturday and a Sunday. It is an event where developers from different backgrounds are invited and introduced to different charity-relaled organisations APIs where developers are encouraged to hack useful apps using these APIs.

We introduced, as JustGiving, our new RESTful APIs and demonstrated how to use them.

Adam Tibi Paypal Charity Hack 2010 speaking to the developers (back to camera)
Having a discussion with JustGiving developers (back to camera)