Symfony and Clean Architecture

I have been reading and viewing conferences, a lot; about architecture and SOLID principles, mostly from Robert (Uncle Bob) Martin and from other good sources like Mathias Verraes and Mathias Noback.

Motivation

How many times do you find your self in front of a legacy project don’t even knowing a single piece of information about it.

So the first thing you see its the project directory structure, from it you can, maybe; identify the framework; but that doesn’t gave you any clue about the business rules.

One concept that caught my mind it’s the clean architecture.

As Uncle Bob says

Software architecture should be easily identified as architectural building blueprints

Lets take for example this Architectural Building Blueprints

Church Plan 152 Floor Plan

Its easy to identify a church in that, we can be confuse for a second with a theater but the little cabins surrounding the inner room gave it away that are confessionals and only church have those.

hnk-zagreb-6

In this case this its a real theater. We can see the balconies, the great space for the scenario.

So the details on the blueprints gave us an idea of the intention, the purpose; of that building.

So the questions is: How the blueprints for a software architecture looks alike?

Lets take an example of a Symfony2 application

symfony2-folder-structure

If we where to identify the purpose of this software we will only be able to identify only one detail, that its build to be a web delivered app and that maybe Symfony2 its the based framework. No single detail about what its the real intention of this software. This its because almost all the frameworks we use force us to follow its blueprint and we forgot that our main architecture, our business rules, our product intention; should not be case about how its delivered to the final user, we where force to hide our architecture inside the delivering mechanism. As Uncle Bob says:

The web is an I/O mechanism

So how to

This post its about a little example of how can you accomplish this with Symfony. Recently i force my self to follow this rules and good practices, keep my architecture as clean and SOLID as possible and i made it with my web framework of preference Symfony.

So lets try to identify this product through my new blueprint

Screenshot from 2015-08-26 11:15:24

We have Costumer, Post, Purchase, Shipment; from that we can identify an e-commerce solution. We can also see Comment, Message and Notification; that gave us the idea that a social and real time integration in that e-commerce. Lastly we can see development tools and framework in there; sure we need those two to make the whole solution works but those are not the main reason to exist. We can even move those two to another repository, we can even have a dedicated repository for each one of the main details of our architecture, that its up to you; but sure we can easily identify our software identity, our product reason to exists.

Now our blueprint explicitly expose the intention of this software. We manage to leave as a single detail the delivering mechanism, we don’t even know from this blueprint if its the web, it can even be console delivered; sure we can open the framework detail and that will lead us to that knowledge that its web delivered but up front we can not be sure about it.

What do we need to do so Symfony can understand our architecture, easy lets import it on the aoutoload.php file like this:

$loader->addPsr4("Costumer\\", __DIR__.'/../../Costumer', true);
$loader->addPsr4("Post\\", __DIR__.'/../../Post', true);
$loader->addPsr4("Purchase\\", __DIR__.'/../../Purchase', true);
$loader->addPsr4("Shipment\\", __DIR__.'/../../Shipment', true);
$loader->addPsr4("Comment\\", __DIR__.'/../../Comment', true);
$loader->addPsr4("Notification\\", __DIR__.'/../../Notification', true);
$loader->addPsr4("Message\\", __DIR__.'/../../Message', true);

Our architecture its much cleaner now. We need to keep following the SOLID principles and the good design patterns, but its a good start.

Conclusions

Besides any good practice proposed by frameworks, no matter how fast you wanna go; dedicate time to design a clean architecture will pay off at the end and you will be going even more fast that you thought.

Thanks

I want to gave all the credits for this post to Robert C Martin since the reason of this post its only to extend this great knowledge.

Advertisements

3 Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s