Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Repository
- class CardsRepository(private val globalDslContext: DSLContext) {
- /* ======== WHAT WE HAVE CURRENTLY ========
- /* 1 - We have a lot of noises with DbOperation.dbCall and in fact we only have transactional behavior in few places.
- /* 2 - We need to create two methods with different names in order to use it in both cases (with/without transactions) or create transactions for single operations
- /* 3 - The DbOperation class is not very clear and it requires a lot of effort to understand it and how to effectivelly use it
- */
- fun findCard(cardId: Card.Id): DbOperation<Card> =
- DbOperation.dbCall {
- selectFrom(TABLE)
- .where(TABLE.ID.eq(cardId))
- .fetchSingle { it.asModel() }
- }
- }
- fun findCardNotTransactional(cardId: Card.Id): Card =
- globalDslContext.selectFrom(TABLE)
- .where(TABLE.ID.eq(cardId))
- .fetchSingle { it.asModel() }
- }
- fun setPin(cardId: Card.Id, pinSetDate: PinSetDate) {
- DbOperation.dbCall {
- update(TABLE)
- .set(TABLE.PIN_SET, Option.just(pinSetDate))
- .where(TABLE.ID.eq(cardId))
- .execute()
- }
- }
- fun setPinNotTransactional(cardId: Card.Id, pinSetDate: PinSetDate) {
- globalDslContext.update(TABLE)
- .set(TABLE.PIN_SET, Option.just(pinSetDate))
- .where(TABLE.ID.eq(cardId))
- .execute()
- }
- /* ============= NEW VERSION =============
- /* 1 - All the repository methods with explicit DSLContext - No magic, it's explicit and clear
- /* 2 - This parameter is optional and defaulting to globalDslContext, so we don't need to pass it if we are not using transactions (95% of our codebase)
- /* 3 - We don't need to wrap it in monads and complicate the usage with maps/flatmaps and also we don't need DbOperation at all
- /* 4 - Last but not least. Only one method and it can be used for both transactional and not transactional situations
- */
- fun findCard(cardId: Card.Id, context: DSLContext = globalDslContext): Card =
- context.selectFrom(TABLE)
- .where(TABLE.ID.eq(cardId))
- .fetchSingle { it.asModel() }
- fun setPin(cardId: Card.Id, pinSetDate: PinSetDate, context: DSLContext = globalDslContext) {
- context.update(TABLE)
- .set(TABLE.PIN_SET, Option.just(pinSetDate))
- .where(TABLE.ID.eq(cardId))
- .execute()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement