Entities

The Theory

Who am I

  • Working for over 3 years on Node.js
  • Authored >40 NPM packages
  • Contributed to even more
  • CTO Insight Replay
  • Founder netscan.co

@thanpolas

The MVC bankruptcy

@thanpolas

  • 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

@thanpolas

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

The MVC bankruptcy

Models represent a schema

@thanpolas

  • 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)

@thanpolas

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

The MVC bankruptcy

Business logic leaks to Controllers

@thanpolas

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?

@thanpolas

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

The MVC bankruptcy

Handling of higher level operations

@thanpolas

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

The MVC bankruptcy

MVC is not enough today

@thanpolas

Ok, now what?

@thanpolas

Decouple
Separate
Draw Lines

@thanpolas

 

Meet

MVCe

@thanpolas

@thanpolas

Think of Entities as...

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

Entities are inspired by the Service Layer design pattern

@thanpolas

Core Principles

  • Human readable API
  • Normalised Input
  • Normalised Output

@thanpolas

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

@thanpolas

Core Principles

Normalised Input

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

@thanpolas

Core Principles

Normalized Output

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

@thanpolas

CRUD API

@thanpolas

The CRUD Primitives

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

@thanpolas

Validations

it depends™

@thanpolas

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

@thanpolas

Are entities only for the backend?

NO

@thanpolas

REMEMBER

Entities are

  • Normalizers
  • Aggregators
  • Pipes
  • Your business logic

@thanpolas

Entities, The Theory, BG WebSummit

By thanpolas

Entities, The Theory, BG WebSummit

The theory behind entities

  • 315
Loading comments...

More from thanpolas