Question

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;
        }
    }
}
Was it helpful?

Solution

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/

OTHER TIPS

When you say your controllers are "long and full of code," does this mean ALL of your code is in the controller? If that is the case, you need to break out most of the logic into supporting ViewModel classes.

I usually put most (if not all) of my code in ViewModel classes, one per View/Controller. All logic is surfaced from the ViewModel, such that each Controller action runs one, maybe two lines of code (within reason.)

UPDATE:
I would take all the logic out of your Action and move it into a ViewModel method that takes an int for the ID. Now your Controller action method is one line:

return View(MyViewModel.AttendanceView(id));

This is a simplistic example, there are more advanced ideas out there.

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

Unfortunately no. All projects I've seen so far aren't very suitable for a beginner to start learning with. Not because they contain a poor code, but because of their complexity.

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?

If your controllers contain many lines, then you're doing it wrong. You need to learn about the separation of concerns and how to write a clean code (and what does it mean). For instance, never write a code to retrieve something from a database in your controller. Such action belongs to a database access layer, logically further divided into multiple classes. Learn about the principles like "Don't repeat yourself" etc.

There's much to discuss on how to write a good code and I'm not sure whether it can be done here. There are whole books discussing this matter, but I hope I gave you at least some useful pointers to start with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top