我应该在哪里创建对象?存储库?工厂?

Where should I create objects? repository? factory?

我有一个业务对象存储库,我需要根据数据创建不同的对象。我应该直接在回购中创建它们还是将该代码移动到其他地方 - 到工厂或业务逻辑层中的某些 class?

/**
 * @returns Applier
 */
class ApplierRepository implements IApplierRepositoryInterface {
  //some code
  public function find($id) {
    $data = $this->findBySql($id);

    //Is it a business logic?
    if($data['profile_id'] != null)
      $object = new ProfileApplier();
    if($data['user_id'] != null) {
      $user = $this->userRepository->find($data['user_id']);
      $object = new UserApplier($user);
    }
    //...
    return $object;
  }
}

我会考虑 Repository 作为 Data 之间的 抽象级别 访问级别 和您的应用程序逻辑find() 方法实际上是 工厂方法

为了清楚起见,假设您需要使用测试框架来测试class的逻辑。你会怎么做?您的 ProfileApplierUserApplier 和其他应用程序似乎调用了一些 datasources 检索用户数据。

在测试方法中,您需要将这些数据源替换为测试数据源。您还需要替换数据源访问方法。这就是 Repository 模式的设计目的。

更简洁的方法如下所示:

class AppliersFactory {
  IApplierRepository applierRepository;

  public AppliersFactory(IApplierRepository repo)
  {
    $this->applierRepository = repo;
  }

  // factory method, it will create your buisness objects, regardless of the data source
  public function create($data) {
    if($data['profile_id'] != null)
      $return new ProfileApplier();
    if($data['user_id'] != null) {
      $user = $this->applierRepository->find($data['user_id']);
      $object = new UserApplier($user);
    }
    //...
    return $object;
  }
}

在您的实际应用程序中使用此存储库

class RealApplierDataStorageRepository implements IApplierRepositoryInterface {
  //some code, retrieves data from real data sources
  public function find($id) {
    //...
  }
}

并在测试模块中使用这个来测试您的逻辑

class TestApplierDataStorageRepository implements IApplierRepositoryInterface {
  // some code, retrieves data from test data sources (lets say, some arrays of data)
  public function find($id) {
    //...
  }
}

希望有帮助