gpt4 book ai didi

c# - 如何重构此 C# 代码以使其更易于阅读?

转载 作者:行者123 更新时间:2023-11-30 15:39:04 26 4
gpt4 key购买 nike

这是我的 Controller 中的一个 Action - 在我的整个项目中,我的 Controller 中都有类似的大型方法。

我正在努力学习将这些东西放在哪里以及如何清理它们。我是这方面的新手,如果我看到一个关于如何更改我自己的一些代码的好例子,它可能会教我如何对我的大量代码执行此操作。

这是我的操作:

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));
}

我应该如何处理上面的代码以使其更具可读性?我是否只是将它的一部分作为单独的方法移出并将它们移到 Controller 的底部?我是否将这些方法放在另一个文件中的某个地方并在此处引用它?

请记住我正在学习并且享受清晰。

最佳答案

如何更改代码完全是主观的,这是一个创造性的过程,您需要感受并努力成为更好的开发人员。

但作为一个伪代码示例,目标是使您的方法变小。不,真的,甚至更小!

使函数名称非常有意义,并确保它们告诉程序员该方法的真正意图是什么。这只是一个结构示例:

public class Something {

public ActionResult Index() {
MakeCallToFunction();
var something = GetSomething();
var somethingElse = GetSomethingElse(something);
var model = new SomeViewModel(somethingElse);
return View(model);
}

private void MakeCallToFunction () {
...
}

private string GetSomething() {
...
return someVal;
}

private SomeObject GetSomethingElse(string something) {
...
return someBigObject;
}
}

或者这可能意味着您采用所有这些小功能并创建一个类

public class Something {

public ActionResult Index(int id) {
var model = new SomeRelatedStuff.Load(id);
return View(model);
}
}

private class SomeRelatedStuff {

public SomeRelatedStuff()

public static SomeRelatedStuff Load(int id) {
var something = GetSomething();
var somethingElse = GetSomethingElse(something);
var model = new SomeViewModel(somethingElse);
MakeObject();
return this;
}

private string GetSomething() {
...
return someVal;
}

private SomeObject GetSomethingElse(string something) {
...
return someBigObject;
}

private void MakeObject () {
...
}

}

作为旁注,拜托请拜托 Robert Martin(Bob 叔叔)的 Clean Code,本周末至少阅读前 4 章。然后再读一遍。

关于c# - 如何重构此 C# 代码以使其更易于阅读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10835102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com