Answer the question
In order to leave comments, you need to log in
When should you use the command bus?
Hello everyone,
When should I use commandBus? Should I use it for regular crud?
I'm using symphony and I like the forms component it provides.
public function new(Request $request): Response
{
$command = new CreateCurrencyCommand();
$form = $this->createForm(CurrencyType::class, $command);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->commandBus->handle($command);
return $this->redirectToRoute('admin_currency_index');
}
return $this->render('admin/currency/new.html.twig', [
'form' => $form->createView(),
]);
}
CreateCurrencyCommand
(Normal DTO which contains fields and getters / setters) and the creation of a currency looks very good, a very thin controller and business logic is placed in a separate handler that creates an entity and saves it to the database. CreateCurrencyCommand
instead of UpdateCurrencyCommand
. This is what the edit method looks like without command interventionpublic function edit(Request $request, Currency $currency): Response
{
$form = $this->createForm(CurrencyType::class, $currency);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('admin_currency_index');
}
return $this->render('admin/currency/edit.html.twig', [
'currency' => $currency,
'form' => $form->createView(),
]);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question