Note that there are some explanatory texts on larger screens.

plurals
  1. POrefactor LINQ TO SQL custom properties that instantiate datacontext
    primarykey
    data
    text
    <p>I am working on an existing ASP.NET MVC app that started small and has grown with time to require a good re-architecture and refactoring.</p> <p>One thing that I am struggling with is that we've got partial classes of the L2S entities so we could add some extra properties, but these props create a new data context and query the DB for a subset of data. This would be the equivalent to doing the following in SQL, which is not a very good way to write this query as oppsed to joins: </p> <pre><code>SELECT tbl1.stuff, (SELECT nestedValue FROM tbl2 WHERE tbl2.Foo = tbl1.Bar), tbl1.moreStuff FROM tbl1 </code></pre> <p>so in short here's what we've got in some of our partial entity classes:</p> <pre><code>public partial class Ticket { public StatusUpdate LastStatusUpdate { get { //this static method call returns a new DataContext but needs to be refactored var ctx = OurDataContext.GetContext(); var su = Compiled_Query_GetLastUpdate(ctx, this.TicketId); return su; } } } </code></pre> <p>We've got some functions that create a compiled query, but the issue is that we also have some DataLoadOptions defined in the DataContext, and because we instantiate a new datacontext for getting these nested property, we get an exception </p> <blockquote> <p>"Compiled Queries across DataContexts with different LoadOptions not supported"</p> </blockquote> <p>. The first DataContext is coming from a DataContextFactory that we implemented with the refactorings, but this second one is just hanging off the entity property getter.</p> <p>We're implementing the Repository pattern in the refactoring process, so we <strong>must</strong> stop doing stuff like the above. Does anyone know of a good way to address this issue?</p> <p>EDIT: I am adding the code for the DataContextFactory that we created during our refactoring efforts. Note that in the code above we have a GetContext() static method in the Linq DataContext class, which news up a datacontext, and is not using the DataContextFactory below:</p> <pre><code>public class DataContextFactory&lt;T&gt; : IDataContextFactory where T : System.Data.Linq.DataContext { public DataContextFactory() { _context = Activator.CreateInstance&lt;T&gt;(); } public DataContextFactory(T context) { _context = context; } private System.Data.Linq.DataContext _context; public System.Data.Linq.DataContext Context { get { return _context; } } public void SaveAll() { Context.SubmitChanges(); } public bool IsContextDirty { get { return Context != null &amp;&amp; (Context.GetChangeSet().Deletes.Count &gt; 0 || Context.GetChangeSet().Inserts.Count &gt; 0 || Context.GetChangeSet().Updates.Count &gt; 0); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload