模型视图控制器:控制器或模型是否从服务器获取数据?

发布于 2024-11-25 01:52:10 字数 107 浏览 0 评论 0 原文

例如:假设我正在获取一个名称列表并将其保存到 NSMutableArray 中。我是否实现了实际调用服务器来获取控制器(UIViewController)或模型(Friends 对象)中的数据的方法?

For example: Let's say I'm grabbing a list of names and saving it to an NSMutableArray. Do I implement the method of actually calling the server to fetch the data in the controller (UIViewController) or the model(Friends object)?

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

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

发布评论

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

评论(6

对你而言 2024-12-02 01:52:10

这是一个设计决策,取决于您想要实现的目标。如果您的模型仅在单个服务的上下文中有意义,或者如果您希望模型提供对服务器上所有数据的访问,则将与服务器的连接构建到您的数据模型中。例如,如果您正在为 Twitter 或 Flickr 等服务构建客户端,这可能是有意义的。

另一方面,如果您只是从服务器获取文件并且事情就这样结束了,那么在控制器中进行通信可能是有意义的。控制器的可重用性往往较低,并且更适合应用程序的特定行为。保留模型中数据来源的细节可以使模型更具可重用性。它还使测试变得容易——您可以编写仅读取本地文件并将数据存储在模型中的测试代码。

It's a design decision that depends on what you're trying to accomplish. If your model only makes sense in the context of a single service, or if you want your model to provide access to all the data on the server, then build the connection to the server into your data model. This might make sense if you are, for example, building a client for a service like Twitter or Flickr.

On the other hand, if you're just grabbing a file from a server and that's the end of it, it may make sense to do the communication in the controller. Controllers tend to be less reusable and more customized for the particular behavior of the application. Keeping the specifics about where the data comes from out of the model makes the model more reusable. It also makes it easy to test -- you can write test code that just reads a local file and stores the data in the model.

屋檐 2024-12-02 01:52:10

这是个好问题。我认为最好的方法是通过控制器,因为它将您的模型与需要其他模型存在才能正常工作的模型分离。尽管我认为在模型中这样做也不会违反“正确的 mvc”。

That's a good question. I think the best way is through a controller because it decouples your model from requiring the other model to be present for it to work properly. Although I don't think you violate "proper mvc" by doing it in the model either.

醉态萌生 2024-12-02 01:52:10

我想你想把它放在模型中。您要做的就是询问模型中的数据,然后模型将处理如何填充自身,无论它是来自内部数据存储还是外部数据存储(例如服务器)。

I think you want to put it in the model. What you'll do is interrogate the model for the data and then the model will handle how to populate itself whether it's from an internal data store or an external one (like a server).

走过海棠暮 2024-12-02 01:52:10

一种方法是使用存储库模式。为此,您在 Model 文件夹中创建 Repository 对象,并将所有与数据库相关的方法放入其中。您的控制器调用存储库类来获取数据。这允许您将真实的模型对象与数据库访问方法分开。

One approach is to use the repository pattern. To do this, you create Repository objects in your Model folder and you place all of you database-related methods in them. Your controllers call the repository classes to get the data. This allows you to separate the real model objects from the database accessing methods.

以可爱出名 2024-12-02 01:52:10

我使用 MVCS 模式(模型-视图-控制器-存储),这是我在 Aaron Hillegass 的书《IOS 编程:大书呆子牧场指南》(http://www.bignerdranch.com/book/ios_programming_the_big_nerd_ranch_guide_rd_edition_)

该存储是专门为获取数据而设计的,无论它来自服务器、本地文件、持久集合、数据库等。

它允许构建非常进化的应用程序。例如,您可以基于 Web 服务构建应用程序,当您想要保留数据时,您只需修改存储即可,而无需修改控制器中的一行代码。

它很像存储库模式(http://msdn.microsoft.com/en -us/library/ff649690.aspx) (参见 BobTurbo 的回答)

I use the MVCS pattern (Model-View-Controller-Store), which I discovered in Aaron Hillegass's book "IOS Programming: The Big Nerd Ranch Guide" (http://www.bignerdranch.com/book/ios_programming_the_big_nerd_ranch_guide_rd_edition_)

The store is specifically designed to fetch the data, whether it comes from a server, a local file, a persisted collection, a database, etc.

It allows to build very evolutive applications. For example, you can build your application based on a web service, and the day you want to persist your data, you juste have to modify the store, without having to modify a single line of code in your controller.

It's a lot like the Repository Pattern (http://msdn.microsoft.com/en-us/library/ff649690.aspx) (cf BobTurbo's answer)

猛虎独行 2024-12-02 01:52:10

我个人会创建一个 DAO,即数据助手类。当事情变得更加复杂时,很难遵循 Objective C 中严格的 MVC。但是,将其放入模型或 VC 中也没有错。

I'd personally make a DAO, or data helper class. It's very hard to follow the strict MVC in objective C when things get more complicated. However, putting it in the model or the VC is not wrong as well.

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