有关问题的硬编码答案的最佳实践问题 [英] Best Practices Question Concerning Hard Coded Answers to Questions

查看:105
本文介绍了有关问题的硬编码答案的最佳实践问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发不需要数据库交互的Web应用程序.我只允许将用户表单条目的结果存储为电子邮件. Web应用程序本身就像一个调查,它会询问一系列问题,其中一些问题需要解释.调查完成后,问题和答案将通过电子邮件发送给调查员.

我的问题是,保存问题的文本和答案以免将它们硬编码到应用程序中的最佳方法是什么?我以前使用过资源文件,它们可以用于此应用程序,但我想看看是否有人愿意共享更好的解决方案.我不是在寻找数据库解决方案.

谢谢!

I am working on a web application that requires no database interaction. I am only allowed to store the results of the user''s form entry as an email. The web application itself is like a survey that asks a bunch of questions, some of which require explanations. When the survey is completed, the questions and answers are then sent through email to the surveyor.

My question, what is the best way to save the questions'' text and answers so that they do not need to be hard-coded into the application? I''ve used resource files before and they would work for this application but I''d wanted to see if anyone had a better solution they would like to share. I am not looking for a database solution.

Thanks!

推荐答案

选项1)有对象保存您的问题结构.将其序列化为XML.优点是您可以编辑XML,也可以通过反序列化的对象轻松访问其内容.如果使用集合对象,则需要使用DataContractSerializer

选项2)为您的问题构建XML,使用ReadXml()方法将其填充到数据集.

选项3)向您开放. CSV,Excel,带格式的文本文件等.
Option 1) Have object which holds your questions structure. serialize it into XML. The adavantage would be you can edit the XML also you can easily access its contents through a deserialized object. If you use collection object then you need to use the DataContractSerializer

Option 2) Have an XML structured for your questions, Populate it to Dataset using ReadXml() method.

Option 3) Open to you. CSV, Excel, Formatted text file etc.,


在我看来,最有益的方式(到目前为止最多)是:创建一些纯数据类并制作一些数据图(不必是树,可以是更通用的结构图)来表达您的数据模型并使用Microsoft Data Contract进行存储/加载.

这种方式是非侵入性的且鲁棒的.您只将[DataContract]属性添加到类型中,并将[DataMember]属性添加到成员中. System.Runtime.Serialization.DataContractSerializer可以解决问题.不要忘记在DataContract中设置您唯一的公司名称空间-自动生成的XML元数据(因此您的数据格式)将被识别为世界唯一.

请参见System.Runtime.Serialization.DataContractAttributeSystem.Runtime.Serialization.DataMemberAttributeSystem.Runtime.Serialization.DataContractSerializer http://msdn .microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx [
In my opinion, the most beneficial way (by far the most) is this: create some pure-data classes and make some data graph (does not have to be tree, can be a more general-structure graph) to express your data model and store/load it using Microsoft Data Contract.

This way is very non-intrusive and robust. You only add [DataContract] attributes to your types and [DataMember] to your members. System.Runtime.Serialization.DataContractSerializer does the trick. Don''t forget to set up your unique company name space in DataContract — the auto-generated XML meta-data (and hence your data format) will be recognized as world-unique.

See System.Runtime.Serialization.DataContractAttribute, System.Runtime.Serialization.DataMemberAttribute and System.Runtime.Serialization.DataContractSerializer, http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^]

—SA


这篇关于有关问题的硬编码答案的最佳实践问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆