【问题标题】:Where's the best place to call methods that interact with my database?调用与我的数据库交互的方法的最佳位置在哪里?
【发布时间】:2019-11-05 03:17:06
【问题描述】:

我正在创建一个与 Firestore 数据库交互的应用。到目前为止,我有一个单例类 DatabaseManager,它包含与 Firestore 数据库相关的所有方法(即 get/post 方法)。

我有一个名为 User 的用户模型,它具有 nameemailphotoURL 等属性和一些特定于应用程序的属性。任何用户都可以编辑他们的个人资料以更新来自名为 EditProfileViewController 的视图控制器的信息。

现在我的问题是:最好从EditProfileViewControllerUser 或其他地方调用DatabaseManager.shared.updateInfo(forUser: user)(其中userUser 实例)?

很抱歉,如果这是一个显而易见的问题,但应用程序中有很多地方我需要类似的逻辑,所以我想知道什么是最好的设计。此外,我确信这个问题与 MVC 的关系比与 Firebase/Swift 的关系更大。

【问题讨论】:

    标签: swift firebase model-view-controller design-patterns architecture


    【解决方案1】:

    一些想法:

    1. 而不是直接使用DatabaseManager.shared.update(for:) 访问单例,我可能有一个用于数据库管理器的属性,使用DatabaseManager.shared 初始化/注入它,并且有任何需要与数据库交互的东西都使用该引用,例如,dataManager.update(for:)。目标是允许您的单元测试在必要时模拟数据库管理器。

    2. 我不倾向于让视图控制器直接与DatabaseManager 交互。我们中的许多人将直接与 UIKit/AppKit 对象交互的视图控制器视为 MVC/MVP/MVVM/whatever 的更广泛的“V”的一部分。我们经常将业务逻辑(包括与数据库管理器的交互)从视图控制器中提取出来。

      我个人也不会把它埋在User 对象下。我会将它放在数据库管理器的扩展中,并从视图模型、演示者或您个人想要使用业务逻辑调用该对象的任何对象中调用。

    【讨论】:

      【解决方案2】:

      您是否有理由使用单例来包含所有 Firestore 逻辑?用户模型应该包含方法 updateInfo。

      这是我在 Firestore 中使用的示例:

      class Group {
      
          // can read the var anywhere, but an only set value in this class
          private(set) var groupName: String!
          private(set) var guestsInGroup: Int!
          private(set) var joinedGroup: Bool!
          private(set) var timeStampGroupCreated: Date!
          private(set) var documentId: String!
      
          init(groupName: String, guestsInGroup: Int, joinedGroup: Bool, timeStampGroupCreated: Date, documentId: String) {
              self.groupName = groupName
              self.guestsInGroup = guestsInGroup
              self.joinedGroup = joinedGroup
              self.timeStampGroupCreated = timeStampGroupCreated
              self.documentId = documentId
          }
      
      
          // method to parse Firestore data to array, that table view will display
          class func parseData(snapshot: QuerySnapshot?) -> [Group]{
              var groups = [Group]()
      
              guard let snap = snapshot else { return groups }
              for document in snap.documents {
                  let data = document.data()
                  let groupName = data[GROUP_NAME] as? String ?? "No Group Name"
                  let guestsInGroup = data[GUESTS_IN_GROUP] as? Int ?? 0
                  let joinedGroup = data[JOINED_GROUP] as? Bool ?? false
                  let timeStampGroupCreated = data[TIMESTAMP_GROUP_CREATED] as? Date ?? Date()
                  let documentId = document.documentID
      
                  // add objects with fetched data into thoughts array
                  let newGroup = Group(groupName: groupName, guestsInGroup: guestsInGroup, joinedGroup: joinedGroup, timeStampGroupCreated: timeStampGroupCreated, documentId: documentId)
      
                  groups.append(newGroup)
              }
              return groups
          }
      }
      

      【讨论】:

      • "用户模型应该包含方法 updateInfo" - 为什么?我项目的另一个目标是尝试在尽可能少的地方包含任何参考任何 Firebase 类/对象/任何内容的内容,以便在我愿意的情况下更轻松地迁移到自定义后端,所以我不会在我的模型中有一个方法可以接收像 QuerySnapshot 这样的 Firebase 对象。
      猜你喜欢
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多