ASP.net User Controls and business entities
Posted
by Chris
on Stack Overflow
See other posts from Stack Overflow
or by Chris
Published on 2010-03-30T10:03:25Z
Indexed on
2010/03/30
10:33 UTC
Read the original article
Hit count: 417
Hi all,
I am currently developing some user controls so that I can use them at several places within a project. One control is a about editing a list of addresses for a customer. Since this needs to be done at several places within the project I want to make it a simple user control. The user control contains a repeater control. By default the repeater displays one address item to be edited. If more addresses need to be added, the user can click on a button to append an additional address to be entered. The user control should work for creating new addresses as well as editing existing ones. The address business entity looks something like this:
public class Address
{
public string Street { get; set; }
public City City { get; set; }
public Address(string street, City city)
{
Check.NotNullOrEmpty(street);
Check.NotNull(city);
Street = street;
City = city;
}
}
As you can see an address can only be instantiated if there is a street and a city.
Now my idea was that the user control exposes a collection property called Addresses
.
The getter of this property collects the addresses from the repeater and return it in a collection. The setter would databind the addresses to be edited to the repeater.
Like this:
public partial class AddressEditControl : System.Web.UI.UserControl
{
public IEnumerable<Address> Addresses
{
get
{
IList<Address> addresses = new List<Address>();
// collect items from repeater and create addresses
foreach (RepeaterItem item in addressRepeater.Items)
{
// collect values from repeater item
addresses.Add(new Address(street, city));
}
return addresses;
}
set
{
addressRepeater.DataSource = value;
addressRepeater.DataBind();
}
}
}
First I liked this approach since it is object oriented makes it very easy to reuse the control. But at some place in my project I wanted to use this control so a user could enter some addresses. And I wanted to pre-fill the street input field of each repeater item since I had that data so the user doesn't need to enter it all by his self.
Now the problem is that this user control only accepts addresses in a valid state (since the address object has only one constructor). So I cannot do:
IList<Addresses> addresses = new List<Address>();
addresses.Add(new Address("someStreet", null)); // i dont know the city yet (user has to find it out)
addressControl.Addresses = addresses;
So the above is not possible since I would get an error from address because the city is null.
Now my question: How would I create such a control? ;) I was thinking about using an Address DTO instead of a real address, so it can later be mapped to an address. That way I can pass in and out an address collection which addresses don't need to be valid. Or did I misunderstood the way user controls work? Are there any best practices?
© Stack Overflow or respective owner