JSON/XML output in GUI, BLL or DTO?
I'm using content-negotiation, so depending on the header of the request I provide JSON/XML output. Now I was wondering what the best location is for providing this functionality.
Info: BLL= business logic layer
DTO= data transfer object DAL= data access layerPseudo-Code example for DTO
class ExampleDTO{
propertie name;
propertie description;
}
Pseudo-Code example for BLL
class ExampleBLL{
GetExample(name) returns ExampleDTO;
GetExamples() returns List<ExampleDTO>;
}
1) In the GUI with a BLL-object: transforms the DTO result from the BLL into JSON/XML
2) In the BLL: something like... getObjectJSON() -> transforms & returns DTO-开发者_如何学编程input into a JSON format 3) In the DTO: behavior like... toJSON() toXML() like a toString() 4) Or extra DTO's with only 1 propertie (json/xml) 5) Something else? ...*Personally I think (1) is wrong for the reason to keep logic out of GUI, (4) seems overkill to have extra DTO's like WebJsonExampleDTO AND a WebXmlExampleDTO with only one propertie
I would suggest following approach, assumed that you know which response to return.
Keep response type separate from BLL as BLL don't have to do anything about response type.
You should be having one layer which will accept two param, one Response Type and second one would be Function to be called.
- This layer will give call to BLL
- BLL will return DTO
- Now you shall convert DTO to requested type i.e. JSON/XML
- Return the converted DTO to cally.
- Finally you will have 3+ layers GUI, Conversion Layer and BLL. Also BLL would be interacting with DAL.
Writing code irrespective of response type will have advantages like, you can have any number of response type. like you could have XML,JSON and another would be string or anything. Also you will have better control also.
精彩评论