Entities

The Theory

The MVC bankruptcy

  • Models represent a schema
  • Models are FAT
  • Business logic leaks to Controllers
  • Handling of higher level operations
  • MVC is not enough today

The MVC bankruptcy

  • Active Record (Single Table)
  • Validations
  • Relations
  • Methods (db related)
  • It's an ORM configuration

The MVC bankruptcy

Models represent a schema

  • You gotta see it to believe it
  • Ok, I bet you've seen it!
  • It's ugly
  • It's not entirely the Model's fault
  • Decouple

The Model bankruptcy

Models are FAT (core models > 4k lines)

  • Send verification email
  • Perform multiple table OPs
  • Talk to services
    • ​memcache
    • pupsub
    • redis

The MVC bankruptcy

Business logic leaks to Controllers

The MVC bankruptcy

Business logic leaks to Controllers

How do you run the operation from a

  • Cron Job?
  • Back office?
  • API?
  • CLI?

How do you test?

  • Multiple tables involved
  • Multiple Services involved
  • Complex Auth Schemes
  • Real Time

The MVC bankruptcy

Handling of higher level operations

  • Way more complex flows
  • Service oriented design
  • SaaS / PaaS Infrastructure
  • Multiple inputs

The MVC bankruptcy

MVC is not enough today

Ok, now what?

Decouple
Separate
Draw Lines

 

Meet

MVCe

Think of Entities as...

  • Pipes
  • Data transformers
  • Operation plexers
  • Switches
  • Adaptors
  • Aggregators

Entities are inspired by the Service Layer design pattern

Core Principles

  • Human readable API
  • Normalised Input
  • Normalised Output

Core Principles

Human readable API

userEnt.create(udo);

userEnt.delete(uid);

userEnt.sendMessage(fromId, toId, message);

videoEnt.process(videoPath);
  • Methods that make sense
  • Think action not operation

Core Principles

Normalised Input

userEnt.create({
    firstName: "Thanasis",
    lastName: "Polychronakis",
    email: "thanpolas@gmail.com",
});
  • Well documented expectations
  • No need to be validated

Core Principles

Normalized Output

console.log(udoOut);
{
    "id": "3kdp349r7fhw",
    "firstName": "Thanasis",
    "lastName": "Polychronakis",
    "email": "thanpolas@gmail.com",
}
  • Aggregates datasets
  • Normalizes db idiomacies
  • Consistency

CRUD API

The CRUD Primitives

  • create()
  • read()
  • readOne()
  • readLimit()
  • update()
  • delete()
  • count()

Validations

it depends™

Validations

  • Is there an ORM CRUD OP?
  • Perform actions before or after ORM?
  • Do you trust your input?
    • Outside world?
    • Internal invocation?
    • Better not to trust anybody

Are entities only for the backend?

NO

REMEMBER

Entities are

  • Normalizers
  • Aggregators
  • Pipes
  • Your business logic

Thank you

(here is where you applaud)

Thanasis Polychronakis

@thanpolas

Questions

Be a critic

Thanasis Polychronakis

@thanpolas

Entities, The Theory

By thanpolas

Entities, The Theory

The theory behind entities

  • 816