2012-04-25 28 views
1

我已經構建了一個簡單的調查工具,使用MVC 3只有1層(MVC)。我現在很後悔。我所有的數據庫訪問和映射都在控制器和其他一些映射類中進行處理。使用多層體系結構時,方法調用的正確順序是什麼?

我想切換到使用三層:

演示(MVC)
業務邏輯
數據/持久性(EF)

我使用實體框架來處理一切與數據庫。實體框架創建它自己的域類。 MVC使用的模型與EF創建的模型之間的映射應該在哪裏進行?

如果映射位於業務層中,是否需要MVC項目中的Models文件夾?

調查問題由問題本身,行和列組成。 Theese是我使用的模型:基於QuestionID

public class Question { 

     public int Question_ID { get; set; } 

     public Boolean Condition_Fullfilled; 

     [Required(ErrorMessage = "Dette felt er påkrævet")] 
     public String Question_Wording { get; set; } 

     public String Question_Type { get; set; } 

     [Required(ErrorMessage = "Dette felt er påkrævet")] 
     public String Question_Order { get; set; } 

     public String Left_scale { get; set; } 
     public String Right_scale { get; set; } 
     public int Scale_Length { get; set; } 
     public String Left_Scale_HelpText { get; set; } 
     public String Right_Scale_HelpText { get; set; } 

     public Boolean Visible { get; set; } 
     public Boolean IsAnswered { get; set; } 
     public String Question_HelpText { get; set; } 
     public int Category_ID { get; set; } 

} 
public class MatrixColumns 
    { 
     public int Column_ID { get; set; } 
     public int Column_Number { get; set; } 
     public String Column_Description { get; set; } 
     public Boolean IsAnswer { get; set; } 
     public int? Procent { get; set; } 
     public bool Delete { get; set; } 
     public bool Visible { get; set; } 
     public int? Numbers { get; set; } 
     public String Help_Text { get; set; } 

    } 

    public class MatrixRows 
    { 
     public bool Delete { get; set; } 
     public bool Visible { get; set; } 
     public int Row_Id { get; set; } 
     public String Row_Number { get; set; } 
     public String Row_Description { get; set; } 
     public String Special_Row_CSS { get; set; } 
     public String Help_Text { get; set; } 
     // Dette er summen af procenterne af alle kolonner i rækken 
     public int RowSum { get; set; } 
    } 

所有數據在控制器檢索theese模型,並映射到一個視圖模型,看起來像這樣:

public class ShowMatrixQuestionViewModel : Question 
    { 
     public Dictionary<MatrixRows, List<MatrixColumns>> columnrow { get; set; } 
     public List<MatrixColumns> columns { get; set; } 
     public List<MatrixRows> rows { get; set; } 

     public ShowMatrixQuestionViewModel() 
     { 
      columns = new List<MatrixColumns>(); 
      rows = new List<MatrixRows>(); 
      columnrow = new Dictionary<MatrixRows, List<MatrixColumns>>(); 
     } 
    } 

所以當我想從我的控制器發送ShowMatrixQuestionViewModel到視圖時,我應該採用什麼路線?

這是我的建議:

- >控制器調用稱爲業務層的方法

public ShowMatrixViewModel GetQuestion(int QuestionID) {} 

- > GetQuestion調用在數據層以下方法:

public Question GetQuestion(int QuestionId) {} 
public MatrixRows GetRows(int QuestionId) {} 
public MatrixColumns GetColumns(int id) {} 

- >實體框架返回「純」對象,我想映射到我上面張貼的那些
- > GetQuestion調用方法到EF模型映射到我自己的模型
- >最後GetQuestion調用映射的問題,行和列的方法:

ShowMatrixQuestionViewModel model = MapShowMatrixQuestionViewModel(Question, MatrixRows, MatrixColumns) 
return model; 

這是正確的嗎?

在此先感謝

回答

0

要回答你的問題的第一部分:

「在哪裏?應該說MVC使用了EF去創建模型和模型之間的映射」

答案是MVC使用的模型 EF創建的模型。您的ASP.NET MVC項目中的EF工具是Linq to SQL Classes或ADO.NET Entity Framework Model。您應該在項目的Models文件夾中創建這些文件夾,並提供您的數據/持久性(EF)。

+0

調查中的問題包含來自3種不同EF模型的數據。我將這些模型中的數據映射到一個ViewModel,該ViewModel包含了所有需要顯示問題的信息。我的想法是,這可能在業務層。 MVC可以參考業務層以及模型。 – Kenci 2012-04-25 21:03:53

+0

聽起來對我很好。如果您從三個獨立的EF源獲取數據,那麼我將創建自己的類(這是業務邏輯層),其中包含我需要的來自三個EF源的數據字段,以及任何額外的數據和我需要的任何操作去做。 MVC通過模型和您自己的自定義類來保持數據和業務邏輯的獨立性。 – markp3rry 2012-04-26 10:32:29

相關問題