Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Suppose I have a controller class with a bunch of properties. Each time a property is changed, I would like to update the model. Now also suppose that I use the command pattern to perform model updates. Is it common to use command classes within property get and sets of the controller class or is there a better way?

Here is an example of what I am currently using:

class MyController {
  private int _myInt;
  public int MyInt {
    get { return _myInt; }
    set {
      MyCommand cmd = new MyCommand();
      cmd.Argument = _myInt;
      cmd.Execute();  // Command object updates the model
    }
  } 
}
share|improve this question
    
How does that work when you have multiple instances of MyController? Wouldn't the value of _myInt get out of sync? –  Dean Harding Jan 6 '11 at 4:53
    
Dean, no because _myInt is not static –  user2567 Jan 6 '11 at 8:09

2 Answers 2

up vote 11 down vote accepted

I would think it's more common to have property value changes raise an event. Then, the model or whatever object is in charge of updating the model can subscribe to it and perform the updates that way.

share|improve this answer

Usually one tries to keep property setters "stupid", that is, perform basic field updates of its own class and avoid doing too much extra work which can potentially fail (throw an exception).

If you need extra work to be done, better make the setter to a method.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.