首页 > 解决方案 > 数据服务的单一责任原则

问题描述

我尝试将我的代码重构为干净,我想知道我是否朝着好的方向发展。我使用模型视图演示者模式,但当我想与数据库建立连接时,我也使用服务类。

 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()))
}

我会很感激你的建议!

标签: iosswiftuikit

解决方案


推荐阅读