Advertisement
Willcode4cash

Re-using DataContext

Aug 5th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 KB | None | 0 0
  1. // From http://www.developmentalmadness.com/2008/07/28/linq-to-sql-reusing-datacontext/
  2.  
  3. /**************************************************************************************
  4.  *  IThreadLocalStorageProvider.cs
  5.  **************************************************************************************/
  6.  
  7. namespace MyApp.Web.Models
  8. {
  9.     public interface IThreadLocalStorageProvider
  10.     {
  11.         void SaveItem(string key, object value);
  12.         object GetItem(string key);
  13.         bool ContainsItem(string key);
  14.     }
  15.  
  16.     public class ThreadLocalStorageProviderFactory
  17.     {
  18.         public static IThreadLocalStorageProvider Current { get; set; }
  19.     }
  20. }
  21.  
  22. /**************************************************************************************
  23.  *  WebThreadLocalStorageProvider.cs
  24.  **************************************************************************************/
  25.  
  26. namespace MyApp.Web.Models
  27. {
  28.     using System.Web;
  29.  
  30.     public class WebThreadLocalStorageProvider : IThreadLocalStorageProvider
  31.     {
  32.         public void SaveItem(string key, object value)
  33.         {
  34.             HttpContext.Current.Items.Add(key, value);
  35.         }
  36.  
  37.         public object GetItem(string key)
  38.         {
  39.             return HttpContext.Current.Items[key];
  40.         }
  41.  
  42.         public bool ContainsItem(string key)
  43.         {
  44.             return HttpContext.Current.Items.Contains(key);
  45.         }
  46.     }
  47. }
  48.  
  49. /**************************************************************************************
  50.  *  MyAppDataContext.cs (partial class)
  51.  **************************************************************************************/
  52.  
  53. namespace MyApp.Web.Models
  54. {
  55.     using System;
  56.     using System.Collections.Generic;
  57.     using System.Data.Linq;
  58.  
  59.     public partial class MyAppDataContext
  60.     {
  61.         public static MyAppDataContext GetDataContext()
  62.         {
  63.             // if TLS isn't available just return a new DataContext
  64.             if (ThreadLocalStorageProviderFactory.Current == null)
  65.                 return new MyAppDataContext();
  66.  
  67.             MyAppDataContext context;
  68.  
  69.             // check to see if DataContext already exists in TLS
  70.             if (ThreadLocalStorageProviderFactory.Current.ContainsItem("DataContext"))
  71.             {
  72.                 // get DataContext
  73.                 context = ThreadLocalStorageProviderFactory.Current.GetItem("DataContext") as MyAppDataContext;
  74.  
  75.                 // verify DataContext
  76.                 if (context != null) return context;
  77.             }
  78.  
  79.             // if we got here, then just create a new DataContext
  80.             context = new MyAppDataContext();
  81.  
  82.             // cache the DataContext
  83.             ThreadLocalStorageProviderFactory.Current.SaveItem("DataContext", context);
  84.  
  85.             // return to caller
  86.             return context;
  87.         }
  88.     }
  89. }
  90.  
  91. /**************************************************************************************
  92.  *  Global.asax.cs
  93.  **************************************************************************************/
  94.  
  95. namespace MyApp.Web
  96. {
  97.     using System;
  98.     using System.Data;
  99.     using System.Data.Linq;
  100.     using System.Security.Principal;
  101.     using System.Web;
  102.     using System.Web.Mvc;
  103.     using System.Web.Optimization;
  104.     using System.Web.Routing;
  105.     using System.Web.Security;
  106.     using MyApp.Web.Models;
  107.  
  108.     public class MvcApplication : System.Web.HttpApplication
  109.     {
  110.         protected void Application_EndRequest(object sender, EventArgs e)
  111.         {
  112.             // Validate that we actually have a DataContext to evaluate.
  113.             if (!ThreadLocalStorageProviderFactory.Current.ContainsItem("DataContext")) return;
  114.  
  115.             // Create an instance of the DataContext
  116.             var ctx = ThreadLocalStorageProviderFactory.Current.GetItem("DataContext") as DataContext;
  117.  
  118.             // If the instanciated DataContext is not null and connected, destroy it.
  119.             if (ctx != null && ctx.Connection.State == ConnectionState.Open) ctx.Dispose();
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement