GUI、BLL 或 DTO 中的 JSON/XML 输出?
我正在使用内容协商,因此根据请求的标头,我提供 JSON/XML 输出。现在我想知道提供此功能的最佳位置是什么。
信息: BLL=业务逻辑层
DTO=数据传输对象
DAL= 数据访问层
DTO 的伪代码示例
class ExampleDTO{
propertie name;
propertie description;
}
BLL 的伪代码示例
class ExampleBLL{
GetExample(name) returns ExampleDTO;
GetExamples() returns List<ExampleDTO>;
}
1) 在带有 BLL 对象的 GUI 中:将 BLL 的 DTO 结果转换为 JSON/XML
2)在BLL中:类似... getObjectJSON() ->变换&将 DTO 输入返回为 JSON 格式
3) 在 DTO 中:行为类似于... toJSON() toXML() 就像 toString()
4) 或者只有 1 个属性的额外 DTO (json/xml)
5)还有别的事吗? ...
*我个人认为 (1) 是错误的,因为将逻辑排除在 GUI 之外,(4) 拥有额外的 DTO(例如 WebJsonExampleDTO 和仅具有一种属性的 WebXmlExampleDTO)似乎太过分了
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 layer
Pseudo-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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议采用以下方法,假设您知道要返回哪个响应。
将响应类型与 BLL 分开,因为 BLL 不必对响应类型执行任何操作。
您应该有一层接受两个参数,一个是响应类型,第二个是要调用的函数。
无论响应类型如何编写代码都会有一些优点,例如您可以拥有任意数量的响应类型。就像你可以有 XML、JSON,另一个可以是字符串或任何东西。而且你也会有更好的控制。
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.
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.