Susheel Kumar
2016-02-27 13:38:52 UTC
Hello,
I wanted to get some thoughts/ or confirm class design for below feed
scenario from Single Responsibility principle perspective and also wanted
to confirm if calling DBHandler & FileHandler methods from
PersonJsonParser.parse(..) method below violates SRP from any perspective
and if so, how should I design.
- We have a service method to receive feeds
- The two parameters we receive are HTTP headers & String (JSON array of
people/person)
- We need to parse headers, validate it
- Parse array of people/person
- For each person, check if it is already in DB and get their last
timestamp.
- if newer record, then write into DB along with metadata and create a file
on filesystem with transformed JSON for another process to pickup
- if record exists, then compare timestamp, update it in DB and create file
on filesystem with transformed JSON for another process to pickup.
- If not a latest document, then don't take any action.
- Success / exception / failure / Any Validation / Business Rules / if
document arrived was older then in database etc, need to be communicated
back in response with appropriate status like (200 (OK), 400(ERROR), 500
(RETRY)
The classes/method design I have like below.
//FeederService
class FeederService
{
public feedPerson(String request, String headers)
{
try
{
RequestHeader reqHeader = new RequestHeader();
PersonRequestHandler personRequestHandler = new PersonRequestHandler();
reqHeader.parse(headers);
response = personRequestHandler.process(request,headers);
return response;
}catch(...)
{
}
}
//PersonRequestHandler
class PersonRequestHandler
{
public Response process(String searchRequest,RequestHeaders headers)
{
....
PersonJsonParser jsonParser = new PersonJsonParser();
try {
JSONObject mainJson = new JSONObject(searchRequest);
JSONArray personsArray = mainJson.getJSONArray("persons");
response = jsonParser.parse(personsArray,headers);
return response;
}
catch(JSONException ex) {
...
}
catch(Exception ex) {
...
}
}
}
//PersonJsonParser
class PersonJsonParser
{
...
public Response parse(JSONArray personsArray, RequestHeaders headers) {
...
StringBuffer outDocument = new StringBuffer();
try
{
for (int i = 0; i < personsArray.length(); i++)
{
JSONObject outputJson;
outputJson = getPersonOutputJson((JSONObject)usersArray.get(i),headers);
...
DBHandler.write(outputJson);
outDocument.append(outpuJson);
..
} // end for loop
//if some docs to be written on FileSystem
if(outDocument.length() > 1 ) {
...
FileHandler.createFile(outDocument);
...
}
} catch(Exception ex) {
...
}
//DBHandler
class DBHandler
{
...
public void write(..)
{....}
}
//FileHandler
class FileHandler
{
...
public void createFile(...)
{
...
}
}
I wanted to get some thoughts/ or confirm class design for below feed
scenario from Single Responsibility principle perspective and also wanted
to confirm if calling DBHandler & FileHandler methods from
PersonJsonParser.parse(..) method below violates SRP from any perspective
and if so, how should I design.
- We have a service method to receive feeds
- The two parameters we receive are HTTP headers & String (JSON array of
people/person)
- We need to parse headers, validate it
- Parse array of people/person
- For each person, check if it is already in DB and get their last
timestamp.
- if newer record, then write into DB along with metadata and create a file
on filesystem with transformed JSON for another process to pickup
- if record exists, then compare timestamp, update it in DB and create file
on filesystem with transformed JSON for another process to pickup.
- If not a latest document, then don't take any action.
- Success / exception / failure / Any Validation / Business Rules / if
document arrived was older then in database etc, need to be communicated
back in response with appropriate status like (200 (OK), 400(ERROR), 500
(RETRY)
The classes/method design I have like below.
//FeederService
class FeederService
{
public feedPerson(String request, String headers)
{
try
{
RequestHeader reqHeader = new RequestHeader();
PersonRequestHandler personRequestHandler = new PersonRequestHandler();
reqHeader.parse(headers);
response = personRequestHandler.process(request,headers);
return response;
}catch(...)
{
}
}
//PersonRequestHandler
class PersonRequestHandler
{
public Response process(String searchRequest,RequestHeaders headers)
{
....
PersonJsonParser jsonParser = new PersonJsonParser();
try {
JSONObject mainJson = new JSONObject(searchRequest);
JSONArray personsArray = mainJson.getJSONArray("persons");
response = jsonParser.parse(personsArray,headers);
return response;
}
catch(JSONException ex) {
...
}
catch(Exception ex) {
...
}
}
}
//PersonJsonParser
class PersonJsonParser
{
...
public Response parse(JSONArray personsArray, RequestHeaders headers) {
...
StringBuffer outDocument = new StringBuffer();
try
{
for (int i = 0; i < personsArray.length(); i++)
{
JSONObject outputJson;
outputJson = getPersonOutputJson((JSONObject)usersArray.get(i),headers);
...
DBHandler.write(outputJson);
outDocument.append(outpuJson);
..
} // end for loop
//if some docs to be written on FileSystem
if(outDocument.length() > 1 ) {
...
FileHandler.createFile(outDocument);
...
}
} catch(Exception ex) {
...
}
//DBHandler
class DBHandler
{
...
public void write(..)
{....}
}
//FileHandler
class FileHandler
{
...
public void createFile(...)
{
...
}
}
--
---
You received this message because you are subscribed to the Google Groups "Growing Object-Oriented Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriented-software+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
---
You received this message because you are subscribed to the Google Groups "Growing Object-Oriented Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriented-software+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.