Spring术语中命令,表单,业务和实体对象之间的区别? [英] Difference between command, form, business, and entity objects in Spring terms?

查看:671
本文介绍了Spring术语中命令,表单,业务和实体对象之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在松耦合系统方面围绕这些对象之间的差异。业务对象是否与实体对象相同?我可以在MVC中使用业务或实体对象作为我的命令对象吗?命令对象与表单对象相同吗?只是想在Spring术语和用法中寻找对象类型的澄清。

I'm trying to wrap my head around the differences between these objects in terms of loosely coupled systems. Is a business object the same as an entity object? Can I use a business or entity object in MVC as my command object? Is command object the same as a form object? Just looking for a clarification of the types of objects in Spring terms and usage.

我在stackoverflow上发现了一些问题,但没有任何解释我喜欢的。

I found a few questions on stackoverflow, but nothing that explained it to my liking.

Spring Web MVC文档似乎说你可以使用你的业务(实体?)对象作为你的命令/表单对象,但这不会违背关注点分离吗?

Spring Web MVC docs seem to say you can use your business (entity?) objects as your command/form objects, but wouldn't this go against separation of concerns?

来自Spring Docs:

From the Spring Docs:


可重复使用的业务代码,无需重复。使用现有业务对象作为命令或表单对象,而不是镜像它们以扩展特定的框架基类。

Reusable business code, no need for duplication. Use existing business objects as command or form objects instead of mirroring them to extend a particular framework base class.


推荐答案

1)从技术上讲,业务对象和业务实体(或称为实体对象)并不相同。

1) Technically, business objects and business entities (or "entity objects" as you call them) are not the same.

业务实体包含数据。业务对象包含有关业务实体的逻辑(如何创建实体,如何更新实体等)。业务对象在技术上是一种旧的J2EE模式,我还没有在当前的代码中看到它,所以我不能详细说明。有些人会说业务对象与DAO相对应,而其他人则更愿意说服务。有些开发人员只是说业务对象和实体是相同的,因为他们认为对象和实体具有相同的粒度,或者因为他们的业务实体也包含逻辑,或者仅仅因为他们不知道。我更喜欢谈论包含数据的对象的(业务)实体,而我从不使用术语业务对象,因为它可以有不同的解释。

Business entities contain the data. Whereas business objects contain the logic about your business entities (how you create your entity, how you update it, etc.). Business objects are technically an old J2EE pattern, I haven't really seen it in current codes so I cannot go into specifics. Some people would say that business objects correspond to DAOs, others would rather say Services. And some developers just say that business objects and entities are the same because they think that "object" and "entity" have the same granularity, or because their business entities also contain logic, or just because they don't know. I just prefer to talk about "(business) entities" for objects containing the data, and I never use the term "business object" because it can have different interpretations.

2)根据Spring MVC文档,命令对象是一个JavaBean,它将填充表单中的数据。另一方面,什么是表单对象,但是支持表单的对象?

2) According to Spring MVC documentation, a command object is a JavaBean which will be populated with the data from your forms. On the other hand, what is a form object, but an object backing your form ?

所以是的,命令对象在语义上与表单对象相同。我更喜欢术语表单对象,我发现它立即可以理解。

So yes, a command object is semantically the same as a form object. I prefer the term form object, that I find immediately understandable.

3)正如你所说,根据Spring MVC文档,框架的一个特性是

3) As you said, according to Spring MVC documentation, one feature of the framework is


可重复使用的业务代码,无需重复。 将现有业务
对象用作命令或表单对象而不是镜像
以扩展
a特定框架基类。

Reusable business code, no need for duplication. Use existing business objects as command or form objects instead of mirroring them to extend a particular framework base class.

所以是的,你可以 - 而且你应该根据Spring使用业务实体作为你的命令/表单对象。如果你不相信,这里有一些原因:

So yes, you can -and you should, according to Spring- use a business entity as your command/form object. If you are not convinced, here are some reasons :


  • 为了简单起见。上帝知道我们的Java软件架构需要更简单。有些公司使用太多层来做非常简单的事情。为了解决这个问题,我们采取了很多措施,由Spring,Java(见下文)等等。

  • 为了您自己,因为它使编程更简单,更容易和更有趣

  • 因为Spring和Java(通过JSR)说出来。确实:我们对表单对象有什么期望?形成支持,可能还有一些验证。我们如何进行此验证?
    Spring MVC 3支持使用JSR-303 @Valid注释验证@Controller输入。我们将约束置于何处进行验证?根据JSR-303,存储这些约束的最佳位置(@NotNull,@ Length等)属于业务实体本身。底线:最好使用业务实体作为命令/表单对象。

  • 仍然尊重关注点的分离。这只是一个问题(形式支持)不再是一个问题! Spring MVC为您处理它。 :-)你只需要担心你的业务实体。

  • For the sake of simplicity. And god knows that our Java software architectures need more simplicity. Some companies use way too much layers to do very simple stuff. Lots of initiatives are done to counter this, led by Spring, Java (see below) and whatnot.
  • For your own sake, because it makes programming simpler, easier and funnier
  • Because Spring and Java (through JSRs) say it. Indeed : what do we expect from a form object ? Form backing and probably some validation. How would we do this validation ? Spring MVC 3 supports the validation of @Controller inputs with the JSR-303 @Valid annotation. Where would we put the constraints to validate ? According to JSR-303, the best place to store these constraints (@NotNull, @Length etc.) is within the business entity itself. Bottom line: it is better to use a business entity as your command/form object.
  • The separation of concerns is still respected. It's just that one concern (form backing) is not a concern any more ! Spring MVC handles it for you. :-) You just have to worry about your business entities.

这篇关于Spring术语中命令,表单,业务和实体对象之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆