We have a big session object held in the web server using Spring session scope (says an object that represents a business object). Due to the expensive size of this bean, we manage to communicate with the view only delta attributes. For now, we know that Java reflection allows applying partial updates on the bean structure by processing fields information. We can manage this by computing the information sent by the view and calculating the differences with the server objects.
To ilustrate the problem more precisely, suppose the following JSON as the session-bean (Java classes) holds in session by Spring:
// session-bean
{
"person": {
"name": "Foo",
"age": 18,
"father": {
"name": "Bar"
},
"phones": [{
"number": 123456789
}, {
"number": 987654321
}]
}
}
Besides that, suppose that we use the following session-view structure (also holds on the session) to store information about the information presented at the view.
// session-view
[{
"path": "person.name",
"value": "Foo"
}, {
"path": "person.age",
"value": 18
}, {
"path": "person.father.name",
"value": "Bar"
}, {
"path": "person.phones[0].number",
"value": 123456789
}, {
"path": "person.phones[1].number",
"value": 987654321
}]
In this context, when the user sends a view-request with updates, says:
// view-request
{
"updates": [{
"path": "person.age",
"path": 20
}]
}
We use the path of each update object from view-request to find the equivalent field on the session-bean and update it (In this example "person.age"). Besides that, during the user view-request processing, if the server changes a property from the session-bean, ex:
public class PersonService {
@Autowired
public Person person;
public void updateFather() {
person.getFather().setName("Baz");
}
}
We recompute the session-view to update each object with new information and return only delta information to view. In this example after PersonService
finished their processing, the view-response will be:
// view-response
[{
"path": "person.age",
"path": 20
}, {
"path": "person.father.name",
"value": "Baz"
}]
And the session-view will be updated to the following:
// session-view
[{
"path": "person.name",
"value": "Foo"
}, {
"path": "person.age",
"value": 20 // <- changed
}, {
"path": "person.father.name",
"value": "Baz" // <- changed
}, {
"path": "person.phones[0].number",
"value": 123456789
}, {
"path": "person.phones[1].number",
"value": 987654321
}]
This example illustrate the case of values modification, but besides that, we have to synchronized when objects are deleted, created or re-arranged in references and lists.
Therefore, the question is: besides this approach, exists some design-pattern or Spring/Java library that is more indicated to execute this processing?
Aucun commentaire:
Enregistrer un commentaire