为什么不在视图中使用存储库

发布于 2024-12-29 08:38:09 字数 1077 浏览 1 评论 0原文

我有一个部分视图,循环遍历其模型(事物列表)以显示 thing.Name 和三个整数值(它们是相关实体的计数)。

首先,我尝试放置:(伪剃刀)

foreach(thing in Model){
    @thing.Name : 
        @thing.related.entities.where(condition1).Count()
        @thing.related.entities.where(condition2).Count()
        @thing.related.entities.where(condition3).Count()
}

但是它真的很慢...所以我在 ThingRepository 中创建了一个函数,它可以更快地执行相同的查询,就像这样(伪代码)

function GetCountofRelatedEntities(relatedID,condition){
    return db.entities.where(relatedID==relatedID && condition).count()
}

并且速度更快,所以我想要来调用它。我想我应该从控制器调用它,但是我需要一个 ViewModel 来保留 (thing, int, int, int) 集合作为模型,或者我可以使用 ViewBag 来将结果传递给视图,但是,这是问题: 为什么不简单地从视图中使用存储库?视图中的这段代码有什么问题? (伪剃刀)

@repo=new ThingRepository()
foreach(thing in Model){
    @thing.Name : 
        @repo.GetCountofRelatedEntities(thing.relatedID,condition1)
        @repo.GetCountofRelatedEntities(thing.relatedID,condition1)
        @repo.GetCountofRelatedEntities(thing.relatedID,condition1)
}

你能告诉我为什么我不应该在视图中实例化存储库吗?或者我可以做吗?

I have a partial view that loops through its Model (a list of things) to show the thing.Name and three integer values that are counts of related entities.

First of all, I tried putting: (pseudo-razor)

foreach(thing in Model){
    @thing.Name : 
        @thing.related.entities.where(condition1).Count()
        @thing.related.entities.where(condition2).Count()
        @thing.related.entities.where(condition3).Count()
}

But its really slow... so I created a function in the ThingRepository that does same queries faster, something like this (pseudo-code)

function GetCountofRelatedEntities(relatedID,condition){
    return db.entities.where(relatedID==relatedID && condition).count()
}

and its much faster, so I want to call it. I think I should call it from the controller, but then I need a ViewModel to keep a (thing, int, int, int) collection as the model, or I can use the ViewBag extremely to pass the results to the view, but, and here is the question:
Why not simply use the repository from the view? whats wrong with this code in the view? (pseudo-razor)

@repo=new ThingRepository()
foreach(thing in Model){
    @thing.Name : 
        @repo.GetCountofRelatedEntities(thing.relatedID,condition1)
        @repo.GetCountofRelatedEntities(thing.relatedID,condition1)
        @repo.GetCountofRelatedEntities(thing.relatedID,condition1)
}

Could you tell me why I shouldn't instantiate the repository inside a View? Or I can do it?

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

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

发布评论

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

评论(4

留一抹残留的笑 2025-01-05 08:38:09

为什么不简单地从视图中使用存储库?

因为你违反了MVC设计模式。视图的职责不是获取数据。它是在视图模型的形式下显示从控制器传递给它的数据。就这么简单。

您可以在视图中调用存储库或任何您喜欢的内容,但不要再用 asp.net-mvc 标记您的问题,因为您不再执行任何 MVC。我的意思是你可以做任何你喜欢的事情 - 如果你愿意的话,你甚至可以在你的视图中编写 SQL 查询。

但 MVC 设计模式的全部要点是将数据检索逻辑与表示逻辑分开。

Why not simply use the repository from the view?

Because you are violating the MVC design pattern. A view's responsibility is not to fetch data. It is to display data that it is being passed to it from the controller under the form a view model. And it's as simple as that.

You could call repositories or whatever you like in your views but just don't tag your questions with asp.net-mvc anymore because you are no longer doing any MVC. I mean you could do whatever you like - you could even write SQL queries in your view if you want.

But the whole point of the MVC design pattern is to separate the data retrieval logic from the presentation logic.

不乱于心 2025-01-05 08:38:09

MVC 模式的一个目的是提供一种适合各种常见编程情况的结构。简单来说:

  • 模型:描述应用程序的形状,即特定于您的业务的软件部分。
  • 视图:向用户显示数据,并将用户事件传输到服务器。
  • 控制器:充当视图和模型之间的中间人。

您提出的建议“有效”,因为它可以在您想要的页面上获取数据。从短期来看,它似乎可以节省您的时间和精力,因为您不必担心控制器、视图袋等。

但是,您正在以一种稍后可能会后悔的方式破坏 MVC 结构。例如,假设几周后您的老板来找您并说:“嘿,您知道您添加的用于显示实体列表的页面吗?我们需要对其进行一些过滤和排序。我昨天需要它。”

现在您面临一个选择:是否将此过滤逻辑添加到我的视图页面并在截止日期前完成,或者我是否花时间将数据访问代码移至控制器并重新设计我的视图,冒着错过视图的风险?截止日期并打破已经有效的方法?

您可能会采取简单的方法并将逻辑添加到视图中,但现在您的手上却出现了越来越混乱的情况。我们一直在使用带有 6,000 行代码隐藏文件的 VB6 和 Webforms 应用程序走这条路。相信我——你不会想去那里的。

另一个问题是 MVC 结构被编程社区很好地理解。如果其他人出现并尝试编写您的代码,您就会通过偏离传统方法来让他们变得更困难。

MVC 结构经过了时间的考验并且是健全的。在您完全理解其目的及其提供的好处之前,请尝试密切关注它。在你牢牢掌握规则之前,打破规则并不是一个好主意。

One purpose of the MVC pattern is to provide a structure that fits a wide range of common programming situations. To simplify:

  • Model: Describes the shape of your application, i.e. the parts of your software specific to your business.
  • View: Display the data to the user and transmit user events to the server.
  • Controller: Acts as a middleman between the view and the model.

What you're proposing "works," in the sense that it gets the data on the page where you want it. In the short term, it appears to be saving you time and effort, as you don't have to bother with controllers, viewbags, etc.

However, you are breaking the MVC structure in a way that you will probably regret later on. For example, say in a few weeks your boss comes to you and says, "Hey, you know that page you added to display that list of entities? We need to do some filtering and sorting on it. And I need it yesterday."

Now you're faced with a choice: Do I add this filtering logic to my view page and meet the deadline, or do I take the time to move the data access code to a controller and rework my view, at the risk of missing the deadline and breaking what's already working?

You'll probably take the easy way out and add the logic to the view, and now you've got a growing mess on your hands. We've been down this road with VB6 and Webforms apps with 6,000-line codebehind files. Trust me--you don't want to go there.

Another problem is that the MVC structure is well understood by the programming community. If someone else comes along and tries to work on your code, you're making it harder for them by deviating from the conventional approach.

The MVC structure is time tested and sound. Until you fully understand its purpose and the benefits it provides, try to follow it closely. It's not a good idea to break the rules until you have a firm grasp on them.

乞讨 2025-01-05 08:38:09

我的主要反对意见是关注点分离。一旦你开始从你的视图访问数据库,你的“视图”就不再只是一个视图了。将数据访问和视图完全分开确实非常方便。

为什么这种关注点分离很重要?它使得使用由这些干净的分离组成的系统变得更加容易。当您需要调整检索的数据时,您永远不需要对视图大惊小怪。只要视图获得正确的值,它就会正确显示。同样,如果您想更改值的显示方式,您可以修改视图,而不会破坏数据。

My main objection would be the separation of concerns. Once you start hitting your DB from your view, your "view" really isn't just a view anymore. It's really handy to have a clean separation between your data access and your view.

Why is this separation of concerns important? It makes it easier to work with systems that are composed with these clean separations. When you need to adjust what data is retrieved, you'll never need to fuss with the view. So long as the view gets the right value, it will display it correctly. Likewise, if you want to change how values are displayed, you can modify the view without any chance of borking the data.

温馨耳语 2025-01-05 08:38:09

问题是您的 View 中不应该有任何逻辑,因为这不是 MVC 方法。

MVC 是关注点分离

因此,您应该创建包含视图所需的所有数据的 ViewModel。

The thing is that you should not have any logic in your View because this is not the MVC approach.

MVC is Seperation of concern.

So you should create your ViewModel wich contains ALL the data your View needs.

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