MVC - 控制器到服务层通信
在我的 ASP.net mvc 应用程序中,我使用服务层和存储库来保持控制器的精简。典型的详细信息只读视图如下所示:
public ActionResult Details(int id)
{
var project = _projectService.GetById(id);
return View(Mapper.Map<Project, ProjectDetails>(project));
}
服务层:
public class ProjectService : IProjectService
{
public Project GetById(int id)
{
var project = _projectRepository.GetProject(id);
// do some stuff
return project;
}
}
public class ProjectRepository : IProjectRepository
{
public Project GetProject(int id)
{
return context.Projects.Find(id);
}
}
由于自动映射器的存在,从服务层转移到视图模型非常容易,它可以很容易地扁平化事物。直接将另一个从视图模型传递到我的服务层是我努力想出一个好的解决方案的地方。
在像创建操作这样的情况下,什么是好的方法?
[HttpPost]
public ActionResult Create(CreateProjectViewModel model)
{
if(!ModelState.IsValid)
{
return View(model);
}
// TODO
return RedirectToAction("Index");
}
我非常确定服务层不应该了解有关视图模型的任何信息,但我也不认为 AutoMapper 在这种情况下工作得很好,因为它不擅长采用平面模型并将其变成复杂的对象。
我的控制器应该是什么样子才能与服务层通信?我想让控制器中的代码尽可能简洁。
In my ASP.net mvc app I am using a Service Layer and Repositories to keep my controllers thin. A typical details read only view looks like this:
public ActionResult Details(int id)
{
var project = _projectService.GetById(id);
return View(Mapper.Map<Project, ProjectDetails>(project));
}
Service Layer:
public class ProjectService : IProjectService
{
public Project GetById(int id)
{
var project = _projectRepository.GetProject(id);
// do some stuff
return project;
}
}
public class ProjectRepository : IProjectRepository
{
public Project GetProject(int id)
{
return context.Projects.Find(id);
}
}
Moving from the service layer to the view model is pretty easy because of automapper, which can flatten things pretty easily. Moving the other direct, from the view model to pass into my service layer is where I struggle to come up with a good solution.
In a situation like a Create action, what is a good approach for this?
[HttpPost]
public ActionResult Create(CreateProjectViewModel model)
{
if(!ModelState.IsValid)
{
return View(model);
}
// TODO
return RedirectToAction("Index");
}
I'm pretty sure that the service layer should not know anything about view models, but I also don't think that AutoMapper works well in this scenario either, since it's not good at taking a flat model and making it into a complex object.
What should my controller look like to communicate with the service layer? I want to keep the code in the controller as light as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以定义双向映射,然后反之亦然:
或者如果您要更新实体,您可能首先要从服务中获取要更新的现有实体:
You could define a bidirectional mapping and then go the other way around:
or if you are updating an entity you might first want to fetch the existing entity that you want to update from the service:
到目前为止,我看到的唯一方法是手动创建一堆模型转换类,例如:
另一个 Transformer 实现以另一种方式返回(
ViewModel -> DataModel
)。并让控制器知道调用正确的变压器。
我+1你的问题,因为我也希望看到一种干净的方法来做到这一点,而不必手动编写一堆代码来映射模型。
The only way I have seen this done so far is to manually create a bunch of model transformation classes, for example:
And another Transformer implementation to go back the other way (
ViewModel -> DataModel
).And having the Controller know to call the correct transformer.
I +1 your question because I would love to see a nice clean way to do this too, without having to manually write a bunch of code to map models.
如果您的服务层专门用于支持您的 MVC 应用程序而没有其他客户端,您可以考虑使用通过服务层传递的对象以及从服务层传递的对象作为视图模型的一部分。这将消除自动映射入站调用的需要,因为您将发送控制器所需的实际对象。
您还可以考虑不让服务返回域对象,这意味着应该使用服务方法而不是控制器操作来调用自动映射。
If your service layer is solely dedicated to support your MVC application and no other clients you could consider using the objects passed through and from your service layer as part of your viewmodels. This would obviate the need to automap the inbound calls as you'd be sending in the actual objects required from the controller.
You could also consider not having the services return domain objects, this would mean that the automapping should be invoked with the service methods rather than the controller actions.