In my applications I always end up implementing a Model-View-Presenter pattern and usually end up scrapping my View object from the screen with a get property.
For example
Person IBasicRegistration.Person
{
get
{
if (ViewState["View.Person"] == null)
ViewState["View.Person"] = new Person();
var Person = (Person) ViewState["View.Person"];
Person.Email = txtEmail.Text.Trim();
Person.FirstName = txtFirstName.Text.Trim();
Person.LastName = txtLastName.Text.Trim();
Person.Zip = txtZip.Text.Trim();
Person.Phone = txtPhone.Text.Trim();
Person.ResidentPersonLicenseState =
EnumerationParser.Parse<States?>(ddState.SelectedValue);
return Person;
}
}
However during debugging I've noticed when I access IBasicRegistration.Person in my Presenter/Model that I get quite a few traversals of my get { } property.
I started thinking this pattern seems to very similar to the INotifyPropertyChanged pattern and I started thinking about implementing a similar pattern and having each textfield implement an OnChanged event that would update it's related value in the person object that sits in the view state however the further I thought on that it require numerous server requests every time a person mouses out of a field and seems like it could lead to scalability issues at some point.
My next thought would it just make sense to create an IsDirty flag and wrap my code where it touches the fields similar to:
Person IBasicRegistration.Person
{
get
{
if (ViewState["View.Person"] == null)
ViewState["View.Person"] = new Person();
var Person = (Person) ViewState["View.Person"];
if(IsDirty)
{
Person.Email = txtEmail.Text.Trim();
...others
IsDirty = false;
}
return Person;
}
}
And set on any method that invokes a post back, (ie usually just the submit button) to set IsDirty = true and then it will skip repeating alot of work for no reason.
Has anyone come up with something more elegant for this task?