2012-05-27 26 views
6

現在我已經開發了我的第一個大型(對我來說)MVC項目,現在事情變得非常難以瀏覽。.NET MVC儘可能保持控制器不被讀取的最佳實踐

我一直在重構重構,爲了保持控制器精簡併將所有數據移動到模型中,我正在尋求「最佳實踐」的現代示例。

我讀this article,詳細討論了一些事情,但沒有提供示例項目。

這裏發佈的大部分'最佳實踐'主題都傾向於鏈接到MVC Music Store或Nerd Dinner項目,但同時評論往往會說他們更多是'初學者指南'而不是'最佳實踐'的例子。 「

有誰知道任何最新的開源MVC項目,展示適當的開發結構?

請注意:我想學會解決的一個典型問題:我的控制器非常長,充滿了驅動網站的代碼 - 我需要將此代碼移入僅由控制器引用的方法。我在哪裏扔所有這些方法?

下面是一個來自控制器的代碼示例,如其中一個回覆的評論所示。我將如何將這些信息移到我的ViewModel? (我已經包含下面的視圖模型):

控制器:

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

回答

6

什麼你基本上是尋找的是一個分層架構。例如,服務層模式要求您在服務層而不是控制器中定義大量邏輯。

有這樣的例子,他們從模式&實踐團隊在微軟是絲綢之路的一個:http://silk.codeplex.com/

+0

我正在檢查代碼,發現很多錯誤。例如類handlers.GetFillupsForVehicle,首先它的矯枉過正(定義一個類作爲動詞),第二個是在類中做一些有用的唯一函數,它返回一個IEnumerable,顯然這個函數的代碼只返回一個List,所以它的在沒有的地方強制歧義。 – magallanes

3

當你說你的控制器是「漫長而充滿碼」,這是否意味着所有的代碼在控制器中?如果是這種情況,您需要將大部分邏輯分解爲支持ViewModel類。

我通常將ViewModel類中的大部分(如果不是全部)代碼放在ViewModel類中,每個View/Controller使用一個。所有的邏輯從視圖模型浮出水面,使得每個控制器的操作會運行一個,代碼也許兩行

UPDATE(在合理範圍內。):
我會採取所有的邏輯出你的行動,並將其移動到一個ViewModel方法,它爲這個ID提供一個int。現在你的控制器的行動方法是一行:

return View(MyViewModel.AttendanceView(id)); 

這是一個簡單的例子,有更先進的想法。

2

有誰知道任何最新的開源MVC項目,展示適當的發展結構?

不幸的是沒有。到目前爲止,我所見過的所有項目都不適合初學者開始學習。不是因爲它們包含糟糕的代碼,而是因爲它們的複雜性。

我想學會解決的一個典型問題:我的控制器非常長,充滿驅動網站的代碼 - 我需要將此代碼移到僅由控制器引用的方法中。我在哪裏扔所有這些方法?

如果你的控制器包含很多行,那麼你做錯了。您需要了解關注點的分離以及如何編寫乾淨的代碼(以及它的意思)。例如,永遠不要編寫代碼來從控制器的數據庫中檢索某些內容。這種行爲屬於數據庫訪問層,邏輯上進一步分爲多個類。瞭解「不要重複自己」等原理。

關於如何編寫一個好的代碼有很多討論,我不確定是否可以在這裏完成。有很多書討論這個問題,但我希望我給你至少一些有用的指針開始。