.NET MVC Best practices as far as keeping the controller from being very unreadable

Ecnalyr picture Ecnalyr · May 27, 2012 · Viewed 9.5k times · Source

I have been developing my first large (for me) MVC project for a couple of months now and things are getting exceedingly difficult to navigate.

I have been slacking off on refactoring and am seeking modern examples of 'best practices' as far as keeping your controller thin and moving all of that data to your models.

I read this article that discusses things in detail, but does not provide an example project.

Most 'best practices' threads posted here tend to link to MVC Music Store or the Nerd Dinner project, but at the same time the comments tend to say they are more 'beginner's guides' rather than examples of 'best practices.'

Does anyone know of any up-to-date open-source MVC projects that demonstrate proper development structure?

note: A typical problem that I'd like to learn to resolve: My controllers are very long and full of code that drives the website - I need to move this code off into methods that are merely referenced by the controller. Where do I throw all of these methods?

Here is a an sample of my code from a controller as suggested by a comment on one of the replies. How would I move some of this information over to my ViewModel? (I have included the ViewModel below):

Controller:

public ActionResult AttendanceView(int id)
{
    //
    // Generates list of Attendances specifically for current Course
    var attendanceItems = db.Attendance.Where(s => s.CourseID == id);
    List<Attendance> attendanceItemsList = attendanceItems.ToList();
    // End of generating list of Attendances

    //
    // Generates list of Students in alphabetical order sorted by LastName
    var student = attendanceItemsList.Select(a => a.Student).Distinct().OrderBy(s => s.LastName);
    List<Student> StudentList = student.ToList();
    // End of generating list of Students


    //
    // Generates list of AttendingDays specifically for current Course
    Course course = db.Courses.FirstOrDefault(p => p.CourseID == id);
    List<int> attDayList = new List<int>();
    for (int i = 0; i < course.AttendingDays; i++)
    {
        attDayList.Add(i + 1);
    };
    // End of generating list of AttendingDays

    AttendanceReportViewModel model = new AttendanceReportViewModel
    {
        AttendanceDays = attDayList,
        Students = StudentList,
        Attendances = attendanceItemsList,
        courseId = id
    };
    return View(model);
}

ViewModel:

namespace MyApp.ViewModels
{
    public class AttendanceReportViewModel
    {
        public List<int> AttendanceDays { get; set; }

        public List<Student> Students { get; set; }

        public List<Attendance> Attendances { get; set; }

        public int courseId { get; set; }

        public string IsPresent(Student student, int attendanceDay)
        {
            return Attendances.Single(a => a.StudentID == student.StudentID && a.AttendanceDay == attendanceDay).Present ? MyAppResource.Present_Text : MyAppResource.Absent_Text;
        }
    }
}

Answer

Leon Cullens picture Leon Cullens · May 27, 2012

What you're basically looking for is a layered architecture. For example the Service Layer pattern requires you to define a lot of the logic in the service layer instead of in your controllers.

There are examples of this, one of them being Silk from the Pattern & Practices team at Microsoft: http://silk.codeplex.com/