在 MVC 控制器中使用存储库和服务引用

发布于 2024-12-02 04:21:41 字数 457 浏览 0 评论 0原文

我在为我的 mvc 应用程序确定解决方案时遇到了一些麻烦。

背景。

我们有一个 EF 模型,通过 WCF 服务(而不是数据服务)对其执行操作。

我有一个 MVC 应用程序,它有许多存储库,这些存储库直接与服务通信并将 WCF 类型返回到调用存储库方法的控制器,该类型称为例如 WCFUserEntity(实际上并不以 WCF 为前缀)。

在控制器内部,我计划将 WCFUserEntity 自动映射到 ViewModel 实体。

这个解决方案让我烦恼的是,因为我将 WCFUserEntity 返回到控制器,所以我必须在控制器中引用 WebService 代理,这不适合我,我希望我的控制器一无所知存储库从哪里获取数据。所以我的另一个选择是在存储库内部进行自动映射并将 ViewModel 实体返回到控制器,但我找不到太多支持这个想法的东西,所以我真正想要的是验证第二个解决方案或帮助第三个。

谢谢,多姆

I'm having some trouble with deciding on a solution for my mvc application.

Background.

We have an EF model which we perform operations on via WCF Services (not data services).

I have an MVC application which has a number of Repositories that talk directly to the Services and return WCF types back to a controller which is calling the repository method, a type called for example WCFUserEntity (it's not actually prefixed with WCF).

Inside the controller I plan to automap the WCFUserEntity to a ViewModel entity.

What is bugging me about this solution is that because i'm returning WCFUserEntity to the controller I have to have a reference to the WebService proxy in my controller which doesn't sit well with me, i'd like my controllers to know nothing of where the repository has got the data from. So another option for me is to do the automapping inside of the repository and return the ViewModel entity to the controller, i can't find much around which supports this idea though, so really what i'm looking for is validation of this 2nd solution or help with a 3rd.

thanks, Dom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深白境迁sunset 2024-12-09 04:21:41

您可能需要考虑第三种选择。

ViewModelBuilder 的使用。

在您的控制器中,它们将像这样工作:

var myViewModel = myViewModelBuilder.WithX().WithY().Build();

WithX 和 WithY 是在内部向视图模型添加内容的方法(在构建器中,例如 WithCountriesList() 如果您想添加显示视图中国家/地区的下拉列表),并且 Build 方法将在添加所有内容后返回内部视图模型使用 WithXXX 方法的位。之所以如此,是因为大多数时候您可能想要添加下拉列表和不属于原始模型(在本例中为您的 userEntity)的内容。

这样,您的控制器就不知道如何构建视图模型,您的存储库也与视图模型无关。所有工作都在 Builder 中完成。缺点是,您需要为每个 ViewModel 创建一个 ViewModelBuilder。

我希望这有帮助。

You may want to consider a third option.

The use of ViewModelBuilders.

in your controller they would work like this:

var myViewModel = myViewModelBuilder.WithX().WithY().Build();

WithX and WithY would be methods that would add stuff to your viewmodel internally (within the builder, for example WithCountriesList() if you want to add a dropdown showing the countries in your view) and the Build method would return the internal viewmodel after adding all the bits with the WithXXX methods. This is so because most of the time you may want to add lists for dropdowns and things that are not part of your original model (your userEntity in this case).

This way, your controller doesn't know anything about how to build the viewmodel, your repository is also agnostic of viewmodels. All the work is done in the Builder. On the downside, you need to create a ViewModelBuilder for each ViewModel.

I hope this helps.

欢烬 2024-12-09 04:21:41

我如何处理这个问题可能需要一些架构更改,但我建议您使用 WCF API 返回 ViewModel 而不是实体。

首先,请考虑带宽问题(如果您在 Azure 或云中托管 WCF,这将是一个问题)。如果您的 ViewModel 仅使用一些特定属性,为什么要浪费带宽返回其他数据呢?在高流量情况下,这可能会导致流量浪费,最终可能会造成金钱损失。例如,如果您的视图仅显示用户及其问题,则没有理由通过网络发送他的电子邮件、答案、点数等。

另一个需要考虑的问题是急切加载。通过让 WCF 服务返回一个 ViewModel,您就知道您在一次访问 WCF 服务的过程中就拥有了视图所需的所有数据(即使它与相关实体相关)。您不需要获取 WCFUserEntity,然后向 WCF 请求与该特定用户相关的 WCFDocumentEntities

最后,如果您的 WCF API 是围绕 ViewModel 构建的,那么您对所涉及的业务流程会有更清晰的了解。您知道该特定请求(以及系统中的视图)将为您提供该特定信息,并且如果您需要不同视图的不同信息,那么您就知道这是一个完全不同的业务请求,具有不同的业务需求。以堆栈溢出为例,很容易看出该业务流程正在向当前用户询问相关问题,而该业务流程正在向当前用户询问相关答案。

在数据检索 WCF API 中使用 ViewModel 意味着您的前端层不一定知道数据来自哪里,它只知道它调用了业务流程并获取了所需的数据。据了解,数据层直接连接数据库而不是WCF。

编辑:
重新阅读后,这实际上看起来像是你的第三个选择。网上的大多数研究都没有谈论这个选项,我不知道为什么,但是在经历了一些类似的挫折之后(加上本文中列出的其他内容),这就是我处理业务层的方式。它更有意义,而且实际上(恕我直言)更容易管理。

How I would approach this might require some architecture changes, but I would suggest you approach your WCF API to return ViewModels instead of entities.

For starters, think about bandwidth issues (which would be an issue if you are hosting the WCF in Azure or the cloud). If your ViewModel is only using a few specific properties, why waste the bandwidth returning the other data? In high traffic scenarios, this could cause a waste of traffic that could end up costing money. For example, if your view is only display a user and his questions, there's no reason to send his email, answers, point count, etc.. over the wire.

Another issue to think about is eager loading. By having the WCF service return a ViewModel, you know you have all the data (even when it pertains to related entities) required from the view in one trip to the WCF service. You do not need to get the WCFUserEntity and then ask WCF for WCFDocumentEntities that are related to that specific user.

Finally, if your WCF API is built around ViewModels then you have a MUCH clearer understanding of the business processes involved. You know that this specific request (and view in the system) will give you this specific information, and if you need different information for a different view then you know that it's a completely different business request that has different business requirements. Using stack overflow as an example, it makes it trivial to see that this business process is asking for the current user with his related questions, while this business process is requesting the current user with his related answers.

Using ViewModels in your data retrieval WCF API means that your frontend layers do not necessarily know where the data came from, it just knows that it called a business process and got the data it needs. As far as it knows the data layer connected to the database directly instead of WCF.

Edit:
After re-reading, this actually looks like your 3rd option. Most research on the net don't talk about this option, and I don't know why, but after having some similar frustrations you are having (plus others listed in this post) this is the way I have gone with my business layer. It makes more sense and is actually (imho) easier to manage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文