2013-02-26 39 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

相关问题