0

I have faced a well-known scenarios whereby I really need to move a number of utilities private functions (that should not have been there in the first place) from my Service class to a utility class. My question is around using service methods in a utility class. I have attempted at the following refactoring:

class Utils(
  val serviceIneed : ServiceIneed) {

  companion object {
    private val someMapper = ObjectMapperConfig.mapper
  }

  
  fun someUtility(): ResponseEntity<Any> {
    // lots of stuff
    return serviceIneed.someFunction()
  }
}

Then this is the other service where I need to use the method I have moved to the newly created utility class:

class anotherService(
  private val serviceIneed: ServiceIneed
) {

  fun someMethod() {
    // lots of things happening
    val utilityUsage =  Utils(serviceIneed).someUtility()
  }
}

Is this the correct way to go about this? Can you recommend any approach on refactoring service classes in a way that only service-oriented methods and not helper ones remain in my Service class?

Thank you

panza
  • 1,341
  • 7
  • 38
  • 68
  • What is the reason you need to move them out of the service class definition? Reusability? I ask because that affects how I would approach the design. Also, you mention they are private functions but they don't appear to be private above. – Tenfour04 Nov 04 '20 at 18:15
  • Mostly to avoid long and cluttered service classes as it is not a good practice. My example shows a potential approach, therefore it does not have the private function, which is now someUtility() function in the Utility Class – panza Nov 04 '20 at 21:22
  • @Tenfour04 do you think the above approach is a recommended one or would you go differently? The implement itself works, so it is not an issue with the actual working parts. – panza Nov 05 '20 at 08:18

0 Answers0