title: How to expose the DataContext from with-in a DataContext class?
body: <p>Is it possible to expose the DataContext when extending a class in the DataContext? Consider this:</p>
  
  <pre><code>public partial class SomeClass {
      public object SomeExtraProperty {
          this.DataContext.ExecuteQuery&lt;T&gt;("{SOME_REALLY_COMPLEX_QUERY_THAT_HAS_TO_BE_IN_RAW_SQL_BECAUSE_LINQ_GENERATES_CRAP_IN_THIS INSTANCE}");
      }
  }
  </code></pre>
  
  <p>How can I go about doing this? I have a sloppy version working now, where I pass the DataContext to the view model and from there I pass it to the method I have setup in the partial class. I'd like to avoid the whole DataContext passing around and just have a property that I can reference.</p>
  
  <p><strong>UPDATE FOR @Aaronaught</strong></p>
  
  <p>So, how would I go about writing the code? I know that's a vague question, but from what I've seen online so far, all the tutorials feel like they assume I know where to place the code and how use it, etc.</p>
  
  <p>Say I have a very simple application structured as (in folders):</p>
  
  <ul>
  <li>Controllers</li>
  <li>Models</li>
  <li>Views</li>
  </ul>
  
  <p>Where do the repository files go? In the Models folder or can I create a "Repositories" folder just for them?</p>
  
  <p>Past that how is the repository aware of the DataContext? Do I have to create a new instance of it in each method of the repository (if so that seems in-efficient... and wouldn't that cause problems with pulling an object out of one instance and using it in a controller that's using a different instance...)?</p>
  
  <p>For example I currently have this setup:</p>
  
  <pre><code>public class BaseController : Controller {
      protected DataContext dc = new DataContext();
  }
  
  public class XController : BaseController {
      // stuff
  }
  </code></pre>
  
  <p>This way I have a "global" DataContext available to all controllers who inherit from <code>BaseController</code>. It is my understanding that that is efficient (I could be wrong...).</p>
  
  <p>In my Models folder I have a "Collections" folder, which really serve as the ViewModels:</p>
  
  <pre><code>public class BaseCollection {
      // Common properties for the Master page
  }
  
  public class XCollection : BaseCollection {
      // X View specific properties
  }
  </code></pre>
  
  <p>So, taking all of this where and how would the repository plug-in? Would it be something like this (using the real objects of my app):</p>
  
  <pre><code>public interface IJobRepository {
      public Job GetById(int JobId);
  }
  
  public class JobRepository : IJobRepository {
      public Job GetById(int JobId) {
          using (DataContext dc = new DataContext()) {
              return dc.Jobs.Single(j =&gt; (j.JobId == JobId));
          };
      }
  }
  </code></pre>
  
  <p>Also, what's the point of the interface? Is it so other services can hook up to my app? What if I don't plan on having any such capabilities?</p>
  
  <p>Moving on, would it be better to have an abstraction object that collects all the information for the real object? For example an <code>IJob</code> object which would have all of the properties of the <code>Job</code> + the additional properties I may want to add such as the <code>Name</code>? So would the repository change to:</p>
  
  <pre><code>public interface IJobRepository {
      public IJob GetById(int JobId);
  }
  
  public class JobRepository : IJobRepository {
      public IJob GetById(int JobId) {
          using (DataContext dc = new DataContext()) {
              return dc.Jobs.Single(j =&gt; new IJob {
                  Name = dc.SP(JobId)  // of course, the project here is wrong,
                                       // but you get the point...
              });
          };
      }
  }
  </code></pre>
  
  <p>My head is so confused now. I would love to see a tutorial from start to finish, i.e., "File -> New -> Do this -> Do that".</p>
  
  <p>Anyway, @Aaronaught, sorry for slamming such a huge question at you, but you obviously have substantially more knowledge at this than I do, so I want to pick your brain as much as I can.</p>