MVC 设计模式中的项目结构 iOS
Project Structure in MVC design Pattern iOS
首先,我很了解 MVC 并且一直在项目中使用它,但是当涉及到组织 classes 和那里的角色时,我有点不确定是否正确实施。让我们来看一个场景:
将显示所有员工和部门的示例。数据将从 Web 服务中获取(Json)并将存储为离线(核心数据)。
所以 MVC 模式将是:
- View 将是我的故事板,包含 Employee 和 Department UIViewController。
- 控制器将是 EmployeeViewController.swift 和 DepartmentViewController.swift
模型将是 Employee.swift 和 Department.swift
class Employee: NSObject {
var name: String?
}
class Department: NSObject {
var departmentName: String?
}
将调用 Web 服务的 ServiceManager。
ParseData 将解析 Web 服务响应并将其转换为 Employee 和 Department 对象
CoreDataManager 是单例 class 来管理离线数据库上的 CRUD 操作。
以下是关于上述场景的一系列问题:
- 我的理解对吗?我尝试构建的结构是否遵循正确的 MVC?
- 控制器将如何与这些组件(服务管理器、ParseData、CoreDataManager)交互。是否应该有另一个 class 来促进控制器和数据管理之间的通信(如果控制器这样做,那么它将是一个紧耦合的结构并且也是巨大的)。
- 模型是否应该有除 属性 和初始化方法之外的任何代码,因为我看到的大多数模型只有 属性 声明?
- 是否应该有单独的 UIView classes 而不是故事板来创建合适的 MVC 结构?
Is my understanding correct? Is the structure which i am trying to
build follows proper MVC?
首先我会说 "proper" MVC 将取决于你问的是谁。它的起源通常归因于 Trygve Reenskaug,当时他在 70 年代将其引入 Smalltalk。然而,他的 MVC 类型与当今最常用的臃肿版本有很大不同。关于 MVC 的现代思考方式是
- 模型 = 主要是一个哑巴 class,主要封装数据
- View = 我们在屏幕上显示的任何内容
- 控制器 = 几乎可以做所有事情的一大块代码,
有时由经理 class 或两个
分担
但是,Reenskaug 会有一个模型、一个视图和一个按钮控制器。为了一个标签。对于一个字段。我并不是说那是我们应该争取的,但应该有比使用 Massive View 更好的方法来构建项目C控制器模式(在 iOS 社区中被戏称为)。
幸运的是,有。
Uncle Bob is preaching Clean Architecture. There are several implementations of this, and various people have made their own implementations of this for iOS, like VIPER and Clean Swift.
How the controller will interact with these components (Service
Manager, ParseData, CoreDataManager). Should there be another class
which will facilitate the communication between controller and data
management(if controller does this then it will a tightly-coupled
structure and massive as well).
遵循 Clean Architecture 的原则,您应该将这些功能封装到层中,这样您不仅可以将代码拆分为多个组件,还可以在需要时用其他组件替换它们. (但是,是的,至少要避免将所有这些都放在你的控制器中!)
Should Model be having any code other then property and initialization
method as most of the model which i have seen only have property
declaration?
同样,这里没有单一的答案。 "real" OOP 的一些支持者会说每个对象都应该是自服务的(即模型对象应该知道如何持久化自身),而其他人则将此类操作的知识提取到 "managers" 中。将代码用于将对象持久化到对象中可能意味着将持久化功能散布到许多对象中,或者需要您依赖 subclassing 或其他解决方案来避免这种耦合。
Should there be separate UIView classes instead of storyboard to
create a proper MVC structure?
Storyboard 与否并不能确定您是否使用 "proper" MVC。此外,您选择 class 的 种类 (UIView 或 UIViewController)来表示视图也不重要。您的 ViewController 可以被简化到不包含任何逻辑的程度(将它确实具有的逻辑转发给另一个 class,即 VIPER 中的 Presenter)。
我建议阅读有关 Clean Architecture 的内容,也许还可以观看 video of Uncle Bob explaining it, read other people's reports on implementing it,然后考虑 MVC 是否是您的 iOS 项目的正确模式。
首先,我很了解 MVC 并且一直在项目中使用它,但是当涉及到组织 classes 和那里的角色时,我有点不确定是否正确实施。让我们来看一个场景:
将显示所有员工和部门的示例。数据将从 Web 服务中获取(Json)并将存储为离线(核心数据)。
所以 MVC 模式将是:
- View 将是我的故事板,包含 Employee 和 Department UIViewController。
- 控制器将是 EmployeeViewController.swift 和 DepartmentViewController.swift
模型将是 Employee.swift 和 Department.swift
class Employee: NSObject { var name: String? } class Department: NSObject { var departmentName: String? }
将调用 Web 服务的 ServiceManager。
ParseData 将解析 Web 服务响应并将其转换为 Employee 和 Department 对象
CoreDataManager 是单例 class 来管理离线数据库上的 CRUD 操作。
以下是关于上述场景的一系列问题:
- 我的理解对吗?我尝试构建的结构是否遵循正确的 MVC?
- 控制器将如何与这些组件(服务管理器、ParseData、CoreDataManager)交互。是否应该有另一个 class 来促进控制器和数据管理之间的通信(如果控制器这样做,那么它将是一个紧耦合的结构并且也是巨大的)。
- 模型是否应该有除 属性 和初始化方法之外的任何代码,因为我看到的大多数模型只有 属性 声明?
- 是否应该有单独的 UIView classes 而不是故事板来创建合适的 MVC 结构?
Is my understanding correct? Is the structure which i am trying to build follows proper MVC?
首先我会说 "proper" MVC 将取决于你问的是谁。它的起源通常归因于 Trygve Reenskaug,当时他在 70 年代将其引入 Smalltalk。然而,他的 MVC 类型与当今最常用的臃肿版本有很大不同。关于 MVC 的现代思考方式是
- 模型 = 主要是一个哑巴 class,主要封装数据
- View = 我们在屏幕上显示的任何内容
- 控制器 = 几乎可以做所有事情的一大块代码, 有时由经理 class 或两个 分担
但是,Reenskaug 会有一个模型、一个视图和一个按钮控制器。为了一个标签。对于一个字段。我并不是说那是我们应该争取的,但应该有比使用 Massive View 更好的方法来构建项目C控制器模式(在 iOS 社区中被戏称为)。
幸运的是,有。
Uncle Bob is preaching Clean Architecture. There are several implementations of this, and various people have made their own implementations of this for iOS, like VIPER and Clean Swift.
How the controller will interact with these components (Service Manager, ParseData, CoreDataManager). Should there be another class which will facilitate the communication between controller and data management(if controller does this then it will a tightly-coupled structure and massive as well).
遵循 Clean Architecture 的原则,您应该将这些功能封装到层中,这样您不仅可以将代码拆分为多个组件,还可以在需要时用其他组件替换它们. (但是,是的,至少要避免将所有这些都放在你的控制器中!)
Should Model be having any code other then property and initialization method as most of the model which i have seen only have property declaration?
同样,这里没有单一的答案。 "real" OOP 的一些支持者会说每个对象都应该是自服务的(即模型对象应该知道如何持久化自身),而其他人则将此类操作的知识提取到 "managers" 中。将代码用于将对象持久化到对象中可能意味着将持久化功能散布到许多对象中,或者需要您依赖 subclassing 或其他解决方案来避免这种耦合。
Should there be separate UIView classes instead of storyboard to create a proper MVC structure?
Storyboard 与否并不能确定您是否使用 "proper" MVC。此外,您选择 class 的 种类 (UIView 或 UIViewController)来表示视图也不重要。您的 ViewController 可以被简化到不包含任何逻辑的程度(将它确实具有的逻辑转发给另一个 class,即 VIPER 中的 Presenter)。
我建议阅读有关 Clean Architecture 的内容,也许还可以观看 video of Uncle Bob explaining it, read other people's reports on implementing it,然后考虑 MVC 是否是您的 iOS 项目的正确模式。