Skip to content

Instantly share code, notes, and snippets.

@OliverMensahDev
Created July 27, 2020 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OliverMensahDev/30aecb1c2ed4830e27336f58956fcdbd to your computer and use it in GitHub Desktop.
Save OliverMensahDev/30aecb1c2ed4830e27336f58956fcdbd to your computer and use it in GitHub Desktop.
<?php
//Not meant to be extended from.
final class Service
{
//All properties are immutable
private $properties;
//All arguments are required. Don’t inject contextual information; make the service reusable. Dependencies are actual dependencies, not service locators.
public function __construct($dependencies, $configurationValues)
{
// Only assign arguments to properties.
}
public function commandMethod($input, $context): void
{
// Pass data relevant to the job, plus contextual information (current time, current user, etc.). Validate input arguments. Perform the task. Produce side effects.
}
public function queryMethod($input, $context): returnType
{
//Validate input arguments. Produce no side effects, only return information.
}
}
//Not meant to be extended from.
final class Entity
{
//Properties can be mutable.
private $properties;
private array $events = [];
private function __construct()
{
}
// Use named constructors as meaningful ways of instantiating the object.
public static function namedConstructor($values)
{
//Validate arguments. Instantiate a new copy. Assign arguments to properties. Record domain event(s).
}
//Pass data relevant to the job, plus contextual information (current time, current user, etc.).
public function commandMethod($input, $context): void
{
// Validate input arguments. Validate state transitions. Record domain events.
}
// Limit the number of query methods exposing state
public function queryMethod(): returnType
{
// ...
}
public function releaseEvents(): array
{
// Return recorded domain events.
}
}
//Not meant to be extended from.
final class ValueObject
{
//All properties are immutable.
private $properties;
private function __construct()
{
}
//Named constructors as meaningful ways of instantiating the object.
public static function namedConstructor($values)
{
// Validate arguments. Instantiate a new copy. Assign arguments to properties. Record domain event(s).
}
// Use declarative names for modifiers (e.g. with…())
public function modifier($input): ValueObject
{
//Return a modified copy of the original.
}
// Limit the number of query methods
public function queryMethod(): returnType
{
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment