官术网_书友最值得收藏!

Value objects

Value objects (VOs) simplify a design. Entities have traits such as an identity, a thread of continuity, and attributes that do not define their identity. In contrast to entities, value objects have only attributes and no conceptual identity. Best practice is to keep value objects as immutable objects. If possible, you should even keep entity objects immutable too.

Entity concepts may bias you toward keeping all objects as entities, as a uniquely identifiable object in the memory or database with life cycle continuity, but there has to be one instance for each object. Now, let's say you are creating customers as entity objects. Each customer object would represent the restaurant guest, and this cannot be used for booking orders for other guests.

This may create millions of customer entity objects in the memory if millions of customers are using the system. Not only are there millions of uniquely identifiable objects that exist in the system, but each object is being tracked. Tracking as well as creating an identity is complex. A highly credible system is required to create and track these objects, which is not only very complex, but also resource-heavy. It may result in system performance degradation. Therefore, it is important to use value objects instead of using entities. The reasons for this are explained in the next few paragraphs.

Applications don't always need to be traceable and have an identifiable customer object; there are cases when you just need to have some or all attributes of the domain element. These are the cases when value objects can be used by the application. This makes things simple and improves performance.

Value objects can easily be created and destroyed, owing to the absence of identity. This simplifies the design—it makes value objects available for garbage collection if no other object has referenced them.

Let's discuss the immutability of value objects. Value objects should be designed and coded as immutable. Once they have been created, they should never be modified during their life cycle. If you need a different value for the VO, or any of its objects, then simply create a new value object, but don't modify the original value object. Here, immutability carries all the significance from object-oriented programming (OOP). A value object can be shared and used without impacting on its integrity if, and only if, it is immutable.

FAQs

The following are the most commonly asked questions:

  • Can a value object contain another value object?
    Yes, it can.
  • Can a value object refer to another value object or entity?
    Yes, it can.
  • Can I create a value object using the attributes of different value objects or entities?
    Yes, you can.

主站蜘蛛池模板: 西平县| 渝北区| 桂阳县| 博爱县| 讷河市| 和静县| 盐边县| 虹口区| 弥渡县| 洪泽县| 榕江县| 垦利县| 福海县| 五大连池市| 乌鲁木齐市| 英德市| 巴楚县| 莱芜市| 蓝山县| 奉贤区| 黑龙江省| 理塘县| 盐亭县| 五家渠市| 措美县| 九龙城区| 保定市| 安图县| 河北区| 陇西县| 富平县| 娄底市| 郴州市| 淅川县| 康乐县| 平武县| 辽源市| 本溪| 临武县| 遵化市| 承德县|