所以,我一直在建立一个全新的网站来取代我们目前的经典asp网站。我建立了一个基础项目,并添加了我将要使用的所有不同技术。
Ninject Automapper PagedList EF 4.0
到目前为止,我有一个网站的工作原型。它从数据库中提取数据,并显示在我的页面上。我想我没有正确地遵循模式。因为我的控制器动作看起来很奇我开始认为我需要更改注射以将我的服务注入我的viewmodel?将服务插入ViewModel是否常见?
public class ParcelDetailViewModel
{
public Property Property { get; set; }
public int CurrentYear { get; set; }
public IEnumerable<AltOwnership> AltOwnership { get; set; }
public Ownership Ownership { get; set; }
public TotalValues TotalValues { get; set; }
public SiteAddressViewModel SiteAddress { get; set; }
public Value CurrentValues { get; set; }
public Transfer LatestTransfer { get; set; }
public LegalDescription LegalDescription { get; set; }
}
public class PropertyController : Controller
{
ICamaService _service = null;
[Inject]
public PropertyController(ICamaService service)
{
_service = service;
}
//TODO: ADD ACTIONS
public ViewResult List(int? page, int? size = 100)
{
var result = _service.GetProperties(page,size);
IList<ParcelDetailViewModel> ViewModel;
ViewModel = new List<ParcelDetailViewModel>();
Mapper.Map<IEnumerable<Models.Property>, IEnumerable<ParcelDetailViewModel>>(result, ViewModel);
return View(ViewModel.ToPagedList(page ?? 1, size ?? 100));
}
public ViewResult Details(string id)
{
var property = _service.GetProperty(id);
var acct = property.AccountNumber;
var AltOwners = _service.GetAltOwners(id);
var LegalDescription = _service.GetLegalDescription(id);
var Legals = _service.GetLegals(id).FirstOrDefault();
var Ownership = _service.GetOwnership((decimal)property.OwnerLookup);
var Values = _service.GetValues(acct);
var TotalValues = _service.GetTotalValues(acct);
var Location = _service.GetLocation(property.LocationLookup);
ParcelDetailViewModel ViewModel = new ParcelDetailViewModel();
SiteAddressViewModel SitusAddress = new SiteAddressViewModel();
ViewModel.SiteAddress = SitusAddress;
Mapper.Map<Models.Property, ParcelDetailViewModel>(property, ViewModel);
Mapper.Map<IEnumerable<AltOwnership>, ParcelDetailViewModel>(AltOwners, ViewModel);
Mapper.Map<Ownership, ParcelDetailViewModel>(Ownership, ViewModel);
Mapper.Map<TotalValues, ParcelDetailViewModel>(TotalValues, ViewModel);
Mapper.Map(Location, SitusAddress);
Mapper.Map(property, SitusAddress);
return View(ViewModel);
}
答案 0 :(得分:8)
将服务注入控制器是完全正确的。
另一方面,将任何服务注入到视图模型中将违背视图模型的想法 - 包含数据而没有操作的类。您将如何处理视图模型中的服务?