Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

2/12/2014

SOA - Service Oriented Architecture in Laravel

My previous post title was n-tier meets MVC.
In this post I want to add one more thing to that conclusion - n-tier meets MVC with the help of SOA.

Yes, besides n-tier (client/server) and MVC our favourite framework is implementing one more well known architectural pattern. It is called SOA or Service Oriented Architecture. Wikipedia has got pretty good explanation of SOA.


Take a look at this sentence from Wiki:
Service provider: The service provider creates a web service and possibly publishes its interface and access information to the service registry.

Sounds familiar ? Well it took me a while untill I finally realized what is Laravel doing in one of its core parts. Like many Laravel developers I was struggling with facades, providers and containers terminology without knowing that it is just an implementation of well known pattern. Modified, but concept is still the same.

Less obvious part of this  is why Taylor Otwell actually decided to design framework with SOA style incorporeted ?
Knowing that he has strong .NET background can help. In Microsoft world, they traditionally care about services. I was teached to think Restfully, so this was hard concept to grasp.

So the story is:

In the middle of the Laravel  kingdom lives Application, that depends on its citizens and servants. Application can exists by its own, but there is no point in empty kingdom. Every kingom has a treasury.  So they build one. In this kingdom it is called Container . 
Then Application defined  a rules and exposed them publically through its main  accociate - ServiceProvider class. All new citizens have to behave and look the way it is defined by the king.
ServiceProvider announced that everyone who wants to be a part of Application kingdom must implements two methods:

  • register and
  • boot

Register to become part of kingdom and boot to invite their friends to join. Also, Application allows some level of freedom to their citizens and let them choose their names and how they will be called. Speeking in Laravel terms, they can use Aliases and Facades for their access.

But, not everything is perfect . Some citizens are on the higher position then others. So they are treated specially. e.g. Request class is called very early, and it is bound directly, without use of ServiceProvider class.
On the other hand some citizens choose fancy names like Config, but underneath they are originally called Repository.

What would be a kingdom without a police ? Events are everywhere.
They provide welcome to every new citizen (provider) and take care of illegal activities. If something bad happened they will fire its main weapon - Exception. In our kingdom it is allowed to have private police.
It is no problem. Even better - it is a free gift from one of the highly rated Applications servants. If you want to take care if right type of rules are applied in your Validation, just call Event police - and it will fire all exceptions.  

So, even it sometimes looks that things are out of order in typical Laravel application, it is just because developer didn't understood properly how framework is internally designed and with what intention in mind. In all other casess, we have great tool for building web applications.

Thanks for reading.

No comments:

Post a Comment