我应该如何在Cocoa应用程序中进行数据库调用以符合MVC?

时间:2022-10-05 12:41:07

I am developing a Cocoa based application that will make several calls to a Postgres database (using PGSQLKit) over the course of a user's transaction. The application is a kiosk of sorts.

我正在开发一个基于Cocoa的应用程序,它将在用户的事务过程中多次调用Postgres数据库(使用PGSQLKit)。该应用程序是各种各样的信息亭。

What I'd like to know is the proper way to make database calls throughout the application. In the spirit of MVC, I know I should create objects for the data that is returned from the database (which I plan on doing) and then work with those objects. Where I need clarification is where I should place my database calls.

我想知道的是在整个应用程序中进行数据库调用的正确方法。根据MVC的精神,我知道我应该为从数据库返回的数据创建对象(我计划这样做),然后使用这些对象。我需要澄清的地方是我应该放置数据库调用的地方。

Basically it comes down to these questions I have:
-In order to interact with the database, should I create a separate database controller class that would make database calls? I feel making database calls directly from a view controller would not be appropriate.
-Assuming I create a database controller, should my view controllers trigger the database controller class and make DB calls that way?
-When the application loads, should I make a connection to the datase through the database controller and close it when the user's transaction is complete? I'll be making several calls throughout the transaction so I don't think i should close the connection each time.

基本上它归结为我的这些问题: - 为了与数据库交互,我应该创建一个单独的数据库控制器类来进行数据库调用吗?我觉得直接从视图控制器进行数据库调用是不合适的。 - 假设我创建了一个数据库控制器,我的视图控制器是否应该触发数据库控制器类并以这种方式调用DB? - 当应用程序加载时,我应该通过数据库控制器建立数据连接,并在用户的事务完成时关闭它吗?我将在整个交易过程中进行多次调用,所以我认为我不应该每次关闭连接。

1 个解决方案

#1


1  

-In order to interact with the database, should I create a separate database controller class that would make database calls? I feel making database calls directly from a view controller would not be appropriate.

- 为了与数据库交互,我应该创建一个单独的数据库控制器类来进行数据库调用吗?我觉得直接从视图控制器进行数据库调用是不合适的。

Correct. In some cases you may even have another layer of model objects on top of the database controller. Most of the app shouldn't care that the data is stored in a Postgres database rather than some other way.

正确。在某些情况下,您甚至可能在数据库控制器的顶部有另一层模型对象。大多数应用程序不应该关心数据存储在Postgres数据库中而不是其他方式。

-Assuming I create a database controller, should my view controllers trigger the database controller class and make DB calls that way?

- 假设我创建了一个数据库控制器,我的视图控制器是否应该触发数据库控制器类并以这种方式调用DB?

Generally yes. Your view controllers will observe the model and tell the model when they need data. The model will let observers (including view controllers, though the model doesn't care who they are) know when relevant data becomes available or changes.

一般是的。您的视图控制器将观察模型并在需要数据时告诉模型。该模型将让观察者(包括视图控制器,尽管模型不关心他们是谁)了解相关数据何时可用或更改。

-When the application loads, should I make a connection to the datase through the database controller and close it when the user's transaction is complete? I'll be making several calls throughout the transaction so I don't think i should close the connection each time.

- 当应用程序加载时,我应该通过数据库控制器建立数据连接,并在用户的事务完成时关闭它吗?我将在整个交易过程中进行多次调用,所以我认为我不应该每次关闭连接。

That is an internal detail to the database controller. Generally this kind of object would maintain a long-lived connection. Usually that connection is made the first time the model is asked for data that it needs from the database rather than "at application start."

这是数据库控制器的内部细节。通常,这种对象将保持长期连接。通常,该模型第一次被要求从数据库中获取所需的数据而不是“在应用程序启动时”。

#1


1  

-In order to interact with the database, should I create a separate database controller class that would make database calls? I feel making database calls directly from a view controller would not be appropriate.

- 为了与数据库交互,我应该创建一个单独的数据库控制器类来进行数据库调用吗?我觉得直接从视图控制器进行数据库调用是不合适的。

Correct. In some cases you may even have another layer of model objects on top of the database controller. Most of the app shouldn't care that the data is stored in a Postgres database rather than some other way.

正确。在某些情况下,您甚至可能在数据库控制器的顶部有另一层模型对象。大多数应用程序不应该关心数据存储在Postgres数据库中而不是其他方式。

-Assuming I create a database controller, should my view controllers trigger the database controller class and make DB calls that way?

- 假设我创建了一个数据库控制器,我的视图控制器是否应该触发数据库控制器类并以这种方式调用DB?

Generally yes. Your view controllers will observe the model and tell the model when they need data. The model will let observers (including view controllers, though the model doesn't care who they are) know when relevant data becomes available or changes.

一般是的。您的视图控制器将观察模型并在需要数据时告诉模型。该模型将让观察者(包括视图控制器,尽管模型不关心他们是谁)了解相关数据何时可用或更改。

-When the application loads, should I make a connection to the datase through the database controller and close it when the user's transaction is complete? I'll be making several calls throughout the transaction so I don't think i should close the connection each time.

- 当应用程序加载时,我应该通过数据库控制器建立数据连接,并在用户的事务完成时关闭它吗?我将在整个交易过程中进行多次调用,所以我认为我不应该每次关闭连接。

That is an internal detail to the database controller. Generally this kind of object would maintain a long-lived connection. Usually that connection is made the first time the model is asked for data that it needs from the database rather than "at application start."

这是数据库控制器的内部细节。通常,这种对象将保持长期连接。通常,该模型第一次被要求从数据库中获取所需的数据而不是“在应用程序启动时”。