问题
I am in a situation where I have to perform a transaction master detail record (Drop prev details, insert new details, Update Master status)
- Master Business Object has 20 fields
- 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