有人可以告诉我这是否是创建视图模型的正确方法。我正在使用Ninject,我可以使视图模型工作的唯一方法是使用下面的代码。
除非我创建第二个接口,否则我似乎无法将数据从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;
}
}
}
}
}
}
------------- Code ViewModel --------------------
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);
答案 0 :(得分: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);
}
}