ios - 数据服务的单一责任原则
问题描述
我尝试将我的代码重构为干净,我想知道我是否朝着好的方向发展。我使用模型视图演示者模式,但当我想与数据库建立连接时,我也使用服务类。
protocol NewWorkoutServiceType {
func checkNewWorkout(workoutName: String) -> Bool
func saveNewWorkout(exercises: [ExerciseCellInNewWorkout], workoutName: String, workoutDate: String, completion: (WorkoutModelInfo) -> Void)
}
class NewWorkoutService: NewWorkoutServiceType {
let newWorkoutDBHandler: SaveNewWorkoutProtocol
let checkWorkoutDBHandler: CheckIfWorkoutExistsProtocol
init(newWorkoutDBHandler: SaveNewWorkoutProtocol, checkWorkoutDBHandler: CheckIfWorkoutExistsProtocol) {
self.newWorkoutDBHandler = newWorkoutDBHandler
self.checkWorkoutDBHandler = checkWorkoutDBHandler
}
func checkNewWorkout(workoutName: String) -> Bool {
return checkWorkoutDBHandler.checkIfWorkoutExists(workoutToCheck: workoutName)
}
func saveNewWorkout(exercises: [ExerciseCellInNewWorkout], workoutName: String, workoutDate: String, completion: (WorkoutModelInfo) -> Void) {
let savedWorkout = newWorkoutDBHandler.saveNewWorkout(exercises: exercises, workoutName: workoutName, workoutDate: workoutDate)
completion(savedWorkout)
}
}
这是我的服务类,我使用协议使其松散耦合。我已经阅读了有关 SOLID 和 SRP 的内容,并且我决定将我的代码分解成小块并承担一项责任。
protocol SaveNewWorkoutProtocol {
func saveNewWorkout(exercises: [ExerciseCellInNewWorkout], workoutName: String, workoutDate: String) -> WorkoutModelInfo
}
protocol CheckIfWorkoutExistsProtocol {
func checkIfWorkoutExists(workoutToCheck: String) -> Bool
}
class CheckWorkoutDBHandler: CheckIfWorkoutExistsProtocol {
func checkIfWorkoutExists(workoutToCheck: String) -> Bool {
let workoutModel = RealmService.shared.realm.object(ofType: WorkoutModelInfo.self, forPrimaryKey: workoutToCheck)
if workoutModel == nil {
return false
} else {
return true
}
}
}
class NewWorkoutDBHandler: SaveNewWorkoutProtocol {
func saveNewWorkout(exercises: [ExerciseCellInNewWorkout], workoutName: String, workoutDate: String) -> WorkoutModelInfo {
var newExerciseModel = [ExerciseModel]()
for section in 0..<exercises.count {
newExerciseModel.append(ExerciseModel(number: (section + 1),
exercise:
exercises[section].workoutModel.exercise,
exerciseSet:
Array(exercises[section].workoutModel.exerciseSet)))
}
let workoutModel = WorkoutModelInfo(workoutName: workoutName, workoutDate: workoutDate, exercises: newExerciseModel)
RealmService.shared.addModified(workoutModel)
return workoutModel
}
}
class NewWorkoutPresenter {
private let newWorkoutService: NewWorkoutServiceType
weak var newWorkoutPresenterDelegate: NewWorkoutPresenterDelegate?
init(newWorkoutPresenterDelegate: NewWorkoutPresenterDelegate, newWorkoutService:
NewWorkoutServiceType) {
self.newWorkoutPresenterDelegate = newWorkoutPresenterDelegate
self.newWorkoutService = newWorkoutService
}
}
而且我认为每个人都认为还可以,但是后来当我在 VC 中初始化 Presenter 时,它太大了,看起来很脏。因此,也许更好的选择不是将我的服务类划分为较小的部分(用于检查锻炼是否存在并保存锻炼的单独类)。
class ViewController: UIViewController {
private lazy var presenter = NewWorkoutPresenter(newWorkoutPresenterDelegate: self, newWorkoutService: NewWorkoutService(newWorkoutDBHandler: NewWorkoutDBHandler(), checkWorkoutDBHandler: CheckWorkoutDBHandler()))
}
我会很感激你的建议!
解决方案
推荐阅读
- c# - 获取第一个字符 '<' 和最后一个字符 '>' 之间的所有内容
- xamarin.forms - Xamarin Forms 如何结束当前通话
- uml - 试图确定我的图表中的哪些用例符合主要用例
- python - PyCharm:重构项目名称后,在 Settings->Django Support 中没有指定 manage.py 文件
- java - 将元素添加到数组和 switch 语句的问题,错误 ArrayIndexOutOfBoundsException
- python - 关键字之间的余弦相似度
- android - 将字符串解析为 Int Kotlin
- javascript - 为什么有时 javascript 菜单切换会变成链接而不是切换?
- javascript - 如何对数组 JavaScript 中的每个数组进行切片
- r - 在 R 中创建组织结构图