从域返回状态
在我们的域驱动应用程序中,我们使用一种名为 ServiceResponse<>
的类型在应用程序的各层之间发送数据 - 具体来说,域中的每个方法都会返回一个数据。截至目前,它封装了从该方法返回的数据(如果有),或者它可能生成的任何错误。
那么,我的问题是:向该对象添加可能在应用程序的其他层中有用的字段是否是可接受的做法?例如,向其添加 Status
或 StatusCode
字段是否是一种好的形式,稍后服务层可以将其解释为用作 HTTP 状态代码(带或不带)一些映射)?
In our domain-driven application, we use a type called ServiceResponse<>
to send data between layers of our application - specifically, one is returned by every method in the domain. As of right now, it encapsulates the data (if any) which was returned from the method, or any errors that it may have generated.
My question, then, is this: is it an acceptable practice to add fields to this object that may be useful in other layers of the application? For example, is it good form to add a Status
or StatusCode
field to it that may be interpreted later by the service layer for use as an HTTP status code (with or without some mapping)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说这听起来是个好地方。每个方法都返回某种“响应”的想法有点像试图过度解耦,但在某些情况下,这种极端的解耦是有必要的。
无论如何,
ServiceResponse
可以很容易地拥有一个状态,如果它需要一个状态,那就是我将其放置的位置。It sounds like a fine place to me. The idea that every method returns a "response" of some sort smells a bit like trying to decouple too much, but there are some cases where such extreme decoupling is warranted.
In any case, the
ServiceResponse
could easily have a status, and if it needed one, that is where I would put it.