title: C#: How to cleverly design a GUI for a Desktop Application with encapsulation in mind
body: <p>I am developing a desktop application with a MainForm that's grown rather large. There are lots controls in several containers on different levels (SplitContainers, TabPages, Panels).</p>
  
  <p>Now I want to refactor this class by separating all the controller code responsible for data binding &amp; clearing the controls or carrying out user interactions into separate Controller classes.</p>
  
  <p><strong>[EDIT]</strong></p>
  
  <p><strong>Purpose</strong></p>
  
  <p>The purpose of this application is to track orders (called Job) and their positions (called Jobitem). Each Job is related to a customer, each Jobitem to an Article (Jobitems contain information that's specific to the manufacturing process of its article, therefore it's wrapping the article). Job, Jobitem, Customer and Article have dynamic properties (basicalyl key/value pairings) which are managed by a PropertyManager class.</p>
  
  <p><strong>Architecture</strong></p>
  
  <p>My application is split into 2 main projects: The core contains the data model in 3 LinqToSQL DataContext Classes:</p>
  
  <ul>
  <li><code>ArticleManagement</code></li>
  <li><code>JobManagement</code></li>
  <li><code>PropertyManagement</code></li>
  </ul>
  
  <p>For every DataContext class there is a <code>XyzManager</code> class which provides the LinqToSQL Object in a Collection to the GUI, encapsulating the LinqToSQL data source. It also handles creation, deletion and modification to its corresponding DataContext types.</p>
  
  <p>Then I've got a GUI project, naturally containing a main form and some additional forms for stuff like options etc. As of now, my GUI project holds a reference to each XyzManager class and gets &amp; sets the data through the Manager classes as the user interactions dictate, containing all the controlling logic.</p>
  
  <p>I've put most of the independent logic for this into a static Utility class outside of the main form class, but that's an less than ideal approach I guess.</p>
  
  <p><strong>[/EDIT]</strong></p>
  
  <p>This is my first bigger Desktop Application and I am having a hard time encapsulating the GUI. As of right now, all the controller code responsible for handling user interactions resides within the MainForm class.</p>
  
  <p>I know about the MVC pattern but I'm not sure how to apply it on a C# WinForm with my current design.</p>
  
  <p>My custom code alone is over 2400 lines of code long, not counting in the automatic generated code of the form, with a rising tendency. I want to refactor this, but am lost as to how I could solve this in a more elegant fashion.</p>