C# >EF对于另一种技术,您的代码是可测试的。即使从您的应答服务层闻起来也像是额外的层。“除了企业应用程序,服务层是多余的。@DarthVader-实际上,我见过的大多数MVC应用程序都使用服务层(业务层)。不管怎样,我在这里使用“服务”层作为示例来展示如何使
C# >EF对于另一种技术,您的代码是可测试的。即使从您的应答服务层闻起来也像是额外的层。“除了企业应用程序,服务层是多余的。@DarthVader-实际上,我见过的大多数MVC应用程序都使用服务层(业务层)。不管怎样,我在这里使用“服务”层作为示例来展示如何使,c#,asp.net-mvc,entity-framework,viewmodel,C#,Asp.net Mvc,Entity Framework,Viewmodel,>EF对于另一种技术,您的代码是可测试的。即使从您的应答服务层闻起来也像是额外的层。“除了企业应用程序,服务层是多余的。@DarthVader-实际上,我见过的大多数MVC应用程序都使用服务层(业务层)。不管怎样,我在这里使用“服务”层作为示例来展示如何使用和填充域和视图模型。您是对的,视图模型在哪里生成并不重要。然而,这里和其他地方的许多例子()都警告不要使用“胖”控制器。我同意汤米的观点,瘦控制器肯定比胖控制器好ones@Tommy有了新的web api,这种方法就有了变化,因为web ap
>EF对于另一种技术,您的代码是可测试的。即使从您的应答服务层闻起来也像是额外的层。“除了企业应用程序,服务层是多余的。@DarthVader-实际上,我见过的大多数MVC应用程序都使用服务层(业务层)。不管怎样,我在这里使用“服务”层作为示例来展示如何使用和填充域和视图模型。您是对的,视图模型在哪里生成并不重要。然而,这里和其他地方的许多例子()都警告不要使用“胖”控制器。我同意汤米的观点,瘦控制器肯定比胖控制器好ones@Tommy有了新的web api,这种方法就有了变化,因为web api 2附带了服务。但是现在有了MVC6,这是我升级时的另一个困惑。。。你能详细说明选择哪一个以及他们在你的表格中提供了什么吗perpective@Tommy-嗨,我有一个疑问,正如上面CSharper的回答所解释的(这里不一定重复)。我想知道你的观点。好的,这是用户/开发者的决定!!解释得很好!在populate中,您是指类似DBContext.populate()的内容吗?我更深层次的问题是,表之间的关系会发生什么,谁来处理这一责任,例如,在master details网格场景中,
populate()
只是我调用的方法的名称。DataAccess类将处理数据库操作。我将颠倒顺序,这样代码将在您阅读时按顺序执行,从控制器开始。@CSharper-我正在尝试为我的项目找出正确的方法,我看到了这篇文章和您的答案。。。我有一个疑问:通常我们在Web项目中保留ViewModel,而在单独的业务层项目中保留服务,在这种情况下,为了在服务层中填充ViewModel,我必须添加对Web项目的引用。这不是有点奇怪吗?您不能从服务层返回web应用程序中定义的模型,因为web应用程序依赖于服务层。你需要定义这个模型,不管你想在什么地方称它为什么,你的服务和web项目都可以访问它。@Tommy:Tommy,CSharper:我想我明白了:-)谢谢所有三个答案都很好。。我也喜欢C Shaper的反应。。。但是你有一个详细的样品!
//Controller
public ActionResult Index()
{
List<OrderViewModel>() model = new List<OrderViewModel>();
model = new ServiceClass().GetOrders();
return View(model);
}
//here is your Service Class, this layer transfers the Domain Model into your ViewModel
public List<OrderViewModel> GetOrders()
{
List<OrderDomain> model = new List<OrderDomain>();
model = new DataAccess().GetOrders();
List<OrderViewModel> viewModel = new List<OrderViewModel>();
foreach (var order in model)
{
OrderViewModel vm = new OrderViewModel();
vm.OrderId = order.OrderId;
vm.OrderName = order.OrderName;
viewModel.Add(vm);
}
return viewModel;
}
//some DataAccess class, this class is used for database access
Public List<OrderDomain> GetOrders()
{
List<OrderDomain> model = new List<OrderDomain>();
using (var context = new MyEntities())
{
model = (from x in context.Order
select new OrderDomain
{
OrderId = x.OrderId,
OrderName = x.OrderName
}).ToList();
}
return model;
}
public class PurchaseRecordsViewModel
{
public IEnumerable<PurchaseRecordViewModel> PurchaseRecords {get;set;}
}
public class PurchaseRecordViewModel
{
public DateTime Date {get;set;}
public decimal Cost {get;set;}
// .... some other properties
public PurchaseRecordsViewModel(PurchaseRecordsDomainModel domainModel)
{
Date = domainModel.Date;
Cost = domainModel.Cost;
// .... some other property mappings
}
}
public class PurchaseRecordsService
{
private readonly IPurchaseRecordsRepository _purchaseRecordsRepository;
public PurchaseRecordsService(IPurchaseRecordsRepository purchaseRecordsRepository)
{
if(purchaseRecordsRepository == null)
{
throw new ArgumentNullException("purchaseRecordsRepository");
}
_purchaseRecordsRepository = purchaseRecordsRepository;
}
public IEnumerable<PurchaseRecordsDomainModel> GetPurchaseRecords()
{
// trivial case, real code can be more complex
return _purchaseRecordsRepository.GetPurchaseRecords();
}
}
public interface IPurchaseRecordsRepository
{
IEnumerable<PurchaseRecordsDomainModel > GetPurchaseRecords();
}
public class EfPurchaseRecordsRepository: IPurchaseRecordsRepository
{
private readonly EfObjectContext _objectContext;
public EfPurchaseRecordsRepository(string connectionString)
{
_objectContext = new EfObjectContext(connectionString);
}
public IEnumerable<PurchaseRecordsDomainModel > GetPurchaseRecords()
{
var purchaseRecords = (from p in _objectContext.PurchaseRecords
....
select p).AsEnumerable();
return purchaseRecords .Select(p => p.ConvertToDomainPurchaseRecord());
}
}
public class PurchaseController: Controller
{
private readonly IPurchaseRecordsRepository _repository;
public PurchaseController(IPurchaseRecordsRepository repository)
{
if(repository == null)
{
throw new ArgumentNullException("repository");
}
_repository = repository;
}
public ActionResult Index()
{
var purchaseRecordsService = new PurchaseRecordsService(_repository);
var purchaseRecordsViewModel = new PurchaseRecordsViewModel();
var purchaseRecords = purchaseRecordsService.GetPurchaseRecords();
foreach(var purchaseRecord in purchaseRecords)
{
var purchaseRecordViewModel = new PurchaseRecordViewModel(purchaseRecord);
purchaseRecordsViewModel.PurchaseRecords.Add(purchaseRecordViewModel);
}
return View(purchaseRecordsViewModel);
}
}