dimanche 14 novembre 2021

How to design a data source adapter in a Spring Boot application

I need help implementing the correct design pattern on a small part of a Spring Boot project I've been given to code.

I'm definitely not experienced with the framework but think I appreciate the broad underlying concepts.

I am not posting a section of broken code where I am looking for an answer on how to fix it. Please don't shoot me down because of that. I can only really try to describe my issue in more generic terms, and I cannot disclose the existing project code.

That existing code is a Spring Service called BaselineHandler. It's public handle() method is called every day to calculate a baseline (essentially an algorithm to calculate a sort of rolling average) from some time-series data which we can call DatasetA. Data is fetched from DatasetA with a JPA call to SQL via the standard sort of Entity/Repository approach, so nothing unusual there.

However, I now have to modify the baseline algorithm. The data source for these modifications can be called DatasetB and is unrelated to DatasetA. The issue is that the exact implementation of DatasetB isn't yet known; it might turn out to be a new SQL table, but might not. Please also assume that there won't be a primary key, so I cannot implement a new DatasetB Entity and Repository (yes I know that primary keys can be manufactured but I have been told that I mustn't - let's not go there for the purpose of this post!).

The only thing I can safely start with is coding a simple Java bean with 4 or 5 fields representing the minimal attributes that a DatasetB 'tuple' would have to include.

This at least allows me to start thinking about creating a new test for the modified algorithm, one where I mock a few instances of DatasetB beans that I can inject, somehow, into the BaselineHandler. But how can I do that injection in as abstract and decoupled a way as possible that suits the Spring approach ?

It feels like I should be designing and injecting some sort of "DatasetBDataSourceAdapter" interface into the BaselineHandler. The adapter would know how to return a collection of DatasetB beans from different data sources. This means that BaselineHandler could rely on a contract with the adapter which wouldn't be broken even when the final implementation becomes clear (only the adapter would need additional code and constructing differently?).

Am I thinking about this correctly? And how should I go about coding it in a Spring way?

Aucun commentaire:

Enregistrer un commentaire