I agree with you Ron, the NPetshop is here to show best pactrice but it isn't perfect :-)

On 6/23/05, Ron Grabowski <[EMAIL PROTECTED]> wrote:
Gilles, I like the idea extracting the user interface elements of
add/edit UserControls into a UI-UserControl:

http://tinyurl.com/9ykpt
http://svn.apache.org/repos/asf/ibatis/trunk/cs/npetshop2/NPetshop.Web/UserControls/Accounts/AddressUI.ascx.cs

I'm curious why the class isn't implemented like this:

public Address PopulateAddress(Address address)
{
address.FirstName = txtFirstName.Text;
address.LastName = txtLastName.Text;
address.Address1 = txtAddress1.Text;
address.Address2 = txtAddress2.Text;
address.City = txtCity.Text;
address.Zip = txtZip.Text;
address.State = listState.SelectedItem.Value;
address.Country = listCountry.SelectedItem.Value ;
address.Phone = txtPhone.Text;
return address;
}

public Address Address
{
get
{
   return PopulateAddress(new Address());
}
set
{
  txtFirstName.Text = value.FirstName;
  txtLastName.Text = value.LastName;
  txtAddress1.Text = value.Address1;
  txtAddress2.Text = value.Address2;
  txtCity.Text = value.City;
  txtZip.Text = value.Zip;
  SetSelectedItem(listState, value.State);
  SetSelectedItem(listCountry, value.Country);
  txtPhone.Text = value.Phone;
}
}

That would allow for an existing instance of Address to be populated
with values from the UI-UserControl if the UI-UserControl did not set
all the values. Let's assume the Address object has additional
properties like AddressId and ZipCodePlusFour (i.e. 90210-4554) that
for whatever reason aren't present in the UI UserControl.

private void btnSubmit_Click(object sender, EventArgs e)
{
// AccountId and ZipCodePlusFour are populated
Account account = GetAccountByAccountId(3);

// get updated values for FirstName, LastName, etc.
ucAccountUI.PopulateAccount(account);

UpdateAccount(account);
}

I think that would make the UserControls more self-contained and not
have them rely on addtional values coming from an outside context.

- Ron

Reply via email to