title: C# CSLA business object dilemma: read-only vs read/write
body: <p>I'm part of a team tasked to revamping our old VB6 UI/COBOL database application to modern times.  Before I was hired, the decision was made (largely on sales, I'm sure) to redo the UI before the database.  So, now we're using WPF and MVVM to great effect, it's been amazing so far, especially using CSLA as our Model layer.</p>
  
  <p>However, because our development is side-by-side with the next version of the old product, we're constrained a bit.  We can't make any changes (or minimal changes) to the calls made to the COBOL database.  This has been fine so far, albeit pining back to the glory days of SQL Server if you can believe it.</p>
  
  <p>Where I've hit a particularly nasty roadblock regarding our BO design is in dealing with "light" business objects returned in lists and their "full" counterparts.  Let me try and construct an example:</p>
  
  <p>Let's say we have a person object in the DB with a bunch of fields.  When we do a search on that table, we don't return all the fields, so we populate our lite object with these.  These fields may or may not be a subset of the full person.  We may have done a join or two to retrieve some other information specific to the search.  But, if we want to edit our person object, we have to make another call to get the full version to populate the UI.  This leaves us with two objects and attempting to juggle their state in 1 VM, all the while trying to keep the person list in sync on whatever parent object it sits after delete, edit, and add.  Originally, I made our lite person object derive from ReadOnlyBase&lt;>.  But now that I'm dealing with the same list behavior you'd have with a list of full BOs except with half full, half lite, I'm thinking I should've just made both the lite and full versions derive from BusinessBase&lt;> and simply made the lite version setter properties private.</p>
  
  <p>Has anyone else out there come across and found a solution for this?  After sleeping on it, I've come up with this potential solution.  What if we wrap the full and lite version of our BO in another BO, like this:</p>
  
  <pre><code>public class PersonFull : BusinessBase&lt;PersonFull&gt;
  {
    ...
  }
  public class PersonLite : BusinessBase&lt;PersonLite&gt;
  {
    ...
  }
  
  public class Person : BusinessBase&lt;Person&gt;
  {
    public PersonFull PersonFull;
    public PersonLite PersonLite;
  }
  public class PersonList : BusinessListBase&lt;PersonList, Person&gt;
  {
  }
  </code></pre>
  
  <p>Obviously everything would be CSLA registered properties and such, but for the sake of brevity they're fields there.  In this case Person and PersonList would hold all the factory methods.  After a search operation PersonList would be populated by Person objects whose PersonLite members were all populated and the PersonFull objects were all null.  If we needed to get the full version, we simply tell the Person object to do so, and now we have our PersonFull object so we can populate the edit UI.  If the Person object is to be deleted, we can easily do this with the CSLA delete procedures in place, while still maintaining the integrity of our lists across all the VMs that are listening to it.</p>
  
  <p>So, I hope this made sense to everyone, and if anyone has a different solution they've successfully employed or criticism of this one, by all means!</p>
  
  <p>Thanks!</p>
  
  <p>(Reposted from:  <a href="http://forums.lhotka.net/forums/thread/35576.aspx" rel="nofollow noreferrer">http://forums.lhotka.net/forums/thread/35576.aspx</a>)</p>