通过业务类将 CSV 数据(未知格式)转换为 XML 数据(固定格式)。使用反射是个好主意吗?
我正在开发一个可以构建相当大的 XML 文件(电子商务提供)的库。
我使用业务类(例如 ProductInfo、PriceInfo 等),并根据这些类生成结果 XML。
我最新的任务是实现允许从 CSV 输入文件生成此 XML 的功能。
CSV 文件没有任何预定义的格式,用户的任务(我们提供了一个允许他们执行此操作的应用程序)将所有 CSV 列映射到我们固定 XML 格式中各自的对应项。
所以基本上他们的工作是将每一列分配给我的业务类字段之一:“该列中的所有数据都进入 ProductInfo.ShortDescription 等”。
我们的格式将来可能会发生变化 - 它会不断更新和扩展 - 因此我想让我的实现尽可能通用。
我考虑的解决方案如下:
- 库返回所有可用/必填字段的名称
- 我们的用户将 CSV 列映射到这些字段
- 我实现一个自定义属性(包含字段名称 - 字符串)并用它标记我的业务类中的所有字段。因此,一旦他们映射了所有列并以 CSV 格式提交了数据,我的库就会开始创建 ProductInfos 等,将输入值分配给由属性名称标识的字段。这可以通过反思来完成。
这是个好主意吗?我对性能有一些担忧(我知道反射很慢),尽管我不确定这会产生多大的问题。还有其他我应该注意的问题吗?还有更好的选择吗?
I am working on a library that builds fairly large XML files (e-commerce offers).
I use business classes (such as ProductInfo, PriceInfo etc.) and I generate the resulting XML out of these.
My latest task is to implement functionality that allows to generate this XML out of CSV input files.
The CSV files do not have any predefined format and it is user's task (we provide an application that allows them to do that) to map all the CSV columns into their respective counterparts in our fixed XML format.
So basically their job is to assign each column to one of my bussiness classes fields: "all data from this column goes into ProductInfo.ShortDescription etc.".
Our format is likely to change in future - it keeps on being updated and extended - and so I would like to make my implementation as generic as possible.
The solution I considered is following:
- the library returns names of all the available / required fields
- our user maps CSV columns to those fields
- I implement a custom attribute (containing a field name - string) and mark all the fields in my business classes with it. So once they've mapped all the columns and submitted the data in CSV, my library starts creating ProductInfos etc. assigning input values to fields, identified by the attribute name. This would be done by reflection.
Is this a good idea? I have some concerns regarding performance (I know that reflection is slow), although I'm not sure how much of an issue that would be. Is there any other issues I should be aware of? Are there any better alternatives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接建立一个 Web 服务来为最终用户处理这些问题呢?如果您对通过服务公开的业务对象进行更改,WSDL 服务的架构将自动更新。这将使您能够通过模式自动强类型化数据。
Why not just set up a web service to handle these for the end user? If you make changes to your business objects, which are exposed through the service, the schema for the WSDL service will automatically update. This will give you the ability to strongly type your data through a schema automatically.