控制器 - >服务 - >资源库:是否服务实体映射到视图模型? [英] Controller -> Service -> Repository: Does service map Entity to ViewModel?

查看:254
本文介绍了控制器 - >服务 - >资源库:是否服务实体映射到视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的避风港MVC应用程序,与M,包括服务和信息库层。

I haven MVC app, with "M" including Service and Repository layers.

不过,我在何处以及如何做一些事情有点糊涂了。

However, I am a little confused as to where and how to do a couple of things.


  1. 一个服务调用两个库,或调用它自己的仓库和其他服务

例如

我有一个ReferenceDataService,这与我的参考表处理所有的逻辑的CRUD。

I have a ReferenceDataService, which handles all of the logic for CRUD with my reference tables.

然后在我的CustomerService我需要'R'我引用数据来获得如说明,而不是标识。所以,我称了ReferenceDataService或ReferenceDataRepository?

Then in my "CustomerService" I need to 'R' my reference data to get e.g. Description instead of Id. So, do I call the ReferenceDataService or ReferenceDataRepository?


  1. 在一些层,我想从实体映射到视图模型。

难道我这样做是我的服务层,还是在控制器?

Do I do this in my Service layer, or in the Controller?

例如。请问我ServiceLayer做映射/逻辑从虚拟机实体,回来了?

e.g. Does my ServiceLayer do the mapping/logic from VM to Entity and back?

感谢:)

推荐答案


  • 库交谈底层数据源。

  • 服务层的会谈与域模型库。这需要/传递域模型从/存储库层。

  • 控制器会谈服务层。控制器会/通域模型从/到服务层。

  • 控制器调用映射层(如果有的话)的域模型和视图模型之间进行映射。如果你没有一个映射层,你可以做你的控制器的映射虽然这可能很快成为在这种情况下 AutoMapper 可以作为一个非常方便的映射层。

  • Repositories talk to an underlying data source.
  • Service layer talks to repositories with domain models. It takes/passes domain models from/to the repository layer.
  • Controller talks to service layer. Controller takes/passes domain models from/to the service layer.
  • Controller calls mapping layer (if any) to map between the domain models and view models. If you don't have a mapping layer you could do the mapping in your controller although this could quickly become cumbersome in which case AutoMapper could serve as a very handy mapping layer.
  • 另一个更简单的情况是,当你不需要的服务层,它往往是较小的应用程序的情况下。服务层带来任何好处。因此,控制器将直接与储存库的域模型。

    Another more simpler scenario is when you don't need a service layer which is often the case in smaller applications. A service layer brings no benefit. So the controller talks directly to the repositories with the domain models.

    这篇关于控制器 - >服务 - >资源库:是否服务实体映射到视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆