Automapper 与运行时通过配置文件和反射进行映射
我是这个主题的新手,我想知道您的意见我应该做什么或如何解决以下问题:
我正在开发一个 C# Web 服务客户端,它从 Web 服务背后的数据库检索数据。 Web 服务响应将数据作为 XML 保存在 CDATA 元素中。 xsd 架构可用。 XML 中的元素代表一个数据集/行。我必须仅使用一个数据集的几个字段。检索数据后,我必须检查一些业务规则/条件并将这些字段写入另一个系统。从其他系统读取一些值,检查一些业务规则,并将它们根据模式以适当的 XML 格式封装发送回 Web 服务。
到目前为止我做了什么。我使用 XSD.exe 根据底层架构生成 C# 类。我认为我可以通过以下选项来实现我的场景:
ORM 框架: 这对于我的应用程序来说应该是多余的。或者它不是一个解决方案,因为我不直接与数据库通信。
自动映射器: 似乎是一个很好的解决方案。但我认为这不像我的第三种选择那么灵活(见下文)。如果在部署我的应用程序后检测到映射错误,我必须重建该应用程序。另一个缺点是,如果架构发生变化,我将不得不再次重建我的应用程序。 (或者我是否始终根据架构调整我的应用程序?)
通过自定义映射文件和反射: 我不确定这是否真的可以实现,但在我看来这将是最灵活的解决方案。解决方案类似于此 http://www.techrepublic.com/article/applied-reflection-dynamically-accessing-properties-of-a-class-at-runtime/6099345 如果我正确理解这篇文章,那么将会有一个配置文件将类的类型及其属性映射到目标类型,例如: SourceClassType=ClassA, SourcePropertyName=PropA->DestinationClassType=ClassB, DestinationPropertyName=PropX。
Linq2Xml: 使用 Linq2Xml,我可以从 XML 中提取所需的数据并将其放入我自己定义的类中。但是我如何根据 Web 服务所需的架构将数据放回到 XML 中?
我的分析正确吗?我是不是误会了什么?你会推荐什么?我还有其他选择吗?
I am new to this topic and I would like to know your opinion what I should do or how I can solve the following:
I am developing a C# web service client which retrieves data from a database behind the web service. The web service response holds the data in an CDATA element as an XML. The xsd schema is available. In the XML are elements that represent one dataset/row. I have to use only a few fields of one dataset. After the data retrieved I have to check some business rules/conditions and write those fields to another system. Reading some values from the other system, check some business rules and sending them back to the web service encapsulated in an appropriate XML format according to the schema.
What I have done so far. I used XSD.exe to generate c# classes according to the underlying schema. I think I have the following options to get my scenario work:
ORM framework:
This should be overkill for my app. Or it is not a solution since I don´t communicate with a database directly.Automapper:
Seems to be a good solution. But I think it is not that flexibel like my 3. option (see beyond). If an error in the mapping is detected after my app was deployed, I have to rebuild the app. Another negative point would be, if the schema changes, I would have to rebuild my app again. (Or do I have always adapt my app according to the schema?)Via custom mapping file and reflection:
I am not sure if this is really realizable, but it would be the most flexible solution in my opinion. The solution would be similar to this http://www.techrepublic.com/article/applied-reflection-dynamically-accessing-properties-of-a-class-at-runtime/6099345
If I understood this article correct, then there would be a config file that maps the type of class and its properties to destination types, for example:
SourceClassType=ClassA, SourcePropertyName=PropA->DestinationClassType=ClassB, DestinationPropertyName=PropX.Linq2Xml:
With Linq2Xml I could extract the required data from the XML and put it in my own defined classes. But how would I put data back into the XML according to schema that the web service expects?
Is my analysis correct? Did I misunderstood something? What would you recommend? Any other options I have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有不同的方法可以避免由于所需的代码更改而重建应用程序。一种方法是动态编译和使用组件。例如,这可以是源代码文件。该决定涉及许多因素,例如需要多久进行一次更改、谁将进行更改、性能的重要性等。
另一种选择可能是使用 xslt 将 xml 输入转换为 xml 输出,但它可能取决于您的业务规则中的具体要求。
我要做的是使用 xml(反)序列化和 Automapper。将 xml 反序列化为对象,使用自动映射器和自定义业务逻辑将源转换为目标对象,然后将目标对象序列化为 xml 作为输出。
There are different ways to avoid having to rebuild an app as a result of required code changes. One way is to compile and use a component dynamically. This can be a source code file for example. This decision involves many factors such as how often will a change be required, who will be making the change(s), how important is performance, etc.
Another option could be to use xslt to transform the xml input into xml output, but it may depend on what the specific requirements are in your business rules.
What I would do is use xml (de)serialization and Automapper. Deserialize the xml to an object, use automapper and your custom business logic to convert the source to the destination object, and then serialize the destination object to xml as your output.