entities, the theory

25
Entities, the theory Entities, the theory

Upload: thanasis-polychronakis

Post on 07-Jan-2017

314 views

Category:

Technology


2 download

TRANSCRIPT

Entities, the theoryEntities, the theory

The MVC bankruptcyThe MVC bankruptcy

Models represent a schemaModels are FATBusiness logic leaks to ControllersHandling of higher level operationsMVC is not enough today

The MVC bankruptcyThe MVC bankruptcy

Active Record (Single Table)ValidationsRelationsMethods (db related)It's an ORM configuration

The MVC bankruptcyThe MVC bankruptcyModels represent a schemaModels represent a schema

You gotta see it to believe itOk, I bet you've seen it!It's uglyIt's not entirely the Model's faultDecouple

The Model bankruptcyThe Model bankruptcyModels are FAT (core models > 4k lines)Models are FAT (core models > 4k lines)

Send verification emailPerform multiple table OPsTalk to servicesmemcachepupsubredis

The MVC bankruptcyThe MVC bankruptcyBusiness logic leaks to ControllersBusiness logic leaks to Controllers

The MVC bankruptcyThe MVC bankruptcyBusiness logic leaks to ControllersBusiness logic leaks to Controllers

How do you run the operation from aCron Job?Back office?API?CLI?

How do you test?

Multiple tables involvedMultiple Services involvedComplex Auth SchemesReal Time

The MVC bankruptcyThe MVC bankruptcyHandling of higher level operationsHandling of higher level operations

Way more complex flowsService oriented designSaaS / PaaS InfrastructureMultiple inputs

The MVC bankruptcyThe MVC bankruptcyMVC is not enough todayMVC is not enough today

EntitiesEntitiesThe TheoryThe Theory

Ok, now what?Ok, now what?

DecoupleDecoupleSeparateSeparateDraw LinesDraw Lines

MeetMeetMVCeMVCe

Think of Entities as...Think of Entities as...PipesData transformersOperation plexersSwitchesAdaptorsAggregators

Entities are inspired by the Service Layer design pattern

Core PrinciplesCore PrinciplesHuman readable APINormalised InputNormalised Output

Core PrinciplesCore PrinciplesHuman readable APIHuman readable API

userEnt.create(udo);

userEnt.delete(uid);

userEnt.sendMessage(fromId, toId, message);

videoEnt.process(videoPath);

Methods that make senseThink action not operation

Core PrinciplesCore PrinciplesNormalised InputNormalised Input

userEnt.create({ firstName: "Thanasis", lastName: "Polychronakis", email: "[email protected]",});

Well documented expectationsNo need to be validated

Core PrinciplesCore PrinciplesNormalized OutputNormalized Output

console.log(udoOut);{ "id": "3kdp349r7fhw", "firstName": "Thanasis", "lastName": "Polychronakis", "email": "[email protected]",}

Aggregates datasetsNormalizes db idiomaciesConsistency

CRUD APICRUD API

The CRUD PrimitivesThe CRUD Primitivescreate()

read()

readOne()

readLimit()

update()

delete()

count()

ValidationsValidationsit depends™it depends™

ValidationsValidationsIs 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 forAre entities only forthe backend?the backend?

NONO

REMEMBERREMEMBEREntities areEntities are

NormalizersAggregatorsPipesYour business logic