gpt4 book ai didi

php - 交响乐 : should logic about formatting a parameter be in repository or controller?

转载 作者:搜寻专家 更新时间:2023-10-31 21:00:12 24 4
gpt4 key购买 nike

根据 Symfony 方式的最佳实践是什么?

选项 1:

class MyController extends Controller
{
public function myAction(...)
{
// ..
$example = $this->getDoctrine()->getRepository('AppBundle:Contract')->getContracts(array(
'company_id' => $company->getId(),
'contract_month_date_start' => date('Y-m-d', strtotime('first day of this month', $contractDate->getTimestamp())),
'contract_month_date_end' => date('Y-m-d', strtotime('last day of this month', $contractDate->getTimestamp())),
));
// ..
}
}

class ExampleRepository extends EntityRepository
{
public function getContracts($options)
{

//..

$qb->select('contract')
->from('AppBundle:Contract', 'contract')
->where('contract.companyId = :company_id')
->andWhere('contract.startDate < :contract_month_date_end')
->andWhere('contract.endDate < :contract_month_date_end')
->setParameter('company_id', $options['company_id'])
->setParameter('contract_month_date_start', $options['contract_month_date_start'])
->setParameter('contract_month_date_end', $options['contract_month_date_end']);

//..
}
}

选项 2:

class MyController extends Controller
{
public function myAction(...)
{
// ..
$example = $this->getDoctrine()->getRepository('AppBundle:Contract')->getContracts(array(
'company' => $company,
'contractDate' => $contractDate
));
// ..
}
}


class ExampleRepository extends EntityRepository
{
public function getContracts($options)
{

//..

$company_id = $options['company']->getId();
$contract_month_date_start = date('Y-m-d', strtotime('first day of this month', $options['contractDate']));
$contract_month_date_end = date('Y-m-d', strtotime('last day of this month', $options['contractDate']));

$qb->select('contract')
->from('AppBundle:Contract', 'contract')
->where('contract.companyId = :company_id')
->andWhere('contract.startDate < :contract_month_date_end')
->andWhere('contract.endDate < :contract_month_date_end')
->setParameter('company_id', $company_id)
->setParameter('contract_month_date_start', $contract_month_date_start)
->setParameter('contract_month_date_end', $contract_month_date_end);

//..
}
}

我觉得第二个更好(使用存储库的不同 Controller 之间的重复代码更少)。但我也觉得它给了存储库太多的责任。

最佳答案

TL;DR: in your example, your formatting logic should ideally be handled in an intermediate layer.

这里有一些改进:

首先,不要从您的 Controller 获取存储库。相反,将其声明为服务(例如,在您的 app/config/services.yml 中)。

此外,您可以使用新层抽象化对数据(= 您的存储库)的访问。通信将像这样完成:

Controller <=> 业务层 <=> 数据访问

  • Controller:负责调用业务层的正确方法并处理响应(呈现 HTML、返回 JSON 等)。
  • 业务层:负责应用的逻辑
  • 数据访问:负责查询您的数据库(例如)并获取不包含任何业务逻辑的数据。只能由业务层访问。

使用这样的架构将帮助您拥有更具可扩展性的应用程序,而无需花费太长时间来实现它。

在您的代码中,它将如下所示:

你的 Controller ,负责将用户的请求“路由”到正确的服务

class MyController extends Controller
{
public function myAction()
{
// Accessing the contract manager, previously declared as a service in services.yml
$contractManager = $this->get('manager.contract');
$contracts = $contractManager->getMonthlyContracts($company);
}
}

您的业务层,负责执行逻辑:

// This class must be declared as a service in services.yml
class ContractManager {
private $contractRepository;

public function __construct(ContractRepository $contractRepository)
{
$this->contractRepository = $contractRepository;
}

public function getMonthlyContracts(Company $company)
{
// The business layer is responsible for the logic of fetching the data. Therefore, its methods are business-related (here "find the monthly contracts for company X", which is very specific).
// It is its role to set the start and end dates, not the controller's
$contracts = $this->contractRepository->findByCompany(
$company,
new \DateTime('first day of this month'),
new \DateTime('last day of this month')
);

// Do some logic with the contracts if required...

return $contracts;
}
}

您的存储库,负责访问数据:

class ContractRepository extends EntityRepository
{
// The repository handles basic access to data without any logic, hence the generic "findByCompany" method name
public function findByCompany(Company $company, \DateTime $from, \DateTime $to)
{
$qb->select('contract')
->from('AppBundle:Contract', 'contract')
->where('contract.company = :company')
->andWhere('contract.startDate > :from')
->andWhere('contract.endDate < :to')
->setParameter('company', $company)
->setParameter('from', $from)
->setParameter('to', $to);

//...
}
}

请查看 Symfony 文档,了解如何在 services.yml 中将存储库声明为服务。

关于php - 交响乐 : should logic about formatting a parameter be in repository or controller?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44763724/

24 4 0