0

I've come across the following situation multiple times when writing code (especially webapps) and would like to know the best practice (possibly design patterns) to solve it correctly:

My data is stored in some database or repository where I retrieve the data itself through an API. (therefor needing some sort of GET request)

My retriven data is processed and turned into a object representing the data. Fx a Dog with name, race, age.

In one way or the other I represent this Dog's data on my frontend using classic js, html.

I now want my users to be able to change the data on my frontend, send a request to my backend, and through my backend send a request to the API, updating my database / repository.

This is where my question arises.

What is the best way to structure this processflow? The first naive solution that pops into my head is having a Dog class like the following:

public class Dog() {

 // relevant fields

 public Dog(String json) {
  // map json result to Dog's fields
 }

 public static Dog getDogFromDb() {
  // call api
  // map api's json result to dog object
  // return Dog object
 }
 public void updateDogInDb(credentials, etc) {
  // take current dog, with its id, name, etc. and submit a request to the api, to update it according to Dog's fields and provided credentials.
 }
 // other dog methods, that might be relevant to the context...
 ...
}

In reality the Dog class would be much more complex, containing fields that represent other objects or lists of objects. Fx a Dog could have the following field List<Owner> ownersDogHasHad, this shouldn't change the question I simply want to clarify the complexity.

This approach works in practise, but seems to involve alot of different responsibilities in a single class, since Dog both represents a Dog and its methods, but also the practise of making a dog from a json response.body, making a dog from a database entity, and updating a dog in the databse.

How would this be written in a cleaner and clearer way? Is there any appropriate design patterns relavant?

Markus B
  • 83
  • 1
  • 10
  • 1
    You may want to find out about the buzzwords given [here](https://stackoverflow.com/q/4637124/1712135) and [there](https://stackoverflow.com/questions/1203377/dto-dao-poco-bo)… – deHaar Mar 28 '23 at 11:58
  • Have you tried any of available JSON binding libraries i.e [Jackson](https://github.com/FasterXML/jackson), [JSOB-B](https://javaee.github.io/jsonb-spec/), [Gson](https://github.com/google/gson)? – piotrowicki Mar 28 '23 at 12:53
  • @piotrowicki I have. I use both Jackson and Lombok to avoid boilerplate and to map / serialize my json to objects and objects to json. I'm more concerned with the responsibilities of my classes (essentially what code should go where, and how my system should interact with itself) – Markus B Mar 28 '23 at 13:22
  • 1
    It depends a lot on what the `Dog` needs to do. One option would be to have a dedicated object for talking to the database and performing some crud operations. Then, you can have a different object for the presentation: for instance a `DogDto` - this can contain the logic for mapping to json. Now, in your "domain" `Dog` object, you can keep only the business-related behavior. If you want to read more about this approach, take a look here: https://www.baeldung.com/hexagonal-architecture-ddd-spring – Emanuel Trandafir Apr 12 '23 at 08:23

1 Answers1

0

Generally we do not map the pojo directly to an entity.We could use Adapter to adapt to and fro from entity and pojo(in your case frontend object). A similar example is custom converter used in springboot

You could create your custom converter service and define the mapping to and fro for an object.No doubt you need to define such converter for each of the object.A hacky way when both objects are ditto ,you could serialize it as JSON and deserialize using targetClass (this could impact performance though)

class DogConverter<DogDTO, DogEntity> implements Converter<DogDTO,DogEntity>{

    @Override
    public DogEntity toEntity(DogDto dogDto){
        DogEntity entity = new DogEntity();
        //setters of entity
        return entity;
    }

    @Override
    public DogDTO toDTO(DogEntity dogEntity){
        DogDTO dto = new DogDTO();
        //setters of dto
        return dto;
    }
    
    
}
Sagar
  • 104
  • 1
  • 5