域对象是否应该实现 IXmlSerialized?

发布于 2024-08-14 02:49:45 字数 300 浏览 7 评论 0原文

我正在构建一个将数据公开为 XML 的 REST API。我的域层中有一大堆域类,旨在供 API 背后的服务层以及我们将提供给客户的客户端 API 使用。 (客户确实可以选择直接与 REST API 交互,但客户端 API 简化了事情)。我想让我的域类远离任何数据持久性逻辑,但我正在努力弄清楚域类是否可以实现 IXmlSerialized 以帮助简化序列化通过其公开和检索的数据的过程API。我开始认为我应该让域类不受任何序列化逻辑的影响,而是用序列化行为来装饰它们,例如将域对象包装在处理序列化的对象内。我是否让事情变得比需要的更复杂?关于我应该如何处理这个问题有什么想法吗?谢谢!

I'm building a REST API that exposes data as XML. I've got a whole bunch of domain classes in my domain layer that are intended for consumption by both the service layer behind the API, and the client API that we will be providing to customers. (Customers do have the option of interacting directly with the REST API, but the client API simplifies things). I want to keep my domain classes clean of any data persistence logic, but I'm strugling with trying to figure out if it's OK for the domain classes to implement IXmlSerializable to help simplify the process of serializing the data that is exposed through and retrieved from the API. I started out thinking that I'd keep the domain classes free of any serialization logic and instead decorate them with serialization behaviors, e.g. wrap the domain object inside of an object that handles the serialization. Am I making things more complicated than they need to be? Any thoughts on how I should approach this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈甜 2024-08-21 02:49:45

域类应该只关心业务逻辑,而不关心持久性或序列化。

您应该创建一组数据传输对象 (DTO) 类,每个类对应一个域类。这些类仅包含您决定公开的域类的属性。这允许域类具有未通过持久层或序列化层公开的属性。

只有 DTO 对象才会被序列化和反序列化。

然后,您可能会发现创建静态“转换”方法在域和 DTO 对象之间进行转换很方便。

Domain classes should be concerned with business logic only, not with persistence or serialization.

You should create a set of Data Transfer Object (DTO) classes, each corresponding to one of the domain classes. These classes would only contain the properties, from the domain classes, that you have decided to expose. This permits the domain classes to have properties which are not exposed through your persistence or serialization layers.

Only the DTO objects would be serialized and deserialized.

You may then find it convenient to create static "translate" methods to translate between the domain and DTO objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文