Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 291219

Entity Controller/Handler as a service

$
0
0

Problems / Motivations

#1909418: Entity Controller/Handler Injection Brings good ideas in term of object encapsulation but instantation of components is still done the old way (through "isset" and "if" mechanisms). The controllers are ContainerAware but does not seems to use the provided ContainerAwareInterface, thus it seems it was not invented here and is bad practice in general: controllers configures themselves in a static createInstance method. Moreover the complexity of discovering controllers can be cached at container compile-time, avoiding expensives operations at runtime.

Proposed resolution

  • Entity Controllers need to be services so they can get things like database connection or cache backends/bins injected.
  • The entity manager should be able to get instances of the controllers, without getting them injected, as we want them to be lazy loaded.

The proposed service name for these controllers would be:

$entity_type.controller.$controller_type

For the user storage controller:

<?php

$container
->setDefinition('user.controller.storage', new Definition('Drupal\user\UserStorageController',
  array(
'user', new Reference('database'), new Reference('password'), new Reference('user.data')));

?>

Then the refactored Manager, or "service locator".

<?php
 
public function hasController($entity_type, $controller_type) {
    return
$this->container->has("$entity_type.controller.$controller_type");
  }

 
// TODO: Remove the getControllerClass().

  /**
   * @return Drupal\Core\Entity\EntityStorageControllerInterface
   */
 
public function getStorageController($entity_type) {
    return
$this->container->get("$entity_type.controller.storage");
  }
?>

We can then get rid of EntityControllerInterface, which mimics the provided ContainerAwareInterface in term of functionalities. Remove the createInstance methods and de-couple controllers from container. Tree inheritance and dependency management becomes less complexe and easier to maintain as we all know principles of good programming: loose coupling and high cohesion.


Viewing all articles
Browse latest Browse all 291219

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>