.NET MVC's Best Practices for Keeping a Controller Very Unreadable

I’ve been developing my first large (for me) MVC project for a couple of months, and everything becomes extremely difficult to navigate.

I refuse refactoring and I am looking for modern examples of "best practices" because your controller is thin and moves all this data into your models.

I am reading this article , which discusses things in detail, but does not provide an example project.

The most popular "best practices" topics posted here tend to be linked to the MVC Music Store or the Nerd Dinner project, but at the same time, comments tend to say they are more of a "beginner's guide" rather than examples of "best practices" "

Does anyone know of any modern open source MVC projects that demonstrate the right development structure?

note: a typical problem I would like to study: my controllers are very long and full of code that controls the website - I need to translate this code into methods that the controller simply refers to. Where can I use all of these methods?

Here is an example of my code from a controller, as suggested by a comment on one of the answers. How to move some of this information into my ViewModel? (I have included 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; } } } 
+6
source share
3 answers

What you are mostly looking for is multi-level architecture. For example, the Service Layer pattern requires that you define a lot of logic at the service level, and not in your controllers.

There are examples of this, one of which is Silk from the Pattern and Practices team at Microsoft: http://silk.codeplex.com/

+6
source

When you say that your controllers are β€œlong and full of code,” does that mean ALL your code is in the controller? If so, you need to wrest out most of the logic in support for the ViewModel classes.

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

UPDATE:
I would take all the logic from your Action and move it to the ViewModel method, which takes an int for an ID. Now your controller action method is one line:

 return View(MyViewModel.AttendanceView(id)); 

This is a simplified example, there are more advanced ideas.

+3
source

Does anyone know of any modern open source MVC projects that demonstrate the right development structure?

Unfortunately not. All the projects that I have seen so far are not very suitable for beginners to start learning. Not because they contain bad code, but because of their complexity.

A typical problem I would like to study: My controllers are very long and full of code that controls the site - I need to translate this code into methods that the controller simply refers to. Where can I use all of these methods?

If your controllers contain many lines, then you are doing it wrong. You need to learn about the separation of problems and how to write clean code (and what it means). For example, never write code to retrieve anything from a database in your controller. This action refers to the database access level, logically divided into several classes. Learn about principles such as Don't Repeat Yourself, etc.

There is much to discuss about how to write good code, and I'm not sure if this can be done here. There are whole books discussing this issue, but I hope I have given you at least a few useful pointers to get you started.

+2
source

Source: https://habr.com/ru/post/916801/


All Articles