Building the layered server application achieve...

OKUNOKENTARO
December 01, 2019

Building the layered server application achieved by InversifyJS

The slide that used when I talked in JSConf JP.

OKUNOKENTARO

December 01, 2019

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

65 slides
  1. PS

  2. function makeUserId(): UserId { return new UserId(uuid()); } function makeUserIdMock(mockValue:

    string): UserId { return new UserId(mockValue); } class UserId { constructor(readonly value: string) {} } #FUUFS
  3. class UsersGetMediator { async handle(req: Request, res: Response) { const

    repo = new UsersRepository( new DatabaseProvider(), ); const users = await repo.getAll(); // Processing to send response } } )NNN
  4. class Application { private router: Router; constructor() { this.router =

    new Router( new UsersGetMediator( new UsersRepository( new DatabaseProvider(), ) ) ) } } :PVLOPX
  5. class Application { private router: Router; constructor() { this.router =

    new Router( new UsersGetMediator( new UsersRepository( new DatabaseProvider(), ) ) ) } } )!%0,&/
  6. import { injectable } from 'inversify'; @injectable() export class Application

    { constructor( private readonly auth: AuthService, private readonly router: Router, private readonly express: ExpressProvider, ) {} main() { this.auth.prepare(); this.router.prepare(); this.express.listen(); } } *OKFDUBCMFEFDPSBUPS
  7. @injectable() export class UsersGetMediator { constructor( private readonly auth: AuthService,

    private readonly permissionVerifier: PermissionVerifier, private readonly errorHandler: ErrorHandler, private readonly usersRepo: UsersRepository, ) {} async handle(req: Request, res: Response) { if (!(await this.auth.isAuthenticated(req))) { return this.errorHandler.unauthorized(res); } if (!(await this.permissionVerifier.canReadUsers(req.user.id))) { return this.errorHandler.forbidden(res); } const users = await this.usersRepo.getAll(); const body: UsersGetResponse = { users: users.serialize(), }; res.send(body).end(); } }
  8. +4$POG+1 Express Http Handler (Router) Google Cloud SQL Mediator Repository

    Database Provider Reader Writer Http Request (Not TS) Domain model Data access object Knex.js ORM Raw SQL Query (Not TS)
  9. +4$POG+1 Express Http Handler (Router) Google Cloud SQL Mediator Repository

    Database Provider Reader Writer Serialized JSON (Not TS) Domain model Data transfer object Results as JSON (Not TS)

More Decks by OKUNOKENTARO

Other Decks in Technology