What is the best way to Update only 1 field of a big Business Object inside a transaction?

允我心安 提交于 2019-12-30 07:49:07

问题


I am in a situation where I have to perform a transaction master detail record (Drop prev details, insert new details, Update Master status)

  1. Master Business Object has 20 fields
  2. Details Business Object has 4 fields only

Now I have to update only 1 field in master table and 4 fields in details table for insert.

If I initialize a new master object, 19 fields are being wasted for a simple update. What do I do to efficiently handle this situation ?

Can I make a new object and inherit only one field from my master business object ? Please give me a little working example if you advise me a DTO or something with inheritance. Thanks.


回答1:


I guess you mean exposing those 4 fields, instead of inheriting them since you cannot really inherit fields, only a class.

You could make a smaller, simpler 'update object', but I would only do so if this smaller object also exists logically in your model. Ideally, you really don't want to create special objects for updating only parts of your business objects. Instead, it's the task of your persistence layer to be smart enough to know which fields have changed and act accordingly (ie only update those fields).

So in summary:

  • Make an update object only if its also a logical part of your Domain Model
  • Trust in your persistence layer to see what has changed.



回答2:


Create DTO with those 4 fields. UI will send this DTO to business layer which will use new values to modify business object. Than you persist that modified business object.



来源:https://stackoverflow.com/questions/3519029/what-is-the-best-way-to-update-only-1-field-of-a-big-business-object-inside-a-tr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!