Hear my dulcet tones on the AuthorsCast podcast

by Scott MIllett 16. February 2011 20:09

I was recently interview about my ASP.NET Design Patterns book for AuthorsCast.com, here what they say about the episode... 

"In this episode, Scott talks about how his book builds on the existing software design literature, clears up the confusion between design patterns and design principles, explains how to quickly become proficient at applying patterns, and expounds on the benefits of creating your own ASP.NET framework. Along the way, he offers pointers on becoming a better developer, tells how he learned to embrace the power of positive thinking, and waxes philosophical on the problem with American beer."

You can also get it on iTunes.

I was a bit nervous so please forgive my mumbling answers!

Keep your head down son
Scott

Tags: , ,

ASP.NET MVC | Design Patterns

ASP.NET MVC- Single Responsibility Principle and Tall Controllers

by Scott MIllett 11. February 2011 09:01

In order to keep my fat controllers thin I ensure that they are only concerned with a single responsibility, i.e. handling the coordination of my UI logic. To achieve this my controller delegates to other classes to actually perform tasks and retrieve data. However, even though I can keep my ActionResult methods thin and highly cohesive I can still end up with a very tall controller that is handling the coordination of too much UI logic. Let me show you what I mean.

I often see controllers like the class below:

public class CustomerController : Controller
{
    public ActionResult Details()
    {
        //....
    }

    public ActionResult Edit()
    {
        //....
    }

    [HttpPost]
    public ActionResult Edit(EditCustomerViewModel edit_customer_view_model)
    {
        //....
    }

    public ActionResult AddDeliveryAddress()
    {
        //....
    }

    [HttpPost]
    public ActionResult AddDeliveryAddress(AddCustomerAddressViewModel add_customer_address_view_model)
    {
        //....
    }


    public ActionResult EditDeliveryAddress()
    {
        //....
    }

    [HttpPost]
    public ActionResult EditDeliveryAddress(EditCustomerAddressViewModel edit_customer_address_view_model)
    {
        //....
    }
}

This controller is concerned with everything to do with managing a customer, and even though it may have thin and cohesive ActionResult methods it can quickly become large and difficult to work with as it is handling too many UI tasks.

A better way I have found to tackle this issue is to create a controller class per business UI task or concern. So in the case of the tall controller shown above I would instead split this into multiple controllers, having one for editing a customer, one for displaying a customer and so on as shown in the following listing.

The CustomerDisplayController class is concerned with UI tasks to display a customer.

public class CustomerDisplayController : Controller
{
    public ActionResult Details()
    {
        //....
    }
}

The CustomerEditController class is concerned with UI tasks to edit a customer.

public class CustomerEditController : Controller
{
    public ActionResult Edit()
    {
        //....
    }

    [HttpPost]
    public ActionResult Edit(EditCustomerViewModel edit_customer_view_model)
    {
        //....
    }
}

The AddDeliveryAddressController class is concerned with UI tasks that add an address for a customer.

public class AddDeliveryAddressController : Controller
{
    public ActionResult AddDeliveryAddress()
    {
        //....
    }

    [HttpPost]
    public ActionResult AddDeliveryAddress(AddCustomerAddressViewModel add_customer_address_view_model)
    {
        //....
    }
}

The EditDeliveryAddressController class is concerned with UI tasks that edit a customers address.

public class EditDeliveryAddressController : Controller
{
    public ActionResult EditDeliveryAddress()
    {
        //....
    }

    [HttpPost]
    public ActionResult EditDeliveryAddress(EditCustomerAddressViewModel edit_customer_address_view_model)
    {
        //....
    }
}

You probably figured this out for yourself but I thought I may as well jot it down for future reference :0)

 

Keep on rockin
Scott

Tags: ,

ASP.NET MVC | Design Patterns

About Me

Hello my name is Scott Millett, this is my blog designed to capture my thoughts on development. You can find out more info on me by checking out my linked in profile below.

View Scott Millett's profile on LinkedIn

Coming soon...

Awards

MVP Logo

ASP.NET MVP
2010-2011

Month List

Powered by BlogEngine.NET 2.0.0.36 - Eco Theme by n3o Web Designers