Do not repeat yourself with switching properties.

Suppose I have a Person model class with the name property.

I also have a corresponding PersonView that contains a NameView as one of its subzones.

PersonView also contains a link to a person (his model), and NameView contains a link to a name (his model).

If I'm inside a PersonView , now I have two ways to get the name: either

 self.person.name 

or

 self.nameView.name 

All links are natural. However, this is apparently a violation of DRY. The above paths must invoke the same name. But if something fails, they may not.

The above example is much simpler than these things, usually in practice. For example, a person may be part of the Family object referenced by the FamilyView object. There are currently three different paths from FamilyView to this person’s name. When a tree grows, it only worsens.

What is the best way to handle the situation?

+4
source share
1 answer

I think that in this situation you need to choose between DRY violation or failure to clutch. I do not think you can avoid both.

If NameView does not contain its own copy of the data, then it will need a pointer to the data. Now you have tightened the clutch because NameView cannot be used on its own.

On the other hand, if NameView stores a duplicate of model data, you violate DRY and you may run into synchronization problems. Many GUIs have a β€œbinding” concept that stores values ​​that are synchronized between the model and the view.

Personally, I would choose a loose connection plus bindings. You should not have synchronization problems if the bindings you use are good. The widespread use of bindings in many different languages ​​and frameworks suggests that this is a decent approach.

Regarding the use of self.person.name or self.nameView.name , it depends on whether you are trying to change the state of the model or the state of the view. If you are trying to change the model, just change self.person.name . The view must somehow observe the change in the model, and should be able to update it accordingly. If you want to update the GUI and you do not need a model, use self.nameView.name .

+1
source

All Articles