是否有一种设计模式可用于创建和填充从 xml 模式生成的复杂对象层次结构?
我有一个从 xml 模式生成的复合对象,我需要用数据库中的数据填充该对象。
生成的代码不会强制执行架构关于所需元素、最大出现次数等的规则。我想创建一个类(或类集合)来帮助填充复合对象并确保其序列化时生成的 xml 将验证反对模式。
我研究了各种创建设计模式,构建器模式似乎适用于此,但我从未使用过它,我只是不确定。
构建器模式是最好的方法还是我偏离了轨道?有更好的方法来解决这个问题吗?
I have a composite object that was generated from an xml schema that I need to populate with data from a database.
The generated code doesn't enforce the schema's rules regarding required elements, max occurances, etc. I'd like to create a class (or collection of classes) to help fill in the composite object and ensure when its serialized the resulting xml will validate against the schema.
I've looked into the various creational design patterns and the Builder Pattern seems like it might work for this but having never used it I'm just not sure.
Is the Builder pattern the best way or am I off track? Is there a better way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答是肯定的,构建器听起来是最合适的方式。
但现实是,这里并没有真正的错误。任何复合材料、建造者、工厂都可以达到目的。我会从类似构建器(或者可能是工厂,但这是个人喜好)之类的东西开始,也许它会演变成一个稍微修改的模式,具体取决于您的用例。
模式只是起点,仅此而已:)
the short answer is yes, builder sounds like the most appropriate way to go with.
but the reality is that there's no REAL wrong way here. anything composite, builder, factory will all do the trick. I would start with something like builder (or maybe factory, but thats a personal preference) and maybe it'll evolve into a slightly modified pattern, depending on your use case.
patterns are just starting points, nothing more :)