I've recently come across a couple of articles that identify the problem of mapping domain objects to the UI layer. Paul Wilson suggests that we might be able to solve the problem by implementing a UI Mapper. A UI Mapper would serve a similar purpose as the OR Mapper. However, rather then mapping our object to our relational database as we do with our OR Mappers, we use the UI Mapper to map our objects to our user interfaces. To continue on with some thoughts from the blogosphere we travel over to Jimmy Nilsson blog, where he wonders if the recent buzz surrounding the topic is a sign of things to come.
I haven't put a ton of thought into the topic, however, I think we can gain efficiencies from a UI Mapper as we can with OR Mappers. On a recent project I didn't exactly flesh out a complete UI Mapper, however, I did do some UI Mapper'ish things to help speed the development of the UI of the application.
On many project in the past I've gotten a little bored writing a bunch of repetitive code to load data within my domain objects into my UI, as well as pulling information out of the UI and placing it into my objects. The process went something like this...
protected void Page_Load(object sender, EventArgs e) {
if(!Page.IsPostBack) {
Customer customer = new CustomerRepository().FindById(99);
LoadFormFormCustomer(customer);
}
}
protected void saveButton_Click(object sender, EventArgs e) {
if(Page.IsValid) {
Customer customer = new Customer();
LoadCustomerFromForm(customer);
}
}
private Customer LoadCustomerFromForm(Customer customer) {
customer.FirstName = firstName.Text;
customer.LastName = lastName.Text;
// and on and on and on....
}
private void LoadFormFormCustomer(Customer customer) {
firstName.Text = customer.FirstName;
lastName.Text = customer.LastName;
// and on and on and on....
}
Rather then continuing on the same basic path outlined above I set out to automate the process and reduce the amount of code required for creating "edit pages" within my application. The solution I developed uses reflection to automatically load forms from domain objects, and domains objects from forms. I created a controller class that takes a domain object and control as parameters. The controller then loops over the properties of the domain object (via reflection) and finds controls on the form with a matching ID.
Control foundControl = control.FindControl(property.Name);
If a control is found for the property the controller loads the control with the value within the associated properties of the domain object. With this infrastructure in place the edit page for the Customer becomes much simpler:
public class CustomerEdit : Steve.Eichert.Sample.Web.UI.EditPage {
protected override Type DomainType() {
return typeof(Customer);
}
}Could UI Mappers be the next big thing? Maybe, maybe not, all I know is my simple UI Mapper saved me from writing a bunch of boring repetative code and allowed me to concentrate on developing the domain logic within the application which in my opinion is a very good thing.