● +12 years of programming ○ Mostly backend PHP ○ CTO & Architect on big local projects ○ Lately backend Java ● Trainings for ○ Git ○ Testing ○ Correct™ usage of ORM @ProchazkaFilip
Bad api: registration form class RegistrationFacade { function register(string $email): User {} class User { function __construct(string $email) {} HOPE
Bad api: registration form class RegistrationFacade { function register(string $email): User { // validation } class User { function __construct(string $email) { // validation } DUPLICATION
Good api: registration form class EmailAddress { private __construct(string $email) {} public static function fromString(string $email): EmailAddress { // validation return new EmailAddress($email); }
Good api: registration form ● Private constructor + factory methods (yes, there can be more) ● Email can never be created invalid ● No duplication of logic
Good api: registration form class RegistrationFacade { function register(EmailAddress $email): User {} class User { function __construct(EmailAddress $email) {} SAFE & STRICT
Bad api: draw a pixel ● Doesn’t prevent “stupid mistakes” ● “Works” even when you pass the wrong value ○ Misplaced coordinates & color RGB values ● Too many arguments ● Bad variable names ● Duplicated validation ● ….
Good api: draw a pixel public function draw( Coordinates $coordinates, Color $color ); $board->draw( new Coordinates(1, 2), Color::fromRGB(255, 105, 180) );
Value objects: few ideas ● No Time & Date, only Datetime ○ https://github.com/brick/date-time ● Money and Currency API ○ https://github.com/brick/money ● EmailAddress ● PhoneNumber ● Url ● DateRange, TimeRange, NumberRange ● PostalAddress
Summary: Value Objects ● Define a concept of your domain ● Defined by values, not identity ● Valid from the moment of creation ● Immutable (preferably) ● Should have behaviour ● VO is not ○ Data Transfer Object ○ Reference Object (Entity)
Bonus: VO vs RO (Entity) ● Reference Object ○ Is defined by identity (we don’t care about equality) ○ Has behaviour ● Examples ○ User, Order, … ○ Address VO vs Address Entity
Bonus: VO vs DTO ● Data Transfer Object ○ Is a dummy structure ○ No behaviour ○ We don’t care about equality/identity ○ Groups together related data ● Example: ○ API Request object structure (for mapping from JSON) ○ Form data ○ ...