Client REST process JSON replay without knowing domain objects

Asked

Viewed 693 times

1

I am making a Restfull server with Spring MVC, the controller only returns JSON, until ai blz, would my client know nothing of the domain classes and still process the requests?

I was thinking of turning JSON into an object like a database table, so you could manipulate the data like columns and values.

The fact is that I don’t want my client to know any domain class, it just receives the information, process, and show the user, the user makes the appropriate changes and sends.

I created a class called Dataset, the goal is to manipulate data as if it were a table in a generic way.

example

    DataSet dataset = new DataSet();

    //Pode importar a partir de um pojo
    /*dataset.fromPOJO(customerObject);
    dataset.fromJSON(JSONString);
    dataset.fromCSV(CSV);*/

    dataset.setDataSource(datasource);
    dataset.setTableName("Clientes");
    dataset.setLimit(100);
    dataset.setFetchRows(100);

    dataset.getFields().add(new Field("Nome", FieldType.STRING, 100) );
    dataset.getFields().add(new Field("DataNascimento", FieldType.DATE));
    dataset.getFields().add(new Field("DataHoraCadastro", FieldType.DATETIME));
    dataset.getFields().add(new Field("Foto", FieldType.BINARY));
    dataset.getFields().add(new Field("Credito", FieldType.DOUBLE, 2));
    dataset.getFields().add(new Field("Vendedor", FieldType.LOOKUP, "TABELA_PESSOA", new String[]{"id"}, new String[]{"id_vendedor"}));
    dataset.getFields().add(new Field("Contatos", FieldType.DETAIL, "PESSOA_CONTATO", new String[]{"pessoa_id"}, new String[]{"id"} ));


    //Adicionando registro
    dataset.append();       
    dataset.getField("Nome").setValue("RODRIGO RODRIGUES DA COSTA");
    dataset.getField("DataNascimento").setValue("06/04/1985");
    dataset.getField("Credito").setValue(1765.87);      
    dataset.post();

    dataset.append();
    dataset.getField("Nome").setValue("MARIA CECILIA CABRAL RODRIGUES");
    dataset.getField("DataNascimento").setValue("11/03/2011");
    dataset.getField("Credito").setValue(333);      
    dataset.post();

    System.out.println("Mostrando uma quanitdade de "+dataset.getRecordCount() );
    //Monstrando todos os registros
    for (Record record : dataset.getRecords()) {
        System.out.println("Registro número "+record.getIndex() );
        System.out.println(record.getField("Nome").getValue() );
        System.out.println(record.getField("DataNascimento").getValue() );
    }

I ask you, is there another way to do this? I don’t want my client to know about the existence of the domain class, so with Dataset in my view it works, now there is another way?

1 answer

2


Let’s simplify?

Why don’t you apply the concept of KISS? Keep It Simple Stupid! I always try to apply this to my projects.

If you don’t want to expose your domain classes why don’t you do something like:

public class AtualizarClienteVO{
    String nome;
    Date aniversario;
    // etc
}

And in your domain you would have:

public class Pessoa{
    String nome;
    Date aniversario;
    // etc
}

Just do it person.setName(updateVO.getName()) and you’re done. Your model is protected.

This is good practice?

It is common practice for you to have an object that will only serve to carry the data (usually known as VO, DTO). At the same time you keep your domain hidden.

Don’t try something too complex where you would have gained little. [=

From the above mode you would be mapping only the screen data to any object and then you do the parse for your model.

Completion

I see this approach as good practice because:

  1. You will keep your domain/VIEW offline. I have had plenty of experience between making domain changes and not having to change your Vos
  2. You can reuse View objects in other projects. If you have another view, it will only be able to configure specific things in the VOS view, it will not affect your domain
  3. Creating a bizarre type of entity to protect your domain will only bring more maintenance difficulty later. Make your classes easy to read and also easy to use.
  • Would the VO classes be a representation of my model (taxes only) of the domain classes there on the server, but only for the client side? Initially it would be a copy/Past of the domains? If I add an attribute in the domain, I would have to add it in the right VO as well?

  • It is worth noting that what I posted is just to show how the information would be assigned, however, this assignment would be in Runtime and dynamically, I agree that I would have a little work to pass to the view. Example, on the client screen, would receive the answer in JSON, populate this object automatically and show in a grid for example. I would not need to create any concrete class to represent the data, since the client side the only responsibility is, receiving data, manipulating and sending... Using the VO, I can still know something of the domain, I’m still in doubt...

  • You can have 15 attributes in the domain and only 5 in a VO. They don’t need to be interconnected. Be careful not to do everything "automatic" and then worsen and greatly the maintenance of the project. There are frameworks that already make this population from one side to the other, a simple thing that even you could do and it wouldn’t be complicated.

  • Got it, it’s really hard without the class that represents the data on the client side, in which case it’s easier to parse the JSON for my objects. I am a reader of his two books, JPA Eficaz and JSF, I confess that I only use JPA today because I will work with Vaadin instead of JSF, congratulations for the books, very practical and goes straight to the subjects.

  • Oops, thank you for the strength! = D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.