By using a comprehensive feature-filled framework we can build software fast. On the other hand, by decoupling our applications we can build sofware that is independent of our framework and infrastructure choices, and therefore longer lasting.
We can't do both, is one approach always right?
In this talk we'll look at different decoupling techniques, what problems they solve, and when they make sense. We will learn some concrete techniques to discover where we should be investing in decoupling, and when we should let the framework maintainers do the work for us.
15. interface Vehicle
{
public function goTo($destination);
}
class TaxiDispatcher {
function dispatch (Vehicle $vehicle, $destination)
{
$this->chargeCustomer();
$vehicle->goTo($destination);
}
}
20. Benefits of abstraction
—Now we only need to rewrite Dispatcher and Car
when Vehicle changes
—Vehicle can be very stable; it's just an interface
—Can make new transportation types that
implement Vehicle
21. Defining abstractions
—Start with the use case, not the implementation
detail
—Contract should be a simple as possible
—Contract should focus on responsibilities
—Hide details of underlying APIs
22. Example - upgrade eligibility
Bad abstraction:
interface HttpClient
{
public function getData : array ($url, $parameters);
}
23. Example - upgrade eligibility
Better abstraction:
interface UpgradeEligibilityChecker
{
public function getContractId : int ($phoneNumber);
public function isEligibleForUpgrade : bool ($contractId);
}
24. Example - upgrade eligibility
Best abstraction:
interface UpgradeEligibilityChecker
{
public function isEligibleForUpgrade : bool ($phoneNumber);
}
27. —Migrate actions from method calls to objects
—Depend on a bus rather than depending on
contracts
—Don't know anything about what handles the
action
29. Commands
Use case / service style:
$invoiceApprover->approveInvoice(1234);
Command style (thephpleague/tactician):
$commandBus->handle(
new ApproveInvoice(1234);
);
30. Advantages of decoupling via abstractions
—Cleaner APIs
—Swappable components
—Separation of concerns
—Easier to test
—No 'ripple effect' around changes
31. Disadvantages of decoupling via abstractions
—Makes execution flow harder to follow
—Adds more complexity (more files in codebase)
—Cognitive cost of thinking of good abstractions
33. —Abstractions are going to change when the use
cases change
—Interfaces are more tightly coupled to code that
uses them
—Interfaces 'belong' in the same architectural
boundary as the code that uses them.
37. Highly coupled frameworks
Some frameworks let you go really quickly by
coupling directly to them.
You probably won't be able to reuse your code
without considerable effort.
—Drupal
—Magento
—Wordpress
38. More coupled framworks
Some frameworks let you go faster when you adopt
their conventions.
You can reuse your code with a fair amount of
additional effort.
—Symfony (1)
—CakePHP
—Laravel
39. More decoupled frameworks
Some frameworks encourage decoupling so your
code is more reusable.
You will go a little slower because you need to
explicitly configure things
—Symfony 2+
—Zend Framework 2+
40. Very decoupled frameworks
Some frameworks are extremely decoupled so code
is almost entirely reusable.
You almost have to construct the entire framework
yourself from components - this can be hard!
—D.I.Y.
—Silex
—Zend Expressive
46. Organizations which design
systems ... are constrained
to produce designs which
are copies of the
communication structures of
these organizations
— Melvin Conway, 1967
54. Service-orientation
—Good abstractions help you define boundaries
—Abstractions inside a monolith are easier to move
when you get it wrong
—Share values, not entities
—Don't share persistence