Skip to main content

Laravel is Good, Facades Aren't

I've been working on some Laravel 4 based packages lately which inevitably results in me also looking at other packages.

I've noticed sometimes that people use facades at times that give me pause. The most troubling being from inside their model classes.

A quick google turned up this video which assures people "there's still an instance behind everything, we're fine".  Everything mentioned in the video is true except that there is a very glaring omission.

Scope

What usually goes out the door at the start of a long series of mishaps in software design is scope.

When the desire to obtain a solution is stronger than the desire to consider the implications of a firm approach, mistakes are sure to follow.  Sacrifices like this are made due to the assumption of a high cost to developing carefully.

What really is happening however is a false dilemma, being responded to with a convenience decision.

It's very easy to write model code like this:

     class MyModel extends Model {
          public function getUri() {

               return Request::root() . '/mymodel/' . $this->slug;

          }
     }

...and then think that you've done a great job at putting the code where it belongs, encapsulation, simplicity, elegance and other nice things.

Unfortunately, what you've also done is created a dependency by conflating an infrastructure concern with your model.  Understand that this isn't because you've used a static method, but because you've used a class resolved via the container that might not be ready yet.

So what?

You could be blithe about this and think "I'm always going to be in a request because I'm coding for the web!"  At which point, we're facing another convenient assumption and it becomes all too easy to see where things unravel:
  • Scope - Imagine you find the model class above performing in a scope that doesn't find itself within a request.  This could be in a cron, or it could be when you need your class running unauthenticated.  These are just two examples, but a change in context can and will catch you by surprise and your programs will crash trying to use a method that was made in haste.
  • Cross Cutting Concerns - Suddenly a method you've authored on a class requires a security check, but you don't have access to the necessary information from within the class to do this.  You're faced now with further convenience decisions that lead to bloated model code.  That can lead to duplication at best because you're now having to consider all angles up front.
  • Difficult usage tracking - Facades lean on some PHP magic to do their thing. This also causes a break in the dependency graph of your application.

What instead?

Make services!  There is a fine line here, but when you need to do an operation that blends disparate pieces of information and resources, make a service.  The dependencies of that service will represent the scope you wish to create for that particular operation.  Once that service is made, you can rest easy knowing it exists in a space that has all the right dependencies and state to perform the singular operation you're after.

The other benefit is that it gives you a centralized point to handle cross cutting concerns.  If one day you need to perform a secondary operation (security checks, analytics, accounting), you won't be adding more bloat to your model.  You will simply find the scope (read: service) you know the operation transpires in, and add it there.

I can assure you that by taking this approach, you'll be doing yourself a favour in the single responsibility, cohesion, repeatability and DRY departments.

Comments

Post a Comment

Popular posts from this blog

ASP.net Core 1 Projects on .NET Core 2

A small post for today although something I think could trip others up.  I'm trying out the latest WebJobs 3.0 beta which is a port of the 2.0 version with .net Core 2.0 support.  This is really exciting as .net Core projects are considerably easier to work with than .net 4.x.  There tends to be less magic and they're a lot more command line friendly. One issue is that in order to use the new WebJobs 3.0 library, my current ASP.net Core 1.x web applications need to be running on .net Core 2 to use it for dispatch.  This hasn't been too much of a hassle, it was largely just a matter of updating the TargetFramework entry in my csproj to netcoreapp2.0 .  You may also end up with a few other items to clean up after that as the netcoreapp2.0 TFM comes with some useful configurations out of the box to ensure well behaved .net 4.6 PCLs are easier to get into your projects. The one surprise I hit was that when I shipped things up to Azure App Service, I got a fairl...

Amazon in Winnipeg?

Late last week, Amazon put word out that they're starting to look for a city to locate a second headquarters .  The fuel this announcement provides has the hype train in Winnipeg going so fast, it's missing stops. I'm curious though.  As you've been digesting this exciting news, who have you spoken or listened to? Was it this guy  and his party's historic theft of public infrastructure (pdf) and incompetence with digital strategy ? Or how about this guy , who maintains that Winnipeg doesn't need a proper rapid transit option ?  You know, the kind that might actually catch the eye of handsome Amazon who now has Winnipeg trying to be the belle of the ball. Transit "system", Peggo glitches are free - thanks  @infidelatheist Runner up articles are  Winnipeg's inability to join the 21st centry on active transport  and the  compounding of the aforementioned digital strategy mistakes . Stop Listening to These Guys They are not technol...