2013-02-26 75 views
0

任何人都可以告訴我這是否是創建視圖模型的正確方法。我正在使用Ninject,我可以使視圖模型正常工作的唯一方法是使用下面的代碼。MVC中的ViewModel訪問數據的正確方式

此外,我似乎無法將數據從視圖模型傳遞到控制器,除非我創建第二個接口。

下面的代碼確實有效,但是閱讀我所看到的所有示例,我似乎正在複製很多來自我的域圖層的代碼。

---------------------代碼數據訪問層------

using System.Collections.Generic; 
    using System.Configuration; 
    using System.Data; 
    using System.Data.SqlClient; 

namespace Web.Domain.SearchEngine 
    { 
    public class DisplaySearchResults 
     { 
     public string Title   { get; set; } 
     public string Description { get; set; } 
     public string URL   { get; set; } 
     } 
    public class GetSearchResults : IGetSearchResults 
     { 
     private string dbConn; 

     public GetSearchResults() 
      { 
      dbConn = ConfigurationManager.ConnectionStrings["Search"].ConnectionString; 
      } 

     public IEnumerable<DisplaySearchResults> SearchResults(string q, string option, int pagenumber) 
      { 
      List<DisplaySearchResults> Data = new List<DisplaySearchResults>(); 
      string spName = "dbo.FTS_On_at_Websites"; 
      using (SqlConnection cn = new SqlConnection(dbConn)) 
       { 
       using (SqlCommand cmd = new SqlCommand(spName, cn)) 
        { 
        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.Parameters.Add(new SqlParameter("@strSearchPhrase", SqlDbType.VarChar, 100)); 
        cmd.Parameters.Add(new SqlParameter("@SearchMode", SqlDbType.Int, 4)); 
        cmd.Parameters.Add(new SqlParameter("@intPageNumber", SqlDbType.Int)); 
        cmd.Parameters.Add(new SqlParameter("@intRecordsPerPage", SqlDbType.Int)); 
        cmd.Parameters.Add(new SqlParameter("@intTotalRecordsReturned", SqlDbType.Int)); 
        cmd.Parameters["@strSearchPhrase"].Value = q; 
        cmd.Parameters["@SearchMode"].Value = 1; 
        cmd.Parameters["@intPageNumber"].Value = pagenumber; 
        cmd.Parameters["@intRecordsPerPage"].Value = 10; 
        cmd.Parameters["@intTotalRecordsReturned"].Value = 10; 

        cn.Open(); 
        using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.Default)) 
         { 
         if (rdr.HasRows) 
          { 
          while (rdr.Read()) 
           { 
           Data.Add(new DisplaySearchResults 
           { 
            Title   = (string)rdr["PageTitle"], 
            Description  = (string)rdr["PageParagraph"], 
            URL    = (string)rdr["PageURL"] 
           }); 
           } 
          } 
         return Data; 
         } 
        } 
       } 
      } 
     } 
    } 

-------- -----代碼視圖模型--------------------

using Microsoft.Security.Application; 
    using System.Collections.Generic; 
    using System.Linq; 
    using Web.Domain.SearchEngine; 

namespace Web.UI.ModelHelpers.Search 
    { 
    public class DisplaySearchResultsViewModel 
     { 
     public string Title   { get; set; } 
     public string Description { get; set; } 
     public string URL   { get; set; } 
     } 

    public class GetSearchResultsViewModel : IGetSearchResultsViewModel 
     { 
     private readonly IGetSearchResults _IGSR; 
     public GetSearchResultsViewModel(IGetSearchResults IGSR) 
      { 
      _IGSR = IGSR; 
      } 

     public IEnumerable<DisplaySearchResultsViewModel> SearchResultsViewModel(string q, string option, int pagenumber) 
      { 
      var searchResults = _IGSR.SearchResults(q, option, pagenumber).AsEnumerable(); 

      List<DisplaySearchResultsViewModel> GetData = new List<DisplaySearchResultsViewModel>(); 

      foreach (var details in searchResults.AsEnumerable()) 
       { 
       GetData.Add(new DisplaySearchResultsViewModel() 
       { 
        Title   = Sanitizer.GetSafeHtmlFragment(details.Title), 
        Description  = Sanitizer.GetSafeHtmlFragment(details.Description).ToLower(), 
        URL    = Sanitizer.GetSafeHtmlFragment(details.URL), 
       }); 
       } 
      return GetData; 
      } 
     } 
    } 

在控制器我有

var DisplaySearchResults = _IGSR.SearchResultsViewModel(cleanText, "1", 1); 

回答

3

不,這不是構建視圖模型的正確方法。視圖模型不應該包含任何數據訪問邏輯。這是模型的責任。

你應該做的是使用Ninject將IGetSearchResults實例注入到控制器中,而不是讓GetSearchResultsViewModel視圖模型將其視爲構造函數依賴項。其實你根本不需要這個GetSearchResultsViewModel。您已擁有名爲DisplaySearchResultsViewModel的正確視圖模型。然後,您的控制器有責任使用您的數據訪問層並構建此視圖模型。

例如:

public class SomeController : Controller 
{ 
    private readonly IGetSearchResults repository; 
    public SomeController(IGetSearchResults repository) 
    { 
     this.repository = repository; 
    } 

    public ActionResult SomeAction(string q, string option, int pagenumber) 
    { 
     // query your data access layer and build the view model that you will 
     // pass to the view 
     IEnumerable<DisplaySearchResultsViewModel> model = this.repository 
      .SearchResults(q, option, pagenumber) 
      .AsEnumerable() 
      .Select(details => new DisplaySearchResultsViewModel 
      { 
       Title = Sanitizer.GetSafeHtmlFragment(details.Title), 
       Description = Sanitizer.GetSafeHtmlFragment(details.Description).ToLower(), 
       URL = Sanitizer.GetSafeHtmlFragment(details.URL) 
      }) 
      .ToList(); 

     return View(model); 
    } 
} 
+0

更改代碼,永不分離域和視圖模型,域代碼在不同的類視圖模型,我的錯誤不表明 – CareerChange 2013-02-26 12:03:05

+0

這是很好的那些層爲更好的可讀性分開,但我能立即看到在視圖模型中使用數據訪問層的錯誤,這是不正確的。 – 2013-02-26 12:06:32

+0

嗨達林,這是我曾經這樣做,但在一些書籍中,我一直在閱讀作者說保持控制器亮,並移動所有presention,即在我的情況下淨化數據視圖模型。當有些書說某事和其他書等說別的話時,它確實有點混亂。很難想要走哪條路 – CareerChange 2013-02-26 12:10:03

相關問題