这是我的一个控制器中的操作 - 我在整个项目的控制器中有类似的大型方法。
我正在尝试学习在哪里放置这些东西以及如何清理代码。我刚开始做这个,如果我看到一个好的例子来改变我的一些代码,它很可能会教我如何处理大量的代码。
这是我的操作:
我该如何使上述代码更易读?我应该将其中的部分提取为单独的方法并将它们移至控制器底部吗?还是将这些方法放在另一个文件中,并在此引用它们? 请记住,我正在学习,希望可以更加清晰易懂。
我正在尝试学习在哪里放置这些东西以及如何清理代码。我刚开始做这个,如果我看到一个好的例子来改变我的一些代码,它很可能会教我如何处理大量的代码。
这是我的操作:
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.TitleSortParm = String.IsNullOrEmpty(sortOrder) ? "Title desc" : "";
ViewBag.CreditsSortParm = sortOrder == "Credits" ? "Credits desc" : "Credits";
ViewBag.ElectiveSortParm = sortOrder == "Elective" ? "Elective desc" : "Elective";
ViewBag.InstructorSortParm = sortOrder == "Instructor" ? "Instructor desc" : "Instructor";
ViewBag.YearSortParm = sortOrder == "Year" ? "Year desc" : "Year";
ViewBag.AttendingDaysSortParm = sortOrder == "AttendingDays" ? "AttendingDays desc" : "AttendingDays";
ViewBag.AttendanceCapSortParm = sortOrder == "AttendanceCap" ? "AttendanceCap desc" : "AttendanceCap";
ViewBag.StartDateSortParm = sortOrder == "StartDate" ? "StartDate desc" : "StartDate";
ViewBag.LocationSortParm = sortOrder == "Location" ? "Location desc" : "Location";
ViewBag.ParishSortParm = sortOrder == "Parish" ? "Parish desc" : "Parish";
ViewBag.DescriptionSortParm = sortOrder == "Description" ? "Description desc" : "Description";
ViewBag.ApprovedSortPArm = sortOrder == "Approved" ? "Approved desc" : "Approved";
ViewBag.CompletedSortPArm = sortOrder == "Completed" ? "Completed desc" : "Completed";
ViewBag.ArchivedSortPArm = sortOrder == "Archived" ? "Archived desc" : "Archived";
if (Request.HttpMethod == "GET")
{
searchString = currentFilter;
}
else
{
page = 1;
}
ViewBag.CurrentFilter = searchString;
var courses = from s in db.Courses
select s;
if (!String.IsNullOrEmpty(searchString))
{
courses = courses.Where(s => s.Title.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "Title desc":
courses = courses.OrderByDescending(s => s.Title);
break;
case "Credits":
courses = courses.OrderBy(s => s.Credits);
break;
case "Credits desc":
courses = courses.OrderByDescending(s => s.Credits);
break;
case "Elective":
courses = courses.OrderBy(s => s.Credits);
break;
case "Elective desc":
courses = courses.OrderByDescending(s => s.Credits);
break;
case "Instructor":
courses = courses.OrderBy(s => s.Instructor.LastName);
break;
case "Instructor desc":
courses = courses.OrderByDescending(s => s.Instructor.LastName);
break;
case "Year":
courses = courses.OrderBy(s => s.Year);
break;
case "Year desc":
courses = courses.OrderByDescending(s => s.Year);
break;
case "AttendingDays":
courses = courses.OrderBy(s => s.AttendingDays);
break;
case "AttendingDays desc":
courses = courses.OrderByDescending(s => s.AttendingDays);
break;
case "AttendanceCap":
courses = courses.OrderBy(s => s.AttendanceCap);
break;
case "AttendanceCap desc":
courses = courses.OrderByDescending(s => s.AttendanceCap);
break;
case "StartDate":
courses = courses.OrderBy(s => s.StartDate);
break;
case "StartDate desc":
courses = courses.OrderByDescending(s => s.StartDate);
break;
case "Location":
courses = courses.OrderBy(s => s.Location);
break;
case "Location desc":
courses = courses.OrderByDescending(s => s.Location);
break;
case "Parish":
courses = courses.OrderBy(s => s.Parish);
break;
case "Parish desc":
courses = courses.OrderByDescending(s => s.Parish);
break;
case "Description":
courses = courses.OrderBy(s => s.Description);
break;
case "Description desc":
courses = courses.OrderByDescending(s => s.Description);
break;
case "Approved":
courses = courses.OrderBy(s => s.Approved);
break;
case "Approved desc":
courses = courses.OrderByDescending(s => s.Approved);
break;
case "Completed":
courses = courses.OrderBy(s => s.Completed);
break;
case "Completed desc":
courses = courses.OrderByDescending(s => s.Completed);
break;
case "Archived":
courses = courses.OrderBy(s => s.Archived);
break;
case "Archived desc":
courses = courses.OrderByDescending(s => s.Archived);
break;
default:
courses = courses.OrderBy(s => s.Title);
break;
}
int pageSize = 4;
int pageNumber = (page ?? 1);
return View(courses.ToPagedList(pageNumber, pageSize));
}
我该如何使上述代码更易读?我应该将其中的部分提取为单独的方法并将它们移至控制器底部吗?还是将这些方法放在另一个文件中,并在此引用它们? 请记住,我正在学习,希望可以更加清晰易懂。