SHARE
TWEET

Untitled

a guest Sep 16th, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Repository
  2. class CardsRepository(private val globalDslContext: DSLContext) {
  3.  
  4.   /* ======== WHAT WE HAVE CURRENTLY ========
  5.   /* 1 - We have a lot of noises with DbOperation.dbCall and in fact we only have transactional behavior in few places.
  6.   /* 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
  7.   /* 3 - The DbOperation class is not very clear and it requires a lot of effort to understand it and how to effectivelly use it
  8.   */
  9.  
  10.   fun findCard(cardId: Card.Id): DbOperation<Card> =
  11.     DbOperation.dbCall {
  12.       selectFrom(TABLE)
  13.         .where(TABLE.ID.eq(cardId))
  14.         .fetchSingle { it.asModel() }
  15.     }
  16.   }
  17.  
  18.   fun findCardNotTransactional(cardId: Card.Id): Card =
  19.     globalDslContext.selectFrom(TABLE)
  20.       .where(TABLE.ID.eq(cardId))
  21.       .fetchSingle { it.asModel() }
  22.   }
  23.  
  24.   fun setPin(cardId: Card.Id, pinSetDate: PinSetDate) {
  25.     DbOperation.dbCall {
  26.       update(TABLE)
  27.         .set(TABLE.PIN_SET, Option.just(pinSetDate))
  28.         .where(TABLE.ID.eq(cardId))
  29.         .execute()
  30.     }
  31.   }
  32.  
  33.   fun setPinNotTransactional(cardId: Card.Id, pinSetDate: PinSetDate) {
  34.     globalDslContext.update(TABLE)
  35.       .set(TABLE.PIN_SET, Option.just(pinSetDate))
  36.       .where(TABLE.ID.eq(cardId))
  37.       .execute()
  38.   }
  39.  
  40.   /* ============= NEW VERSION =============
  41.   /* 1 - All the repository methods with explicit DSLContext - No magic, it's explicit and clear
  42.   /* 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)
  43.   /* 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  
  44.   /* 4 - Last but not least. Only one method and it can be used for both transactional and not transactional situations
  45.   */
  46.   fun findCard(cardId: Card.Id, context: DSLContext = globalDslContext): Card =
  47.     context.selectFrom(TABLE)
  48.       .where(TABLE.ID.eq(cardId))
  49.       .fetchSingle { it.asModel() }
  50.  
  51.   fun setPin(cardId: Card.Id, pinSetDate: PinSetDate, context: DSLContext = globalDslContext) {
  52.     context.update(TABLE)
  53.       .set(TABLE.PIN_SET, Option.just(pinSetDate))
  54.       .where(TABLE.ID.eq(cardId))
  55.       .execute()
  56.   }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top