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.
I found a few questions on stackoverflow, but nothing that explained it to my liking.
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?
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.
Entity: An entity represents a single instance of your domain object saved into the database as a record. It has some attributes that we represent as columns in our tables. Model: A model typically represents a real world object that is related to the problem or domain space.
In Spring Framework, Data Transfer Object (DTO) is an object that carries data between processes. When you're working with a remote interface, each call is expensive. As a result, you need to reduce the number of calls. The solution is to create a Data Transfer Object that can hold all the data for the call.
Form Backing Object/Command Object This is a POJO that is used to collect all information on a form. It contains data only. It is also called a Command Object in some Spring tutorials. For example, an add a new car form page will have a Car form backing object with attribute data such as Year, Make and Model.
Because commands and DTOs are different things, they tackle different problems. Commands are serializable method calls - calls of the methods in the domain model. Whereas DTOs are the data contracts.
1) Technically, business objects and business entities (or "entity objects" as you call them) are not the same.
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) 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) As you said, according to Spring MVC documentation, one feature of the framework is
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.
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 :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With