G
Guest
This sounds like a real stoopid question I know, and it would most likely
take me less to to do a bit of reading than to write the question, but here I
am in fornt of my PC and well, the books are all the way over the other side
of the room, and my google searching technique rather leaves something to be
desired.
So, I wrote myself an interface
public interface IPerson { string Name { get; set; } }
and I wrote an implementation or two
public class Man : IPerson
{
public Man(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name = value; } }
}
public class Woman : IPerson
{
public Woman(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name = value; } }
}
Then I got around to writing a controller
public class PersonController
{
public event EventHandler NameChanged;
private IPerson _person = null;
public PersonController(IPerson p) { _person = p; }
public string Name
{
get { return (_person.Name); }
set
{
_person.Name = value;
if (NameChanged != null)
NameChanged(this, EventArgs.Empty);
}
}
public void Update(IPerson p) { _person = p; }
}
And then I wrote a view (aka a User control with a textbox on it)
public partial class CustomerView : UserControl
{
private PersonController _personController = null;
public CustomerView() { InitializeComponent(); }
public void Update(IPerson p)
{
_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));
}
}
then I thought that I should place my CustomerView onto a windows form with
a couple of buttons to loop through my List<IPerson> of people (forwards and
back)
public partial class Ui : Form
{
private List<IPerson> _list = new List<IPerson>();
private int _position = -1;
public Ui()
{
InitializeComponent();
_list.Add(new Man("Dave"));
_list.Add(new Woman("Sally"));
_list.Add(new Man("Geoff"));
_list.Add(new Woman("Nicki"));
AdvanceIterator();
}
private void AdvanceIterator()
{
if (++_position == _list.Count)
_position = 0;
_ctrlCustomer.Update(_list[_position]);
}
private void RetardIterator()
{
if (--_position == -1)
_position += _list.Count;
_ctrlCustomer.Update(_list[_position]);
}
private void _btnPrevious_Click(object sender, EventArgs e)
{
RetardIterator();
}
private void _btnNext_Click(object sender, EventArgs e)
{
AdvanceIterator();
}
}
No problems here, it all works as one would like and expect.
So it works at least. Yes, it compiles, yes it runs, yes the Model is
updated when the Controller receives an update from the View, and it's all
great .... but is this the best way to achieve the required end? I mean look
in particular at the following code
::In the form containing my UserControl
_ctrlCustomer.Update(_list[_position]);
::In the UserControl's Update method
_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));
Originally I attempted to simply update the UserControl's reference to
IPerson, but the display was not updated, and one boviously does not wnat
multiple items in a collection bound to the same control.
I'm wondering should I be binding a new Controller for each call to Update
instead of clearing the existing bindings? But then won't that leave lost of
references hanging around eating away at my meagre amount of RAM.?
So here I am trying to see what others might say on the matter.
take me less to to do a bit of reading than to write the question, but here I
am in fornt of my PC and well, the books are all the way over the other side
of the room, and my google searching technique rather leaves something to be
desired.
So, I wrote myself an interface
public interface IPerson { string Name { get; set; } }
and I wrote an implementation or two
public class Man : IPerson
{
public Man(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name = value; } }
}
public class Woman : IPerson
{
public Woman(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name = value; } }
}
Then I got around to writing a controller
public class PersonController
{
public event EventHandler NameChanged;
private IPerson _person = null;
public PersonController(IPerson p) { _person = p; }
public string Name
{
get { return (_person.Name); }
set
{
_person.Name = value;
if (NameChanged != null)
NameChanged(this, EventArgs.Empty);
}
}
public void Update(IPerson p) { _person = p; }
}
And then I wrote a view (aka a User control with a textbox on it)
public partial class CustomerView : UserControl
{
private PersonController _personController = null;
public CustomerView() { InitializeComponent(); }
public void Update(IPerson p)
{
_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));
}
}
then I thought that I should place my CustomerView onto a windows form with
a couple of buttons to loop through my List<IPerson> of people (forwards and
back)
public partial class Ui : Form
{
private List<IPerson> _list = new List<IPerson>();
private int _position = -1;
public Ui()
{
InitializeComponent();
_list.Add(new Man("Dave"));
_list.Add(new Woman("Sally"));
_list.Add(new Man("Geoff"));
_list.Add(new Woman("Nicki"));
AdvanceIterator();
}
private void AdvanceIterator()
{
if (++_position == _list.Count)
_position = 0;
_ctrlCustomer.Update(_list[_position]);
}
private void RetardIterator()
{
if (--_position == -1)
_position += _list.Count;
_ctrlCustomer.Update(_list[_position]);
}
private void _btnPrevious_Click(object sender, EventArgs e)
{
RetardIterator();
}
private void _btnNext_Click(object sender, EventArgs e)
{
AdvanceIterator();
}
}
No problems here, it all works as one would like and expect.
So it works at least. Yes, it compiles, yes it runs, yes the Model is
updated when the Controller receives an update from the View, and it's all
great .... but is this the best way to achieve the required end? I mean look
in particular at the following code
::In the form containing my UserControl
_ctrlCustomer.Update(_list[_position]);
::In the UserControl's Update method
_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));
Originally I attempted to simply update the UserControl's reference to
IPerson, but the display was not updated, and one boviously does not wnat
multiple items in a collection bound to the same control.
I'm wondering should I be binding a new Controller for each call to Update
instead of clearing the existing bindings? But then won't that leave lost of
references hanging around eating away at my meagre amount of RAM.?
So here I am trying to see what others might say on the matter.