Reducing call sites with dependency injection and context passing -Php training centre in trivandrum -soddisfare technologies

Reducing call sites with dependency injection and context passing


Singletons, service locators, registries...

$mailer = $this->container->get('mailer');
$request = sfContext::getInstance()->getRequest();
$request = sfContext::createInstance()->getRequest();
if (sfContext::hasInstance()) {
    $request = sfContext::getInstance()->getRequest();
} else {
    $request = sfContext::createInstance()->getRequest();
}

Dependency injection

final class SomeService
{
    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function doSomething()
    {
        $request = ...
    }
}

Context passing

final class SomeService
{
    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function doSomething(Request $request)
    {
        ...
    }
}
By the way, context passing doesn't necessarily mean that you should be passing a Context object. In several projects I've worked on, we introduced a Context object at some point, to hold things like the ID of the user who owns the current session, a tenant or customer ID, and miscellaneous things like the client's IP address, or the request time. This may seem very convenient, but such a Context object itself becomes very unfocused. We are tempted to add more and more to it. Maybe user settings, maybe entire entities (just so we don't need to make a call to the repository anymore). In conclusion, passing around a Context object is often the first step in separating dependencies from context, but there's always another step after that: passing only the values from the context that a given method needs.

Dependency injection & Context passing: effective means for reducing call sites

In projects that don't apply dependency injection and context passing (everywhere), you will find that there are issues with the number of call sites for certain methods. In particular: ServiceLocator::get() or things like Context::getRequest(). If we apply "computer think", we use these static methods to fetch all the things we need, instead of receiving them as constructor or method arguments. If we use "object thinking", we get rid of these static calls, and so we drastically reduce the number of call sites for them. This in turn allows us to prepare the project for the future. Because today's ServiceLocator is yesterday's façade, last week's Zend_Registry, and last month's sfContext. Instead of using whatever convenient utility your favorite framework uses for fetching things, just make sure you always inject your dependencies and pass along your context. If you always do this, you can easily migrate your project once the framework you use isn't maintained anymore (or when you just want to try out something better).




Comments

Popular posts from this blog

A Tailwind CSS Preset for Laravel 5.5

Short and safe array iteration

PHPStorm's performance