几个月来,我一直在开发我的第一个大型(对我来说)MVC项目,事情变得非常难以驾驭。
我一直在重构方面懈怠,并正在寻找"最佳实践"的现代示例,以保持控制器的精简并将所有这些数据移动到模型中。
我阅读了这篇文章,其中详细讨论了事情,但没有提供示例项目。
这里发布的大多数"最佳实践"主题倾向于链接到MVC音乐商店或书晚餐项目,但与此同时,评论倾向于说它们更像是"初学者指南",而不是"最佳实践"的例子。
有谁知道任何最新的开源MVC项目展示了适当的开发结构?
注意:我想学习解决的一个典型问题:我的控制器很长,充满了驱动网站的代码 - 我需要将此代码移到仅由控制器引用的方法中。 我把所有这些方法放在哪里?
下面是来自控制器的代码示例,由对其中一个回复的评论所建议。 如何将其中一些信息移动到我的视图模型? (我在下面包含了视图模型):
控制器:
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);
}
视图模型:
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;
}
}
}
你基本上在寻找的是一个分层架构。例如,服务层模式要求您在服务层而不是控制器中定义大量逻辑。
有这样的例子,其中之一是来自Microsoft的模式与实践团队的Silk:http://silk.codeplex.com/
当你说你的控制器"很长而且充满了代码"时,这是否意味着你的所有代码都在控制器中? 如果是这种情况,则需要将大部分逻辑分解为支持 ViewModel 类。
我通常将大部分(如果不是全部)代码放在 ViewModel 类中,每个视图/控制器一个。 所有逻辑都从 ViewModel 中呈现,因此每个控制器操作运行一行,也许两行代码(在合理范围内)。
更新:
我会从您的操作中删除所有逻辑,并将其移动到一个 ViewModel 方法中,该方法采用 int 作为 ID。 现在,控制器操作方法为一行:
return View(MyViewModel.AttendanceView(id));
这是一个简单的例子,那里有更先进的想法。
有谁知道任何最新的开源 MVC 项目展示了正确的开发结构?
不幸的是没有。到目前为止,我看到的所有项目都不太适合初学者开始学习。不是因为它们包含糟糕的代码,而是因为它们的复杂性。
我想学习解决的一个典型问题:我的控制器很长,充满了驱动网站的代码 - 我需要将这些代码移动到仅由控制器引用的方法中。我把所有这些方法放在哪里?
如果您的控制器包含许多行,那么您做错了。您需要了解关注点的分离以及如何编写干净的代码(以及它的含义)。例如,永远不要编写代码来从控制器中的数据库中检索某些内容。这样的动作属于数据库访问层,逻辑上进一步分为多个类。了解"不要重复自己"等原则。
关于如何编写一个好的代码,有很多东西要讨论,我不确定是否可以在这里完成。有一整本书都在讨论这个问题,但我希望我至少给了你一些有用的指示。