Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony project design pattern

The symfony 2 installer gives a best practice directory structure, but not a lot is said about how these directories should be used, where does each piece of code belong, sure the symfony framework narrows it down to views, entities and controllers, services etc. but more often then not, programmers end up putting a DQL query inside a controller and some logic to handle a specific task, while this does the job, there has to be a better method, even beyond symfony's out of the box directory structure, currently on a quest for better design pattern that would modularize and reuse as much as possible

On this quest, found a couple good articles on the interwebz, and spent a day on it, came up with a plan to separate all DB interactions to repositories, all logic to services, and keeping the controller 'thin' which acts as a central point for calling methods from repository and calling services. sounds good, will be modular and code can be reused.... BUT

This somehow moves away from OOP concepts and right into procedural programming, not that there is anything wrong with it, its just not leveraging the powerful concepts of OOP

Could making the objects more 'powerful' by adding in more functionality make it better? the services by definition should do a unit of task, will my approach make them big and ugly

a few good points on this blog here but could't really make out what they were trying to suggest as a solution

In general, the more behavior you find in the services, the more likely you are to be robbing yourself of the benefits of a domain model. If all your logic is in services, you've robbed yourself blind -Martin Fowler

to summarize is Service oriented approach not leveraging the concepts of OOP? and how could this be supplemented

like image 409
pinch boi triggered af Avatar asked May 11 '16 05:05

pinch boi triggered af


People also ask

Which design pattern is used in Symfony?

Symfony is based on the classic web design pattern known as the MVC architecture, which consists of three levels: The Model represents the information on which the application operates--its business logic. The View renders the model into a web page suitable for interaction with the user.

What is Symfony architecture?

Advertisements. Symfony is basically a collection of high quality components and bundles. Components are collection of classes providing a single core functionality. For example, Cache component provides cache functionality, which can be added to any application.

What is Symfony project?

Nov 17–18, 2022. SymfonyLive Paris 2023. Mar 23–24, 2023. Symfony is an Open Source PHP Web applications development framework. It was originally conceived by the interactive agency SensioLabs for the development of web sites for its own customers.

What is strategy pattern PHP?

Strategy is a behavioral design pattern that turns a set of behaviors into objects and makes them interchangeable inside original context object. The original object, called context, holds a reference to a strategy object.


3 Answers

One of most important thoughts which you can find in Fowler's "Patterns of Enterprise Application Architecture", is that you should use right tools for your job. Depending on the complexity of your problem and domain, one or the other design pattern/architectural pattern may perform better. And following that way, in contrast to old frameworks (ZF1, SF1), SF2+ doesn't force you to go for any architectural pattern or organizing your business logic in specific way. You can even put everything outside bundles structure. So the best thing, which you can do, is to try learn more about design patterns (generally). And then, when you need, you will be able to pick one which will work best for your case.

You may want to look at:

  • mentioned earlier "Patterns of Enterprise Application Architecture", especially chapter: "Domain Logic Patterns".
  • The Four Architectures that will inspire your programming short review of different architectures.
  • any of books covering DDD, like: E.Evans "Domain-Driven Design", V.Vernon "Implementing Domain-Driven Design" or S.Millett and N.Tune "Patterns, Principles and Practices of Domain-Driven Design"
  • Domain-Driven Design in PHP decent book covering implementation of DDD patterns in PHP application, with some examples based on SF.
  • The Clean Architecture in PHP implementation of The Clean Architecture for PHP, focusing a lot on framework independence (with examples for ZF2 and Laravel).
like image 127
michaJlS Avatar answered Oct 12 '22 18:10

michaJlS


With Domain Driven Design it creates fat model layers, and also calling a lot of services inside your controller may modularize your code, it still has its own set of challenges, it makes the design harder to create, and at some point there will be too many service calls inside the controller, what can actually be done is to use a few concepts from DDD.

two things to remember before starting DDD is :-

  1. cohesion - modules that change together
  2. coupling - modules that are dependent on each other

You need to lessen the coupling and recognize the cohesion and design the project according to it, main goal is to allow adding and removing or enable/disable/recombining of modules in future, in case you decide to alter the project

instead of having bundles depend on each other use interfaces instead

namespace MyProject\UserBundle\Activity ;

use MyProject\NotificationBundle\Notification;
use MyProject\MailBundle\Mail;

where UserBundle is coupled with NotificationBundle and MailBundle use interfaces that abstract the bundles instead

namespace MyProject\UserBundle\Activity ;

use MyProject\ServiceBundle\NotificationInterface;
use MyProject\ServiceBundle\MailInterface;

Main concepts of DDD are

  1. value objects - an object that holds some data and is passed back and forth and does not focus identity but rather on the data that it holds, value objects can be used in symfony using embeddables
  2. Entity - an object that holds some data but is dependent on an identity value rather than its data value
  3. Repository - use of repository classes to make database operations make it possible to change the implementation later on, as it is centralized to one place
  4. Aggregate - when several object belong together like a number of things that belong to a single category (eg: songs that belong to a genre), the entire collection of objects are treated as one and to make alteration to these objects would be thought on single aggregate object
  5. Domain Events - any change in the state of the domain/data is regarded as an event, in symfony there are Event Listeners and Subscribers which can stand in for domain events, this is one of the most important concepts of DDD, filter also a great deal of improvement to the code
  6. services - services are callable modules of code that do a unit of work, services allow code to be reused and are easier for testing and updating, services in symfony should contain the domain logic and the service layer can get pretty fat, this along with events make the code faster and more maintainable
  7. Factory patter - this patter uses a 'factory' object that generates a different object, by doing this the name of the second object is abstracted, this helps when refactoring the code

these are generalized concepts that need to be applied to code that is highly situational, proper DDD requires the code to be modular which is a challenging task to achieve and need some experience on the engineer's part.

as already stated by others, books by 'martin fowler', ' V.Vernon' design patterns by ' gang of four' etc would be a good read

like image 31
some_groceries Avatar answered Oct 12 '22 19:10

some_groceries


First of all there are best practices where to place files/components: http://symfony.com/doc/current/cookbook/bundles/best_practices.html.

Secondly http://symfony.com/doc/current/best_practices/business-logic.html:

In Symfony applications, business logic is all the custom code you write for your app that's not specific to the framework (e.g. routing and controllers). Domain classes, Doctrine entities and regular PHP classes that are used as services are good examples of business logic.

So you are not forced to use any architecture for your business logic.

Regarding your question - you can implement objects with operations and declare them as services (or use them directly, but in this case when you want to replace some functionality you'll need to rework more code).

  • there are always a lot of principles of software designing and we select what we think is more correct and makes more sense for a project and Symfony doesn't force us to use some special design pattern.
like image 1
Dmitry Grachikov Avatar answered Oct 12 '22 17:10

Dmitry Grachikov