jeudi 31 octobre 2019

What is the best practice: business logic in Verticles or separate?

Recently I started programming in Vert.x with Kotlin, the question is generic for any technology with which we use the Vertx toolkit. I created my verticles, but in these I have business logic, and I would like to know what is the best practice:

1.Separate the logic to an independent class, instantiate it in the verticle and execute its operations, leaving only the verticle to receive and deliver the messages to the eventBus.

2.Implement whenever possible the logic in the same verticle.

Below I show an example of how the code has been, but as you can see is the logic within the verticle itself. It is well like this from the design point of view or you should take it out to an independent class and leave that verticle only for the operations of receiving and sending messages on the bus

class MyAPIAgentImpl(vertx: Vertx) : CoroutineVerticle() {

    var httpClient = HttpClient(vertx)
    var apiConfig: JsonObject = Config.get().getJsonObject("myapi")

override suspend fun start() {
      vertx.eventBus().consumer<JsonObject>(BusChannel.MY_API_CHANNEL) {
        message -> launch { message.reply(findElem(message.body().getLong("Id"),
        message.body().getString("countryCode"))) }
      }
  }

  override suspend fun findElem(Id: Long, countryCode: String): String {

      var hashMap = HashMap<String, String>()
      hashMap.put("Accept", "*/*")
      hashMap.put("Authorization", "Bearer XXXX")

      val baseUrl: String = apiConfig.get("apiBaseUrl")
      val port: Int = apiConfig.get("apiPort")
      val apiRelativeUrl: String = apiConfig.get("apiRelativeUrl")

      val resp: HttpResponse = httpClient.GET(String.format(baseUrl, countryCode), port,
        String.format(apiRelativeUrl, Id), hashMap, apiConfig.getLong("requestTimeout"))

    if (resp.status == HttpCode.OK) {
        return resp.message.get("msg")
      } else {
        logger.error("[HTTP CALL] My API response with error status code: ${resp.status}")
        throw ApiException(ErrorMessage(-1, resp.status, "Error calling external API"))
      }
  }

  companion object {
    private val logger = LoggerFactory.getLogger(MyAPIAgentImpl::class.java)
  }
}

I would like to know your comments and experiences in this regard in the design of applications with Vertx

Aucun commentaire:

Enregistrer un commentaire