Loading objects
-
A common task we always have to accomplish is to allow users to edit objects from the UI. For example, lets say I have an employee class with the regular properties. The UI will have textboxes and other controls to display the properties. A user can select an employee by name from a listbox, treeview, combobox or whatever (not important to this question) and the form will display the employee. These are different approaches I have been taking to display the employee and I was wondering which is a better approach or if you can recommend one: 1. Create a property within the employeeForm called Employee and when it is set the form will call its private ReLoad() method and display the employee. 2. Create a property as mentioned in 1, but do not ReLoad in the setter, instead make the ReLoad method public and clients should call the ReLoad method after setting the employee. 3. Forget the property altogether and just have a method called Load(Employee emp). This is basically a method which takes employee as a parameter and then displays it. What do you think?
-
A common task we always have to accomplish is to allow users to edit objects from the UI. For example, lets say I have an employee class with the regular properties. The UI will have textboxes and other controls to display the properties. A user can select an employee by name from a listbox, treeview, combobox or whatever (not important to this question) and the form will display the employee. These are different approaches I have been taking to display the employee and I was wondering which is a better approach or if you can recommend one: 1. Create a property within the employeeForm called Employee and when it is set the form will call its private ReLoad() method and display the employee. 2. Create a property as mentioned in 1, but do not ReLoad in the setter, instead make the ReLoad method public and clients should call the ReLoad method after setting the employee. 3. Forget the property altogether and just have a method called Load(Employee emp). This is basically a method which takes employee as a parameter and then displays it. What do you think?
-
CodingYoshi wrote:
What do you think?
It sounds like you might not be familiar with the Model-View-Controller design pattern[^]. Good luck
led mike
I am very familiar with MVC and use it all the time.
-
I am very familiar with MVC and use it all the time.
-
CodingYoshi wrote:
I am very familiar with MVC and use it all the time.
Really? How are any of the options you posted in your first post, part of the MVC design? I don't see it.
led mike
led mike wrote:
How are any of the options you posted in your first post, part of the MVC design?
I never said my options had anything to do with MVC pattern. I use MVC pattern but in this case my question is not about MVC. My question is closely related to the observer pattern. You have an object of a class which is being observed by the form by subscribing to its events. Now when the user selects a different object from a treeview (or whatever), this class will become the new object the form is observing and the form should display its data. But the form can only do so once the object fires the event. But the object will fire the event when its state changes so the form has to wait until the user changes the state of the object. So now we are stuck because this is what has been happening so far: First an object was selected from the treeview. The object is sent to the form. The form subscribes to its events. The events are not fired so how do we display it? The solution I came up with is to trigger the event through a public method--just like we can trigger events in .NET by calling OnPaint. Now the form can display the data. So I think the answer to my question is to set the property and send a reference of the object to the form. Afterwards, force the objects event by triggering it through a public method. But I am still not sure if this a good solution.