N
N
nepster-web2016-11-15 18:56:25
symfony
nepster-web, 2016-11-15 18:56:25

Is it the right way to design an application in symfony 3?

Previously, I spent a very long time on Yii2, then migrated to Laravel5, now I need to start comprehending a new zen.
The question will be regarding the approach to application design (we do not consider DDD).
I organized a certain repository, which will be a wrapper over the Doctrine repository:

<?php

namespace AppBundle\Contract\Repository;

use AppBundle\Entity\Product;

/**
 * Interface Product Repository
 * @package AppBundle\Contract\Repository
 */
interface ProductRepository
{
    public function findById($id);

    public function findAll();

    public function save(Product $product);

    public function delete(Product $product);
}

<?php

namespace AppBundle\Repository;

use AppBundle\Contract\Repository\ProductRepository as ProductRepositoryInterface;
use AppBundle\Entity\Product;

class ProductRepository implements ProductRepositoryInterface
{
    public function findById($id)
    {
        return 'findById';
    }

    public function findAll()
    {
        return 'findAll';
    }

    public function save(Product $product)
    {
        return 'save Product';
    }

    public function delete(Product $product)
    {
        return 'delete Product';
    }
}

Handler (service):
<?php

namespace AppBundle\Contract\Handler;

use AppBundle\Entity\Product;

interface ProductCreateHandler
{
    public function process(Product $product);
}

<?php

namespace AppBundle\Services\Hendler;

use AppBundle\Contract\Handler\ProductCreateHandler as ProductCreateHandlerInterface;
use AppBundle\Contract\Repository\ProductRepository;
use AppBundle\Entity\Product;

class ProductCreateHandler implements ProductCreateHandlerInterface
{
    protected $productRepository;

    public function __construct(ProductRepository $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public function process(Product $product)
    {
        return 'Создать товар: ' . $product->getName();
    }
}

Configuration:
services:
   ...

# Репозитории
    app.product.repository:
        class: AppBundle\Repository\TestProductRepository

# Сервисы
    app.product.create.handler:
        class: AppBundle\Services\Hendler\ProductCreateHandler
        arguments: ['@app.product.repository']

There are several questions:
- Will it be correct for each action (create / edit / delete) to make its own handler (for example, ProductCreateHandler)?
- How to properly organize work with controllers?
I paid attention to these 3 articles, but not everyone understood what the authors are doing?
ankitchauhan22.blogspot.com.au/2013/02/controllers...
knpuniversity.com/screencast/question-answer-day/c...
https://pooteeweet.org/blog/1947
That is , they create a service for each controller and inject it into the action? But in fact, the handler can do this, the only difference is that it throws an exception and does not generate a response.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2016-11-15
@nepster-web

services:
    app.product_repository:
        class: AppBundle\Repository\ProductRepository
        factory: ['@doctrine.orm.default_entity_manager', getRepository]
        arguments:
            - AppBundle\Entity\Product

// $this->container->get('app.product_repository')->getClassName();
Remove the save and delete methods. No need to drag old habits onto the new system.
Symfony and Doctrine use DataMapper, not ActiveRecord The
handler is an entity_manager
In the AppBundle context, you don't need interfaces and other abstraction - it's really redundant, except when you are working with or with domain models that are in the vendor folder or vice versa - you yourself vendor and AppBundle should depend on your interfaces.
I suppose that 90% of your questions related to architecture you will find in the answers of Sergey Protko
When working with Symfony, 80% of the time should be spent solely on the configuration of the application. Read the "Best Practices" and if you have questions, "how to stick it here" - believe me, google will not let you down, everything is much simpler than it seems.
"Will it be right for each action (create / edit / delete) to make its own handler" - no, of course. Why do you need to produce for each action in the class, only to call just a couple of lines in it.
Regarding the articles ... It would be fresher, really. Go to github and take a look at the most popular bundles.

S
Sergey, 2016-11-18
Protko @Fesor

Not correct. Read Eric Evans. Read about unit of work. Read about "save your repositories from save".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question