我想在控制器中使用ViewModel而不是模型直接的主要原因之一是我注意到包含数据访问权限的控制器对我来说似乎是这项工作的错误位置。不应该在ViewModel中完成数据访问/读写吗?
像这样的事情:即。 EF创建了Model:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace TestIODSManagement.Models
{
using System;
using System.Collections.Generic;
public partial class ClientJob
{
public int Id { get; set; }
public int ClientId { get; set; }
public int JobTypeId { get; set; }
public string OlapAppName { get; set; }
public string EdgeAppName { get; set; }
public Nullable<int> DatabaseServer { get; set; }
public Nullable<int> ProcessingServer { get; set; }
public Nullable<int> QueryServer { get; set; }
public string DatabaseName { get; set; }
public int DomainId { get; set; }
public int CurrencyTypeId { get; set; }
public virtual Client Client { get; set; }
public virtual CurrencyType CurrencyType { get; set; }
public virtual Domain Domain { get; set; }
public virtual JobType JobType { get; set; }
public virtual Server DbServer { get; set; }
public virtual Server OpServer { get; set; }
public virtual Server OqServer { get; set; }
}
}
可能的ViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestIODSManagement.Models;
namespace TestIODSManagement.ViewModels
{
public class ClientJobsViewModel
{
private DeploymentsEntities db = new DeploymentsEntities();
public int Id { get; set; }
public int ClientId { get; set; }
public int JobTypeId { get; set; }
public string OlapAppName { get; set; }
public string EdgeAppName { get; set; }
public Nullable<int> DatabaseServer { get; set; }
public Nullable<int> ProcessingServer { get; set; }
public Nullable<int> QueryServer { get; set; }
public string DatabaseName { get; set; }
public int DomainId { get; set; }
public int CurrencyTypeId { get; set; }
public virtual Client Client { get; set; }
public virtual CurrencyType CurrencyType { get; set; }
public virtual Domain Domain { get; set; }
public virtual JobType JobType { get; set; }
public virtual Server DbServer { get; set; }
public virtual Server OpServer { get; set; }
public virtual Server OqServer { get; set; }
private List<Client> _clients;
public IList<Client> Clients { get { return _clients.AsReadOnly(); } }
private List<Domain> _domains;
public IList<Domain> Domains { get { return _domains.AsReadOnly(); } }
private List<CurrencyType> _currencyTypes;
public IList<CurrencyType> CurrencyTypes { get { return _currencyTypes.AsReadOnly(); } }
private List<JobType> _jobTypes;
public IList<JobType> JobTypes { get { return _jobTypes.AsReadOnly(); } }
private List<Server> _databaseServers;
public IList<Server> DatabaseServers { get { return _databaseServers.AsReadOnly(); } }
private List<Server> _processingServers;
public IList<Server> ProcessingServers { get { return _processingServers.AsReadOnly(); } }
private List<Server> _queryServers;
public IList<Server> QueryServers { get { return _queryServers.AsReadOnly(); } }
public ClientJobsViewModel(Client client)
{
ForceClientList(client);
SetSelectLists();
}
public ClientJobsViewModel(int jobId)
{
ClientJob job = db.ClientJobs.Find(jobId);
Client thisClient = db.Clients.Find(job.ClientId);
ForceClientList(thisClient);
SetSelectLists();
}
private void ForceClientList(Client client)
{
_clients = db.Clients
.Where(c => c.Id == client.Id)
.ToList();
}
private void SetSelectLists()
{
_databaseServers = db.Servers
.Where(s => s.ServerPurpos.PurposeName == "DataWarehouse").ToList();
_processingServers = db.Servers
.Where(s => s.ServerPurpos.PurposeName == "Processing").ToList();
_queryServers = db.Servers
.Where(s => s.ServerPurpos.PurposeName == "Querying").ToList();
_domains = db.Domains.ToList();
_currencyTypes = db.CurrencyTypes.ToList();
_jobTypes = db.JobTypes.ToList();
}
}
}
答案 0 :(得分:0)
你是对的,控制器不是正确的地方,但视图模型也不是。理想情况下,您的视图模型应该只包含封装数据的属性,而且很少包含任何功能。我会将所有逻辑放在专用数据层中,如果它是一个小型,简单的项目,专门用于数据访问的类,则会失败。
然后,控制器应该是调用该层,类或服务的控制器,并请求填充视图模型。
答案 1 :(得分:0)
ViewModel和Model之间存在差异。 ViewModel只是自定义类 - crate,它以强类型方式在视图和控制器之间传递数据。模型是控制器调用以获取数据,业务逻辑等的“东西”。它可以是服务层,业务逻辑类,存储库或其他任何东西。数据访问是Model的一部分。