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?
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?
– Rodrigo Rodrigues
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...
– Rodrigo Rodrigues
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.
– uaiHebert
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.
– Rodrigo Rodrigues
Oops, thank you for the strength! = D
– uaiHebert