在MVC中,你将什么引用到你的模型类? [英] In MVC where do you put references to your model Classes?

查看:282
本文介绍了在MVC中,你将什么引用到你的模型类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想,有一段时间,在询问不同的人之后,没有任何人提供我所说的至少有一个具体的答案:



问题:



在iPhone应用程序中,应用程序应该保留对其模型类的引用(使用 MVC 方法)?



在iPhone(和Cocoa)应用程序中,我们拥有我们所谓的App Delegate,它们基本上开始了我们的应用程序,并处理了我们的控制器,也处理了UITouch事件。


$ b $应用程序委派是控制器吗?一个模型类?两个都没有?我想不知道这也令人困惑地知道在哪里放入模型参考。



示例:



你具有应用程序代理,该委托包含对应用程序的View Controller的引用。如果我的应用程序将使用模型类A(它是一个Web服务器守护程序类)和一个存储由该网络服务器查询的数据的B类。



您将在哪里存储参考A和B? (App Delegate?View Controller?Both?)



这里有很多选项,但作为一个例子,我真的很想知道你们将如何使用mvc来一起使用一个View的应用程序。

解决方案

将App放在AppDelegate中是很诱人的,但如果你开始这样做那么你的AppDelegate将会被充满参考资料。如果你正在做一个严格的MVC,那么你应该有三件事:




  • 一个模型

  • 视图控制器(仅用于视图逻辑)

  • 控制器(用于在视图和模型之间进行协调)



所以例如,我有一个模型Foo和一个Foo控制器。我会有:




  • Foo.m(Model)

  • FooViewController.m(显示Foo )

  • FooController.m(控制逻辑)



最后,回答你的问题我会在foo控制器中存储我的Foo的引用。我喜欢为我的控制器使用单身,但只是我。如果您使用单例,您可以执行以下操作: [[FooController sharedInstance] listOfFoos] 以获取您的Foo的


I have been wondering for a while, after asking different people and without any of them providing what I would call an "at least a bit concrete answer":

Question:

Where, in an iPhone application should an application keep the references to it's Model Classes (using the MVC approach) ?

In iPhone (and Cocoa) applications we have what we call the "App Delegate", which basically start's up our application and inits our controllers, also handles UITouch events.

So is the App Delegate a controller ? a model class ? none of the two ? I think not knowing that also makes confusing to know where to put the Model References.

Example:

You have the Application Delegate, that delegate contains a reference to your Application's View Controller. If my Application would use Model Class A ( which is a webserver daemon class ), and a Class B which stores data queried by that webserver.

Where would you guys store the references to A and B ? (App Delegate ? View Controller ? Both ? )

There are many options here, but as an example, I would really like to know how would you guys use mvc to put together this application which only uses one View.

解决方案

It is tempting to put everything in the AppDelegate, but if you start doing this, then your AppDelegate will be full of reference hacks. If you are doing a strict MVC, then you should have 3 things:

  • A Model
  • A View Controller (Only for view logic)
  • A Controller (For coordinating between the view and the model)

So for example, I have a model Foo and a Foo controller. I would have:

  • Foo.m (Model)
  • FooViewController.m (Displays a Foo)
  • FooController.m (Controls the logic)

And finally, to answer your question, I would store my references to Foo's in the foo Controller. I like to use singletons for my controllers, but thats just me. If you do use a singleton, you can just do something like this: [[FooController sharedInstance] listOfFoos] to get your Foo's

这篇关于在MVC中,你将什么引用到你的模型类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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