title: Business/Domain Object in ASP.NET
body: <p>Just trying to gather thoughts on what works/doesn't work for manipulating Business/Domain objects through an ASP.NET (2.0+) UI/Presentation layer.  Specifically in classic ASP.NET LOB application situations where the ASP.NET code talks directly to the business layer.  I come across this type of design quite often and wondering what is the ideal solution (i.e. implementing a specific pattern) and what is the best pragmatic solution that won't require a complete rewrite where no "pattern" is implemented.</p>
  
  <p>Here is a sample scenario.</p>
  
  <p>A single ASP.NET page that is the "Edit/New" page for a particular Business/Domain object, let's use "Person" as an example.  We want to edit Name and Address information from within this page.  As the user is making edits or entering data, there are some situations where the form should postback to refresh itself.  For example, when editing their Address, they select a "Country".  After which a State/Region dropdown becomes enabled and refreshed with relevant information for the selected country.  This is essentially business logic (restricting available selections based on some dependent field) and this logic is handled by the business layer (remember this is just one example, there are lots of business situations where the logic is more complex during the post back - for example insurance industry when selecting certain things dictates what other data is needed/required).</p>
  
  <p>Ideally this logic is stored only in the Business/Domain object (i.e. not having the logic duplicated in the ASP.NET code).  To accomplish this, I believe the Business/Domain object would need to be reinitialized and have it's state set based on current UI values on each postback.</p>
  
  <p>For example:</p>
  
  <pre><code>private Person person = null;
  
  protected void Page_Load()
  {
      person = PersonRepository.Load(Request.QueryString["id"]);
  
      if (Page.IsPostBack)
          SetPersonStateFromUI(person);
      else
          SetUIStateFromPerson(person);
  }
  
  protected void CountryDropDownList_OnChange()
  {
      this.StateRegionDropDownList.Enabled = true;
      this.StateRegionDropDownList.Items.Clear();
      this.StateRegionDropDownList.DataSource = person.AvailableStateRegions;
      this.StateRegionDropDownList.DataBind();
  }
  </code></pre>
  
  <p>Other options I have seen are storing the Business object in SessionState rather than loading it from the repository (aka database) each time the page loads back up.</p>
  
  <p>Thoughts?</p>