MVCS Best Practice - Repository Layer
Table of Contents
- Introduction
- Use PCG Query Builder to easily write standard queries
- Only use PCG handler to execute sql query
- Keep the repository as lean as possible
Introduction
Repositories interface with private data stores owned by the service. It is the classic DAO layer in JAVA world, where you application interface with databases. It makes no judgement on what is acceptable or not by the application, simply execute query and returns the mapped result
Use PCG Query Builder to easily write standard queries.
- PCG query builder has flexible pre-built functions like
psql.PrepareSelectStatementthat can save you from hand-crafting query, which could be error-prone. - It helps you avoid risk like sql injection.
- It also have the column mapper which can avoid potential error when using wild care ’*’ in select query.
- At the minimum, you should use param binding to build sql query instead of concatenation.
Only use PCG handler to execute sql query
- PCG sql handle are JDBI styled functions that helps you execute sql query.
- PCG sql handle will make sure binding and db transactions are properly managed. There is NO exception to this rule.
Keep the repository as lean as possible
- It should be simple and dump. No biz logic, calculation or logging should be happening in this layer
- In rare cases, it is allowed to expose the tx handle to service layer when service layer has to perform actions that requires explicit transaction management.
Last updated on